comctl32: Introduce _PSP structure to store HPROPSHEETPAGE data.
[wine.git] / tools / wrc / utils.c
blobbafc763692abeea696b107b6ef3073babdb0d75d
1 /*
2 * Utility routines
4 * Copyright 1998 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 <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "../tools.h"
31 #include "wrc.h"
32 #include "winternl.h"
33 #include "utils.h"
34 #include "parser.h"
36 /* #define WANT_NEAR_INDICATION */
38 #ifdef WANT_NEAR_INDICATION
39 void make_print(char *str)
41 while(*str)
43 if(!isprint(*str))
44 *str = ' ';
45 str++;
48 #endif
50 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
52 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
53 vfprintf(stderr, s, ap);
54 #ifdef WANT_NEAR_INDICATION
56 char *cpy;
57 if(n)
59 cpy = xstrdup(n);
60 make_print(cpy);
61 fprintf(stderr, " near '%s'", cpy);
62 free(cpy);
65 #endif
69 int parser_error(const char *s, ...)
71 va_list ap;
72 va_start(ap, s);
73 generic_msg(s, "Error", parser_text, ap);
74 fputc( '\n', stderr );
75 va_end(ap);
76 exit(1);
77 return 1;
80 int parser_warning(const char *s, ...)
82 va_list ap;
83 va_start(ap, s);
84 generic_msg(s, "Warning", parser_text, ap);
85 va_end(ap);
86 return 0;
89 void fatal_perror( const char *msg, ... )
91 va_list valist;
92 va_start( valist, msg );
93 fprintf(stderr, "Error: ");
94 vfprintf( stderr, msg, valist );
95 perror( " " );
96 va_end( valist );
97 exit(2);
100 void error(const char *s, ...)
102 va_list ap;
103 va_start(ap, s);
104 fprintf(stderr, "Error: ");
105 vfprintf(stderr, s, ap);
106 va_end(ap);
107 exit(2);
110 void warning(const char *s, ...)
112 va_list ap;
113 va_start(ap, s);
114 fprintf(stderr, "Warning: ");
115 vfprintf(stderr, s, ap);
116 va_end(ap);
119 void chat(const char *s, ...)
121 if(debuglevel & DEBUGLEVEL_CHAT)
123 va_list ap;
124 va_start(ap, s);
125 fprintf(stderr, "FYI: ");
126 vfprintf(stderr, s, ap);
127 va_end(ap);
131 int compare_striA( const char *str1, const char *str2 )
133 for (;;)
135 /* only the A-Z range is case-insensitive */
136 char ch1 = (*str1 >= 'a' && *str1 <= 'z') ? *str1 + 'A' - 'a' : *str1;
137 char ch2 = (*str2 >= 'a' && *str2 <= 'z') ? *str2 + 'A' - 'a' : *str2;
138 if (!ch1 || ch1 != ch2) return ch1 - ch2;
139 str1++;
140 str2++;
144 int compare_striW( const WCHAR *str1, const WCHAR *str2 )
146 for (;;)
148 /* only the A-Z range is case-insensitive */
149 WCHAR ch1 = (*str1 >= 'a' && *str1 <= 'z') ? *str1 + 'A' - 'a' : *str1;
150 WCHAR ch2 = (*str2 >= 'a' && *str2 <= 'z') ? *str2 + 'A' - 'a' : *str2;
151 if (!ch1 || ch1 != ch2) return ch1 - ch2;
152 str1++;
153 str2++;
157 int compare_striAW( const char *str1, const WCHAR *str2 )
159 for (;;)
161 /* only the A-Z range is case-insensitive */
162 WCHAR ch1 = (*str1 >= 'a' && *str1 <= 'z') ? *str1 + 'A' - 'a' : (unsigned char)*str1;
163 WCHAR ch2 = (*str2 >= 'a' && *str2 <= 'z') ? *str2 + 'A' - 'a' : *str2;
164 if (!ch1 || ch1 != ch2) return ch1 - ch2;
165 str1++;
166 str2++;
171 *****************************************************************************
172 * Function : compare_name_id
173 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
174 * Input :
175 * Output :
176 * Description :
177 * Remarks :
178 *****************************************************************************
180 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
182 if (n1->type != n2->type) return n1->type == name_ord ? 1 : -1;
183 if (n1->type == name_ord) return n1->name.i_name - n2->name.i_name;
185 if (n1->name.s_name->type == str_char)
187 if (n2->name.s_name->type == str_char)
188 return compare_striA(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
189 return compare_striAW(n1->name.s_name->str.cstr, n2->name.s_name->str.wstr);
191 else
193 if (n2->name.s_name->type == str_char)
194 return -compare_striAW(n2->name.s_name->str.cstr, n1->name.s_name->str.wstr);
195 return compare_striW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
199 #ifdef _WIN32
201 int is_valid_codepage(int id)
203 return IsValidCodePage( id );
206 static WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
208 WCHAR *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
209 DWORD ret = MultiByteToWideChar( codepage, MB_ERR_INVALID_CHARS, src, srclen, dst, srclen );
210 if (!ret) return NULL;
211 dst[ret] = 0;
212 *dstlen = ret;
213 return dst;
216 int get_language_codepage( language_t lang )
218 DWORD codepage;
220 if (!lang) return 1252;
221 if (!GetLocaleInfoW( lang, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
222 (WCHAR *)&codepage, sizeof(codepage)/sizeof(WCHAR) )) return -1;
223 return codepage;
226 #else /* _WIN32 */
228 struct nls_info
230 unsigned short codepage;
231 unsigned short unidef;
232 unsigned short trans_unidef;
233 unsigned short *cp2uni;
234 unsigned short *dbcs_offsets;
237 static struct nls_info nlsinfo[128];
239 static void init_nls_info( struct nls_info *info, unsigned short *ptr )
241 unsigned short hdr_size = ptr[0];
243 info->codepage = ptr[1];
244 info->unidef = ptr[4];
245 info->trans_unidef = ptr[6];
246 ptr += hdr_size;
247 info->cp2uni = ++ptr;
248 ptr += 256;
249 if (*ptr++) ptr += 256; /* glyph table */
250 info->dbcs_offsets = *ptr ? ptr + 1 : NULL;
253 static const struct nls_info *get_nls_info( unsigned int codepage )
255 unsigned short *data;
256 char *path;
257 unsigned int i;
258 size_t size;
260 for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
261 if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
263 assert( i < ARRAY_SIZE(nlsinfo) );
265 for (i = 0; nlsdirs[i]; i++)
267 path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage );
268 if ((data = read_file( path, &size )))
270 free( path );
271 init_nls_info( &nlsinfo[i], data );
272 return &nlsinfo[i];
274 free( path );
276 return NULL;
279 int is_valid_codepage(int cp)
281 return cp == CP_UTF8 || get_nls_info( cp );
284 static WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
286 const struct nls_info *info = get_nls_info( codepage );
287 unsigned int i;
288 WCHAR dbch, *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
290 if (!info) error( "codepage %u not supported\n", codepage );
292 if (info->dbcs_offsets)
294 for (i = 0; srclen; i++, srclen--, src++)
296 unsigned short off = info->dbcs_offsets[(unsigned char)*src];
297 if (off)
299 if (srclen == 1) return NULL;
300 dbch = (src[0] << 8) | (unsigned char)src[1];
301 src++;
302 srclen--;
303 dst[i] = info->dbcs_offsets[off + (unsigned char)*src];
304 if (dst[i] == info->unidef && dbch != info->trans_unidef) return NULL;
306 else
308 dst[i] = info->cp2uni[(unsigned char)*src];
309 if (dst[i] == info->unidef && *src != info->trans_unidef) return NULL;
313 else
315 for (i = 0; i < srclen; i++)
317 dst[i] = info->cp2uni[(unsigned char)src[i]];
318 if (dst[i] == info->unidef && src[i] != info->trans_unidef) return NULL;
321 dst[i] = 0;
322 *dstlen = i;
323 return dst;
326 static const NLS_LOCALE_LCID_INDEX *lcids_index;
327 static const NLS_LOCALE_HEADER *locale_table;
328 static const NLS_LOCALE_LCNAME_INDEX *lcnames_index;
329 static const WCHAR *locale_strings;
331 static void load_locale_nls(void)
333 struct
335 unsigned int ctypes;
336 unsigned int unknown1;
337 unsigned int unknown2;
338 unsigned int unknown3;
339 unsigned int locales;
340 unsigned int charmaps;
341 unsigned int geoids;
342 unsigned int scripts;
343 } *header;
344 char *path;
345 unsigned int i;
346 size_t size;
348 for (i = 0; nlsdirs[i]; i++)
350 path = strmake( "%s/locale.nls", nlsdirs[i] );
351 header = read_file( path, &size );
352 free( path );
353 if (!header) continue;
354 locale_table = (const NLS_LOCALE_HEADER *)((char *)header + header->locales);
355 lcids_index = (const NLS_LOCALE_LCID_INDEX *)((char *)locale_table + locale_table->lcids_offset);
356 lcnames_index = (const NLS_LOCALE_LCNAME_INDEX *)((char *)locale_table + locale_table->lcnames_offset);
357 locale_strings = (const WCHAR *)((char *)locale_table + locale_table->strings_offset);
358 return;
360 error( "unable to load locale.nls\n" );
363 static const NLS_LOCALE_LCID_INDEX *find_lcid_entry( LCID lcid )
365 int min = 0, max = locale_table->nb_lcids - 1;
367 while (min <= max)
369 int pos = (min + max) / 2;
370 if (lcid < lcids_index[pos].id) max = pos - 1;
371 else if (lcid > lcids_index[pos].id) min = pos + 1;
372 else return &lcids_index[pos];
374 return NULL;
377 static const NLS_LOCALE_DATA *get_locale_data( UINT idx )
379 ULONG offset = locale_table->locales_offset + idx * locale_table->locale_size;
380 return (const NLS_LOCALE_DATA *)((const char *)locale_table + offset);
383 int get_language_codepage( language_t lang )
385 const NLS_LOCALE_LCID_INDEX *entry;
387 if (!lang) return 1252;
388 if (lang == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT )) return 1252;
389 if (!locale_table) load_locale_nls();
390 if (!(entry = find_lcid_entry( lang ))) return -1;
391 return get_locale_data( entry->idx )->idefaultansicodepage;
394 #endif /* _WIN32 */
396 static WCHAR *utf8_to_unicode( const char *src, int srclen, int *dstlen )
398 static const char utf8_length[128] =
400 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
401 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
402 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
403 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
404 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
405 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
406 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
407 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
409 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
411 const char *srcend = src + srclen;
412 int len, res;
413 WCHAR *ret, *dst;
415 dst = ret = xmalloc( (srclen + 1) * sizeof(WCHAR) );
416 while (src < srcend)
418 unsigned char ch = *src++;
419 if (ch < 0x80) /* special fast case for 7-bit ASCII */
421 *dst++ = ch;
422 continue;
424 len = utf8_length[ch - 0x80];
425 if (len && src + len <= srcend)
427 res = ch & utf8_mask[len];
428 switch (len)
430 case 3:
431 if ((ch = *src ^ 0x80) >= 0x40) break;
432 res = (res << 6) | ch;
433 src++;
434 if (res < 0x10) break;
435 case 2:
436 if ((ch = *src ^ 0x80) >= 0x40) break;
437 res = (res << 6) | ch;
438 if (res >= 0x110000 >> 6) break;
439 src++;
440 if (res < 0x20) break;
441 if (res >= 0xd800 >> 6 && res <= 0xdfff >> 6) break;
442 case 1:
443 if ((ch = *src ^ 0x80) >= 0x40) break;
444 res = (res << 6) | ch;
445 src++;
446 if (res < 0x80) break;
447 if (res <= 0xffff) *dst++ = res;
448 else
450 res -= 0x10000;
451 *dst++ = 0xd800 | (res >> 10);
452 *dst++ = 0xdc00 | (res & 0x3ff);
454 continue;
457 *dst++ = 0xfffd;
459 *dst = 0;
460 *dstlen = dst - ret;
461 return ret;
464 static char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen )
466 char *ret, *dst;
468 dst = ret = xmalloc( srclen * 3 + 1 );
469 for ( ; srclen; srclen--, src++)
471 unsigned int ch = *src;
473 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
475 *dst++ = ch;
476 continue;
478 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
480 dst[1] = 0x80 | (ch & 0x3f);
481 ch >>= 6;
482 dst[0] = 0xc0 | ch;
483 dst += 2;
484 continue;
486 if (ch >= 0xd800 && ch <= 0xdbff && srclen > 1 && src[1] >= 0xdc00 && src[1] <= 0xdfff)
488 /* 0x10000-0x10ffff: 4 bytes */
489 ch = 0x10000 + ((ch & 0x3ff) << 10) + (src[1] & 0x3ff);
490 dst[3] = 0x80 | (ch & 0x3f);
491 ch >>= 6;
492 dst[2] = 0x80 | (ch & 0x3f);
493 ch >>= 6;
494 dst[1] = 0x80 | (ch & 0x3f);
495 ch >>= 6;
496 dst[0] = 0xf0 | ch;
497 dst += 4;
498 src++;
499 srclen--;
500 continue;
502 if (ch >= 0xd800 && ch <= 0xdfff) ch = 0xfffd; /* invalid surrogate pair */
504 /* 0x800-0xffff: 3 bytes */
505 dst[2] = 0x80 | (ch & 0x3f);
506 ch >>= 6;
507 dst[1] = 0x80 | (ch & 0x3f);
508 ch >>= 6;
509 dst[0] = 0xe0 | ch;
510 dst += 3;
512 *dst = 0;
513 *dstlen = dst - ret;
514 return ret;
517 string_t *convert_string_unicode( const string_t *str, int codepage )
519 string_t *ret = xmalloc(sizeof(*ret));
521 ret->type = str_unicode;
522 ret->loc = str->loc;
524 if (str->type == str_char)
526 if (!codepage) parser_error( "Current language is Unicode only, cannot convert string" );
528 if (codepage == CP_UTF8)
529 ret->str.wstr = utf8_to_unicode( str->str.cstr, str->size, &ret->size );
530 else
531 ret->str.wstr = codepage_to_unicode( codepage, str->str.cstr, str->size, &ret->size );
532 if (!ret->str.wstr) parser_error( "Invalid character in string '%.*s' for codepage %u",
533 str->size, str->str.cstr, codepage );
535 else
537 ret->size = str->size;
538 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
539 memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
540 ret->str.wstr[ret->size] = 0;
542 return ret;
545 char *convert_string_utf8( const string_t *str, int codepage )
547 int len;
548 string_t *wstr = convert_string_unicode( str, codepage );
549 char *ret = unicode_to_utf8( wstr->str.wstr, wstr->size, &len );
550 free_string( wstr );
551 return ret;
554 void free_string(string_t *str)
556 if (str->type == str_unicode) free( str->str.wstr );
557 else free( str->str.cstr );
558 free( str );
561 /* check if the string is valid utf8 despite a different codepage being in use */
562 int check_valid_utf8( const string_t *str, int codepage )
564 int i, count;
565 WCHAR *wstr;
567 if (!check_utf8) return 0;
568 if (!codepage) return 0;
569 if (codepage == CP_UTF8) return 0;
570 if (!is_valid_codepage( codepage )) return 0;
572 for (i = count = 0; i < str->size; i++)
574 if ((unsigned char)str->str.cstr[i] >= 0xf5) goto done;
575 if ((unsigned char)str->str.cstr[i] >= 0xc2) { count++; continue; }
576 if ((unsigned char)str->str.cstr[i] >= 0x80) goto done;
578 if (!count) return 0; /* no 8-bit chars at all */
580 wstr = utf8_to_unicode( str->str.cstr, str->size, &count );
581 for (i = 0; i < count; i++) if (wstr[i] == 0xfffd) break;
582 free( wstr );
583 return (i == count);
585 done:
586 check_utf8 = 0; /* at least one 8-bit non-utf8 string found, stop checking */
587 return 0;
590 const char *get_nameid_str(const name_id_t *n)
592 int len;
594 if (!n) return "<none>";
595 if (n->type == name_ord) return strmake( "%u", n->name.i_name );
596 if (n->name.s_name->type == str_char) return n->name.s_name->str.cstr;
597 return unicode_to_utf8( n->name.s_name->str.wstr, n->name.s_name->size, &len );