utils.c 18.7 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5
/*
 * Utility routines
 *
 * Copyright 1998 Bertho A. Stultiens
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include "config.h"
22
#include "wine/port.h"
23

24
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
25 26 27 28 29 30
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>

31
#include "wine/unicode.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
32 33
#include "wrc.h"
#include "utils.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
34
#include "parser.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35

36
/* #define WANT_NEAR_INDICATION */
Alexandre Julliard's avatar
Alexandre Julliard committed
37 38 39 40 41 42 43 44 45 46 47 48 49

#ifdef WANT_NEAR_INDICATION
void make_print(char *str)
{
	while(*str)
	{
		if(!isprint(*str))
			*str = ' ';
		str++;
	}
}
#endif

50
static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
Alexandre Julliard's avatar
Alexandre Julliard committed
51
{
52
	fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
Alexandre Julliard's avatar
Alexandre Julliard committed
53 54 55
	vfprintf(stderr, s, ap);
#ifdef WANT_NEAR_INDICATION
	{
56 57 58 59 60 61 62 63
		char *cpy;
		if(n)
		{
			cpy = xstrdup(n);
			make_print(cpy);
			fprintf(stderr, " near '%s'", cpy);
			free(cpy);
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
64 65
	}
#endif
66 67 68
}


69
int parser_error(const char *s, ...)
70 71 72
{
	va_list ap;
	va_start(ap, s);
73
	generic_msg(s, "Error", parser_text, ap);
74
        fputc( '\n', stderr );
Alexandre Julliard's avatar
Alexandre Julliard committed
75 76 77 78 79
	va_end(ap);
	exit(1);
	return 1;
}

80
int parser_warning(const char *s, ...)
Alexandre Julliard's avatar
Alexandre Julliard committed
81 82 83
{
	va_list ap;
	va_start(ap, s);
84
	generic_msg(s, "Warning", parser_text, ap);
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	va_end(ap);
	return 0;
}

void internal_error(const char *file, int line, const char *s, ...)
{
	va_list ap;
	va_start(ap, s);
	fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
	vfprintf(stderr, s, ap);
	va_end(ap);
	exit(3);
}

99 100 101 102 103 104 105 106 107 108 109
void fatal_perror( const char *msg, ... )
{
        va_list valist;
        va_start( valist, msg );
	fprintf(stderr, "Error: ");
        vfprintf( stderr, msg, valist );
        perror( " " );
        va_end( valist );
        exit(2);
}

Alexandre Julliard's avatar
Alexandre Julliard committed
110 111 112 113 114 115 116 117 118 119 120 121
void error(const char *s, ...)
{
	va_list ap;
	va_start(ap, s);
	fprintf(stderr, "Error: ");
	vfprintf(stderr, s, ap);
	va_end(ap);
	exit(2);
}

void warning(const char *s, ...)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
122
	va_list ap;
Alexandre Julliard's avatar
Alexandre Julliard committed
123 124 125 126 127 128 129
	va_start(ap, s);
	fprintf(stderr, "Warning: ");
	vfprintf(stderr, s, ap);
	va_end(ap);
}

void chat(const char *s, ...)
Alexandre Julliard's avatar
Alexandre Julliard committed
130
{
Alexandre Julliard's avatar
Alexandre Julliard committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	if(debuglevel & DEBUGLEVEL_CHAT)
	{
		va_list ap;
		va_start(ap, s);
		fprintf(stderr, "FYI: ");
		vfprintf(stderr, s, ap);
		va_end(ap);
	}
}

