/src/FreeImage/Source/LibTIFF/tif_wince.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 288 lines · 174 code · 55 blank · 59 comment · 23 complexity · d0eb96262040cec56fcd3fd544cfd82e MD5 · raw file

  1. /* $Id: tif_wince.c,v 1.18 2011/04/10 17:14:09 drolon Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * Windows CE-specific routines for TIFF Library.
  27. * Adapted from tif_win32.c 01/10/2006 by Mateusz Loskot (mateusz@loskot.net)
  28. */
  29. #ifndef _WIN32_WCE
  30. # error "Only Windows CE target is supported!"
  31. #endif
  32. #include "tiffiop.h"
  33. #include <windows.h>
  34. /* Turn off console support on Windows CE. */
  35. #undef TIF_PLATFORM_CONSOLE
  36. /*
  37. * Open a TIFF file for read/writing.
  38. */
  39. TIFF*
  40. TIFFOpen(const char* name, const char* mode)
  41. {
  42. static const char module[] = "TIFFOpen";
  43. thandle_t fd;
  44. int m;
  45. DWORD dwMode;
  46. TIFF* tif;
  47. size_t nLen;
  48. size_t nWideLen;
  49. wchar_t* wchName;
  50. m = _TIFFgetMode(mode, module);
  51. switch(m)
  52. {
  53. case O_RDONLY:
  54. dwMode = OPEN_EXISTING;
  55. break;
  56. case O_RDWR:
  57. dwMode = OPEN_ALWAYS;
  58. break;
  59. case O_RDWR|O_CREAT:
  60. dwMode = OPEN_ALWAYS;
  61. break;
  62. case O_RDWR|O_TRUNC:
  63. dwMode = CREATE_ALWAYS;
  64. break;
  65. case O_RDWR|O_CREAT|O_TRUNC:
  66. dwMode = CREATE_ALWAYS;
  67. break;
  68. default:
  69. return ((TIFF*)0);
  70. }
  71. /* On Windows CE, CreateFile is mapped to CreateFileW,
  72. * but file path is passed as char-based string,
  73. * so the path has to be converted to wchar_t.
  74. */
  75. nWideLen = 0;
  76. wchName = NULL;
  77. nLen = strlen(name) + 1;
  78. nWideLen = MultiByteToWideChar(CP_ACP, 0, name, nLen, NULL, 0);
  79. wchName = (wchar_t*)malloc(sizeof(wchar_t) * nWideLen);
  80. if (NULL == wchName)
  81. {
  82. TIFFErrorExt(0, module, "Memory allocation error!");
  83. return ((TIFF *)0);
  84. }
  85. memset(wchName, 0, sizeof(wchar_t) * nWideLen);
  86. MultiByteToWideChar(CP_ACP, 0, name, nLen, wchName, nWideLen);
  87. fd = (thandle_t)CreateFile(wchName,
  88. (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
  89. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
  90. (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
  91. NULL);
  92. free(wchName);
  93. if (fd == INVALID_HANDLE_VALUE) {
  94. TIFFErrorExt(0, module, "%s: Cannot open", name);
  95. return ((TIFF *)0);
  96. }
  97. /* TODO - mloskot: change to TIFFdOpenW and pass wchar path */
  98. tif = TIFFFdOpen((int)fd, name, mode);
  99. if(!tif)
  100. CloseHandle(fd);
  101. return tif;
  102. }
  103. /*
  104. * Open a TIFF file with a Unicode filename, for read/writing.
  105. */
  106. TIFF*
  107. TIFFOpenW(const wchar_t* name, const char* mode)
  108. {
  109. static const char module[] = "TIFFOpenW";
  110. thandle_t fd;
  111. int m;
  112. DWORD dwMode;
  113. int mbsize;
  114. char *mbname;
  115. TIFF *tif;
  116. m = _TIFFgetMode(mode, module);
  117. switch(m) {
  118. case O_RDONLY: dwMode = OPEN_EXISTING; break;
  119. case O_RDWR: dwMode = OPEN_ALWAYS; break;
  120. case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
  121. case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
  122. case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
  123. default: return ((TIFF*)0);
  124. }
  125. /* On Windows CE, CreateFile is mapped to CreateFileW,
  126. * so no conversion of wchar_t to char is required.
  127. */
  128. fd = (thandle_t)CreateFile(name,
  129. (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
  130. FILE_SHARE_READ, NULL, dwMode,
  131. (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
  132. NULL);
  133. if (fd == INVALID_HANDLE_VALUE) {
  134. TIFFErrorExt(0, module, "%S: Cannot open", name);
  135. return ((TIFF *)0);
  136. }
  137. mbname = NULL;
  138. mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
  139. if (mbsize > 0) {
  140. mbname = (char *)_TIFFmalloc(mbsize);
  141. if (!mbname) {
  142. TIFFErrorExt(0, module,
  143. "Can't allocate space for filename conversion buffer");
  144. return ((TIFF*)0);
  145. }
  146. WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
  147. NULL, NULL);
  148. }
  149. tif = TIFFFdOpen((int)fd,
  150. (mbname != NULL) ? mbname : "<unknown>", mode);
  151. if(!tif)
  152. CloseHandle(fd);
  153. _TIFFfree(mbname);
  154. return tif;
  155. }
  156. static void
  157. Win32WarningHandler(const char* module, const char* fmt, va_list ap)
  158. {
  159. /* On Windows CE, MessageBox is mapped to wide-char based MessageBoxW. */
  160. size_t nWideLen = 0;
  161. LPTSTR szWideTitle = NULL;
  162. LPTSTR szWideMsg = NULL;
  163. LPSTR szTitle;
  164. LPSTR szTmp;
  165. LPCSTR szTitleText = "%s Warning";
  166. LPCSTR szDefaultModule = "LIBTIFF";
  167. LPCSTR szTmpModule;
  168. szTmpModule = (module == NULL) ? szDefaultModule : module;
  169. if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED,
  170. (strlen(szTmpModule) + strlen(szTitleText)
  171. + strlen(fmt) + 128) * sizeof(char))) == NULL)
  172. return;
  173. sprintf(szTitle, szTitleText, szTmpModule);
  174. szTmp = szTitle + (strlen(szTitle) + 2) * sizeof(char);
  175. vsprintf(szTmp, fmt, ap);
  176. /* Convert error message to Unicode. */
  177. nWideLen = MultiByteToWideChar(CP_ACP, 0, szTitle, -1, NULL, 0);
  178. szWideTitle = (wchar_t*)malloc(sizeof(wchar_t) * nWideLen);
  179. MultiByteToWideChar(CP_ACP, 0, szTitle, -1, szWideTitle, nWideLen);
  180. nWideLen = MultiByteToWideChar(CP_ACP, 0, szTmp, -1, NULL, 0);
  181. szWideMsg = (wchar_t*)malloc(sizeof(wchar_t) * nWideLen);
  182. MultiByteToWideChar(CP_ACP, 0, szTmp, -1, szWideMsg, nWideLen);
  183. /* Display message */
  184. MessageBox(GetFocus(), szWideMsg, szWideTitle, MB_OK | MB_ICONEXCLAMATION);
  185. /* Free resources */
  186. LocalFree(szTitle);
  187. free(szWideMsg);
  188. free(szWideTitle);
  189. }
  190. TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
  191. static void
  192. Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
  193. {
  194. /* On Windows CE, MessageBox is mapped to wide-char based MessageBoxW. */
  195. size_t nWideLen = 0;
  196. LPTSTR szWideTitle = NULL;
  197. LPTSTR szWideMsg = NULL;
  198. LPSTR szTitle;
  199. LPSTR szTmp;
  200. LPCSTR szTitleText = "%s Error";
  201. LPCSTR szDefaultModule = "LIBTIFF";
  202. LPCSTR szTmpModule;
  203. szTmpModule = (module == NULL) ? szDefaultModule : module;
  204. if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED,
  205. (strlen(szTmpModule) + strlen(szTitleText)
  206. + strlen(fmt) + 128) * sizeof(char))) == NULL)
  207. return;
  208. sprintf(szTitle, szTitleText, szTmpModule);
  209. szTmp = szTitle + (strlen(szTitle) + 2) * sizeof(char);
  210. vsprintf(szTmp, fmt, ap);
  211. /* Convert error message to Unicode. */
  212. nWideLen = MultiByteToWideChar(CP_ACP, 0, szTitle, -1, NULL, 0);
  213. szWideTitle = (wchar_t*)malloc(sizeof(wchar_t) * nWideLen);
  214. MultiByteToWideChar(CP_ACP, 0, szTitle, -1, szWideTitle, nWideLen);
  215. nWideLen = MultiByteToWideChar(CP_ACP, 0, szTmp, -1, NULL, 0);
  216. szWideMsg = (wchar_t*)malloc(sizeof(wchar_t) * nWideLen);
  217. MultiByteToWideChar(CP_ACP, 0, szTmp, -1, szWideMsg, nWideLen);
  218. /* Display message */
  219. MessageBox(GetFocus(), szWideMsg, szWideTitle, MB_OK | MB_ICONEXCLAMATION);
  220. /* Free resources */
  221. LocalFree(szTitle);
  222. free(szWideMsg);
  223. free(szWideTitle);
  224. }
  225. TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
  226. /* vim: set ts=8 sts=8 sw=8 noet: */
  227. /*
  228. * Local Variables:
  229. * mode: c
  230. * c-basic-offset: 8
  231. * fill-column: 78
  232. * End:
  233. */