string_t *convert_string(const string_t *str, enum str_e type, int codepage) { const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL; string_t *ret = xmalloc(sizeof(*ret)); int res; ret->loc = str->loc; if (!codepage && str->type != type) parser_error( "Current language is Unicode only, cannot convert string" ); if((str->type == str_char) && (type == str_unicode)) { 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 ); ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) ); 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) parser_error( "Invalid character in string '%.*s' for codepage %u", str->size, str->str.cstr, codepage ); ret->str.wstr[ret->size] = 0; } else if((str->type == str_unicode) && (type == str_char)) { ret->type = str_char; ret->size = cptable ? wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, NULL, 0, NULL, NULL ) : wine_utf8_wcstombs( 0, str->str.wstr, str->size, NULL, 0 ); ret->str.cstr = xmalloc( ret->size + 1 ); if (cptable) wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size, NULL, NULL ); else wine_utf8_wcstombs( 0, str->str.wstr, str->size, ret->str.cstr, ret->size ); 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; }
int get_language_codepage( unsigned short lang, unsigned short sublang ) { unsigned int i; int cp = -1, defcp = -1; 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; } if (cp == -1) cp = defcp; assert( cp <= 0 || wine_cp_get_table(cp) ); return cp; }
/* 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; }
static void LOCALE_Init(void) { /* extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp, const union cptable *unix_cp ); */ // UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp; UINT unix_cp = 0; #ifdef __APPLE__ /* MacOS doesn't set the locale environment variables so we have to do it ourselves */ CFArrayRef preferred_locales, all_locales; CFStringRef user_language_string_ref = NULL; char user_locale[50]; CFLocaleRef user_locale_ref = CFLocaleCopyCurrent(); CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref ); CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 ); CFRelease( user_locale_ref ); if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" ); unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */ setenv( "LANG", user_locale, 0 ); // TRACE( "setting locale to '%s'\n", user_locale ); /* We still want to set the retrieve the preferred language as chosen in System Preferences.app, because it can differ from CFLocaleCopyCurrent(). */ all_locales = CFLocaleCopyAvailableLocaleIdentifiers(); preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL ); if (preferred_locales && CFArrayGetCount( preferred_locales )) user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 ); CFRelease( all_locales ); #endif /* __APPLE__ */ // FIXME setlocale( LC_ALL, "" ); unix_cp = setup_unix_locales(); if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE; #if 0 #ifdef __APPLE__ /* Override lcid_LC_MESSAGES with user_language if LC_MESSAGES is set to default */ if (lcid_LC_MESSAGES == lcid_LC_CTYPE && user_language_string_ref) { struct locale_name locale_name; WCHAR buffer[128]; CFStringGetCString( user_language_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 ); strcpynAtoW( buffer, user_locale, sizeof(buffer)/sizeof(WCHAR) ); parse_locale_name( buffer, &locale_name ); lcid_LC_MESSAGES = locale_name.lcid; TRACE( "setting lcid_LC_MESSAGES to '%s'\n", user_locale ); } if (preferred_locales) CFRelease( preferred_locales ); #endif NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) ); NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES ); NtSetDefaultLocale( FALSE, lcid_LC_CTYPE ); ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT ); GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER, (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) ); GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER, (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) ); if (!unix_cp) GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER, (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) ); if (!(ansi_cptable = wine_cp_get_table( ansi_cp ))) ansi_cptable = wine_cp_get_table( 1252 ); if (!(oem_cptable = wine_cp_get_table( oem_cp ))) oem_cptable = wine_cp_get_table( 437 ); if (!(mac_cptable = wine_cp_get_table( mac_cp ))) mac_cptable = wine_cp_get_table( 10000 ); if (unix_cp != CP_UTF8) { if (!(unix_cptable = wine_cp_get_table( unix_cp ))) unix_cptable = wine_cp_get_table( 28591 ); } __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable ); TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n", ansi_cptable->info.codepage, oem_cptable->info.codepage, mac_cptable->info.codepage, unix_cp ); setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */ #endif }