2004.09.28 15:26 "[Tiff] TIFF *TIFFOpenW(const wchar_t *name, const char *mode);", by Ian Ameline

2004.09.28 15:26 "[Tiff] TIFF *TIFFOpenW(const wchar_t *name, const char *mode);", by Ian Ameline

Hi again everyone,

While updating the version of LbTIFF that Sketchbook uses to 3.6.1, I once again noticed that besides the configuration and LZW stuff, there was one place where I was merging code -- A new function we have implemented called TIFFOpenW;

We produce a Japanese version of Alias Sketchbook, and on windows, to create or open files with Japanese filenames, you need to use wide characters. On Mac, we don't need to do this -- we just call TIFFOpen with a UTF8 character string and everything works nicely. Not so on Windows. So we created a function TIFFOpenW which is identical to TIFFOpen, but taking wchar_t * instead of char *. TIFFOpen becomes pretty vestigial in that it converts it's char string to wchar and calls TIFFOpenW.

I was wondering what everyone thought of rolling this up into libtiff 3.7.0 as a windows only function? (Saving me the work of merging again in the future :-)

Code from tif_win32.c follows;

/*
  * Open a TIFF file for read/writing.
  */
TIFF*
TIFFOpen(const char* name, const char* mode)
{
     wchar_t wname[MAX_PATH];
     mbstowcs(wname, name, MAX_PATH);
     return TIFFOpenW(wname, mode);
}

/*
  * Open a TIFF file, with a Unicode filename, for read/writing.
  */
TIFF*
TIFFOpenW(const wchar_t* name, const char* mode)
{
     static const char module[] = "TIFFOpen";
     thandle_t fd;
     int       m;
     DWORD     dwMode;
     int       mbsize;
     char     *mbname;
     TIFF     *tif;

     m = _TIFFgetMode(mode, module);

     switch(m)   {
         case O_RDONLY:               dwMode = OPEN_EXISTING; break;
         case O_RDWR:                 dwMode = OPEN_ALWAYS;   break;
         case O_RDWR|O_CREAT:         dwMode = OPEN_ALWAYS;   break;
         case O_RDWR|O_TRUNC:         dwMode = CREATE_ALWAYS; break;
         case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
         default: return ((TIFF*)0);
     }

     fd = (thandle_t)CreateFileW(name, (m == O_RDONLY) ? GENERIC_READ :
                        (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, dwMode,
                        (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, NULL);
     if (fd == INVALID_HANDLE_VALUE) {
         TIFFError(module, "%S: Cannot open", name);
         return ((TIFF *)0);
     }

     mbname = NULL;
     mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
     if (mbsize > 0) {
         mbname = malloc(mbsize);
         if (mbname != NULL) {
             WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
         }
     }


     tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode);

     if (mbname != NULL) {
         free(mbname);
     }

     return tif;
}

Regards,
Ian Ameline,
Software Architect,
Alias SketchBook Technical Lead.

-- Always do the right thing. This will gratify
some people and astonish the rest. -- Mark Twain.