char *dup_basename(const char *name, const char *ext)
{
	int namelen;
	int extlen = strlen(ext);
	char *base;
Alexandre Julliard's avatar
Alexandre Julliard committed
146
	char *slash;
Alexandre Julliard's avatar
Alexandre Julliard committed
147 148 149 150

	if(!name)
		name = "wrc.tab";

Alexandre Julliard's avatar
Alexandre Julliard committed
151 152 153 154
	slash = strrchr(name, '/');
	if (slash)
		name = slash + 1;

Alexandre Julliard's avatar
Alexandre Julliard committed
155 156 157
	namelen = strlen(name);

	/* +4 for later extension and +1 for '\0' */
158
	base = xmalloc(namelen +4 +1);
Alexandre Julliard's avatar
Alexandre Julliard committed
159
	strcpy(base, name);
160
	if(!strcasecmp(name + namelen-extlen, ext))
Alexandre Julliard's avatar
Alexandre Julliard committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	{
		base[namelen - extlen] = '\0';
	}
	return base;
}

void *xmalloc(size_t size)
{
    void *res;

    assert(size > 0);
    res = malloc(size);
    if(res == NULL)
    {
	error("Virtual memory exhausted.\n");
    }
177
    memset(res, 0x55, size);
Alexandre Julliard's avatar
Alexandre Julliard committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    return res;
}


void *xrealloc(void *p, size_t size)
{
    void *res;

    assert(size > 0);
    res = realloc(p, size);
    if(res == NULL)
    {
	error("Virtual memory exhausted.\n");
    }
    return res;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
char *strmake( const char* fmt, ... )
{
    int n;
    size_t size = 100;
    va_list ap;

    for (;;)
    {
        char *p = xmalloc( size );
        va_start( ap, fmt );
        n = vsnprintf( p, size, fmt, ap );
        va_end( ap );
        if (n == -1) size *= 2;
        else if ((size_t)n >= size) size = n + 1;
        else return p;
        free( p );
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
214 215
char *xstrdup(const char *str)
{
216 217 218
	char *s;

	assert(str != NULL);
219
	s = xmalloc(strlen(str)+1);
Alexandre Julliard's avatar
Alexandre Julliard committed
220 221 222 223
	return strcpy(s, str);
}


224 225 226
/*
 *****************************************************************************
 * Function	: compare_name_id
227
 * Syntax	: int compare_name_id(const name_id_t *n1, const name_id_t *n2)
228 229 230 231 232 233
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
234
int compare_name_id(const name_id_t *n1, const name_id_t *n2)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
{
	if(n1->type == name_ord && n2->type == name_ord)
	{
		return n1->name.i_name - n2->name.i_name;
	}
	else if(n1->type == name_str && n2->type == name_str)
	{
		if(n1->name.s_name->type == str_char
		&& n2->name.s_name->type == str_char)
		{
			return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
		}
		else if(n1->name.s_name->type == str_unicode
		&& n2->name.s_name->type == str_unicode)
		{
250
			return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
251 252 253
		}
		else
		{
254
			internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type\n");
255 256 257 258 259 260 261
		}
	}
	else if(n1->type == name_ord && n2->type == name_str)
		return 1;
	else if(n1->type == name_str && n2->type == name_ord)
		return -1;
	else
262
		internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)\n",
263 264 265 266 267
				n1->type, n2->type);

	return 0; /* Keep the compiler happy */
}

268
string_t *convert_string(const string_t *str, enum str_e type, int codepage)
269
{
270
    const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
271
    string_t *ret = xmalloc(sizeof(*ret));
272
    int res;
273

274 275
    ret->loc = str->loc;

276
    if (!codepage && str->type != type)
277
        parser_error( "Current language is Unicode only, cannot convert string" );
278 279 280

    if((str->type == str_char) && (type == str_unicode))
    {
281 282 283
        ret->type = str_unicode;
        ret->size = cptable ? wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 )
                            : wine_utf8_mbstowcs( 0, str->str.cstr, str->size, NULL, 0 );
284
        ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
285 286 287 288 289 290 291
        if (cptable)
            res = wine_cp_mbstowcs( cptable, MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
                                    ret->str.wstr, ret->size );
        else
            res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
                                      ret->str.wstr, ret->size );
        if (res == -2)
292
            parser_error( "Invalid character in string '%.*s' for codepage %u",
293
                   str->size, str->str.cstr, codepage );
294 295 296 297
        ret->str.wstr[ret->size] = 0;
    }
    else if((str->type == str_unicode) && (type == str_char))
    {
298 299
        ret->type = str_char;
        ret->size = cptable ? wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, NULL, 0, NULL, NULL )
300
                            : wine_utf8_wcstombs( 0, str->str.wstr, str->size, NULL, 0 );
301
        ret->str.cstr = xmalloc( ret->size + 1 );
302 303 304
        if (cptable)
            wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size, NULL, NULL );
        else
