gdi32: SetWinMetaFileBits: Use the whole device surface if the METAFILEPICT parameter...
[wine] / tools / wrc / utils.c
1 /*
2  * Utility routines
3  *
4  * Copyright 1998 Bertho A. Stultiens
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
31
32 #include "wine/unicode.h"
33 #include "wrc.h"
34 #include "utils.h"
35 #include "parser.h"
36
37 /* #define WANT_NEAR_INDICATION */
38
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str)
41 {
42         while(*str)
43         {
44                 if(!isprint(*str))
45                         *str = ' ';
46                 str++;
47         }
48 }
49 #endif
50
51 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
52 {
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
56         {
57                 char *cpy;
58                 if(n)
59                 {
60                         cpy = xstrdup(n);
61                         make_print(cpy);
62                         fprintf(stderr, " near '%s'", cpy);
63                         free(cpy);
64                 }
65         }
66 #endif
67         fprintf(stderr, "\n");
68 }
69
70
71 int yyerror(const char *s, ...)
72 {
73         va_list ap;
74         va_start(ap, s);
75         generic_msg(s, "Error", yytext, ap);
76         va_end(ap);
77         exit(1);
78         return 1;
79 }
80
81 int yywarning(const char *s, ...)
82 {
83         va_list ap;
84         va_start(ap, s);
85         generic_msg(s, "Warning", yytext, ap);
86         va_end(ap);
87         return 0;
88 }
89
90 void internal_error(const char *file, int line, const char *s, ...)
91 {
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);
99 }
100
101 void error(const char *s, ...)
102 {
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);
110 }
111
112 void warning(const char *s, ...)
113 {
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);
120 }
121
122 void chat(const char *s, ...)
123 {
124         if(debuglevel & DEBUGLEVEL_CHAT)
125         {
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);
132         }
133 }
134
135 char *dup_basename(const char *name, const char *ext)
136 {
137         int namelen;
138         int extlen = strlen(ext);
139         char *base;
140         char *slash;
141
142         if(!name)
143                 name = "wrc.tab";
144
145         slash = strrchr(name, '/');
146         if (slash)
147                 name = slash + 1;
148
149         namelen = strlen(name);
150
151         /* +4 for later extension and +1 for '\0' */
152         base = (char *)xmalloc(namelen +4 +1);
153         strcpy(base, name);
154         if(!strcasecmp(name + namelen-extlen, ext))
155         {
156                 base[namelen - extlen] = '\0';
157         }
158         return base;
159 }
160
161 void *xmalloc(size_t size)
162 {
163     void *res;
164
165     assert(size > 0);
166     res = malloc(size);
167     if(res == NULL)
168     {
169         error("Virtual memory exhausted.\n");
170     }
171     /*
172      * We set it to 0.
173      * This is *paramount* because we depend on it
174      * just about everywhere in the rest of the code.
175      */
176     memset(res, 0, size);
177     return res;
178 }
179
180
181 void *xrealloc(void *p, size_t size)
182 {
183     void *res;
184
185     assert(size > 0);
186     res = realloc(p, size);
187     if(res == NULL)
188     {
189         error("Virtual memory exhausted.\n");
190     }
191     return res;
192 }
193
194 char *xstrdup(const char *str)
195 {
196         char *s;
197
198         assert(str != NULL);
199         s = (char *)xmalloc(strlen(str)+1);
200         return strcpy(s, str);
201 }
202
203
204 /*
205  *****************************************************************************
206  * Function     : compare_name_id
207  * Syntax       : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
208  * Input        :
209  * Output       :
210  * Description  :
211  * Remarks      :
212  *****************************************************************************
213 */
214 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
215 {
216         if(n1->type == name_ord && n2->type == name_ord)
217         {
218                 return n1->name.i_name - n2->name.i_name;
219         }
220         else if(n1->type == name_str && n2->type == name_str)
221         {
222                 if(n1->name.s_name->type == str_char
223                 && n2->name.s_name->type == str_char)
224                 {
225                         return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
226                 }
227                 else if(n1->name.s_name->type == str_unicode
228                 && n2->name.s_name->type == str_unicode)
229                 {
230                         return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
231                 }
232                 else
233                 {
234                         internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
235                 }
236         }
237         else if(n1->type == name_ord && n2->type == name_str)
238                 return 1;
239         else if(n1->type == name_str && n2->type == name_ord)
240                 return -1;
241         else
242                 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
243                                 n1->type, n2->type);
244
245         return 0; /* Keep the compiler happy */
246 }
247
248 string_t *convert_string(const string_t *str, enum str_e type, int codepage)
249 {
250     const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
251     string_t *ret = xmalloc(sizeof(*ret));
252
253     if (!cptable && str->type != type)
254         error( "Current language is Unicode only, cannot convert strings" );
255
256     if((str->type == str_char) && (type == str_unicode))
257     {
258         ret->type     = str_unicode;
259         ret->size     = wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 );
260         ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
261         wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, ret->str.wstr, ret->size );
262         ret->str.wstr[ret->size] = 0;
263     }
264     else if((str->type == str_unicode) && (type == str_char))
265     {
266         ret->type     = str_char;
267         ret->size     = wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size,
268                                           NULL, 0, NULL, NULL );
269         ret->str.cstr = xmalloc( ret->size + 1 );
270         wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size,
271                      NULL, NULL );
272         ret->str.cstr[ret->size] = 0;
273     }
274     else if(str->type == str_unicode)
275     {
276         ret->type     = str_unicode;
277         ret->size     = str->size;
278         ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
279         memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
280         ret->str.wstr[ret->size] = 0;
281     }
282     else /* str->type == str_char */
283     {
284         ret->type     = str_char;
285         ret->size     = str->size;
286         ret->str.cstr = xmalloc( ret->size + 1 );
287         memcpy( ret->str.cstr, str->str.cstr, ret->size );
288         ret->str.cstr[ret->size] = 0;
289     }
290     return ret;
291 }
292
293
294 void free_string(string_t *str)
295 {
296     if (str->type == str_unicode) free( str->str.wstr );
297     else free( str->str.cstr );
298     free( str );
299 }
300
301
302 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
303 {
304     int ok;
305     string_t *teststr = convert_string( str_w, str_char, codepage );
306
307     ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));
308
309     if (!ok)
310     {
311         int i;
312
313         fprintf( stderr, "Source: %s", str_a->str.cstr );
314         for (i = 0; i < str_a->size; i++)
315             fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
316         fprintf( stderr, "\nUnicode: " );
317         for (i = 0; i < str_w->size; i++)
318             fprintf( stderr, " %04x", str_w->str.wstr[i] );
319         fprintf( stderr, "\nBack: %s", teststr->str.cstr );
320         for (i = 0; i < teststr->size; i++)
321             fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
322         fprintf( stderr, "\n" );
323     }
324     free_string( teststr );
325     return ok;
326 }
327
328
329 struct lang2cp
330 {
331     unsigned short lang;
332     unsigned short sublang;
333     unsigned int   cp;
334 } lang2cp_t;
335
336 /* language to codepage conversion table */
337 /* specific sublanguages need only be specified if their codepage */
338 /* differs from the default (SUBLANG_NEUTRAL) */
339 static const struct lang2cp lang2cps[] =
340 {
341     { LANG_AFRIKAANS,      SUBLANG_NEUTRAL,              1252 },
342     { LANG_ALBANIAN,       SUBLANG_NEUTRAL,              1250 },
343     { LANG_ARABIC,         SUBLANG_NEUTRAL,              1256 },
344     { LANG_ARMENIAN,       SUBLANG_NEUTRAL,              0    },
345     { LANG_AZERI,          SUBLANG_NEUTRAL,              1254 },
346     { LANG_AZERI,          SUBLANG_AZERI_CYRILLIC,       1251 },
347     { LANG_BASQUE,         SUBLANG_NEUTRAL,              1252 },
348     { LANG_BELARUSIAN,     SUBLANG_NEUTRAL,              1251 },
349 #ifdef LANG_BRETON
350     { LANG_BRETON,         SUBLANG_NEUTRAL,              1252 },
351 #endif /* LANG_BRETON */
352     { LANG_BULGARIAN,      SUBLANG_NEUTRAL,              1251 },
353     { LANG_CATALAN,        SUBLANG_NEUTRAL,              1252 },
354     { LANG_CHINESE,        SUBLANG_NEUTRAL,              950  },
355     { LANG_CHINESE,        SUBLANG_CHINESE_SINGAPORE,    936  },
356     { LANG_CHINESE,        SUBLANG_CHINESE_SIMPLIFIED,   936  },
357 #ifdef LANG_CORNISH
358     { LANG_CORNISH,        SUBLANG_NEUTRAL,              1252 },
359 #endif /* LANG_CORNISH */
360     { LANG_CROATIAN,       SUBLANG_NEUTRAL,              1250 },
361     { LANG_CZECH,          SUBLANG_NEUTRAL,              1250 },
362     { LANG_DANISH,         SUBLANG_NEUTRAL,              1252 },
363     { LANG_DIVEHI,         SUBLANG_NEUTRAL,              0    },
364     { LANG_DUTCH,          SUBLANG_NEUTRAL,              1252 },
365     { LANG_ENGLISH,        SUBLANG_NEUTRAL,              1252 },
366 #ifdef LANG_ESPERANTO
367     { LANG_ESPERANTO,      SUBLANG_NEUTRAL,              1252 },
368 #endif /* LANG_ESPERANTO */
369     { LANG_ESTONIAN,       SUBLANG_NEUTRAL,              1257 },
370     { LANG_FAEROESE,       SUBLANG_NEUTRAL,              1252 },
371     { LANG_FARSI,          SUBLANG_NEUTRAL,              1256 },
372     { LANG_FINNISH,        SUBLANG_NEUTRAL,              1252 },
373     { LANG_FRENCH,         SUBLANG_NEUTRAL,              1252 },
374 #ifdef LANG_GAELIC
375     { LANG_GAELIC,         SUBLANG_NEUTRAL,              1252 },
376 #endif /* LANG_GAELIC */
377     { LANG_GALICIAN,       SUBLANG_NEUTRAL,              1252 },
378     { LANG_GEORGIAN,       SUBLANG_NEUTRAL,              0    },
379     { LANG_GERMAN,         SUBLANG_NEUTRAL,              1252 },
380     { LANG_GREEK,          SUBLANG_NEUTRAL,              1253 },
381     { LANG_GUJARATI,       SUBLANG_NEUTRAL,              0    },
382     { LANG_HEBREW,         SUBLANG_NEUTRAL,              1255 },
383     { LANG_HINDI,          SUBLANG_NEUTRAL,              0    },
384     { LANG_HUNGARIAN,      SUBLANG_NEUTRAL,              1250 },
385     { LANG_ICELANDIC,      SUBLANG_NEUTRAL,              1252 },
386     { LANG_INDONESIAN,     SUBLANG_NEUTRAL,              1252 },
387     { LANG_ITALIAN,        SUBLANG_NEUTRAL,              1252 },
388     { LANG_JAPANESE,       SUBLANG_NEUTRAL,              932  },
389     { LANG_KANNADA,        SUBLANG_NEUTRAL,              0    },
390     { LANG_KAZAK,          SUBLANG_NEUTRAL,              1251 },
391     { LANG_KONKANI,        SUBLANG_NEUTRAL,              0    },
392     { LANG_KOREAN,         SUBLANG_NEUTRAL,              949  },
393     { LANG_KYRGYZ,         SUBLANG_NEUTRAL,              1251 },
394     { LANG_LATVIAN,        SUBLANG_NEUTRAL,              1257 },
395     { LANG_LITHUANIAN,     SUBLANG_NEUTRAL,              1257 },
396     { LANG_MACEDONIAN,     SUBLANG_NEUTRAL,              1251 },
397     { LANG_MALAY,          SUBLANG_NEUTRAL,              1252 },
398     { LANG_MARATHI,        SUBLANG_NEUTRAL,              0    },
399     { LANG_MONGOLIAN,      SUBLANG_NEUTRAL,              1251 },
400     { LANG_NEUTRAL,        SUBLANG_NEUTRAL,              1252 },
401     { LANG_NORWEGIAN,      SUBLANG_NEUTRAL,              1252 },
402     { LANG_POLISH,         SUBLANG_NEUTRAL,              1250 },
403     { LANG_PORTUGUESE,     SUBLANG_NEUTRAL,              1252 },
404     { LANG_PUNJABI,        SUBLANG_NEUTRAL,              0    },
405     { LANG_ROMANIAN,       SUBLANG_NEUTRAL,              1250 },
406     { LANG_RUSSIAN,        SUBLANG_NEUTRAL,              1251 },
407     { LANG_SANSKRIT,       SUBLANG_NEUTRAL,              0    },
408     { LANG_SERBIAN,        SUBLANG_NEUTRAL,              1250 },
409     { LANG_SERBIAN,        SUBLANG_SERBIAN_CYRILLIC,     1251 },
410     { LANG_SLOVAK,         SUBLANG_NEUTRAL,              1250 },
411     { LANG_SLOVENIAN,      SUBLANG_NEUTRAL,              1250 },
412     { LANG_SPANISH,        SUBLANG_NEUTRAL,              1252 },
413     { LANG_SWAHILI,        SUBLANG_NEUTRAL,              1252 },
414     { LANG_SWEDISH,        SUBLANG_NEUTRAL,              1252 },
415     { LANG_SYRIAC,         SUBLANG_NEUTRAL,              0    },
416     { LANG_TAMIL,          SUBLANG_NEUTRAL,              0    },
417     { LANG_TATAR,          SUBLANG_NEUTRAL,              1251 },
418     { LANG_TELUGU,         SUBLANG_NEUTRAL,              0    },
419     { LANG_THAI,           SUBLANG_NEUTRAL,              874  },
420     { LANG_TURKISH,        SUBLANG_NEUTRAL,              1254 },
421     { LANG_UKRAINIAN,      SUBLANG_NEUTRAL,              1251 },
422     { LANG_URDU,           SUBLANG_NEUTRAL,              1256 },
423     { LANG_UZBEK,          SUBLANG_NEUTRAL,              1254 },
424     { LANG_UZBEK,          SUBLANG_UZBEK_CYRILLIC,       1251 },
425     { LANG_VIETNAMESE,     SUBLANG_NEUTRAL,              1258 }
426 #ifdef LANG_WALON
427     , { LANG_WALON,          SUBLANG_NEUTRAL,              1252 }
428 #endif /* LANG_WALON */
429 #ifdef LANG_WELSH
430     , { LANG_WELSH,          SUBLANG_NEUTRAL,              1252 }
431 #endif /* LANG_WELSH */
432 };
433
434 int get_language_codepage( unsigned short lang, unsigned short sublang )
435 {
436     unsigned int i;
437     int cp = -1, defcp = -1;
438
439     for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
440     {
441         if (lang2cps[i].lang != lang) continue;
442         if (lang2cps[i].sublang == sublang)
443         {
444             cp = lang2cps[i].cp;
445             break;
446         }
447         if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
448     }
449
450     if (cp == -1) cp = defcp;
451     assert( cp <= 0 || wine_cp_get_table(cp) );
452     return cp;
453 }