PageRenderTime 76ms CodeModel.GetById 28ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

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

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