305
            wine_utf8_wcstombs( 0, str->str.wstr, str->size, ret->str.cstr, ret->size );
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        ret->str.cstr[ret->size] = 0;
    }
    else if(str->type == str_unicode)
    {
        ret->type     = str_unicode;
        ret->size     = str->size;
        ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
        memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
        ret->str.wstr[ret->size] = 0;
    }
    else /* str->type == str_char */
    {
        ret->type     = str_char;
        ret->size     = str->size;
        ret->str.cstr = xmalloc( ret->size + 1 );
        memcpy( ret->str.cstr, str->str.cstr, ret->size );
        ret->str.cstr[ret->size] = 0;
    }
    return ret;
}


void free_string(string_t *str)
{
    if (str->type == str_unicode) free( str->str.wstr );
    else free( str->str.cstr );
    free( str );
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/* check if the string is valid utf8 despite a different codepage being in use */
int check_valid_utf8( const string_t *str, int codepage )
{
    unsigned int i;

    if (!check_utf8) return 0;
    if (!codepage) return 0;
    if (!wine_cp_get_table( codepage )) return 0;

    for (i = 0; i < str->size; i++)
    {
        if ((unsigned char)str->str.cstr[i] >= 0xf5) goto done;
        if ((unsigned char)str->str.cstr[i] >= 0xc2) break;
        if ((unsigned char)str->str.cstr[i] >= 0x80) goto done;
    }
    if (i == str->size) return 0;  /* no 8-bit chars at all */

    if (wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size, NULL, 0 ) >= 0) return 1;

done:
    check_utf8 = 0;  /* at least one 8-bit non-utf8 string found, stop checking */
    return 0;
}
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
{
    int ok;
    string_t *teststr = convert_string( str_w, str_char, codepage );

    ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));

    if (!ok)
    {
        int i;

        fprintf( stderr, "Source: %s", str_a->str.cstr );
        for (i = 0; i < str_a->size; i++)
            fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
        fprintf( stderr, "\nUnicode: " );
        for (i = 0; i < str_w->size; i++)
            fprintf( stderr, " %04x", str_w->str.wstr[i] );
        fprintf( stderr, "\nBack: %s", teststr->str.cstr );
        for (i = 0; i < teststr->size; i++)
            fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
        fprintf( stderr, "\n" );
    }
    free_string( teststr );
    return ok;
383 384 385 386 387 388 389 390
}


struct lang2cp
{
    unsigned short lang;
    unsigned short sublang;
    unsigned int   cp;
391
};
392 393 394 395 396 397 398 399

