#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/uloc.h"
#include "putilimp.h"
#include "ustr_imp.h"
#include "ulocimp.h"
#include "uresimp.h"
#include "umutex.h"
#include "cstring.h"
#include "cmemory.h"
#include "ucln_cmn.h"
#include "locmap.h"
#include "uarrsort.h"
#include "uenumimp.h"
#include "uassert.h"
#include <stdio.h>
U_CFUNC void locale_set_default(const char *id);
U_CFUNC const char *locale_get_default(void);
U_CFUNC int32_t
locale_getKeywords(const char *localeID,
char prev,
char *keywords, int32_t keywordCapacity,
char *values, int32_t valuesCapacity, int32_t *valLen,
UBool valuesToo,
UErrorCode *status);
static const char _kLanguages[] = "Languages";
static const char _kScripts[] = "Scripts";
static const char _kCountries[] = "Countries";
static const char _kVariants[] = "Variants";
static const char _kKeys[] = "Keys";
static const char _kTypes[] = "Types";
static const char _kIndexLocaleName[] = "res_index";
static const char _kRootName[] = "root";
static const char _kIndexTag[] = "InstalledLocales";
static const char _kCurrency[] = "currency";
static const char _kCurrencies[] = "Currencies";
static char** _installedLocales = NULL;
static int32_t _installedLocalesCount = 0;
static const char * const LANGUAGES[] = {
"aa", "ab", "ace", "ach", "ada", "ady", "ae", "af", "afa",
"afh", "ak", "akk", "ale", "alg", "am", "an", "ang", "apa",
"ar", "arc", "arn", "arp", "art", "arw", "as", "ast",
"ath", "aus", "av", "awa", "ay", "az", "ba", "bad",
"bai", "bal", "ban", "bas", "bat", "be", "bej",
"bem", "ber", "bg", "bh", "bho", "bi", "bik", "bin",
"bla", "bm", "bn", "bnt", "bo", "br", "bra", "bs",
"btk", "bua", "bug", "byn", "ca", "cad", "cai", "car", "cau",
"ce", "ceb", "cel", "ch", "chb", "chg", "chk", "chm",
"chn", "cho", "chp", "chr", "chy", "cmc", "co", "cop",
"cpe", "cpf", "cpp", "cr", "crh", "crp", "cs", "csb", "cu", "cus",
"cv", "cy", "da", "dak", "dar", "day", "de", "del", "den",
"dgr", "din", "doi", "dra", "dsb", "dua", "dum", "dv", "dyu",
"dz", "ee", "efi", "egy", "eka", "el", "elx", "en",
"enm", "eo", "es", "et", "eu", "ewo", "fa",
"fan", "fat", "ff", "fi", "fiu", "fj", "fo", "fon",
"fr", "frm", "fro", "fur", "fy", "ga", "gaa", "gay",
"gba", "gd", "gem", "gez", "gil", "gl", "gmh", "gn",
"goh", "gon", "gor", "got", "grb", "grc", "gu", "gv",
"gwi", "ha", "hai", "haw", "he", "hi", "hil", "him",
"hit", "hmn", "ho", "hr", "hsb", "ht", "hu", "hup", "hy", "hz",
"ia", "iba", "id", "ie", "ig", "ii", "ijo", "ik",
"ilo", "inc", "ine", "inh", "io", "ira", "iro", "is", "it",
"iu", "ja", "jbo", "jpr", "jrb", "jv", "ka", "kaa", "kab",
"kac", "kam", "kar", "kaw", "kbd", "kg", "kha", "khi",
"kho", "ki", "kj", "kk", "kl", "km", "kmb", "kn",
"ko", "kok", "kos", "kpe", "kr", "krc", "kro", "kru", "ks",
"ku", "kum", "kut", "kv", "kw", "ky", "la", "lad",
"lah", "lam", "lb", "lez", "lg", "li", "ln", "lo", "lol",
"loz", "lt", "lu", "lua", "lui", "lun", "luo", "lus",
"lv", "mad", "mag", "mai", "mak", "man", "map", "mas",
"mdf", "mdr", "men", "mg", "mga", "mh", "mi", "mic", "min",
"mis", "mk", "mkh", "ml", "mn", "mnc", "mni", "mno",
"mo", "moh", "mos", "mr", "ms", "mt", "mul", "mun",
"mus", "mwr", "my", "myn", "myv", "na", "nah", "nai", "nap",
"nb", "nd", "nds", "ne", "new", "ng", "nia", "nic",
"niu", "nl", "nn", "no", "nog", "non", "nr", "nso", "nub",
"nv", "nwc", "ny", "nym", "nyn", "nyo", "nzi", "oc", "oj",
"om", "or", "os", "osa", "ota", "oto", "pa", "paa",
"pag", "pal", "pam", "pap", "pau", "peo", "phi", "phn",
"pi", "pl", "pon", "pra", "pro", "ps", "pt", "qu",
"raj", "rap", "rar", "rm", "rn", "ro", "roa", "rom",
"ru", "rw", "sa", "sad", "sah", "sai", "sal", "sam",
"sas", "sat", "sc", "sco", "sd", "se", "sel", "sem",
"sg", "sga", "sgn", "shn", "si", "sid", "sio", "sit",
"sk", "sl", "sla", "sm", "sma", "smi", "smj", "smn",
"sms", "sn", "snk", "so", "sog", "son", "sq", "sr",
"srr", "ss", "ssa", "st", "su", "suk", "sus", "sux",
"sv", "sw", "syr", "ta", "tai", "te", "tem", "ter",
"tet", "tg", "th", "ti", "tig", "tiv", "tk", "tkl",
"tl", "tlh", "tli", "tmh", "tn", "to", "tog", "tpi", "tr",
"ts", "tsi", "tt", "tum", "tup", "tut", "tvl", "tw",
"ty", "tyv", "udm", "ug", "uga", "uk", "umb", "und", "ur",
"uz", "vai", "ve", "vi", "vo", "vot", "wa", "wak",
"wal", "war", "was", "wen", "wo", "xal", "xh", "yao", "yap",
"yi", "yo", "ypk", "za", "zap", "zen", "zh", "znd",
"zu", "zun",
NULL,
"in", "iw", "ji", "jw", "sh",
NULL
};
static const char * const LANGUAGES_3[] = {
"aar", "abk", "ace", "ach", "ada", "ady", "ave", "afr", "afa",
"afh", "aka", "akk", "ale", "alg", "amh", "arg", "ang", "apa",
"ara", "arc", "arn", "arp", "art", "arw", "asm", "ast",
"ath", "aus", "ava", "awa", "aym", "aze", "bak", "bad",
"bai", "bal", "ban", "bas", "bat", "bel", "bej",
"bem", "ber", "bul", "bih", "bho", "bis", "bik", "bin",
"bla", "bam", "ben", "bnt", "bod", "bre", "bra", "bos",
"btk", "bua", "bug", "byn", "cat", "cad", "cai", "car", "cau",
"che", "ceb", "cel", "cha", "chb", "chg", "chk", "chm",
"chn", "cho", "chp", "chr", "chy", "cmc", "cos", "cop",
"cpe", "cpf", "cpp", "cre", "crh", "crp", "ces", "csb", "chu", "cus",
"chv", "cym", "dan", "dak", "dar", "day", "deu", "del", "den",
"dgr", "din", "doi", "dra", "dsb", "dua", "dum", "div", "dyu",
"dzo", "ewe", "efi", "egy", "eka", "ell", "elx", "eng",
"enm", "epo", "spa", "est", "eus", "ewo", "fas",
"fan", "fat", "ful", "fin", "fiu", "fij", "fao", "fon",
"fra", "frm", "fro", "fur", "fry", "gle", "gaa", "gay",
"gba", "gla", "gem", "gez", "gil", "glg", "gmh", "grn",
"goh", "gon", "gor", "got", "grb", "grc", "guj", "glv",
"gwi", "hau", "hai", "haw", "heb", "hin", "hil", "him",
"hit", "hmn", "hmo", "hrv", "hsb", "hat", "hun", "hup", "hye", "her",
"ina", "iba", "ind", "ile", "ibo", "iii", "ijo", "ipk",
"ilo", "inc", "ine", "inh", "ido", "ira", "iro", "isl", "ita",
"iku", "jpn", "jbo", "jpr", "jrb", "jav", "kat", "kaa", "kab",
"kac", "kam", "kar", "kaw", "kbd", "kon", "kha", "khi",
"kho", "kik", "kua", "kaz", "kal", "khm", "kmb", "kan",
"kor", "kok", "kos", "kpe", "kau", "krc", "kro", "kru", "kas",
"kur", "kum", "kut", "kom", "cor", "kir", "lat", "lad",
"lah", "lam", "ltz", "lez", "lug", "lim", "lin", "lao", "lol",
"loz", "lit", "lub", "lua", "lui", "lun", "luo", "lus",
"lav", "mad", "mag", "mai", "mak", "man", "map", "mas",
"mdf", "mdr", "men", "mlg", "mga", "mah", "mri", "mic", "min",
"mis", "mkd", "mkh", "mal", "mon", "mnc", "mni", "mno",
"mol", "moh", "mos", "mar", "msa", "mlt", "mul", "mun",
"mus", "mwr", "mya", "myn", "myv", "nau", "nah", "nai", "nap",
"nob", "nde", "nds", "nep", "new", "ndo", "nia", "nic",
"niu", "nld", "nno", "nor", "nog", "non", "nbl", "nso", "nub",
"nav", "nwc", "nya", "nym", "nyn", "nyo", "nzi", "oci", "oji",
"orm", "ori", "oss", "osa", "ota", "oto", "pan", "paa",
"pag", "pal", "pam", "pap", "pau", "peo", "phi", "phn",
"pli", "pol", "pon", "pra", "pro", "pus", "por", "que",
"raj", "rap", "rar", "roh", "run", "ron", "roa", "rom",
"rus", "kin", "san", "sad", "sah", "sai", "sal", "sam",
"sas", "sat", "srd", "sco", "snd", "sme", "sel", "sem",
"sag", "sga", "sgn", "shn", "sin", "sid", "sio", "sit",
"slk", "slv", "sla", "smo", "sma", "smi", "smj", "smn",
"sms", "sna", "snk", "som", "sog", "son", "sqi", "srp",
"srr", "ssw", "ssa", "sot", "sun", "suk", "sus", "sux",
"swe", "swa", "syr", "tam", "tai", "tel", "tem", "ter",
"tet", "tgk", "tha", "tir", "tig", "tiv", "tuk", "tkl",
"tgl", "tlh", "tli", "tmh", "tsn", "ton", "tog", "tpi", "tur",
"tso", "tsi", "tat", "tum", "tup", "tut", "tvl", "twi",
"tah", "tyv", "udm", "uig", "uga", "ukr", "umb", "und", "urd",
"uzb", "vai", "ven", "vie", "vol", "vot", "wln", "wak",
"wal", "war", "was", "wen", "wol", "xal", "xho", "yao", "yap",
"yid", "yor", "ypk", "zha", "zap", "zen", "zho", "znd",
"zul", "zun",
NULL,
"ind", "heb", "yid", "jaw", "srp",
NULL
};
static const char * const COUNTRIES[] = {
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN",
"AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ",
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI",
"BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV",
"BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG",
"CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR",
"CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK",
"DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER",
"ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR",
"GA", "GB", "GD", "GE", "GF", "GH", "GI", "GL",
"GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU",
"GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU",
"ID", "IE", "IL", "IN", "IO", "IQ", "IR", "IS",
"IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI",
"KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA",
"LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU",
"LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK",
"ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS",
"MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
"NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP",
"NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG",
"PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT",
"PW", "PY", "QA", "RE", "RO", "RU", "RW", "SA",
"SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ",
"SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV",
"SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ",
"TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV",
"TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ",
"VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF",
"WS", "YE", "YT", "YU", "ZA", "ZM", "ZW",
NULL,
"FX", "RO", "TP", "ZR",
NULL
};
static const char * const COUNTRIES_3[] = {
"AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT",
"AGO", "ATA", "ARG", "ASM", "AUT", "AUS", "ABW", "AZE",
"BIH", "BRB", "BGD", "BEL", "BFA", "BGR", "BHR", "BDI",
"BEN", "BMU", "BRN", "BOL", "BRA", "BHS", "BTN", "BVT",
"BWA", "BLR", "BLZ", "CAN", "CCK", "COD", "CAF", "COG",
"CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI",
"CUB", "CPV", "CXR", "CYP", "CZE", "DEU", "DJI", "DNK",
"DMA", "DOM", "DZA", "ECU", "EST", "EGY", "ESH", "ERI",
"ESP", "ETH", "FIN", "FJI", "FLK", "FSM", "FRO", "FRA",
"GAB", "GBR", "GRD", "GEO", "GUF", "GHA", "GIB", "GRL",
"GMB", "GIN", "GLP", "GNQ", "GRC", "SGS", "GTM", "GUM",
"GNB", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN",
"IDN", "IRL", "ISR", "IND", "IOT", "IRQ", "IRN", "ISL",
"ITA", "JAM", "JOR", "JPN", "KEN", "KGZ", "KHM", "KIR",
"COM", "KNA", "PRK", "KOR", "KWT", "CYM", "KAZ", "LAO",
"LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", "LUX",
"LVA", "LBY", "MAR", "MCO", "MDA", "MDG", "MHL", "MKD",
"MLI", "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR",
"MLT", "MUS", "MDV", "MWI", "MEX", "MYS", "MOZ", "NAM",
"NCL", "NER", "NFK", "NGA", "NIC", "NLD", "NOR", "NPL",
"NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", "PNG",
"PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT",
"PLW", "PRY", "QAT", "REU", "ROU", "RUS", "RWA", "SAU",
"SLB", "SYC", "SDN", "SWE", "SGP", "SHN", "SVN", "SJM",
"SVK", "SLE", "SMR", "SEN", "SOM", "SUR", "STP", "SLV",
"SYR", "SWZ", "TCA", "TCD", "ATF", "TGO", "THA", "TJK",
"TKL", "TLS", "TKM", "TUN", "TON", "TUR", "TTO", "TUV",
"TWN", "TZA", "UKR", "UGA", "UMI", "USA", "URY", "UZB",
"VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF",
"WSM", "YEM", "MYT", "YUG", "ZAF", "ZMB", "ZWE",
NULL,
"FXX", "ROM", "TMP", "ZAR",
NULL
};
typedef struct CanonicalizationMap {
const char *id;
const char *canonicalID;
const char *keyword;
const char *value;
} CanonicalizationMap;
static const CanonicalizationMap CANONICALIZE_MAP[] = {
{ "", "en_US_POSIX", NULL, NULL },
{ "C", "en_US_POSIX", NULL, NULL },
{ "art_LOJBAN", "jbo", NULL, NULL },
{ "az_AZ_CYRL", "az_Cyrl_AZ", NULL, NULL },
{ "az_AZ_LATN", "az_Latn_AZ", NULL, NULL },
{ "ca_ES_PREEURO", "ca_ES", "currency", "ESP" },
{ "cel_GAULISH", "cel__GAULISH", NULL, NULL },
{ "de_1901", "de__1901", NULL, NULL },
{ "de_1906", "de__1906", NULL, NULL },
{ "de__PHONEBOOK", "de", "collation", "phonebook" },
{ "de_AT_PREEURO", "de_AT", "currency", "ATS" },
{ "de_DE_PREEURO", "de_DE", "currency", "DEM" },
{ "de_LU_PREEURO", "de_LU", "currency", "LUF" },
{ "el_GR_PREEURO", "el_GR", "currency", "GRD" },
{ "en_BOONT", "en__BOONT", NULL, NULL },
{ "en_SCOUSE", "en__SCOUSE", NULL, NULL },
{ "en_BE_PREEURO", "en_BE", "currency", "BEF" },
{ "en_IE_PREEURO", "en_IE", "currency", "IEP" },
{ "es__TRADITIONAL", "es", "collation", "traditional" },
{ "es_ES_PREEURO", "es_ES", "currency", "ESP" },
{ "eu_ES_PREEURO", "eu_ES", "currency", "ESP" },
{ "fi_FI_PREEURO", "fi_FI", "currency", "FIM" },
{ "fr_BE_PREEURO", "fr_BE", "currency", "BEF" },
{ "fr_FR_PREEURO", "fr_FR", "currency", "FRF" },
{ "fr_LU_PREEURO", "fr_LU", "currency", "LUF" },
{ "ga_IE_PREEURO", "ga_IE", "currency", "IEP" },
{ "gl_ES_PREEURO", "gl_ES", "currency", "ESP" },
{ "hi__DIRECT", "hi", "collation", "direct" },
{ "it_IT_PREEURO", "it_IT", "currency", "ITL" },
{ "ja_JP_TRADITIONAL", "ja_JP", "calendar", "japanese" },
{ "nb_NO_NY", "nn_NO", NULL, NULL },
{ "nl_BE_PREEURO", "nl_BE", "currency", "BEF" },
{ "nl_NL_PREEURO", "nl_NL", "currency", "NLG" },
{ "pt_PT_PREEURO", "pt_PT", "currency", "PTE" },
{ "sl_ROZAJ", "sl__ROZAJ", NULL, NULL },
{ "sr_SP_CYRL", "sr_Cyrl_SP", NULL, NULL },
{ "sr_SP_LATN", "sr_Latn_SP", NULL, NULL },
{ "uz_UZ_CYRL", "uz_Cyrl_UZ", NULL, NULL },
{ "uz_UZ_LATN", "uz_Latn_UZ", NULL, NULL },
{ "zh_CHS", "zh_Hans", NULL, NULL },
{ "zh_CHT", "zh_TW", NULL, NULL },
{ "zh_GAN", "zh__GAN", NULL, NULL },
{ "zh_GUOYU", "zh", NULL, NULL },
{ "zh_HAKKA", "zh__HAKKA", NULL, NULL },
{ "zh_MIN", "zh__MIN", NULL, NULL },
{ "zh_MIN_NAN", "zh__MINNAN", NULL, NULL },
{ "zh_WUU", "zh__WUU", NULL, NULL },
{ "zh_XIANG", "zh__XIANG", NULL, NULL },
{ "zh_YUE", "zh__YUE", NULL, NULL },
{ "th_TH_TRADITIONAL", "th_TH", "calendar", "buddhist" },
{ "zh_TW_STROKE", "zh_TW", "collation", "stroke" },
{ "zh__PINYIN", "zh", "collation", "pinyin" }
};
#define ULOC_KEYWORD_BUFFER_LEN 25
#define ULOC_MAX_NO_KEYWORDS 25
static const char *
locale_getKeywordsStart(const char *localeID) {
const char *result = NULL;
static const uint8_t ebcdicSigns[] = { 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 };
if((result = uprv_strchr(localeID, '@')) != NULL) {
return result;
} else if(U_CHARSET_FAMILY == U_EBCDIC_FAMILY) {
const uint8_t *charToFind = ebcdicSigns;
while(*charToFind) {
if((result = uprv_strchr(localeID, *charToFind)) != NULL) {
return result;
}
charToFind++;
}
}
return NULL;
}
static int32_t locale_canonKeywordName(char *buf, const char *keywordName, UErrorCode *status)
{
int32_t i;
int32_t keywordNameLen = uprv_strlen(keywordName);
if(keywordNameLen >= ULOC_KEYWORD_BUFFER_LEN) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
for(i = 0; i < keywordNameLen; i++) {
buf[i] = uprv_tolower(keywordName[i]);
}
buf[i] = 0;
return keywordNameLen;
}
typedef struct {
char keyword[ULOC_KEYWORD_BUFFER_LEN];
int32_t keywordLen;
const char *valueStart;
int32_t valueLen;
} KeywordStruct;
static int32_t U_CALLCONV
compareKeywordStructs(const void *context, const void *left, const void *right) {
const char* leftString = ((const KeywordStruct *)left)->keyword;
const char* rightString = ((const KeywordStruct *)right)->keyword;
return uprv_strcmp(leftString, rightString);
}
static int32_t
_getKeywords(const char *localeID,
char prev,
char *keywords, int32_t keywordCapacity,
char *values, int32_t valuesCapacity, int32_t *valLen,
UBool valuesToo,
const char* addKeyword,
const char* addValue,
UErrorCode *status)
{
KeywordStruct keywordList[ULOC_MAX_NO_KEYWORDS];
int32_t maxKeywords = ULOC_MAX_NO_KEYWORDS;
int32_t numKeywords = 0;
const char* pos = localeID;
const char* equalSign = NULL;
const char* semicolon = NULL;
int32_t i = 0, j, n;
int32_t keywordsLen = 0;
int32_t valuesLen = 0;
if(prev == '@') {
do {
UBool duplicate = FALSE;
while(*pos == ' ') {
pos++;
}
if (!*pos) {
break;
}
if(numKeywords == maxKeywords) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
equalSign = uprv_strchr(pos, '=');
semicolon = uprv_strchr(pos, ';');
if(!equalSign || (semicolon && semicolon<equalSign)) {
*status = U_INVALID_FORMAT_ERROR;
return 0;
}
if(equalSign - pos >= ULOC_KEYWORD_BUFFER_LEN) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
for(i = 0, n = 0; i < equalSign - pos; ++i) {
if (pos[i] != ' ') {
keywordList[numKeywords].keyword[n++] = uprv_tolower(pos[i]);
}
}
keywordList[numKeywords].keyword[n] = 0;
keywordList[numKeywords].keywordLen = n;
equalSign++;
while(*equalSign == ' ') {
equalSign++;
}
keywordList[numKeywords].valueStart = equalSign;
pos = semicolon;
i = 0;
if(pos) {
while(*(pos - i - 1) == ' ') {
i++;
}
keywordList[numKeywords].valueLen = pos - equalSign - i;
pos++;
} else {
i = uprv_strlen(equalSign);
while(equalSign[i-1] == ' ') {
i--;
}
keywordList[numKeywords].valueLen = i;
}
for (j=0; j<numKeywords; ++j) {
if (uprv_strcmp(keywordList[j].keyword, keywordList[numKeywords].keyword) == 0) {
duplicate = TRUE;
break;
}
}
if (!duplicate) {
++numKeywords;
}
} while(pos);
if (addKeyword != NULL) {
UBool duplicate = FALSE;
U_ASSERT(addValue != NULL);
for (j=0; j<numKeywords; ++j) {
if (uprv_strcmp(keywordList[j].keyword, addKeyword) == 0) {
duplicate = TRUE;
break;
}
}
if (!duplicate) {
if (numKeywords == maxKeywords) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
uprv_strcpy(keywordList[numKeywords].keyword, addKeyword);
keywordList[numKeywords].keywordLen = uprv_strlen(addKeyword);
keywordList[numKeywords].valueStart = addValue;
keywordList[numKeywords].valueLen = uprv_strlen(addValue);
++numKeywords;
}
} else {
U_ASSERT(addValue == NULL);
}
uprv_sortArray(keywordList, numKeywords, sizeof(KeywordStruct), compareKeywordStructs, NULL, FALSE, status);
for(i = 0; i < numKeywords; i++) {
if(keywordsLen + keywordList[i].keywordLen + 1< keywordCapacity) {
uprv_strcpy(keywords+keywordsLen, keywordList[i].keyword);
if(valuesToo) {
keywords[keywordsLen + keywordList[i].keywordLen] = '=';
} else {
keywords[keywordsLen + keywordList[i].keywordLen] = 0;
}
}
keywordsLen += keywordList[i].keywordLen + 1;
if(valuesToo) {
if(keywordsLen + keywordList[i].valueLen < keywordCapacity) {
uprv_strncpy(keywords+keywordsLen, keywordList[i].valueStart, keywordList[i].valueLen);
}
keywordsLen += keywordList[i].valueLen;
if(i < numKeywords - 1) {
if(keywordsLen < keywordCapacity) {
keywords[keywordsLen] = ';';
}
keywordsLen++;
}
}
if(values) {
if(valuesLen + keywordList[i].valueLen + 1< valuesCapacity) {
uprv_strcpy(values+valuesLen, keywordList[i].valueStart);
values[valuesLen + keywordList[i].valueLen] = 0;
}
valuesLen += keywordList[i].valueLen + 1;
}
}
if(values) {
values[valuesLen] = 0;
if(valLen) {
*valLen = valuesLen;
}
}
return u_terminateChars(keywords, keywordCapacity, keywordsLen, status);
} else {
return 0;
}
}
U_CFUNC int32_t
locale_getKeywords(const char *localeID,
char prev,
char *keywords, int32_t keywordCapacity,
char *values, int32_t valuesCapacity, int32_t *valLen,
UBool valuesToo,
UErrorCode *status) {
return _getKeywords(localeID, prev, keywords, keywordCapacity,
values, valuesCapacity, valLen, valuesToo,
NULL, NULL, status);
}
U_CAPI int32_t U_EXPORT2
uloc_getKeywordValue(const char* localeID,
const char* keywordName,
char* buffer, int32_t bufferCapacity,
UErrorCode* status)
{
const char* nextSeparator = NULL;
int32_t keywordNameLen;
char keywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
char localeKeywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
int32_t i = 0;
int32_t result = 0;
if(status && U_SUCCESS(*status) && localeID) {
const char* startSearchHere = uprv_strchr(localeID, '@');
if(startSearchHere == NULL) {
return 0;
}
keywordNameLen = locale_canonKeywordName(keywordNameBuffer, keywordName, status);
if(U_FAILURE(*status)) {
return 0;
}
while(startSearchHere) {
startSearchHere++;
while(*startSearchHere == ' ') {
startSearchHere++;
}
nextSeparator = uprv_strchr(startSearchHere, '=');
if(!nextSeparator) {
break;
}
if(nextSeparator - startSearchHere >= ULOC_KEYWORD_BUFFER_LEN) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
for(i = 0; i < nextSeparator - startSearchHere; i++) {
localeKeywordNameBuffer[i] = uprv_tolower(startSearchHere[i]);
}
while(startSearchHere[i-1] == ' ') {
i--;
}
localeKeywordNameBuffer[i] = 0;
startSearchHere = uprv_strchr(nextSeparator, ';');
if(uprv_strcmp(keywordNameBuffer, localeKeywordNameBuffer) == 0) {
nextSeparator++;
while(*nextSeparator == ' ') {
nextSeparator++;
}
if(startSearchHere && startSearchHere - nextSeparator < bufferCapacity) {
while(*(startSearchHere-1) == ' ') {
startSearchHere--;
}
uprv_strncpy(buffer, nextSeparator, startSearchHere - nextSeparator);
result = u_terminateChars(buffer, bufferCapacity, startSearchHere - nextSeparator, status);
} else if(!startSearchHere && (int32_t)uprv_strlen(nextSeparator) < bufferCapacity) {
i = uprv_strlen(nextSeparator);
while(nextSeparator[i - 1] == ' ') {
i--;
}
uprv_strncpy(buffer, nextSeparator, i);
result = u_terminateChars(buffer, bufferCapacity, i, status);
} else {
*status = U_BUFFER_OVERFLOW_ERROR;
if(startSearchHere) {
result = startSearchHere - nextSeparator;
} else {
result = uprv_strlen(nextSeparator);
}
}
return result;
}
}
}
return 0;
}
U_CAPI int32_t U_EXPORT2
uloc_setKeywordValue(const char* keywordName,
const char* keywordValue,
char* buffer, int32_t bufferCapacity,
UErrorCode* status)
{
int32_t keywordNameLen;
int32_t keywordValueLen;
int32_t bufLen;
int32_t needLen = 0;
int32_t foundValueLen;
int32_t keywordAtEnd = 0;
char keywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
char localeKeywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
int32_t i = 0;
int32_t rc;
char* nextSeparator = NULL;
char* nextEqualsign = NULL;
char* startSearchHere = NULL;
char* keywordStart = NULL;
char *insertHere = NULL;
if(U_FAILURE(*status)) {
return -1;
}
if(keywordValue && !*keywordValue) {
keywordValue = NULL;
}
if(keywordValue) {
keywordValueLen = uprv_strlen(keywordValue);
} else {
keywordValueLen = 0;
}
keywordNameLen = locale_canonKeywordName(keywordNameBuffer, keywordName, status);
if(U_FAILURE(*status)) {
return 0;
}
startSearchHere = (char*)locale_getKeywordsStart(buffer);
if(bufferCapacity>1) {
bufLen = uprv_strlen(buffer);
} else {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(startSearchHere == NULL || (startSearchHere[1]==0)) {
if(!keywordValue) {
return bufLen;
}
needLen = bufLen+1+keywordNameLen+1+keywordValueLen;
if(startSearchHere) {
needLen--;
} else {
startSearchHere=buffer+bufLen;
}
if(needLen >= bufferCapacity) {
*status = U_BUFFER_OVERFLOW_ERROR;
return needLen;
}
*startSearchHere = '@';
startSearchHere++;
uprv_strcpy(startSearchHere, keywordNameBuffer);
startSearchHere += keywordNameLen;
*startSearchHere = '=';
startSearchHere++;
uprv_strcpy(startSearchHere, keywordValue);
startSearchHere+=keywordValueLen;
return needLen;
}
keywordStart = startSearchHere;
while(keywordStart) {
keywordStart++;
while(*keywordStart == ' ') {
keywordStart++;
}
nextEqualsign = uprv_strchr(keywordStart, '=');
if(!nextEqualsign) {
break;
}
if(nextEqualsign - keywordStart >= ULOC_KEYWORD_BUFFER_LEN) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
for(i = 0; i < nextEqualsign - keywordStart; i++) {
localeKeywordNameBuffer[i] = uprv_tolower(keywordStart[i]);
}
while(keywordStart[i-1] == ' ') {
i--;
}
localeKeywordNameBuffer[i] = 0;
nextSeparator = uprv_strchr(nextEqualsign, ';');
rc = uprv_strcmp(keywordNameBuffer, localeKeywordNameBuffer);
if(rc == 0) {
nextEqualsign++;
while(*nextEqualsign == ' ') {
nextEqualsign++;
}
if (nextSeparator) {
keywordAtEnd = 0;
foundValueLen = nextSeparator - nextEqualsign;
} else {
keywordAtEnd = 1;
foundValueLen = uprv_strlen(nextEqualsign);
}
if(keywordValue) {
if(foundValueLen == keywordValueLen) {
uprv_strncpy(nextEqualsign, keywordValue, keywordValueLen);
return bufLen;
} else if(foundValueLen > keywordValueLen) {
int32_t delta = foundValueLen - keywordValueLen;
if(nextSeparator) {
uprv_memmove(nextSeparator - delta, nextSeparator, bufLen-(nextSeparator-buffer));
}
uprv_strncpy(nextEqualsign, keywordValue, keywordValueLen);
bufLen -= delta;
buffer[bufLen]=0;
return bufLen;
} else {
int32_t delta = keywordValueLen - foundValueLen;
if((bufLen+delta) >= bufferCapacity) {
*status = U_BUFFER_OVERFLOW_ERROR;
return bufLen+delta;
}
if(nextSeparator) {
uprv_memmove(nextSeparator+delta,nextSeparator, bufLen-(nextSeparator-buffer));
}
uprv_strncpy(nextEqualsign, keywordValue, keywordValueLen);
bufLen += delta;
buffer[bufLen]=0;
return bufLen;
}
} else {
if(keywordAtEnd) {
keywordStart[-1] = 0;
return (keywordStart-buffer)-1;
} else {
uprv_memmove(keywordStart, nextSeparator+1, bufLen-((nextSeparator+1)-buffer));
keywordStart[bufLen-((nextSeparator+1)-buffer)]=0;
return bufLen-((nextSeparator+1)-keywordStart);
}
}
} else if(rc<0){
insertHere = keywordStart;
}
keywordStart = nextSeparator;
}
if(!keywordValue) {
return bufLen;
}
needLen = bufLen+1+keywordNameLen+1+keywordValueLen;
if(needLen >= bufferCapacity) {
*status = U_BUFFER_OVERFLOW_ERROR;
return needLen;
}
if(insertHere) {
uprv_memmove(insertHere+(1+keywordNameLen+1+keywordValueLen), insertHere, bufLen-(insertHere-buffer));
keywordStart = insertHere;
} else {
keywordStart = buffer+bufLen;
*keywordStart = ';';
keywordStart++;
}
uprv_strncpy(keywordStart, keywordNameBuffer, keywordNameLen);
keywordStart += keywordNameLen;
*keywordStart = '=';
keywordStart++;
uprv_strncpy(keywordStart, keywordValue, keywordValueLen);
keywordStart+=keywordValueLen;
if(insertHere) {
*keywordStart = ';';
keywordStart++;
}
buffer[needLen]=0;
return needLen;
}
#define _isIDSeparator(a) (a == '_' || a == '-')
#define _isPrefixLetter(a) ((a=='x')||(a=='X')||(a=='i')||(a=='I'))
#define _isIDPrefix(s) (_isPrefixLetter(s[0])&&_isIDSeparator(s[1]))
#define _isTerminator(a) ((a==0)||(a=='.')||(a=='@'))
static char* _strnchr(const char* str, int32_t len, char c) {
U_ASSERT(str != 0 && len >= 0);
while (len-- != 0) {
char d = *str;
if (d == c) {
return (char*) str;
} else if (d == 0) {
break;
}
++str;
}
return NULL;
}
static int16_t _findIndex(const char* const* list, const char* key)
{
const char* const* anchor = list;
int32_t pass = 0;
while (pass++ < 2) {
while (*list) {
if (uprv_strcmp(key, *list) == 0) {
return (int16_t)(list - anchor);
}
list++;
}
++list;
}
return -1;
}
static U_INLINE int32_t
_copyCount(char *dest, int32_t destCapacity, const char *src) {
const char *anchor;
char c;
anchor=src;
for(;;) {
if((c=*src)==0) {
return (int32_t)(src-anchor);
}
if(destCapacity<=0) {
return (int32_t)((src-anchor)+uprv_strlen(src));
}
++src;
*dest++=c;
--destCapacity;
}
}
static int32_t
_getLanguage(const char *localeID,
char *language, int32_t languageCapacity,
const char **pEnd) {
int32_t i=0;
int32_t offset;
char lang[4]={ 0, 0, 0, 0 };
if(_isIDPrefix(localeID)) {
if(i<languageCapacity) {
language[i]=(char)uprv_tolower(*localeID);
}
if(i<languageCapacity) {
language[i+1]='-';
}
i+=2;
localeID+=2;
}
while(!_isTerminator(*localeID) && !_isIDSeparator(*localeID)) {
if(i<languageCapacity) {
language[i]=(char)uprv_tolower(*localeID);
}
if(i<3) {
lang[i]=(char)uprv_tolower(*localeID);
}
i++;
localeID++;
}
if(i==3) {
offset=_findIndex(LANGUAGES_3, lang);
if(offset>=0) {
i=_copyCount(language, languageCapacity, LANGUAGES[offset]);
}
}
if(pEnd!=NULL) {
*pEnd=localeID;
}
return i;
}
static int32_t
_getScript(const char *localeID,
char *script, int32_t scriptCapacity,
const char **pEnd)
{
int32_t idLen = 0;
if (pEnd != NULL) {
*pEnd = localeID;
}
while(!_isTerminator(localeID[idLen]) && !_isIDSeparator(localeID[idLen])) {
idLen++;
}
if (idLen == 4) {
int32_t i;
if (pEnd != NULL) {
*pEnd = localeID+idLen;
}
if(idLen > scriptCapacity) {
idLen = scriptCapacity;
}
if (idLen >= 1) {
script[0]=(char)uprv_toupper(*(localeID++));
}
for (i = 1; i < idLen; i++) {
script[i]=(char)uprv_tolower(*(localeID++));
}
}
else {
idLen = 0;
}
return idLen;
}
static int32_t
_getCountry(const char *localeID,
char *country, int32_t countryCapacity,
const char **pEnd)
{
int32_t i=0;
char cnty[ULOC_COUNTRY_CAPACITY]={ 0, 0, 0, 0 };
int32_t offset;
while(!_isTerminator(*localeID) && !_isIDSeparator(*localeID)) {
if(i<countryCapacity) {
country[i]=(char)uprv_toupper(*localeID);
}
if(i<(ULOC_COUNTRY_CAPACITY-1)) {
cnty[i]=(char)uprv_toupper(*localeID);
}
i++;
localeID++;
}
if(i==3) {
offset=_findIndex(COUNTRIES_3, cnty);
if(offset>=0) {
i=_copyCount(country, countryCapacity, COUNTRIES[offset]);
}
}
if(pEnd!=NULL) {
*pEnd=localeID;
}
return i;
}
static int32_t
_getVariantEx(const char *localeID,
char prev,
char *variant, int32_t variantCapacity,
UBool needSeparator) {
int32_t i=0;
if(_isIDSeparator(prev)) {
while(!_isTerminator(*localeID)) {
if (needSeparator) {
if (i<variantCapacity) {
variant[i] = '_';
}
++i;
needSeparator = FALSE;
}
if(i<variantCapacity) {
variant[i]=(char)uprv_toupper(*localeID);
if(variant[i]=='-') {
variant[i]='_';
}
}
i++;
localeID++;
}
}
if(i==0) {
if(prev=='@') {
} else if((localeID=locale_getKeywordsStart(localeID))!=NULL) {
++localeID;
} else {
return 0;
}
while(!_isTerminator(*localeID)) {
if (needSeparator) {
if (i<variantCapacity) {
variant[i] = '_';
}
++i;
needSeparator = FALSE;
}
if(i<variantCapacity) {
variant[i]=(char)uprv_toupper(*localeID);
if(variant[i]=='-' || variant[i]==',') {
variant[i]='_';
}
}
i++;
localeID++;
}
}
return i;
}
static int32_t
_getVariant(const char *localeID,
char prev,
char *variant, int32_t variantCapacity) {
return _getVariantEx(localeID, prev, variant, variantCapacity, FALSE);
}
static int32_t
_deleteVariant(char* variants, int32_t variantsLen,
const char* toDelete, int32_t toDeleteLen) {
int32_t delta = 0;
for (;;) {
UBool flag = FALSE;
if (variantsLen < toDeleteLen) {
return delta;
}
if (uprv_strncmp(variants, toDelete, toDeleteLen) == 0 &&
(variantsLen == toDeleteLen ||
(flag=(variants[toDeleteLen] == '_')))) {
int32_t d = toDeleteLen + (flag?1:0);
variantsLen -= d;
delta += d;
uprv_memmove(variants, variants+d, variantsLen);
} else {
char* p = _strnchr(variants, variantsLen, '_');
if (p == NULL) {
return delta;
}
++p;
variantsLen -= p - variants;
variants = p;
}
}
}
typedef struct UKeywordsContext {
char* keywords;
char* current;
} UKeywordsContext;
static void U_CALLCONV
uloc_kw_closeKeywords(UEnumeration *enumerator) {
uprv_free(((UKeywordsContext *)enumerator->context)->keywords);
uprv_free(enumerator->context);
uprv_free(enumerator);
}
static int32_t U_CALLCONV
uloc_kw_countKeywords(UEnumeration *en, UErrorCode *status) {
char *kw = ((UKeywordsContext *)en->context)->keywords;
int32_t result = 0;
while(*kw) {
result++;
kw += uprv_strlen(kw)+1;
}
return result;
}
static const char* U_CALLCONV
uloc_kw_nextKeyword(UEnumeration* en,
int32_t* resultLength,
UErrorCode* status) {
const char* result = ((UKeywordsContext *)en->context)->current;
int32_t len = 0;
if(*result) {
len = uprv_strlen(((UKeywordsContext *)en->context)->current);
((UKeywordsContext *)en->context)->current += len+1;
} else {
result = NULL;
}
if (resultLength) {
*resultLength = len;
}
return result;
}
static void U_CALLCONV
uloc_kw_resetKeywords(UEnumeration* en,
UErrorCode* status) {
((UKeywordsContext *)en->context)->current = ((UKeywordsContext *)en->context)->keywords;
}
static const UEnumeration gKeywordsEnum = {
NULL,
NULL,
uloc_kw_closeKeywords,
uloc_kw_countKeywords,
uenum_unextDefault,
uloc_kw_nextKeyword,
uloc_kw_resetKeywords
};
U_CAPI UEnumeration* U_EXPORT2
uloc_openKeywordList(const char *keywordList, int32_t keywordListSize, UErrorCode* status)
{
UKeywordsContext *myContext = NULL;
UEnumeration *result = NULL;
if(U_FAILURE(*status)) {
return NULL;
}
result = (UEnumeration *)uprv_malloc(sizeof(UEnumeration));
uprv_memcpy(result, &gKeywordsEnum, sizeof(UEnumeration));
myContext = uprv_malloc(sizeof(UKeywordsContext));
if (myContext == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(result);
return NULL;
}
myContext->keywords = (char *)uprv_malloc(keywordListSize+1);
uprv_memcpy(myContext->keywords, keywordList, keywordListSize);
myContext->keywords[keywordListSize] = 0;
myContext->current = myContext->keywords;
result->context = myContext;
return result;
}
U_CAPI UEnumeration* U_EXPORT2
uloc_openKeywords(const char* localeID,
UErrorCode* status)
{
int32_t i=0;
char keywords[256];
int32_t keywordsCapacity = 256;
if(status==NULL || U_FAILURE(*status)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
const char *scriptID;
_getScript(localeID+1, NULL, 0, &scriptID);
if(scriptID != localeID+1) {
localeID = scriptID;
}
if (_isIDSeparator(*localeID)) {
_getCountry(localeID+1, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
_getVariant(localeID+1, *localeID, NULL, 0);
}
}
}
if((localeID = locale_getKeywordsStart(localeID)) != NULL) {
i=locale_getKeywords(localeID+1, '@', keywords, keywordsCapacity, NULL, 0, NULL, FALSE, status);
}
if(i) {
return uloc_openKeywordList(keywords, i, status);
} else {
return NULL;
}
}
#define _ULOC_STRIP_KEYWORDS 0x2
#define _ULOC_CANONICALIZE 0x1
#define OPTION_SET(options, mask) ((options & mask) != 0)
static int32_t
_canonicalize(const char* localeID,
char* result,
int32_t resultCapacity,
uint32_t options,
UErrorCode* err) {
int32_t j, len, fieldCount=0, scriptSize=0, variantSize=0, nameCapacity;
char localeBuffer[ULOC_FULLNAME_CAPACITY];
const char* keywordAssign = NULL;
const char* separatorIndicator = NULL;
const char* addKeyword = NULL;
const char* addValue = NULL;
char* name;
char* variant = NULL;
int32_t sawEuro = 0;
if (U_FAILURE(*err)) {
return 0;
}
if (localeID==NULL) {
localeID=uloc_getDefault();
}
if (OPTION_SET(options, _ULOC_CANONICALIZE) &&
(result == NULL || resultCapacity < sizeof(localeBuffer))) {
name = localeBuffer;
nameCapacity = sizeof(localeBuffer);
} else {
name = result;
nameCapacity = resultCapacity;
}
len=_getLanguage(localeID, name, nameCapacity, &localeID);
if(_isIDSeparator(*localeID)) {
const char *scriptID;
++fieldCount;
if(len<nameCapacity) {
name[len]='_';
}
++len;
scriptSize=_getScript(localeID+1, name+len, nameCapacity-len, &scriptID);
if(scriptSize > 0) {
localeID = scriptID;
++fieldCount;
len+=scriptSize;
if (_isIDSeparator(*localeID)) {
if(len<nameCapacity) {
name[len]='_';
}
++len;
}
}
if (_isIDSeparator(*localeID)) {
len+=_getCountry(localeID+1, name+len, nameCapacity-len, &localeID);
if(_isIDSeparator(*localeID)) {
++fieldCount;
if(len<nameCapacity) {
name[len]='_';
}
++len;
variantSize = _getVariant(localeID+1, *localeID, name+len, nameCapacity-len);
if (variantSize > 0) {
variant = name+len;
len += variantSize;
localeID += variantSize + 1;
}
}
}
}
if (!OPTION_SET(options, _ULOC_CANONICALIZE) && *localeID == '.') {
UBool done = FALSE;
do {
char c = *localeID;
switch (c) {
case 0:
case '@':
done = TRUE;
break;
default:
if (len<nameCapacity) {
name[len] = c;
}
++len;
++localeID;
break;
}
} while (!done);
}
if ((localeID=locale_getKeywordsStart(localeID))!=NULL) {
keywordAssign = uprv_strchr(localeID, '=');
separatorIndicator = uprv_strchr(localeID, ';');
}
if (!OPTION_SET(options, _ULOC_CANONICALIZE) &&
localeID != NULL && keywordAssign == NULL) {
for (;;) {
char c = *localeID;
if (c == 0) {
break;
}
if (len<nameCapacity) {
name[len] = c;
}
++len;
++localeID;
}
}
if (OPTION_SET(options, _ULOC_CANONICALIZE)) {
if (localeID!=NULL && keywordAssign==NULL) {
int32_t posixVariantSize;
if (fieldCount < 2 || (fieldCount < 3 && scriptSize > 0)) {
do {
if(len<nameCapacity) {
name[len]='_';
}
++len;
++fieldCount;
} while(fieldCount<2);
}
posixVariantSize = _getVariantEx(localeID+1, '@', name+len, nameCapacity-len,
(UBool)(variantSize > 0));
if (posixVariantSize > 0) {
if (variant == NULL) {
variant = name+len;
}
len += posixVariantSize;
variantSize += posixVariantSize;
}
}
sawEuro = _deleteVariant(variant, variantSize, "EURO", 4);
len -= sawEuro;
if (sawEuro > 0 && name[len-1] == '_') {
--len;
}
for (j=0; j<(int32_t)(sizeof(CANONICALIZE_MAP)/sizeof(CANONICALIZE_MAP[0])); j++) {
const char* id = CANONICALIZE_MAP[j].id;
int32_t n = uprv_strlen(id);
if (len == n && uprv_strncmp(name, id, n) == 0) {
if (n == 0 && localeID != NULL) {
break;
}
len = _copyCount(name, nameCapacity, CANONICALIZE_MAP[j].canonicalID);
addKeyword = CANONICALIZE_MAP[j].keyword;
addValue = CANONICALIZE_MAP[j].value;
break;
}
}
if (sawEuro > 0) {
addKeyword = "currency";
addValue = "EUR";
}
}
if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) {
if (localeID!=NULL && keywordAssign!=NULL &&
(!separatorIndicator || separatorIndicator > keywordAssign)) {
if(len<nameCapacity) {
name[len]='@';
}
++len;
++fieldCount;
len += _getKeywords(localeID+1, '@', name+len, nameCapacity-len, NULL, 0, NULL, TRUE,
addKeyword, addValue, err);
} else if (addKeyword != NULL) {
U_ASSERT(addValue != NULL);
len += _copyCount(name+len, nameCapacity-len, "@");
len += _copyCount(name+len, nameCapacity-len, addKeyword);
len += _copyCount(name+len, nameCapacity-len, "=");
len += _copyCount(name+len, nameCapacity-len, addValue);
}
}
if (U_SUCCESS(*err) && name == localeBuffer) {
uprv_strncpy(result, localeBuffer, (len > resultCapacity) ? resultCapacity : len);
}
return u_terminateChars(result, resultCapacity, len, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getParent(const char* localeID,
char* parent,
int32_t parentCapacity,
UErrorCode* err)
{
const char *lastUnderscore;
int32_t i;
if (U_FAILURE(*err))
return 0;
if (localeID == NULL)
localeID = uloc_getDefault();
lastUnderscore=uprv_strrchr(localeID, '_');
if(lastUnderscore!=NULL) {
i=(int32_t)(lastUnderscore-localeID);
} else {
i=0;
}
if(i>0) {
uprv_memcpy(parent, localeID, uprv_min(i, parentCapacity));
}
return u_terminateChars(parent, parentCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getLanguage(const char* localeID,
char* language,
int32_t languageCapacity,
UErrorCode* err)
{
int32_t i=0;
if (err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
i=_getLanguage(localeID, language, languageCapacity, NULL);
return u_terminateChars(language, languageCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getScript(const char* localeID,
char* script,
int32_t scriptCapacity,
UErrorCode* err)
{
int32_t i=0;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
i=_getScript(localeID+1, script, scriptCapacity, NULL);
}
return u_terminateChars(script, scriptCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getCountry(const char* localeID,
char* country,
int32_t countryCapacity,
UErrorCode* err)
{
int32_t i=0;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
const char *scriptID;
_getScript(localeID+1, NULL, 0, &scriptID);
if(scriptID != localeID+1) {
localeID = scriptID;
}
if(_isIDSeparator(*localeID)) {
i=_getCountry(localeID+1, country, countryCapacity, NULL);
}
}
return u_terminateChars(country, countryCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getVariant(const char* localeID,
char* variant,
int32_t variantCapacity,
UErrorCode* err)
{
int32_t i=0;
UBool haveVariant=FALSE;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
const char *scriptID;
_getScript(localeID+1, NULL, 0, &scriptID);
if(scriptID != localeID+1) {
localeID = scriptID;
}
if (_isIDSeparator(*localeID)) {
_getCountry(localeID+1, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
haveVariant=TRUE;
i=_getVariant(localeID+1, *localeID, variant, variantCapacity);
}
}
}
return u_terminateChars(variant, variantCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getName(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
return _canonicalize(localeID, name, nameCapacity, 0, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getBaseName(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
return _canonicalize(localeID, name, nameCapacity, _ULOC_STRIP_KEYWORDS, err);
}
U_CAPI int32_t U_EXPORT2
uloc_canonicalize(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
return _canonicalize(localeID, name, nameCapacity, _ULOC_CANONICALIZE, err);
}
U_CAPI const char* U_EXPORT2
uloc_getISO3Language(const char* localeID)
{
int16_t offset;
char lang[ULOC_LANG_CAPACITY];
UErrorCode err = U_ZERO_ERROR;
if (localeID == NULL)
{
localeID = uloc_getDefault();
}
uloc_getLanguage(localeID, lang, ULOC_LANG_CAPACITY, &err);
if (U_FAILURE(err))
return "";
offset = _findIndex(LANGUAGES, lang);
if (offset < 0)
return "";
return LANGUAGES_3[offset];
}
U_CAPI const char* U_EXPORT2
uloc_getISO3Country(const char* localeID)
{
int16_t offset;
char cntry[ULOC_LANG_CAPACITY];
UErrorCode err = U_ZERO_ERROR;
if (localeID == NULL)
{
localeID = uloc_getDefault();
}
uloc_getCountry(localeID, cntry, ULOC_LANG_CAPACITY, &err);
if (U_FAILURE(err))
return "";
offset = _findIndex(COUNTRIES, cntry);
if (offset < 0)
return "";
return COUNTRIES_3[offset];
}
U_CAPI uint32_t U_EXPORT2
uloc_getLCID(const char* localeID)
{
UErrorCode status = U_ZERO_ERROR;
char langID[ULOC_FULLNAME_CAPACITY];
uloc_getLanguage(localeID, langID, sizeof(langID), &status);
if (U_FAILURE(status)) {
return 0;
}
return uprv_convertToLCID(langID, localeID, &status);
}
U_CAPI const char* U_EXPORT2
uloc_getDefault()
{
return locale_get_default();
}
U_CAPI void U_EXPORT2
uloc_setDefault(const char* newDefaultLocale,
UErrorCode* err)
{
if (U_FAILURE(*err))
return;
locale_set_default(newDefaultLocale);
}
static const UChar *
_res_getTableStringWithFallback(const char *path, const char *locale,
const char *tableKey, const char *subTableKey,
const char *itemKey,
int32_t *pLength,
UErrorCode *pErrorCode)
{
char localeBuffer[ULOC_FULLNAME_CAPACITY*4];
UResourceBundle *rb, table;
const UChar *item;
UErrorCode errorCode;
char explicitFallbackName[ULOC_FULLNAME_CAPACITY] = {0};
int32_t efnLen =0;
const UChar* ef = NULL;
UBool overrideExplicitFallback = FALSE;
for(;;) {
errorCode=U_ZERO_ERROR;
rb=ures_open(path, locale, &errorCode);
if(U_FAILURE(errorCode)) {
*pErrorCode=errorCode;
return NULL;
} else if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
errorCode=U_ZERO_ERROR;
ures_initStackObject(&table);
ures_getByKey(rb, tableKey, &table, &errorCode);
if(U_FAILURE(errorCode)) {
ures_close(rb);
*pErrorCode=errorCode;
return NULL;
} else if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
ef = ures_getStringByKey(&table, "Fallback", &efnLen, &errorCode);
if(U_SUCCESS(errorCode)){
u_UCharsToChars(ef, explicitFallbackName, efnLen);
explicitFallbackName[efnLen]=0;
}else if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
errorCode=U_ZERO_ERROR;
if(subTableKey == NULL){
item=ures_getStringByKey(&table, itemKey, pLength, &errorCode);
}else{
UResourceBundle subTable;
ures_initStackObject(&subTable);
ures_getByKey(&table, subTableKey, &subTable, &errorCode);
item = ures_getStringByKey(&subTable, itemKey, pLength, &errorCode);
ures_close(&subTable);
}
if(U_SUCCESS(errorCode)) {
if(item[0]==0 && efnLen > 0){
overrideExplicitFallback = TRUE;
}else{
ures_close(&table);
ures_close(rb);
if(errorCode==U_USING_DEFAULT_WARNING ||
(errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
) {
*pErrorCode=errorCode;
}
return item;
}
}
errorCode=U_ZERO_ERROR;
locale=ures_getLocale(&table, &errorCode);
if(U_FAILURE(errorCode)) {
ures_close(&table);
ures_close(rb);
*pErrorCode=U_INTERNAL_PROGRAM_ERROR;
return NULL;
}
if(*locale==0 || 0==uprv_strcmp(locale, _kRootName) || 0==uprv_strcmp(locale,explicitFallbackName)) {
ures_close(&table);
ures_close(rb);
*pErrorCode=U_MISSING_RESOURCE_ERROR;
return NULL;
}
errorCode=U_ZERO_ERROR;
if(efnLen > 0 && overrideExplicitFallback == FALSE){
locale = explicitFallbackName;
}else{
uloc_getParent(locale, localeBuffer, sizeof(localeBuffer), &errorCode);
if(U_FAILURE(errorCode) || errorCode==U_STRING_NOT_TERMINATED_WARNING) {
*pErrorCode=U_INTERNAL_PROGRAM_ERROR;
return NULL;
}
locale=localeBuffer;
if (uprv_strlen(locale) == 0)
*pErrorCode = U_USING_DEFAULT_WARNING;
else if (*pErrorCode != U_USING_DEFAULT_WARNING)
*pErrorCode = U_USING_FALLBACK_WARNING;
}
ures_close(&table);
ures_close(rb);
}
}
static int32_t
_getStringOrCopyKey(const char *path, const char *locale,
const char *tableKey,
const char* subTableKey,
const char *itemKey,
const char *substitute,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
const UChar *s = NULL;
int32_t length;
if(itemKey==NULL) {
UResourceBundle *rb;
rb=ures_open(path, locale, pErrorCode);
if(U_SUCCESS(*pErrorCode)) {
s=ures_getStringByKey(rb, tableKey, &length, pErrorCode);
ures_close(rb);
}
} else {
s=_res_getTableStringWithFallback(path, locale,
tableKey,
subTableKey,
itemKey,
&length,
pErrorCode);
}
if(U_SUCCESS(*pErrorCode)) {
int32_t copyLength=uprv_min(length, destCapacity);
if(copyLength>0 && s != NULL) {
u_memcpy(dest, s, copyLength);
}
} else {
length=(int32_t)uprv_strlen(substitute);
u_charsToUChars(substitute, dest, uprv_min(length, destCapacity));
*pErrorCode=U_USING_DEFAULT_WARNING;
}
return u_terminateUChars(dest, destCapacity, length, pErrorCode);
}
static int32_t
_getDisplayNameForComponent(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
int32_t (*getter)(const char *, char *, int32_t, UErrorCode *),
const char *tag,
UErrorCode *pErrorCode) {
char localeBuffer[ULOC_FULLNAME_CAPACITY*4];
int32_t length;
UErrorCode localStatus;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
localStatus = U_ZERO_ERROR;
length=(*getter)(locale, localeBuffer, sizeof(localeBuffer), &localStatus);
if(U_FAILURE(localStatus) || localStatus==U_STRING_NOT_TERMINATED_WARNING) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(length==0) {
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
}
return _getStringOrCopyKey(NULL, displayLocale,
tag, NULL, localeBuffer,
localeBuffer,
dest, destCapacity,
pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayLanguage(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity,
uloc_getLanguage, _kLanguages, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayScript(const char* locale,
const char* displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode)
{
return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity,
uloc_getScript, _kScripts, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayCountry(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity,
uloc_getCountry, _kCountries, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayVariant(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode) {
return _getDisplayNameForComponent(locale, displayLocale, dest, destCapacity,
uloc_getVariant, _kVariants, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayName(const char *locale,
const char *displayLocale,
UChar *dest, int32_t destCapacity,
UErrorCode *pErrorCode)
{
int32_t length, length2, length3 = 0;
UBool hasLanguage, hasScript, hasCountry, hasVariant, hasKeywords;
UEnumeration* keywordEnum = NULL;
int32_t keywordCount = 0;
const char *keyword = NULL;
int32_t keywordLen = 0;
char keywordValue[256];
int32_t keywordValueLen = 0;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
length=uloc_getDisplayLanguage(locale, displayLocale,
dest, destCapacity,
pErrorCode);
hasLanguage= length>0;
if(hasLanguage) {
if(length<destCapacity) {
dest[length]=0x20;
}
++length;
if(length<destCapacity) {
dest[length]=0x28;
}
++length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
if(length<destCapacity) {
length2=uloc_getDisplayScript(locale, displayLocale,
dest+length, destCapacity-length,
pErrorCode);
} else {
length2=uloc_getDisplayScript(locale, displayLocale,
NULL, 0,
pErrorCode);
}
hasScript= length2>0;
length+=length2;
if(hasScript) {
if(length<destCapacity) {
dest[length]=0x2c;
}
++length;
if(length<destCapacity) {
dest[length]=0x20;
}
++length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
if(length<destCapacity) {
length2=uloc_getDisplayCountry(locale, displayLocale,
dest+length, destCapacity-length,
pErrorCode);
} else {
length2=uloc_getDisplayCountry(locale, displayLocale,
NULL, 0,
pErrorCode);
}
hasCountry= length2>0;
length+=length2;
if(hasCountry) {
if(length<destCapacity) {
dest[length]=0x2c;
}
++length;
if(length<destCapacity) {
dest[length]=0x20;
}
++length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
if(length<destCapacity) {
length2=uloc_getDisplayVariant(locale, displayLocale,
dest+length, destCapacity-length,
pErrorCode);
} else {
length2=uloc_getDisplayVariant(locale, displayLocale,
NULL, 0,
pErrorCode);
}
hasVariant= length2>0;
length+=length2;
if(hasVariant) {
if(length<destCapacity) {
dest[length]=0x2c;
}
++length;
if(length<destCapacity) {
dest[length]=0x20;
}
++length;
}
keywordEnum = uloc_openKeywords(locale, pErrorCode);
for(keywordCount = uenum_count(keywordEnum, pErrorCode); keywordCount > 0 ; keywordCount--){
if(U_FAILURE(*pErrorCode)){
break;
}
keyword = uenum_next(keywordEnum, &keywordLen, pErrorCode);
if(length + length3 < destCapacity) {
length3 += uloc_getDisplayKeyword(keyword, displayLocale, dest+length+length3, destCapacity-length-length3, pErrorCode);
} else {
length3 += uloc_getDisplayKeyword(keyword, displayLocale, NULL, 0, pErrorCode);
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
keywordValueLen = uloc_getKeywordValue(locale, keyword, keywordValue, 256, pErrorCode);
if(keywordValueLen) {
if(length + length3 < destCapacity) {
dest[length + length3] = 0x3D;
}
length3++;
if(length + length3 < destCapacity) {
length3 += uloc_getDisplayKeywordValue(locale, keyword, displayLocale, dest+length+length3, destCapacity-length-length3, pErrorCode);
} else {
length3 += uloc_getDisplayKeywordValue(locale, keyword, displayLocale, NULL, 0, pErrorCode);
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
}
if(keywordCount > 1) {
if(length + length3 + 1 < destCapacity && keywordCount) {
dest[length + length3]=0x2c;
dest[length + length3+1]=0x20;
}
length3++;
length3++;
}
}
uenum_close(keywordEnum);
hasKeywords = length3 > 0;
length += length3;
if ((hasScript && !hasCountry)
|| ((hasScript || hasCountry) && !hasVariant && !hasKeywords)
|| ((hasScript || hasCountry || hasVariant) && !hasKeywords)
|| (hasLanguage && !hasScript && !hasCountry && !hasVariant && !hasKeywords))
{
length-=2;
}
if (hasLanguage && (hasScript || hasCountry || hasVariant || hasKeywords)) {
if(length<destCapacity) {
dest[length]=0x29;
}
++length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
}
return u_terminateUChars(dest, destCapacity, length, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
uloc_getDisplayKeyword(const char* keyword,
const char* displayLocale,
UChar* dest,
int32_t destCapacity,
UErrorCode* status){
if(status==NULL || U_FAILURE(*status)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*status=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return _getStringOrCopyKey(NULL, displayLocale,
_kKeys, NULL,
keyword,
keyword,
dest, destCapacity,
status);
}
#define UCURRENCY_DISPLAY_NAME_INDEX 1
U_CAPI int32_t U_EXPORT2
uloc_getDisplayKeywordValue( const char* locale,
const char* keyword,
const char* displayLocale,
UChar* dest,
int32_t destCapacity,
UErrorCode* status){
char keywordValue[ULOC_FULLNAME_CAPACITY*4];
int32_t capacity = ULOC_FULLNAME_CAPACITY*4;
int32_t keywordValueLen =0;
if(status==NULL || U_FAILURE(*status)) {
return 0;
}
if(destCapacity<0 || (destCapacity>0 && dest==NULL)) {
*status=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
keywordValue[0]=0;
keywordValueLen = uloc_getKeywordValue(locale, keyword, keywordValue, capacity, status);
if(uprv_stricmp(keyword, _kCurrency)==0){
int32_t dispNameLen = 0;
const UChar *dispName = NULL;
UResourceBundle *bundle = ures_open(NULL, displayLocale, status);
UResourceBundle *currencies = ures_getByKey(bundle, _kCurrencies, NULL, status);
UResourceBundle *currency = ures_getByKeyWithFallback(currencies, keywordValue, NULL, status);
dispName = ures_getStringByIndex(currency, UCURRENCY_DISPLAY_NAME_INDEX, &dispNameLen, status);
ures_close(currency);
ures_close(currencies);
ures_close(bundle);
if(U_FAILURE(*status)){
if(*status == U_MISSING_RESOURCE_ERROR){
*status = U_USING_DEFAULT_WARNING;
}else{
return 0;
}
}
if(dispName != NULL){
if(dispNameLen <= destCapacity){
uprv_memcpy(dest, dispName, dispNameLen * U_SIZEOF_UCHAR);
return u_terminateUChars(dest, destCapacity, dispNameLen, status);
}else{
*status = U_BUFFER_OVERFLOW_ERROR;
return dispNameLen;
}
}else{
if(keywordValueLen <= destCapacity){
u_charsToUChars(keywordValue, dest, keywordValueLen);
return u_terminateUChars(dest, destCapacity, keywordValueLen, status);
}else{
*status = U_BUFFER_OVERFLOW_ERROR;
return keywordValueLen;
}
}
}else{
return _getStringOrCopyKey(NULL, displayLocale,
_kTypes, keyword,
keywordValue,
keywordValue,
dest, destCapacity,
status);
}
}
static UBool U_CALLCONV uloc_cleanup(void) {
char ** temp;
if (_installedLocales) {
temp = _installedLocales;
_installedLocales = NULL;
_installedLocalesCount = 0;
uprv_free(temp);
}
return TRUE;
}
static void _load_installedLocales()
{
UBool localesLoaded;
umtx_lock(NULL);
localesLoaded = _installedLocales != NULL;
umtx_unlock(NULL);
if (localesLoaded == FALSE) {
UResourceBundle *index = NULL;
UResourceBundle installed;
UErrorCode status = U_ZERO_ERROR;
char ** temp;
int32_t i = 0;
int32_t localeCount;
ures_initStackObject(&installed);
index = ures_openDirect(NULL, _kIndexLocaleName, &status);
ures_getByKey(index, _kIndexTag, &installed, &status);
if(U_SUCCESS(status)) {
localeCount = ures_getSize(&installed);
temp = (char **) uprv_malloc(sizeof(char*) * (localeCount+1));
ures_resetIterator(&installed);
while(ures_hasNext(&installed)) {
ures_getNextString(&installed, NULL, (const char **)&temp[i++], &status);
}
temp[i] = NULL;
umtx_lock(NULL);
if (_installedLocales == NULL)
{
_installedLocales = temp;
_installedLocalesCount = localeCount;
temp = NULL;
ucln_common_registerCleanup(UCLN_COMMON_ULOC, uloc_cleanup);
}
umtx_unlock(NULL);
uprv_free(temp);
ures_close(&installed);
}
ures_close(index);
}
}
U_CAPI const char* U_EXPORT2
uloc_getAvailable(int32_t offset)
{
_load_installedLocales();
if (offset > _installedLocalesCount)
return NULL;
return _installedLocales[offset];
}
U_CAPI int32_t U_EXPORT2
uloc_countAvailable()
{
_load_installedLocales();
return _installedLocalesCount;
}
U_CAPI const char* const* U_EXPORT2
uloc_getISOLanguages()
{
return LANGUAGES;
}
U_CAPI const char* const* U_EXPORT2
uloc_getISOCountries()
{
return COUNTRIES;
}
static char gDecimal = 0;
static
double
_uloc_strtod(const char *start, char **end) {
char *decimal;
char *myEnd;
char buf[30];
double rv;
if (!gDecimal) {
char rep[5];
sprintf(rep, "%+1.1f", 1.0);
gDecimal = rep[2];
}
if(gDecimal == '.') {
return uprv_strtod(start, end);
} else {
uprv_strncpy(buf, start, 29);
buf[29]=0;
decimal = uprv_strchr(buf, '.');
if(decimal) {
*decimal = gDecimal;
} else {
return uprv_strtod(start, end);
}
rv = uprv_strtod(buf, &myEnd);
if(end) {
*end = (char*)(start+(myEnd-buf));
}
return rv;
}
}
typedef struct {
double q;
char *locale;
#if defined(ULOC_DEBUG_PURIFY)
int32_t dummy;
#endif
} _acceptLangItem;
static int32_t U_CALLCONV
uloc_acceptLanguageCompare(const void *context, const void *a, const void *b)
{
const _acceptLangItem *aa = (const _acceptLangItem*)a;
const _acceptLangItem *bb = (const _acceptLangItem*)b;
int32_t rc = 0;
if(bb->q < aa->q) {
rc = -1;
} else if(bb->q > aa->q) {
rc = 1;
} else {
rc = 0;
}
if(rc==0) {
rc = uprv_stricmp(aa->locale, bb->locale);
}
#if defined(ULOC_DEBUG)
#endif
return rc;
}
U_CAPI int32_t U_EXPORT2
uloc_acceptLanguageFromHTTP(char *result, int32_t resultAvailable, UAcceptResult *outResult,
const char *httpAcceptLanguage,
UEnumeration* availableLocales,
UErrorCode *status)
{
_acceptLangItem *j;
_acceptLangItem smallBuffer[30];
char **strs;
char tmp[ULOC_FULLNAME_CAPACITY +1];
int32_t n = 0;
const char *itemEnd;
const char *paramEnd;
const char *s;
const char *t;
int32_t res;
int32_t i;
int32_t l = uprv_strlen(httpAcceptLanguage);
int32_t jSize;
j = smallBuffer;
jSize = sizeof(smallBuffer)/sizeof(smallBuffer[0]);
if(U_FAILURE(*status)) {
return -1;
}
for(s=httpAcceptLanguage;s&&*s;) {
while(isspace(*s))
s++;
itemEnd=uprv_strchr(s,',');
paramEnd=uprv_strchr(s,';');
if(!itemEnd) {
itemEnd = httpAcceptLanguage+l;
}
if(paramEnd && paramEnd<itemEnd) {
t = paramEnd+1;
if(*t=='q') {
t++;
}
while(isspace(*t)) {
t++;
}
if(*t=='=') {
t++;
}
while(isspace(*t)) {
t++;
}
j[n].q = _uloc_strtod(t,NULL);
} else {
j[n].q = 1.0;
paramEnd = itemEnd;
}
#if defined(ULOC_DEBUG_PURIFY)
j[n].dummy=0xDECAFBAD;
#endif
for(t=(paramEnd-1);(paramEnd>s)&&isspace(*t);t--)
;
j[n].locale = uprv_strndup(s,(t+1)-s);
uloc_canonicalize(j[n].locale,tmp,sizeof(tmp)/sizeof(tmp[0]),status);
if(strcmp(j[n].locale,tmp)) {
uprv_free(j[n].locale);
j[n].locale=uprv_strdup(tmp);
}
#if defined(ULOC_DEBUG)
#endif
n++;
s = itemEnd;
while(*s==',') {
s++;
}
if(n>=jSize) {
if(j==smallBuffer) {
j = uprv_malloc(sizeof(j[0])*(jSize*2));
if(j!=NULL) {
uprv_memcpy(j,smallBuffer,sizeof(j[0])*jSize);
}
#if defined(ULOC_DEBUG)
fprintf(stderr,"malloced at size %d\n", jSize);
#endif
} else {
j = uprv_realloc(j, sizeof(j[0])*jSize*2);
#if defined(ULOC_DEBUG)
fprintf(stderr,"re-alloced at size %d\n", jSize);
#endif
}
jSize *= 2;
if(j==NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return -1;
}
}
}
uprv_sortArray(j, n, sizeof(j[0]), uloc_acceptLanguageCompare, NULL, TRUE, status);
if(U_FAILURE(*status)) {
if(j != smallBuffer) {
#if defined(ULOC_DEBUG)
fprintf(stderr,"freeing j %p\n", j);
#endif
uprv_free(j);
}
return -1;
}
strs = uprv_malloc((size_t)(sizeof(strs[0])*n));
for(i=0;i<n;i++) {
#if defined(ULOC_DEBUG)
#endif
strs[i]=j[i].locale;
}
res = uloc_acceptLanguage(result, resultAvailable, outResult,
(const char**)strs, n, availableLocales, status);
for(i=0;i<n;i++) {
uprv_free(strs[i]);
}
uprv_free(strs);
if(j != smallBuffer) {
#if defined(ULOC_DEBUG)
fprintf(stderr,"freeing j %p\n", j);
#endif
uprv_free(j);
}
return res;
}
U_CAPI int32_t U_EXPORT2
uloc_acceptLanguage(char *result, int32_t resultAvailable,
UAcceptResult *outResult, const char **acceptList,
int32_t acceptListCount,
UEnumeration* availableLocales,
UErrorCode *status)
{
int32_t i,j;
int32_t len;
int32_t maxLen=0;
char tmp[ULOC_FULLNAME_CAPACITY+1];
const char *l;
char **fallbackList;
if(U_FAILURE(*status)) {
return -1;
}
fallbackList = uprv_malloc((size_t)(sizeof(fallbackList[0])*acceptListCount));
if(fallbackList==NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return -1;
}
for(i=0;i<acceptListCount;i++) {
#if defined(ULOC_DEBUG)
fprintf(stderr,"%02d: %s\n", i, acceptList[i]);
#endif
while((l=uenum_next(availableLocales, NULL, status))) {
#if defined(ULOC_DEBUG)
fprintf(stderr," %s\n", l);
#endif
len = uprv_strlen(l);
if(!uprv_strcmp(acceptList[i], l)) {
if(outResult) {
*outResult = ULOC_ACCEPT_VALID;
}
#if defined(ULOC_DEBUG)
fprintf(stderr, "MATCH! %s\n", l);
#endif
if(len>0) {
uprv_strncpy(result, l, uprv_min(len, resultAvailable));
}
for(j=0;j<i;j++) {
uprv_free(fallbackList[j]);
}
uprv_free(fallbackList);
return u_terminateChars(result, resultAvailable, len, status);
}
if(len>maxLen) {
maxLen = len;
}
}
uenum_reset(availableLocales, status);
if(uloc_getParent(acceptList[i], tmp, sizeof(tmp)/sizeof(tmp[0]), status)!=0) {
fallbackList[i] = uprv_strdup(tmp);
} else {
fallbackList[i]=0;
}
}
for(maxLen--;maxLen>0;maxLen--) {
for(i=0;i<acceptListCount;i++) {
if(fallbackList[i] && ((int32_t)uprv_strlen(fallbackList[i])==maxLen)) {
#if defined(ULOC_DEBUG)
fprintf(stderr,"Try: [%s]", fallbackList[i]);
#endif
while((l=uenum_next(availableLocales, NULL, status))) {
#if defined(ULOC_DEBUG)
fprintf(stderr," %s\n", l);
#endif
len = uprv_strlen(l);
if(!uprv_strcmp(fallbackList[i], l)) {
if(outResult) {
*outResult = ULOC_ACCEPT_FALLBACK;
}
#if defined(ULOC_DEBUG)
fprintf(stderr, "fallback MATCH! %s\n", l);
#endif
if(len>0) {
uprv_strncpy(result, l, uprv_min(len, resultAvailable));
}
for(i=0;i<acceptListCount;i++) {
uprv_free(fallbackList[i]);
}
uprv_free(fallbackList);
return u_terminateChars(result, resultAvailable, len, status);
}
}
uenum_reset(availableLocales, status);
if(uloc_getParent(fallbackList[i], tmp, sizeof(tmp)/sizeof(tmp[0]), status)!=0) {
uprv_free(fallbackList[i]);
fallbackList[i] = uprv_strdup(tmp);
} else {
uprv_free(fallbackList[i]);
fallbackList[i]=0;
}
}
}
if(outResult) {
*outResult = ULOC_ACCEPT_FAILED;
}
}
for(i=0;i<acceptListCount;i++) {
uprv_free(fallbackList[i]);
}
uprv_free(fallbackList);
return -1;
}