oledlg: Initialize the pastelink list.
[wine/hacks.git] / tools / wrc / utils.c
blob2e1630d4b9fe64dd75b0e99eb564875c38e32674
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 <assert.h>
30 #include <ctype.h>
32 #include "wine/unicode.h"
33 #include "wrc.h"
34 #include "utils.h"
35 #include "parser.h"
37 /* #define WANT_NEAR_INDICATION */
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str)
42 while(*str)
44 if(!isprint(*str))
45 *str = ' ';
46 str++;
49 #endif
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
57 char *cpy;
58 if(n)
60 cpy = xstrdup(n);
61 make_print(cpy);
62 fprintf(stderr, " near '%s'", cpy);
63 free(cpy);
66 #endif
67 fprintf(stderr, "\n");
71 int parser_error(const char *s, ...)
73 va_list ap;
74 va_start(ap, s);
75 generic_msg(s, "Error", parser_text, ap);
76 va_end(ap);
77 exit(1);
78 return 1;
81 int parser_warning(const char *s, ...)
83 va_list ap;
84 va_start(ap, s);
85 generic_msg(s, "Warning", parser_text, ap);
86 va_end(ap);
87 return 0;
90 void internal_error(const char *file, int line, const char *s, ...)
92 va_list ap;
93 va_start(ap, s);
94 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
95 vfprintf(stderr, s, ap);
96 fprintf(stderr, "\n");
97 va_end(ap);
98 exit(3);
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 fprintf(stderr, "\n");
108 va_end(ap);
109 exit(2);
112 void warning(const char *s, ...)
114 va_list ap;
115 va_start(ap, s);
116 fprintf(stderr, "Warning: ");
117 vfprintf(stderr, s, ap);
118 fprintf(stderr, "\n");
119 va_end(ap);
122 void chat(const char *s, ...)
124 if(debuglevel & DEBUGLEVEL_CHAT)
126 va_list ap;
127 va_start(ap, s);
128 fprintf(stderr, "FYI: ");
129 vfprintf(stderr, s, ap);
130 fprintf(stderr, "\n");
131 va_end(ap);
135 char *dup_basename(const char *name, const char *ext)
137 int namelen;
138 int extlen = strlen(ext);
139 char *base;
140 char *slash;
142 if(!name)
143 name = "wrc.tab";
145 slash = strrchr(name, '/');
146 if (slash)
147 name = slash + 1;
149 namelen = strlen(name);
151 /* +4 for later extension and +1 for '\0' */
152 base = xmalloc(namelen +4 +1);
153 strcpy(base, name);
154 if(!strcasecmp(name + namelen-extlen, ext))
156 base[namelen - extlen] = '\0';
158 return base;
161 void *xmalloc(size_t size)
163 void *res;
165 assert(size > 0);
166 res = malloc(size);
167 if(res == NULL)
169 error("Virtual memory exhausted.\n");
171 memset(res, 0x55, size);
172 return res;
176 void *xrealloc(void *p, size_t size)
178 void *res;
180 assert(size > 0);
181 res = realloc(p, size);
182 if(res == NULL)
184 error("Virtual memory exhausted.\n");
186 return res;
189 char *xstrdup(const char *str)
191 char *s;
193 assert(str != NULL);
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)
203 * Input :
204 * Output :
205 * Description :
206 * Remarks :
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);
227 else
229 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
232 else if(n1->type == name_ord && n2->type == name_str)
233 return 1;
234 else if(n1->type == name_str && n2->type == name_ord)
235 return -1;
236 else
237 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
238 n1->type, n2->type);
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,
266 NULL, NULL );
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;
285 return ret;
289 void free_string(string_t *str)
291 if (str->type == str_unicode) free( str->str.wstr );
292 else free( str->str.cstr );
293 free( str );
297 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
299 int ok;
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 ));
304 if (!ok)
306 int i;
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 );
320 return ok;
324 struct lang2cp
326 unsigned short lang;
327 unsigned short sublang;
328 unsigned int cp;
329 } lang2cp_t;
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 },
344 #ifdef LANG_BRETON
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 },
352 #ifdef LANG_CORNISH
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 },
369 #ifdef LANG_GAELIC
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 }
421 #ifdef LANG_WALON
422 , { LANG_WALON, SUBLANG_NEUTRAL, 1252 }
423 #endif /* LANG_WALON */
424 #ifdef LANG_WELSH
425 , { LANG_WELSH, SUBLANG_NEUTRAL, 1252 }
426 #endif /* LANG_WELSH */
429 int get_language_codepage( unsigned short lang, unsigned short sublang )
431 unsigned int i;
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)
439 cp = lang2cps[i].cp;
440 break;
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) );
447 return cp;