d2d1/tests: Declare "level" as D3D_FEATURE_LEVEL in create_d3d11_device().
[wine.git] / tools / wmc / utils.c
blob4454add2f7dde9435389958594c4f777f608aa9f
1 /*
2 * Utility routines
4 * Copyright 1998,2000 Bertho A. Stultiens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "wmc.h"
31 #include "winnls.h"
32 #include "utils.h"
34 #define SUPPRESS_YACC_ERROR_MESSAGE
36 static void generic_msg(const char *s, const char *t, va_list ap)
38 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
39 vfprintf(stderr, s, ap);
43 * The yyerror routine should not exit because we use the error-token
44 * to determine the syntactic error in the source. However, YACC
45 * uses the same routine to print an error just before the error
46 * token is reduced.
47 * The extra routine 'xyyerror' is used to exit after giving a real
48 * message.
50 int mcy_error(const char *s, ...)
52 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
53 va_list ap;
54 va_start(ap, s);
55 generic_msg(s, "Yacc error", ap);
56 va_end(ap);
57 #endif
58 return 1;
61 int xyyerror(const char *s, ...)
63 va_list ap;
64 va_start(ap, s);
65 generic_msg(s, "Error", ap);
66 va_end(ap);
67 exit(1);
68 return 1;
71 int mcy_warning(const char *s, ...)
73 va_list ap;
74 va_start(ap, s);
75 generic_msg(s, "Warning", ap);
76 va_end(ap);
77 return 0;
80 void internal_error(const char *file, int line, const char *s, ...)
82 va_list ap;
83 va_start(ap, s);
84 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
85 vfprintf(stderr, s, ap);
86 va_end(ap);
87 exit(3);
90 void fatal_perror( const char *msg, ... )
92 va_list valist;
93 va_start( valist, msg );
94 fprintf(stderr, "Error: ");
95 vfprintf( stderr, msg, valist );
96 perror( " " );
97 va_end( valist );
98 exit(2);
101 void error(const char *s, ...)
103 va_list ap;
104 va_start(ap, s);
105 fprintf(stderr, "Error: ");
106 vfprintf(stderr, s, ap);
107 va_end(ap);
108 exit(2);
111 void warning(const char *s, ...)
113 va_list ap;
114 va_start(ap, s);
115 fprintf(stderr, "Warning: ");
116 vfprintf(stderr, s, ap);
117 va_end(ap);
120 int unistrlen(const WCHAR *s)
122 int n;
123 for(n = 0; *s; n++, s++)
125 return n;
128 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
130 WCHAR *t = dst;
131 while(*src)
132 *t++ = *src++;
133 *t = 0;
134 return dst;
137 WCHAR *xunistrdup(const WCHAR * str)
139 WCHAR *s;
141 assert(str != NULL);
142 s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
143 return unistrcpy(s, str);
146 int unistricmp(const WCHAR *s1, const WCHAR *s2)
148 int i;
149 int once = 0;
150 static const char warn[] = "Don't know the uppercase equivalent of non ascii characters;"
151 "comparison might yield wrong results";
152 while(*s1 && *s2)
154 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
156 if(!once)
158 once++;
159 mcy_warning(warn);
161 i = *s1++ - *s2++;
163 else
164 i = toupper(*s1++) - toupper(*s2++);
165 if(i)
166 return i;
169 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
171 if(!once)
172 mcy_warning(warn);
173 return *s1 - *s2;
175 else
176 return toupper(*s1) - toupper(*s2);
179 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
181 int i;
182 while(*s1 && *s2)
184 i = *s1++ - *s2++;
185 if(i)
186 return i;
189 return *s1 - *s2;
192 WCHAR *utf8_to_unicode( const char *src, int srclen, int *dstlen )
194 static const char utf8_length[128] =
196 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
197 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
198 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
199 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
200 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
201 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
202 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
203 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
205 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
207 const char *srcend = src + srclen;
208 int len, res;
209 WCHAR *ret, *dst;
211 dst = ret = xmalloc( (srclen + 1) * sizeof(WCHAR) );
212 while (src < srcend)
214 unsigned char ch = *src++;
215 if (ch < 0x80) /* special fast case for 7-bit ASCII */
217 *dst++ = ch;
218 continue;
220 len = utf8_length[ch - 0x80];
221 if (len && src + len <= srcend)
223 res = ch & utf8_mask[len];
224 switch (len)
226 case 3:
227 if ((ch = *src ^ 0x80) >= 0x40) break;
228 res = (res << 6) | ch;
229 src++;
230 if (res < 0x10) break;
231 case 2:
232 if ((ch = *src ^ 0x80) >= 0x40) break;
233 res = (res << 6) | ch;
234 if (res >= 0x110000 >> 6) break;
235 src++;
236 if (res < 0x20) break;
237 if (res >= 0xd800 >> 6 && res <= 0xdfff >> 6) break;
238 case 1:
239 if ((ch = *src ^ 0x80) >= 0x40) break;
240 res = (res << 6) | ch;
241 src++;
242 if (res < 0x80) break;
243 if (res <= 0xffff) *dst++ = res;
244 else
246 res -= 0x10000;
247 *dst++ = 0xd800 | (res >> 10);
248 *dst++ = 0xdc00 | (res & 0x3ff);
250 continue;
253 *dst++ = 0xfffd;
255 *dst = 0;
256 *dstlen = dst - ret;
257 return ret;
260 char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen )
262 char *ret, *dst;
264 dst = ret = xmalloc( srclen * 3 + 1 );
265 for ( ; srclen; srclen--, src++)
267 unsigned int ch = *src;
269 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
271 *dst++ = ch;
272 continue;
274 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
276 dst[1] = 0x80 | (ch & 0x3f);
277 ch >>= 6;
278 dst[0] = 0xc0 | ch;
279 dst += 2;
280 continue;
282 if (ch >= 0xd800 && ch <= 0xdbff && srclen > 1 && src[1] >= 0xdc00 && src[1] <= 0xdfff)
284 /* 0x10000-0x10ffff: 4 bytes */
285 ch = 0x10000 + ((ch & 0x3ff) << 10) + (src[1] & 0x3ff);
286 dst[3] = 0x80 | (ch & 0x3f);
287 ch >>= 6;
288 dst[2] = 0x80 | (ch & 0x3f);
289 ch >>= 6;
290 dst[1] = 0x80 | (ch & 0x3f);
291 ch >>= 6;
292 dst[0] = 0xf0 | ch;
293 dst += 4;
294 src++;
295 srclen--;
296 continue;
298 if (ch >= 0xd800 && ch <= 0xdfff) ch = 0xfffd; /* invalid surrogate pair */
300 /* 0x800-0xffff: 3 bytes */
301 dst[2] = 0x80 | (ch & 0x3f);
302 ch >>= 6;
303 dst[1] = 0x80 | (ch & 0x3f);
304 ch >>= 6;
305 dst[0] = 0xe0 | ch;
306 dst += 3;
308 *dst = 0;
309 *dstlen = dst - ret;
310 return ret;
313 #ifdef _WIN32
315 int is_valid_codepage(int id)
317 return IsValidCodePage( id );
320 WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
322 WCHAR *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
323 DWORD ret = MultiByteToWideChar( codepage, MB_ERR_INVALID_CHARS, src, srclen, dst, srclen );
324 if (!ret) return NULL;
325 dst[ret] = 0;
326 *dstlen = ret;
327 return dst;
330 #else /* _WIN32 */
332 struct nls_info
334 unsigned short codepage;
335 unsigned short unidef;
336 unsigned short trans_unidef;
337 unsigned short *cp2uni;
338 unsigned short *dbcs_offsets;
341 static struct nls_info nlsinfo[128];
343 static void init_nls_info( struct nls_info *info, unsigned short *ptr )
345 unsigned short hdr_size = ptr[0];
347 info->codepage = ptr[1];
348 info->unidef = ptr[4];
349 info->trans_unidef = ptr[6];
350 ptr += hdr_size;
351 info->cp2uni = ++ptr;
352 ptr += 256;
353 if (*ptr++) ptr += 256; /* glyph table */
354 info->dbcs_offsets = *ptr ? ptr + 1 : NULL;
357 static const struct nls_info *get_nls_info( unsigned int codepage )
359 unsigned short *data;
360 char *path;
361 unsigned int i;
362 size_t size;
364 for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
365 if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
367 assert( i < ARRAY_SIZE(nlsinfo) );
369 for (i = 0; nlsdirs[i]; i++)
371 path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage );
372 if ((data = read_file( path, &size )))
374 free( path );
375 init_nls_info( &nlsinfo[i], data );
376 return &nlsinfo[i];
378 free( path );
380 return NULL;
383 int is_valid_codepage(int cp)
385 return cp == CP_UTF8 || get_nls_info( cp );
388 WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
390 const struct nls_info *info = get_nls_info( codepage );
391 unsigned int i;
392 WCHAR dbch, *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
394 if (!info) error( "codepage %u not supported\n", codepage );
396 if (info->dbcs_offsets)
398 for (i = 0; srclen; i++, srclen--, src++)
400 unsigned short off = info->dbcs_offsets[(unsigned char)*src];
401 if (off)
403 if (srclen == 1) return NULL;
404 dbch = (src[0] << 8) | (unsigned char)src[1];
405 src++;
406 srclen--;
407 dst[i] = info->dbcs_offsets[off + (unsigned char)*src];
408 if (dst[i] == info->unidef && dbch != info->trans_unidef) return NULL;
410 else
412 dst[i] = info->cp2uni[(unsigned char)*src];
413 if (dst[i] == info->unidef && *src != info->trans_unidef) return NULL;
417 else
419 for (i = 0; i < srclen; i++)
421 dst[i] = info->cp2uni[(unsigned char)src[i]];
422 if (dst[i] == info->unidef && src[i] != info->trans_unidef) return NULL;
425 dst[i] = 0;
426 *dstlen = i;
427 return dst;
430 #endif /* _WIN32 */
432 unsigned char *output_buffer;
433 size_t output_buffer_pos;
434 size_t output_buffer_size;