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
22 #include "wine/port.h"
32 #include "wine/unicode.h"
37 /* #define WANT_NEAR_INDICATION */
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str)
51 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
53 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
54 vfprintf(stderr, s, ap);
55 #ifdef WANT_NEAR_INDICATION
62 fprintf(stderr, " near '%s'", cpy);
67 fprintf(stderr, "\n");
71 int parser_error(const char *s, ...)
75 generic_msg(s, "Error", parser_text, ap);
81 int parser_warning(const char *s, ...)
85 generic_msg(s, "Warning", parser_text, ap);
90 void internal_error(const char *file, int line, const char *s, ...)
94 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
95 vfprintf(stderr, s, ap);
96 fprintf(stderr, "\n");
101 void error(const char *s, ...)
105 fprintf(stderr, "Error: ");
106 vfprintf(stderr, s, ap);
107 fprintf(stderr, "\n");
112 void warning(const char *s, ...)
116 fprintf(stderr, "Warning: ");
117 vfprintf(stderr, s, ap);
118 fprintf(stderr, "\n");
122 void chat(const char *s, ...)
124 if(debuglevel & DEBUGLEVEL_CHAT)
128 fprintf(stderr, "FYI: ");
129 vfprintf(stderr, s, ap);
130 fprintf(stderr, "\n");
135 char *dup_basename(const char *name, const char *ext)
138 int extlen = strlen(ext);
145 slash = strrchr(name, '/');
149 namelen = strlen(name);
151 /* +4 for later extension and +1 for '\0' */
152 base = xmalloc(namelen +4 +1);
154 if(!strcasecmp(name + namelen-extlen, ext))
156 base[namelen - extlen] = '\0';
161 void *xmalloc(size_t size)
169 error("Virtual memory exhausted.\n");
171 memset(res, 0x55, size);
176 void *xrealloc(void *p, size_t size)
181 res = realloc(p, size);
184 error("Virtual memory exhausted.\n");
189 char *xstrdup(const char *str)
194 s = xmalloc(strlen(str)+1);
195 return strcpy(s, str);
200 *****************************************************************************
201 * Function : compare_name_id
202 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
207 *****************************************************************************
209 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
211 if(n1->type == name_ord && n2->type == name_ord)
213 return n1->name.i_name - n2->name.i_name;
215 else if(n1->type == name_str && n2->type == name_str)
217 if(n1->name.s_name->type == str_char
218 && n2->name.s_name->type == str_char)
220 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
222 else if(n1->name.s_name->type == str_unicode
223 && n2->name.s_name->type == str_unicode)
225 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
229 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
232 else if(n1->type == name_ord && n2->type == name_str)
234 else if(n1->type == name_str && n2->type == name_ord)
237 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
240 return 0; /* Keep the compiler happy */
243 string_t *convert_string(const string_t *str, enum str_e type, int codepage)
245 const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
246 string_t *ret = xmalloc(sizeof(*ret));
248 if (!cptable && str->type != type)
249 error( "Current language is Unicode only, cannot convert strings" );
251 if((str->type == str_char) && (type == str_unicode))
253 ret->type = str_unicode;
254 ret->size = wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 );
255 ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
256 wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, ret->str.wstr, ret->size );
257 ret->str.wstr[ret->size] = 0;
259 else if((str->type == str_unicode) && (type == str_char))
261 ret->type = str_char;
262 ret->size = wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size,
263 NULL, 0, NULL, NULL );
264 ret->str.cstr = xmalloc( ret->size + 1 );
265 wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size,
267 ret->str.cstr[ret->size] = 0;
269 else if(str->type == str_unicode)
271 ret->type = str_unicode;
272 ret->size = str->size;
273 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
274 memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
275 ret->str.wstr[ret->size] = 0;
277 else /* str->type == str_char */
279 ret->type = str_char;
280 ret->size = str->size;
281 ret->str.cstr = xmalloc( ret->size + 1 );
282 memcpy( ret->str.cstr, str->str.cstr, ret->size );
283 ret->str.cstr[ret->size] = 0;
289 void free_string(string_t *str)
291 if (str->type == str_unicode) free( str->str.wstr );
292 else free( str->str.cstr );
297 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
300 string_t *teststr = convert_string( str_w, str_char, codepage );
302 ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));
308 fprintf( stderr, "Source: %s", str_a->str.cstr );
309 for (i = 0; i < str_a->size; i++)
310 fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
311 fprintf( stderr, "\nUnicode: " );
312 for (i = 0; i < str_w->size; i++)
313 fprintf( stderr, " %04x", str_w->str.wstr[i] );
314 fprintf( stderr, "\nBack: %s", teststr->str.cstr );
315 for (i = 0; i < teststr->size; i++)
316 fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
317 fprintf( stderr, "\n" );
319 free_string( teststr );
327 unsigned short sublang;
331 /* language to codepage conversion table */
332 /* specific sublanguages need only be specified if their codepage */
333 /* differs from the default (SUBLANG_NEUTRAL) */
334 static const struct lang2cp lang2cps[] =
336 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
337 { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
338 { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
339 { LANG_ARMENIAN, SUBLANG_NEUTRAL, 0 },
340 { LANG_AZERI, SUBLANG_NEUTRAL, 1254 },
341 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, 1251 },
342 { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
343 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, 1251 },
345 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
346 #endif /* LANG_BRETON */
347 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
348 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
349 { LANG_CHINESE, SUBLANG_NEUTRAL, 950 },
350 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, 936 },
351 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, 936 },
353 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
354 #endif /* LANG_CORNISH */
355 { LANG_CROATIAN, SUBLANG_NEUTRAL, 1250 },
356 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
357 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
358 { LANG_DIVEHI, SUBLANG_NEUTRAL, 0 },
359 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
360 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
361 #ifdef LANG_ESPERANTO
362 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
363 #endif /* LANG_ESPERANTO */
364 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
365 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
366 { LANG_FARSI, SUBLANG_NEUTRAL, 1256 },
367 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
368 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
370 { LANG_GAELIC, SUBLANG_NEUTRAL, 1252 },
371 #endif /* LANG_GAELIC */
372 { LANG_GALICIAN, SUBLANG_NEUTRAL, 1252 },
373 { LANG_GEORGIAN, SUBLANG_NEUTRAL, 0 },
374 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
375 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
376 { LANG_GUJARATI, SUBLANG_NEUTRAL, 0 },
377 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
378 { LANG_HINDI, SUBLANG_NEUTRAL, 0 },
379 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
380 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
381 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
382 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
383 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
384 { LANG_KANNADA, SUBLANG_NEUTRAL, 0 },
385 { LANG_KAZAK, SUBLANG_NEUTRAL, 1251 },
386 { LANG_KONKANI, SUBLANG_NEUTRAL, 0 },
387 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
388 { LANG_KYRGYZ, SUBLANG_NEUTRAL, 1251 },
389 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
390 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
391 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
392 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
393 { LANG_MARATHI, SUBLANG_NEUTRAL, 0 },
394 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, 1251 },
395 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
396 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
397 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
398 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
399 { LANG_PUNJABI, SUBLANG_NEUTRAL, 0 },
400 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
401 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
402 { LANG_SANSKRIT, SUBLANG_NEUTRAL, 0 },
403 { LANG_SERBIAN, SUBLANG_NEUTRAL, 1250 },
404 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, 1251 },
405 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
406 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
407 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
408 { LANG_SWAHILI, SUBLANG_NEUTRAL, 1252 },
409 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
410 { LANG_SYRIAC, SUBLANG_NEUTRAL, 0 },
411 { LANG_TAMIL, SUBLANG_NEUTRAL, 0 },
412 { LANG_TATAR, SUBLANG_NEUTRAL, 1251 },
413 { LANG_TELUGU, SUBLANG_NEUTRAL, 0 },
414 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
415 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
416 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
417 { LANG_URDU, SUBLANG_NEUTRAL, 1256 },
418 { LANG_UZBEK, SUBLANG_NEUTRAL, 1254 },
419 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, 1251 },
420 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 }
422 , { LANG_WALON, SUBLANG_NEUTRAL, 1252 }
423 #endif /* LANG_WALON */
425 , { LANG_WELSH, SUBLANG_NEUTRAL, 1252 }
426 #endif /* LANG_WELSH */
429 int get_language_codepage( unsigned short lang, unsigned short sublang )
432 int cp = -1, defcp = -1;
434 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
436 if (lang2cps[i].lang != lang) continue;
437 if (lang2cps[i].sublang == sublang)
442 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
445 if (cp == -1) cp = defcp;
446 assert( cp <= 0 || wine_cp_get_table(cp) );