/* language to codepage conversion table */
/* specific sublanguages need only be specified if their codepage */
/* differs from the default (SUBLANG_NEUTRAL) */
static const struct lang2cp lang2cps[] =
{
    { LANG_AFRIKAANS,      SUBLANG_NEUTRAL,              1252 },
    { LANG_ALBANIAN,       SUBLANG_NEUTRAL,              1250 },
400 401
    { LANG_ALSATIAN,       SUBLANG_NEUTRAL,              1252 },
    { LANG_AMHARIC,        SUBLANG_NEUTRAL,              0    },
402
    { LANG_ARABIC,         SUBLANG_NEUTRAL,              1256 },
403
    { LANG_ARMENIAN,       SUBLANG_NEUTRAL,              0    },
404
    { LANG_ASSAMESE,       SUBLANG_NEUTRAL,              0    },
405 406
    { LANG_AZERI,          SUBLANG_NEUTRAL,              1254 },
    { LANG_AZERI,          SUBLANG_AZERI_CYRILLIC,       1251 },
407
    { LANG_BASHKIR,        SUBLANG_NEUTRAL,              1251 },
408
    { LANG_BASQUE,         SUBLANG_NEUTRAL,              1252 },
409
    { LANG_BELARUSIAN,     SUBLANG_NEUTRAL,              1251 },
410 411 412
    { LANG_BENGALI,        SUBLANG_NEUTRAL,              0    },
    { LANG_BOSNIAN,        SUBLANG_NEUTRAL,              1250 },
    { LANG_BOSNIAN,        SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, 1251 },
413 414 415
    { LANG_BRETON,         SUBLANG_NEUTRAL,              1252 },
    { LANG_BULGARIAN,      SUBLANG_NEUTRAL,              1251 },
    { LANG_CATALAN,        SUBLANG_NEUTRAL,              1252 },
416 417
    { LANG_CHINESE,        SUBLANG_NEUTRAL,              950  },
    { LANG_CHINESE,        SUBLANG_CHINESE_SIMPLIFIED,   936  },
418
    { LANG_CHINESE,        SUBLANG_CHINESE_SINGAPORE,    936  },
419
#ifdef LANG_CORNISH
420
    { LANG_CORNISH,        SUBLANG_NEUTRAL,              1252 },
421
#endif /* LANG_CORNISH */
422
    { LANG_CORSICAN,       SUBLANG_NEUTRAL,              1252 },
423
    { LANG_CROATIAN,       SUBLANG_NEUTRAL,              1250 },
424 425
    { LANG_CZECH,          SUBLANG_NEUTRAL,              1250 },
    { LANG_DANISH,         SUBLANG_NEUTRAL,              1252 },
426
    { LANG_DARI,           SUBLANG_NEUTRAL,              1256 },
427
    { LANG_DIVEHI,         SUBLANG_NEUTRAL,              0    },
428 429
    { LANG_DUTCH,          SUBLANG_NEUTRAL,              1252 },
    { LANG_ENGLISH,        SUBLANG_NEUTRAL,              1252 },
430
#ifdef LANG_ESPERANTO
431
    { LANG_ESPERANTO,      SUBLANG_NEUTRAL,              1252 },
432
#endif /* LANG_ESPERANTO */
433
    { LANG_ESTONIAN,       SUBLANG_NEUTRAL,              1257 },
434
    { LANG_FAEROESE,       SUBLANG_NEUTRAL,              1252 },
435
    { LANG_FILIPINO,       SUBLANG_NEUTRAL,              1252 },
436 437
    { LANG_FINNISH,        SUBLANG_NEUTRAL,              1252 },
    { LANG_FRENCH,         SUBLANG_NEUTRAL,              1252 },
438
    { LANG_FRISIAN,        SUBLANG_NEUTRAL,              1252 },
439
#ifdef LANG_GAELIC
440
    { LANG_GAELIC,         SUBLANG_NEUTRAL,              1252 },
441
#endif /* LANG_GAELIC */
442 443
    { LANG_GALICIAN,       SUBLANG_NEUTRAL,              1252 },
    { LANG_GEORGIAN,       SUBLANG_NEUTRAL,              0    },
444 445
    { LANG_GERMAN,         SUBLANG_NEUTRAL,              1252 },
    { LANG_GREEK,          SUBLANG_NEUTRAL,              1253 },
446
    { LANG_GREENLANDIC,    SUBLANG_NEUTRAL,              1252 },
447
    { LANG_GUJARATI,       SUBLANG_NEUTRAL,              0    },
448
    { LANG_HAUSA,          SUBLANG_NEUTRAL,              1252 },
449
    { LANG_HEBREW,         SUBLANG_NEUTRAL,              1255 },
450
    { LANG_HINDI,          SUBLANG_NEUTRAL,              0    },
451 452
    { LANG_HUNGARIAN,      SUBLANG_NEUTRAL,              1250 },
    { LANG_ICELANDIC,      SUBLANG_NEUTRAL,              1252 },
453
    { LANG_IGBO,           SUBLANG_NEUTRAL,              1252 },
454
    { LANG_INDONESIAN,     SUBLANG_NEUTRAL,              1252 },
455 456 457
    { LANG_INUKTITUT,      SUBLANG_NEUTRAL,              0    },
    { LANG_INUKTITUT,      SUBLANG_INUKTITUT_CANADA_LATIN, 0  },
    { LANG_IRISH,          SUBLANG_NEUTRAL,              1252 },
458 459
    { LANG_ITALIAN,        SUBLANG_NEUTRAL,              1252 },
    { LANG_JAPANESE,       SUBLANG_NEUTRAL,              932  },
460 461
    { LANG_KANNADA,        SUBLANG_NEUTRAL,              0    },
    { LANG_KAZAK,          SUBLANG_NEUTRAL,              1251 },
462 463 464
    { LANG_KHMER,          SUBLANG_NEUTRAL,              0    },
    { LANG_KICHE,          SUBLANG_NEUTRAL,              1252 },
    { LANG_KINYARWANDA,    SUBLANG_NEUTRAL,              1252 },
465
    { LANG_KONKANI,        SUBLANG_NEUTRAL,              0    },
466
    { LANG_KOREAN,         SUBLANG_NEUTRAL,              949  },
467
    { LANG_KYRGYZ,         SUBLANG_NEUTRAL,              1251 },
468
    { LANG_LAO,            SUBLANG_NEUTRAL,              0    },
469 470
    { LANG_LATVIAN,        SUBLANG_NEUTRAL,              1257 },
    { LANG_LITHUANIAN,     SUBLANG_NEUTRAL,              1257 },
471 472
    { LANG_LOWER_SORBIAN,  SUBLANG_NEUTRAL,              1252 },
    { LANG_LUXEMBOURGISH,  SUBLANG_NEUTRAL,              1252 },
473
    { LANG_MACEDONIAN,     SUBLANG_NEUTRAL,              1251 },
474
    { LANG_MALAY,          SUBLANG_NEUTRAL,              1252 },
475 476 477 478
    { LANG_MALAYALAM,      SUBLANG_NEUTRAL,              0    },
    { LANG_MALTESE,        SUBLANG_NEUTRAL,              0    },
    { LANG_MAORI,          SUBLANG_NEUTRAL,              0    },
    { LANG_MAPUDUNGUN,     SUBLANG_NEUTRAL,              1252 },
479
    { LANG_MARATHI,        SUBLANG_NEUTRAL,              0    },
480
    { LANG_MOHAWK,         SUBLANG_NEUTRAL,              1252 },
481
    { LANG_MONGOLIAN,      SUBLANG_NEUTRAL,              1251 },
482
    { LANG_NEPALI,         SUBLANG_NEUTRAL,              0    },
483
    { LANG_NEUTRAL,        SUBLANG_NEUTRAL,              1252 },
484
    { LANG_NORWEGIAN,      SUBLANG_NEUTRAL,              1252 },
485 486 487 488
    { LANG_OCCITAN,        SUBLANG_NEUTRAL,              1252 },
    { LANG_ORIYA,          SUBLANG_NEUTRAL,              0    },
    { LANG_PASHTO,         SUBLANG_NEUTRAL,              0    },
    { LANG_PERSIAN,        SUBLANG_NEUTRAL,              1256 },
489 490
    { LANG_POLISH,         SUBLANG_NEUTRAL,              1250 },
    { LANG_PORTUGUESE,     SUBLANG_NEUTRAL,              1252 },
491
    { LANG_PUNJABI,        SUBLANG_NEUTRAL,              0    },
492
    { LANG_QUECHUA,        SUBLANG_NEUTRAL,              1252 },
493
    { LANG_ROMANIAN,       SUBLANG_NEUTRAL,              1250 },
494
    { LANG_ROMANSH,        SUBLANG_NEUTRAL,              1252 },
495
    { LANG_RUSSIAN,        SUBLANG_NEUTRAL,              1251 },
496
    { LANG_SAMI,           SUBLANG_NEUTRAL,              1252 },
497 498 499
    { LANG_SANSKRIT,       SUBLANG_NEUTRAL,              0    },
    { LANG_SERBIAN,        SUBLANG_NEUTRAL,              1250 },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_CYRILLIC,     1251 },
