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
22 #include "wine/port.h"
32 #include "wine/unicode.h"
38 /* #define WANT_NEAR_INDICATION */
40 static const union cptable
*current_codepage
;
42 #ifdef WANT_NEAR_INDICATION
43 void make_print(char *str
)
54 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
56 fprintf(stderr
, "%s:%d:%d: %s: ", input_name
? input_name
: "stdin", line_number
, char_number
, t
);
57 vfprintf(stderr
, s
, ap
);
58 #ifdef WANT_NEAR_INDICATION
65 fprintf(stderr
, " near '%s'", cpy
);
70 fprintf(stderr
, "\n");
74 int yyerror(const char *s
, ...)
78 generic_msg(s
, "Error", yytext
, ap
);
84 int yywarning(const char *s
, ...)
88 generic_msg(s
, "Warning", yytext
, ap
);
93 int pperror(const char *s
, ...)
97 generic_msg(s
, "Error", pptext
, ap
);
103 int ppwarning(const char *s
, ...)
107 generic_msg(s
, "Warning", pptext
, ap
);
113 void internal_error(const char *file
, int line
, const char *s
, ...)
117 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
118 vfprintf(stderr
, s
, ap
);
119 fprintf(stderr
, "\n");
124 void error(const char *s
, ...)
128 fprintf(stderr
, "Error: ");
129 vfprintf(stderr
, s
, ap
);
130 fprintf(stderr
, "\n");
135 void warning(const char *s
, ...)
139 fprintf(stderr
, "Warning: ");
140 vfprintf(stderr
, s
, ap
);
141 fprintf(stderr
, "\n");
145 void chat(const char *s
, ...)
147 if(debuglevel
& DEBUGLEVEL_CHAT
)
151 fprintf(stderr
, "FYI: ");
152 vfprintf(stderr
, s
, ap
);
153 fprintf(stderr
, "\n");
158 char *dup_basename(const char *name
, const char *ext
)
161 int extlen
= strlen(ext
);
168 slash
= strrchr(name
, '/');
172 namelen
= strlen(name
);
174 /* +4 for later extension and +1 for '\0' */
175 base
= (char *)xmalloc(namelen
+4 +1);
177 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
179 base
[namelen
- extlen
] = '\0';
184 void *xmalloc(size_t size
)
192 error("Virtual memory exhausted.\n");
196 * This is *paramount* because we depend on it
197 * just about everywhere in the rest of the code.
199 memset(res
, 0, size
);
204 void *xrealloc(void *p
, size_t size
)
209 res
= realloc(p
, size
);
212 error("Virtual memory exhausted.\n");
217 char *xstrdup(const char *str
)
222 s
= (char *)xmalloc(strlen(str
)+1);
223 return strcpy(s
, str
);
226 short *dupcstr2wstr(const char *str
)
231 if (!current_codepage
) set_language( LANG_NEUTRAL
, SUBLANG_NEUTRAL
);
232 len
= cp_mbstowcs( current_codepage
, 0, str
, strlen(str
), NULL
, 0 );
233 ws
= xmalloc( sizeof(WCHAR
) * (len
+ 1) );
234 len
= cp_mbstowcs( current_codepage
, 0, str
, strlen(str
), ws
, len
);
239 char *dupwstr2cstr(const short *str
)
244 if (!current_codepage
) set_language( LANG_NEUTRAL
, SUBLANG_NEUTRAL
);
245 len
= cp_wcstombs( current_codepage
, 0, str
, strlenW(str
), NULL
, 0, NULL
, NULL
);
246 cs
= xmalloc( len
+ 1 );
247 len
= cp_wcstombs( current_codepage
, 0, str
, strlenW(str
), cs
, len
, NULL
, NULL
);
253 *****************************************************************************
254 * Function : compare_name_id
255 * Syntax : int compare_name_id(name_id_t *n1, name_id_t *n2)
260 *****************************************************************************
262 int compare_name_id(name_id_t
*n1
, name_id_t
*n2
)
264 if(n1
->type
== name_ord
&& n2
->type
== name_ord
)
266 return n1
->name
.i_name
- n2
->name
.i_name
;
268 else if(n1
->type
== name_str
&& n2
->type
== name_str
)
270 if(n1
->name
.s_name
->type
== str_char
271 && n2
->name
.s_name
->type
== str_char
)
273 return strcasecmp(n1
->name
.s_name
->str
.cstr
, n2
->name
.s_name
->str
.cstr
);
275 else if(n1
->name
.s_name
->type
== str_unicode
276 && n2
->name
.s_name
->type
== str_unicode
)
278 return strcmpiW(n1
->name
.s_name
->str
.wstr
, n2
->name
.s_name
->str
.wstr
);
282 internal_error(__FILE__
, __LINE__
, "Can't yet compare strings of mixed type");
285 else if(n1
->type
== name_ord
&& n2
->type
== name_str
)
287 else if(n1
->type
== name_str
&& n2
->type
== name_ord
)
290 internal_error(__FILE__
, __LINE__
, "Comparing name-ids with unknown types (%d, %d)",
293 return 0; /* Keep the compiler happy */
296 string_t
*convert_string(const string_t
*str
, enum str_e type
)
298 string_t
*ret
= xmalloc(sizeof(*ret
));
300 if((str
->type
== str_char
) && (type
== str_unicode
))
302 ret
->str
.wstr
= dupcstr2wstr(str
->str
.cstr
);
303 ret
->type
= str_unicode
;
304 ret
->size
= strlenW(ret
->str
.wstr
);
306 else if((str
->type
== str_unicode
) && (type
== str_char
))
308 ret
->str
.cstr
= dupwstr2cstr(str
->str
.wstr
);
309 ret
->type
= str_char
;
310 ret
->size
= strlen(ret
->str
.cstr
);
312 else if(str
->type
== str_unicode
)
314 ret
->type
= str_unicode
;
315 ret
->size
= strlenW(str
->str
.wstr
);
316 ret
->str
.wstr
= xmalloc(sizeof(WCHAR
)*(ret
->size
+1));
317 strcpyW(ret
->str
.wstr
, str
->str
.wstr
);
319 else /* str->type == str_char */
321 ret
->type
= str_char
;
322 ret
->size
= strlen(str
->str
.cstr
);
323 ret
->str
.cstr
= xmalloc( ret
->size
+ 1 );
324 strcpy(ret
->str
.cstr
, str
->str
.cstr
);
333 unsigned short sublang
;
337 /* language to codepage conversion table */
338 /* specific sublanguages need only be specified if their codepage */
339 /* differs from the default (SUBLANG_NEUTRAL) */
340 static const struct lang2cp lang2cps
[] =
342 { LANG_AFRIKAANS
, SUBLANG_NEUTRAL
, 1252 },
343 { LANG_ALBANIAN
, SUBLANG_NEUTRAL
, 1250 },
344 { LANG_ARABIC
, SUBLANG_NEUTRAL
, 1256 },
345 { LANG_BASQUE
, SUBLANG_NEUTRAL
, 1252 },
346 { LANG_BRETON
, SUBLANG_NEUTRAL
, 1252 },
347 { LANG_BULGARIAN
, SUBLANG_NEUTRAL
, 1251 },
348 { LANG_BYELORUSSIAN
, SUBLANG_NEUTRAL
, 1251 },
349 { LANG_CATALAN
, SUBLANG_NEUTRAL
, 1252 },
350 { LANG_CHINESE
, SUBLANG_NEUTRAL
, 936 },
351 { LANG_CORNISH
, SUBLANG_NEUTRAL
, 1252 },
352 { LANG_CZECH
, SUBLANG_NEUTRAL
, 1250 },
353 { LANG_DANISH
, SUBLANG_NEUTRAL
, 1252 },
354 { LANG_DUTCH
, SUBLANG_NEUTRAL
, 1252 },
355 { LANG_ENGLISH
, SUBLANG_NEUTRAL
, 1252 },
356 { LANG_ESPERANTO
, SUBLANG_NEUTRAL
, 1252 },
357 { LANG_ESTONIAN
, SUBLANG_NEUTRAL
, 1257 },
358 { LANG_FAEROESE
, SUBLANG_NEUTRAL
, 1252 },
359 { LANG_FINNISH
, SUBLANG_NEUTRAL
, 1252 },
360 { LANG_FRENCH
, SUBLANG_NEUTRAL
, 1252 },
361 { LANG_GAELIC
, SUBLANG_NEUTRAL
, 1252 },
362 { LANG_GERMAN
, SUBLANG_NEUTRAL
, 1252 },
363 { LANG_GREEK
, SUBLANG_NEUTRAL
, 1253 },
364 { LANG_HEBREW
, SUBLANG_NEUTRAL
, 1255 },
365 { LANG_HUNGARIAN
, SUBLANG_NEUTRAL
, 1250 },
366 { LANG_ICELANDIC
, SUBLANG_NEUTRAL
, 1252 },
367 { LANG_INDONESIAN
, SUBLANG_NEUTRAL
, 1252 },
368 { LANG_ITALIAN
, SUBLANG_NEUTRAL
, 1252 },
369 { LANG_JAPANESE
, SUBLANG_NEUTRAL
, 932 },
370 { LANG_KOREAN
, SUBLANG_NEUTRAL
, 949 },
371 { LANG_LATVIAN
, SUBLANG_NEUTRAL
, 1257 },
372 { LANG_LITHUANIAN
, SUBLANG_NEUTRAL
, 1257 },
373 { LANG_MACEDONIAN
, SUBLANG_NEUTRAL
, 1251 },
374 { LANG_MALAY
, SUBLANG_NEUTRAL
, 1252 },
375 { LANG_NEUTRAL
, SUBLANG_NEUTRAL
, 1252 },
376 { LANG_NORWEGIAN
, SUBLANG_NEUTRAL
, 1252 },
377 { LANG_POLISH
, SUBLANG_NEUTRAL
, 1250 },
378 { LANG_PORTUGUESE
, SUBLANG_NEUTRAL
, 1252 },
379 { LANG_ROMANIAN
, SUBLANG_NEUTRAL
, 1250 },
380 { LANG_RUSSIAN
, SUBLANG_NEUTRAL
, 1251 },
381 { LANG_SERBO_CROATIAN
, SUBLANG_NEUTRAL
, 1250 },
382 { LANG_SERBO_CROATIAN
, SUBLANG_SERBIAN
, 1251 },
383 { LANG_SLOVAK
, SUBLANG_NEUTRAL
, 1250 },
384 { LANG_SLOVENIAN
, SUBLANG_NEUTRAL
, 1250 },
385 { LANG_SPANISH
, SUBLANG_NEUTRAL
, 1252 },
386 { LANG_SWEDISH
, SUBLANG_NEUTRAL
, 1252 },
387 { LANG_THAI
, SUBLANG_NEUTRAL
, 874 },
388 { LANG_TURKISH
, SUBLANG_NEUTRAL
, 1254 },
389 { LANG_UKRAINIAN
, SUBLANG_NEUTRAL
, 1251 },
390 { LANG_VIETNAMESE
, SUBLANG_NEUTRAL
, 1258 },
391 { LANG_WALON
, SUBLANG_NEUTRAL
, 1252 },
392 { LANG_WELSH
, SUBLANG_NEUTRAL
, 1252 }
395 void set_language( unsigned short lang
, unsigned short sublang
)
398 unsigned int cp
= 0, defcp
= 0;
400 for (i
= 0; i
< sizeof(lang2cps
)/sizeof(lang2cps
[0]); i
++)
402 if (lang2cps
[i
].lang
!= lang
) continue;
403 if (lang2cps
[i
].sublang
== sublang
)
408 if (lang2cps
[i
].sublang
== SUBLANG_NEUTRAL
) defcp
= lang2cps
[i
].cp
;
413 error( "No codepage value for language %04x", MAKELANGID(lang
,sublang
) );
414 if (!(current_codepage
= cp_get_table( cp
)))
415 error( "Bad codepage %d for language %04x", cp
, MAKELANGID(lang
,sublang
) );