mshtml: Updated Portuguese (Brazilian) translation.
[wine/multimedia.git] / tools / wrc / utils.c
blobc9445b3a0706998ae2cd267253c0ad3bdea0e21f
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <ctype.h>
31 #include "wine/unicode.h"
32 #include "wrc.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 internal_error(const char *file, int line, const char *s, ...)
91 va_list ap;
92 va_start(ap, s);
93 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
94 vfprintf(stderr, s, ap);
95 va_end(ap);
96 exit(3);
99 void fatal_perror( const char *msg, ... )
101 va_list valist;
102 va_start( valist, msg );
103 fprintf(stderr, "Error: ");
104 vfprintf( stderr, msg, valist );
105 perror( " " );
106 va_end( valist );
107 exit(2);
110 void error(const char *s, ...)
112 va_list ap;
113 va_start(ap, s);
114 fprintf(stderr, "Error: ");
115 vfprintf(stderr, s, ap);
116 va_end(ap);
117 exit(2);
120 void warning(const char *s, ...)
122 va_list ap;
123 va_start(ap, s);
124 fprintf(stderr, "Warning: ");
125 vfprintf(stderr, s, ap);
126 va_end(ap);
129 void chat(const char *s, ...)
131 if(debuglevel & DEBUGLEVEL_CHAT)
133 va_list ap;
134 va_start(ap, s);
135 fprintf(stderr, "FYI: ");
136 vfprintf(stderr, s, ap);
137 va_end(ap);
141 char *dup_basename(const char *name, const char *ext)
143 int namelen;
144 int extlen = strlen(ext);
145 char *base;
146 char *slash;
148 if(!name)
149 name = "wrc.tab";
151 slash = strrchr(name, '/');
152 if (slash)
153 name = slash + 1;
155 namelen = strlen(name);
157 /* +4 for later extension and +1 for '\0' */
158 base = xmalloc(namelen +4 +1);
159 strcpy(base, name);
160 if(!strcasecmp(name + namelen-extlen, ext))
162 base[namelen - extlen] = '\0';
164 return base;
167 void *xmalloc(size_t size)
169 void *res;
171 assert(size > 0);
172 res = malloc(size);
173 if(res == NULL)
175 error("Virtual memory exhausted.\n");
177 memset(res, 0x55, size);
178 return res;
182 void *xrealloc(void *p, size_t size)
184 void *res;
186 assert(size > 0);
187 res = realloc(p, size);
188 if(res == NULL)
190 error("Virtual memory exhausted.\n");
192 return res;
195 char *xstrdup(const char *str)
197 char *s;
199 assert(str != NULL);
200 s = xmalloc(strlen(str)+1);
201 return strcpy(s, str);
206 *****************************************************************************
207 * Function : compare_name_id
208 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
209 * Input :
210 * Output :
211 * Description :
212 * Remarks :
213 *****************************************************************************
215 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
217 if(n1->type == name_ord && n2->type == name_ord)
219 return n1->name.i_name - n2->name.i_name;
221 else if(n1->type == name_str && n2->type == name_str)
223 if(n1->name.s_name->type == str_char
224 && n2->name.s_name->type == str_char)
226 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
228 else if(n1->name.s_name->type == str_unicode
229 && n2->name.s_name->type == str_unicode)
231 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
233 else
235 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type\n");
238 else if(n1->type == name_ord && n2->type == name_str)
239 return 1;
240 else if(n1->type == name_str && n2->type == name_ord)
241 return -1;
242 else
243 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)\n",
244 n1->type, n2->type);
246 return 0; /* Keep the compiler happy */
249 string_t *convert_string(const string_t *str, enum str_e type, int codepage)
251 const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
252 string_t *ret = xmalloc(sizeof(*ret));
253 int res;
255 if (!codepage && str->type != type)
256 parser_error( "Current language is Unicode only, cannot convert string" );
258 if((str->type == str_char) && (type == str_unicode))
260 ret->type = str_unicode;
261 ret->size = cptable ? wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 )
262 : wine_utf8_mbstowcs( 0, str->str.cstr, str->size, NULL, 0 );
263 ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
264 if (cptable)
265 res = wine_cp_mbstowcs( cptable, MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
266 ret->str.wstr, ret->size );
267 else
268 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
269 ret->str.wstr, ret->size );
270 if (res == -2)
271 parser_error( "Invalid character in string '%.*s' for codepage %u",
272 str->size, str->str.cstr, codepage );
273 ret->str.wstr[ret->size] = 0;
275 else if((str->type == str_unicode) && (type == str_char))
277 ret->type = str_char;
278 ret->size = cptable ? wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, NULL, 0, NULL, NULL )
279 : wine_utf8_wcstombs( 0, str->str.wstr, str->size, NULL, 0 );
280 ret->str.cstr = xmalloc( ret->size + 1 );
281 if (cptable)
282 wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size, NULL, NULL );
283 else
284 wine_utf8_wcstombs( 0, str->str.wstr, str->size, ret->str.cstr, ret->size );
285 ret->str.cstr[ret->size] = 0;
287 else if(str->type == str_unicode)
289 ret->type = str_unicode;
290 ret->size = str->size;
291 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
292 memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
293 ret->str.wstr[ret->size] = 0;
295 else /* str->type == str_char */
297 ret->type = str_char;
298 ret->size = str->size;
299 ret->str.cstr = xmalloc( ret->size + 1 );
300 memcpy( ret->str.cstr, str->str.cstr, ret->size );
301 ret->str.cstr[ret->size] = 0;
303 return ret;
307 void free_string(string_t *str)
309 if (str->type == str_unicode) free( str->str.wstr );
310 else free( str->str.cstr );
311 free( str );
314 /* check if the string is valid utf8 despite a different codepage being in use */
315 int check_valid_utf8( const string_t *str, int codepage )
317 unsigned int i;
319 if (!check_utf8) return 0;
320 if (!codepage) return 0;
321 if (!wine_cp_get_table( codepage )) return 0;
323 for (i = 0; i < str->size; i++)
325 if ((unsigned char)str->str.cstr[i] >= 0xf5) goto done;
326 if ((unsigned char)str->str.cstr[i] >= 0xc2) break;
327 if ((unsigned char)str->str.cstr[i] >= 0x80) goto done;
329 if (i == str->size) return 0; /* no 8-bit chars at all */
331 if (wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size, NULL, 0 ) >= 0) return 1;
333 done:
334 check_utf8 = 0; /* at least one 8-bit non-utf8 string found, stop checking */
335 return 0;
338 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
340 int ok;
341 string_t *teststr = convert_string( str_w, str_char, codepage );
343 ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));
345 if (!ok)
347 int i;
349 fprintf( stderr, "Source: %s", str_a->str.cstr );
350 for (i = 0; i < str_a->size; i++)
351 fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
352 fprintf( stderr, "\nUnicode: " );
353 for (i = 0; i < str_w->size; i++)
354 fprintf( stderr, " %04x", str_w->str.wstr[i] );
355 fprintf( stderr, "\nBack: %s", teststr->str.cstr );
356 for (i = 0; i < teststr->size; i++)
357 fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
358 fprintf( stderr, "\n" );
360 free_string( teststr );
361 return ok;
365 struct lang2cp
367 unsigned short lang;
368 unsigned short sublang;
369 unsigned int cp;
372 /* language to codepage conversion table */
373 /* specific sublanguages need only be specified if their codepage */
374 /* differs from the default (SUBLANG_NEUTRAL) */
375 static const struct lang2cp lang2cps[] =
377 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
378 { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
379 { LANG_ALSATIAN, SUBLANG_NEUTRAL, 1252 },
380 { LANG_AMHARIC, SUBLANG_NEUTRAL, 0 },
381 { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
382 { LANG_ARMENIAN, SUBLANG_NEUTRAL, 0 },
383 { LANG_ASSAMESE, SUBLANG_NEUTRAL, 0 },
384 { LANG_AZERI, SUBLANG_NEUTRAL, 1254 },
385 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, 1251 },
386 { LANG_BASHKIR, SUBLANG_NEUTRAL, 1251 },
387 { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
388 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, 1251 },
389 { LANG_BENGALI, SUBLANG_NEUTRAL, 0 },
390 { LANG_BOSNIAN, SUBLANG_NEUTRAL, 1250 },
391 { LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, 1251 },
392 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
393 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
394 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
395 { LANG_CHINESE, SUBLANG_NEUTRAL, 950 },
396 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, 936 },
397 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, 936 },
398 #ifdef LANG_CORNISH
399 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
400 #endif /* LANG_CORNISH */
401 { LANG_CORSICAN, SUBLANG_NEUTRAL, 1252 },
402 { LANG_CROATIAN, SUBLANG_NEUTRAL, 1250 },
403 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
404 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
405 { LANG_DARI, SUBLANG_NEUTRAL, 1256 },
406 { LANG_DIVEHI, SUBLANG_NEUTRAL, 0 },
407 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
408 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
409 #ifdef LANG_ESPERANTO
410 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
411 #endif /* LANG_ESPERANTO */
412 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
413 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
414 { LANG_FILIPINO, SUBLANG_NEUTRAL, 1252 },
415 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
416 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
417 { LANG_FRISIAN, SUBLANG_NEUTRAL, 1252 },
418 #ifdef LANG_GAELIC
419 { LANG_GAELIC, SUBLANG_NEUTRAL, 1252 },
420 #endif /* LANG_GAELIC */
421 { LANG_GALICIAN, SUBLANG_NEUTRAL, 1252 },
422 { LANG_GEORGIAN, SUBLANG_NEUTRAL, 0 },
423 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
424 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
425 { LANG_GREENLANDIC, SUBLANG_NEUTRAL, 1252 },
426 { LANG_GUJARATI, SUBLANG_NEUTRAL, 0 },
427 { LANG_HAUSA, SUBLANG_NEUTRAL, 1252 },
428 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
429 { LANG_HINDI, SUBLANG_NEUTRAL, 0 },
430 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
431 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
432 { LANG_IGBO, SUBLANG_NEUTRAL, 1252 },
433 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
434 { LANG_INUKTITUT, SUBLANG_NEUTRAL, 0 },
435 { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA_LATIN, 0 },
436 { LANG_IRISH, SUBLANG_NEUTRAL, 1252 },
437 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
438 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
439 { LANG_KANNADA, SUBLANG_NEUTRAL, 0 },
440 { LANG_KAZAK, SUBLANG_NEUTRAL, 1251 },
441 { LANG_KHMER, SUBLANG_NEUTRAL, 0 },
442 { LANG_KICHE, SUBLANG_NEUTRAL, 1252 },
443 { LANG_KINYARWANDA, SUBLANG_NEUTRAL, 1252 },
444 { LANG_KONKANI, SUBLANG_NEUTRAL, 0 },
445 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
446 { LANG_KYRGYZ, SUBLANG_NEUTRAL, 1251 },
447 { LANG_LAO, SUBLANG_NEUTRAL, 0 },
448 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
449 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
450 { LANG_LOWER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
451 { LANG_LUXEMBOURGISH, SUBLANG_NEUTRAL, 1252 },
452 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
453 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
454 { LANG_MALAYALAM, SUBLANG_NEUTRAL, 0 },
455 { LANG_MALTESE, SUBLANG_NEUTRAL, 0 },
456 { LANG_MAORI, SUBLANG_NEUTRAL, 0 },
457 { LANG_MAPUDUNGUN, SUBLANG_NEUTRAL, 1252 },
458 { LANG_MARATHI, SUBLANG_NEUTRAL, 0 },
459 { LANG_MOHAWK, SUBLANG_NEUTRAL, 1252 },
460 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, 1251 },
461 { LANG_NEPALI, SUBLANG_NEUTRAL, 0 },
462 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
463 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
464 { LANG_OCCITAN, SUBLANG_NEUTRAL, 1252 },
465 { LANG_ORIYA, SUBLANG_NEUTRAL, 0 },
466 { LANG_PASHTO, SUBLANG_NEUTRAL, 0 },
467 { LANG_PERSIAN, SUBLANG_NEUTRAL, 1256 },
468 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
469 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
470 { LANG_PUNJABI, SUBLANG_NEUTRAL, 0 },
471 { LANG_QUECHUA, SUBLANG_NEUTRAL, 1252 },
472 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
473 { LANG_ROMANSH, SUBLANG_NEUTRAL, 1252 },
474 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
475 { LANG_SAMI, SUBLANG_NEUTRAL, 1252 },
476 { LANG_SANSKRIT, SUBLANG_NEUTRAL, 0 },
477 { LANG_SERBIAN, SUBLANG_NEUTRAL, 1250 },
478 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, 1251 },
479 { LANG_SINHALESE, SUBLANG_NEUTRAL, 0 },
480 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
481 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
482 { LANG_SOTHO, SUBLANG_NEUTRAL, 1252 },
483 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
484 { LANG_SWAHILI, SUBLANG_NEUTRAL, 1252 },
485 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
486 { LANG_SYRIAC, SUBLANG_NEUTRAL, 0 },
487 { LANG_TAJIK, SUBLANG_NEUTRAL, 1251 },
488 { LANG_TAMAZIGHT, SUBLANG_NEUTRAL, 1252 },
489 { LANG_TAMIL, SUBLANG_NEUTRAL, 0 },
490 { LANG_TATAR, SUBLANG_NEUTRAL, 1251 },
491 { LANG_TELUGU, SUBLANG_NEUTRAL, 0 },
492 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
493 { LANG_TIBETAN, SUBLANG_NEUTRAL, 0 },
494 { LANG_TSWANA, SUBLANG_NEUTRAL, 1252 },
495 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
496 { LANG_TURKMEN, SUBLANG_NEUTRAL, 1250 },
497 { LANG_UIGHUR, SUBLANG_NEUTRAL, 1256 },
498 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
499 { LANG_UPPER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
500 { LANG_URDU, SUBLANG_NEUTRAL, 1256 },
501 { LANG_UZBEK, SUBLANG_NEUTRAL, 1254 },
502 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, 1251 },
503 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 },
504 #ifdef LANG_WALON
505 { LANG_WALON, SUBLANG_NEUTRAL, 1252 },
506 #endif /* LANG_WALON */
507 { LANG_WELSH, SUBLANG_NEUTRAL, 1252 },
508 { LANG_WOLOF, SUBLANG_NEUTRAL, 1252 },
509 { LANG_XHOSA, SUBLANG_NEUTRAL, 1252 },
510 { LANG_YAKUT, SUBLANG_NEUTRAL, 1251 },
511 { LANG_YI, SUBLANG_NEUTRAL, 0 },
512 { LANG_YORUBA, SUBLANG_NEUTRAL, 1252 },
513 { LANG_ZULU, SUBLANG_NEUTRAL, 1252 }
516 int get_language_codepage( unsigned short lang, unsigned short sublang )
518 unsigned int i;
519 int cp = -1, defcp = -1;
521 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
523 if (lang2cps[i].lang != lang) continue;
524 if (lang2cps[i].sublang == sublang)
526 cp = lang2cps[i].cp;
527 break;
529 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
532 if (cp == -1) cp = defcp;
533 assert( cp <= 0 || wine_cp_get_table(cp) );
534 return cp;