500
    { LANG_SINHALESE,      SUBLANG_NEUTRAL,              0    },
501 502
    { LANG_SLOVAK,         SUBLANG_NEUTRAL,              1250 },
    { LANG_SLOVENIAN,      SUBLANG_NEUTRAL,              1250 },
503
    { LANG_SOTHO,          SUBLANG_NEUTRAL,              1252 },
504
    { LANG_SPANISH,        SUBLANG_NEUTRAL,              1252 },
505
    { LANG_SWAHILI,        SUBLANG_NEUTRAL,              1252 },
506
    { LANG_SWEDISH,        SUBLANG_NEUTRAL,              1252 },
507
    { LANG_SYRIAC,         SUBLANG_NEUTRAL,              0    },
508 509
    { LANG_TAJIK,          SUBLANG_NEUTRAL,              1251 },
    { LANG_TAMAZIGHT,      SUBLANG_NEUTRAL,              1252 },
510 511 512
    { LANG_TAMIL,          SUBLANG_NEUTRAL,              0    },
    { LANG_TATAR,          SUBLANG_NEUTRAL,              1251 },
    { LANG_TELUGU,         SUBLANG_NEUTRAL,              0    },
513
    { LANG_THAI,           SUBLANG_NEUTRAL,              874  },
514 515
    { LANG_TIBETAN,        SUBLANG_NEUTRAL,              0    },
    { LANG_TSWANA,         SUBLANG_NEUTRAL,              1252 },
