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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "wine/unicode.h"
37 /* #define WANT_NEAR_INDICATION */
39 static const union cptable
*current_codepage
;
41 #ifdef WANT_NEAR_INDICATION
42 void make_print(char *str
)
53 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
55 fprintf(stderr
, "%s:%d:%d: %s: ", input_name
? input_name
: "stdin", line_number
, char_number
, t
);
56 vfprintf(stderr
, s
, ap
);
57 #ifdef WANT_NEAR_INDICATION
64 fprintf(stderr
, " near '%s'", cpy
);
69 fprintf(stderr
, "\n");
73 int yyerror(const char *s
, ...)
77 generic_msg(s
, "Error", yytext
, ap
);
83 int yywarning(const char *s
, ...)
87 generic_msg(s
, "Warning", yytext
, ap
);
92 int pperror(const char *s
, ...)
96 generic_msg(s
, "Error", pptext
, ap
);
102 int ppwarning(const char *s
, ...)
106 generic_msg(s
, "Warning", pptext
, ap
);
112 void internal_error(const char *file
, int line
, const char *s
, ...)
116 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
117 vfprintf(stderr
, s
, ap
);
118 fprintf(stderr
, "\n");
123 void error(const char *s
, ...)
127 fprintf(stderr
, "Error: ");
128 vfprintf(stderr
, s
, ap
);
129 fprintf(stderr
, "\n");
134 void warning(const char *s
, ...)
138 fprintf(stderr
, "Warning: ");
139 vfprintf(stderr
, s
, ap
);
140 fprintf(stderr
, "\n");
144 void chat(const char *s
, ...)
146 if(debuglevel
& DEBUGLEVEL_CHAT
)
150 fprintf(stderr
, "FYI: ");
151 vfprintf(stderr
, s
, ap
);
152 fprintf(stderr
, "\n");
157 char *dup_basename(const char *name
, const char *ext
)
160 int extlen
= strlen(ext
);
167 slash
= strrchr(name
, '/');
171 namelen
= strlen(name
);
173 /* +4 for later extension and +1 for '\0' */
174 base
= (char *)xmalloc(namelen
+4 +1);
176 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
178 base
[namelen
- extlen
] = '\0';
183 void *xmalloc(size_t size
)
191 error("Virtual memory exhausted.\n");
195 * This is *paramount* because we depend on it
196 * just about everywhere in the rest of the code.
198 memset(res
, 0, size
);
203 void *xrealloc(void *p
, size_t size
)
208 res
= realloc(p
, size
);
211 error("Virtual memory exhausted.\n");
216 char *xstrdup(const char *str
)
221 s
= (char *)xmalloc(strlen(str
)+1);
222 return strcpy(s
, str
);
225 short *dupcstr2wstr(const char *str
)
230 if (!current_codepage
) set_language( LANG_NEUTRAL
, SUBLANG_NEUTRAL
);
231 len
= cp_mbstowcs( current_codepage
, 0, str
, strlen(str
), NULL
, 0 );
232 ws
= xmalloc( sizeof(WCHAR
) * (len
+ 1) );
233 len
= cp_mbstowcs( current_codepage
, 0, str
, strlen(str
), ws
, len
);
238 char *dupwstr2cstr(const short *str
)
243 if (!current_codepage
) set_language( LANG_NEUTRAL
, SUBLANG_NEUTRAL
);
244 len
= cp_wcstombs( current_codepage
, 0, str
, strlenW(str
), NULL
, 0, NULL
, NULL
);
245 cs
= xmalloc( len
+ 1 );
246 len
= cp_wcstombs( current_codepage
, 0, str
, strlenW(str
), cs
, len
, NULL
, NULL
);
252 *****************************************************************************
253 * Function : compare_name_id
254 * Syntax : int compare_name_id(name_id_t *n1, name_id_t *n2)
259 *****************************************************************************
261 int compare_name_id(name_id_t
*n1
, name_id_t
*n2
)
263 if(n1
->type
== name_ord
&& n2
->type
== name_ord
)
265 return n1
->name
.i_name
- n2
->name
.i_name
;
267 else if(n1
->type
== name_str
&& n2
->type
== name_str
)
269 if(n1
->name
.s_name
->type
== str_char
270 && n2
->name
.s_name
->type
== str_char
)
272 return strcasecmp(n1
->name
.s_name
->str
.cstr
, n2
->name
.s_name
->str
.cstr
);
274 else if(n1
->name
.s_name
->type
== str_unicode
275 && n2
->name
.s_name
->type
== str_unicode
)
277 return strcmpiW(n1
->name
.s_name
->str
.wstr
, n2
->name
.s_name
->str
.wstr
);
281 internal_error(__FILE__
, __LINE__
, "Can't yet compare strings of mixed type");
284 else if(n1
->type
== name_ord
&& n2
->type
== name_str
)
286 else if(n1
->type
== name_str
&& n2
->type
== name_ord
)
289 internal_error(__FILE__
, __LINE__
, "Comparing name-ids with unknown types (%d, %d)",
292 return 0; /* Keep the compiler happy */
295 string_t
*convert_string(const string_t
*str
, enum str_e type
)
297 string_t
*ret
= xmalloc(sizeof(*ret
));
299 if((str
->type
== str_char
) && (type
== str_unicode
))
301 ret
->str
.wstr
= dupcstr2wstr(str
->str
.cstr
);
302 ret
->type
= str_unicode
;
303 ret
->size
= strlenW(ret
->str
.wstr
);
305 else if((str
->type
== str_unicode
) && (type
== str_char
))
307 ret
->str
.cstr
= dupwstr2cstr(str
->str
.wstr
);
308 ret
->type
= str_char
;
309 ret
->size
= strlen(ret
->str
.cstr
);
311 else if(str
->type
== str_unicode
)
313 ret
->type
= str_unicode
;
314 ret
->size
= strlenW(str
->str
.wstr
);
315 ret
->str
.wstr
= xmalloc(sizeof(WCHAR
)*(ret
->size
+1));
316 strcpyW(ret
->str
.wstr
, str
->str
.wstr
);
318 else /* str->type == str_char */
320 ret
->type
= str_char
;
321 ret
->size
= strlen(str
->str
.cstr
);
322 ret
->str
.cstr
= xmalloc( ret
->size
+ 1 );
323 strcpy(ret
->str
.cstr
, str
->str
.cstr
);
332 unsigned short sublang
;
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
[] =
341 { LANG_AFRIKAANS
, SUBLANG_NEUTRAL
, 1252 },
342 { LANG_ALBANIAN
, SUBLANG_NEUTRAL
, 1250 },
343 { LANG_ARABIC
, SUBLANG_NEUTRAL
, 1256 },
344 { LANG_BASQUE
, SUBLANG_NEUTRAL
, 1252 },
345 { LANG_BRETON
, SUBLANG_NEUTRAL
, 1252 },
346 { LANG_BULGARIAN
, SUBLANG_NEUTRAL
, 1251 },
347 { LANG_BYELORUSSIAN
, SUBLANG_NEUTRAL
, 1251 },
348 { LANG_CATALAN
, SUBLANG_NEUTRAL
, 1252 },
349 { LANG_CHINESE
, SUBLANG_NEUTRAL
, 936 },
350 { LANG_CORNISH
, SUBLANG_NEUTRAL
, 1252 },
351 { LANG_CZECH
, SUBLANG_NEUTRAL
, 1250 },
352 { LANG_DANISH
, SUBLANG_NEUTRAL
, 1252 },
353 { LANG_DUTCH
, SUBLANG_NEUTRAL
, 1252 },
354 { LANG_ENGLISH
, SUBLANG_NEUTRAL
, 1252 },
355 { LANG_ESPERANTO
, SUBLANG_NEUTRAL
, 1252 },
356 { LANG_ESTONIAN
, SUBLANG_NEUTRAL
, 1257 },
357 { LANG_FAEROESE
, SUBLANG_NEUTRAL
, 1252 },
358 { LANG_FINNISH
, SUBLANG_NEUTRAL
, 1252 },
359 { LANG_FRENCH
, SUBLANG_NEUTRAL
, 1252 },
360 { LANG_GAELIC
, SUBLANG_NEUTRAL
, 1252 },
361 { LANG_GERMAN
, SUBLANG_NEUTRAL
, 1252 },
362 { LANG_GREEK
, SUBLANG_NEUTRAL
, 1253 },
363 { LANG_HEBREW
, SUBLANG_NEUTRAL
, 1255 },
364 { LANG_HUNGARIAN
, SUBLANG_NEUTRAL
, 1250 },
365 { LANG_ICELANDIC
, SUBLANG_NEUTRAL
, 1252 },
366 { LANG_INDONESIAN
, SUBLANG_NEUTRAL
, 1252 },
367 { LANG_ITALIAN
, SUBLANG_NEUTRAL
, 1252 },
368 { LANG_JAPANESE
, SUBLANG_NEUTRAL
, 932 },
369 { LANG_KOREAN
, SUBLANG_NEUTRAL
, 949 },
370 { LANG_LATVIAN
, SUBLANG_NEUTRAL
, 1257 },
371 { LANG_LITHUANIAN
, SUBLANG_NEUTRAL
, 1257 },
372 { LANG_MACEDONIAN
, SUBLANG_NEUTRAL
, 1251 },
373 { LANG_MALAY
, SUBLANG_NEUTRAL
, 1252 },
374 { LANG_NEUTRAL
, SUBLANG_NEUTRAL
, 1252 },
375 { LANG_NORWEGIAN
, SUBLANG_NEUTRAL
, 1252 },
376 { LANG_POLISH
, SUBLANG_NEUTRAL
, 1250 },
377 { LANG_PORTUGUESE
, SUBLANG_NEUTRAL
, 1252 },
378 { LANG_ROMANIAN
, SUBLANG_NEUTRAL
, 1250 },
379 { LANG_RUSSIAN
, SUBLANG_NEUTRAL
, 1251 },
380 { LANG_SERBO_CROATIAN
, SUBLANG_NEUTRAL
, 1250 },
381 { LANG_SERBO_CROATIAN
, SUBLANG_SERBIAN
, 1251 },
382 { LANG_SLOVAK
, SUBLANG_NEUTRAL
, 1250 },
383 { LANG_SLOVENIAN
, SUBLANG_NEUTRAL
, 1250 },
384 { LANG_SPANISH
, SUBLANG_NEUTRAL
, 1252 },
385 { LANG_SWEDISH
, SUBLANG_NEUTRAL
, 1252 },
386 { LANG_THAI
, SUBLANG_NEUTRAL
, 874 },
387 { LANG_TURKISH
, SUBLANG_NEUTRAL
, 1254 },
388 { LANG_UKRAINIAN
, SUBLANG_NEUTRAL
, 1251 },
389 { LANG_VIETNAMESE
, SUBLANG_NEUTRAL
, 1258 },
390 { LANG_WALON
, SUBLANG_NEUTRAL
, 1252 },
391 { LANG_WELSH
, SUBLANG_NEUTRAL
, 1252 }
394 void set_language( unsigned short lang
, unsigned short sublang
)
397 unsigned int cp
= 0, defcp
= 0;
399 for (i
= 0; i
< sizeof(lang2cps
)/sizeof(lang2cps
[0]); i
++)
401 if (lang2cps
[i
].lang
!= lang
) continue;
402 if (lang2cps
[i
].sublang
== sublang
)
407 if (lang2cps
[i
].sublang
== SUBLANG_NEUTRAL
) defcp
= lang2cps
[i
].cp
;
412 error( "No codepage value for language %04x", MAKELANGID(lang
,sublang
) );
413 if (!(current_codepage
= cp_get_table( cp
)))
414 error( "Bad codepage %d for language %04x", cp
, MAKELANGID(lang
,sublang
) );