516
    { LANG_TURKISH,        SUBLANG_NEUTRAL,              1254 },
517 518
    { LANG_TURKMEN,        SUBLANG_NEUTRAL,              1250 },
    { LANG_UIGHUR,         SUBLANG_NEUTRAL,              1256 },
519
    { LANG_UKRAINIAN,      SUBLANG_NEUTRAL,              1251 },
520
    { LANG_UPPER_SORBIAN,  SUBLANG_NEUTRAL,              1252 },
521 522 523
    { LANG_URDU,           SUBLANG_NEUTRAL,              1256 },
    { LANG_UZBEK,          SUBLANG_NEUTRAL,              1254 },
    { LANG_UZBEK,          SUBLANG_UZBEK_CYRILLIC,       1251 },
524
    { LANG_VIETNAMESE,     SUBLANG_NEUTRAL,              1258 },
525
#ifdef LANG_WALON
526
    { LANG_WALON,          SUBLANG_NEUTRAL,              1252 },
527
#endif /* LANG_WALON */
528 529 530 531 532 533 534
    { LANG_WELSH,          SUBLANG_NEUTRAL,              1252 },
    { LANG_WOLOF,          SUBLANG_NEUTRAL,              1252 },
    { LANG_XHOSA,          SUBLANG_NEUTRAL,              1252 },
    { LANG_YAKUT,          SUBLANG_NEUTRAL,              1251 },
    { LANG_YI,             SUBLANG_NEUTRAL,              0    },
    { LANG_YORUBA,         SUBLANG_NEUTRAL,              1252 },
    { LANG_ZULU,           SUBLANG_NEUTRAL,              1252 }
535 536
};

537
int get_language_codepage( unsigned short lang, unsigned short sublang )
538
{
539
    unsigned int i;
540
    int cp = -1, defcp = -1;
541 542 543 544 545 546 547 548 549 550 551 552

    for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
    {
        if (lang2cps[i].lang != lang) continue;
        if (lang2cps[i].sublang == sublang)
        {
            cp = lang2cps[i].cp;
            break;
        }
        if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
    }

553
    if (cp == -1) cp = defcp;
554
    assert( cp <= 0 || wine_cp_get_table(cp) );
555
    return cp;
556
}