Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (C) 1997-2013, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File ULOC.CPP |
michael@0 | 8 | * |
michael@0 | 9 | * Modification History: |
michael@0 | 10 | * |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * 04/01/97 aliu Creation. |
michael@0 | 13 | * 08/21/98 stephen JDK 1.2 sync |
michael@0 | 14 | * 12/08/98 rtg New Locale implementation and C API |
michael@0 | 15 | * 03/15/99 damiba overhaul. |
michael@0 | 16 | * 04/06/99 stephen changed setDefault() to realloc and copy |
michael@0 | 17 | * 06/14/99 stephen Changed calls to ures_open for new params |
michael@0 | 18 | * 07/21/99 stephen Modified setDefault() to propagate to C++ |
michael@0 | 19 | * 05/14/04 alan 7 years later: refactored, cleaned up, fixed bugs, |
michael@0 | 20 | * brought canonicalization code into line with spec |
michael@0 | 21 | *****************************************************************************/ |
michael@0 | 22 | |
michael@0 | 23 | /* |
michael@0 | 24 | POSIX's locale format, from putil.c: [no spaces] |
michael@0 | 25 | |
michael@0 | 26 | ll [ _CC ] [ . MM ] [ @ VV] |
michael@0 | 27 | |
michael@0 | 28 | l = lang, C = ctry, M = charmap, V = variant |
michael@0 | 29 | */ |
michael@0 | 30 | |
michael@0 | 31 | #include "unicode/utypes.h" |
michael@0 | 32 | #include "unicode/ustring.h" |
michael@0 | 33 | #include "unicode/uloc.h" |
michael@0 | 34 | |
michael@0 | 35 | #include "putilimp.h" |
michael@0 | 36 | #include "ustr_imp.h" |
michael@0 | 37 | #include "ulocimp.h" |
michael@0 | 38 | #include "umutex.h" |
michael@0 | 39 | #include "cstring.h" |
michael@0 | 40 | #include "cmemory.h" |
michael@0 | 41 | #include "ucln_cmn.h" |
michael@0 | 42 | #include "locmap.h" |
michael@0 | 43 | #include "uarrsort.h" |
michael@0 | 44 | #include "uenumimp.h" |
michael@0 | 45 | #include "uassert.h" |
michael@0 | 46 | |
michael@0 | 47 | #include <stdio.h> /* for sprintf */ |
michael@0 | 48 | |
michael@0 | 49 | /* ### Declarations **************************************************/ |
michael@0 | 50 | |
michael@0 | 51 | /* Locale stuff from locid.cpp */ |
michael@0 | 52 | U_CFUNC void locale_set_default(const char *id); |
michael@0 | 53 | U_CFUNC const char *locale_get_default(void); |
michael@0 | 54 | U_CFUNC int32_t |
michael@0 | 55 | locale_getKeywords(const char *localeID, |
michael@0 | 56 | char prev, |
michael@0 | 57 | char *keywords, int32_t keywordCapacity, |
michael@0 | 58 | char *values, int32_t valuesCapacity, int32_t *valLen, |
michael@0 | 59 | UBool valuesToo, |
michael@0 | 60 | UErrorCode *status); |
michael@0 | 61 | |
michael@0 | 62 | /* ### Data tables **************************************************/ |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Table of language codes, both 2- and 3-letter, with preference |
michael@0 | 66 | * given to 2-letter codes where possible. Includes 3-letter codes |
michael@0 | 67 | * that lack a 2-letter equivalent. |
michael@0 | 68 | * |
michael@0 | 69 | * This list must be in sorted order. This list is returned directly |
michael@0 | 70 | * to the user by some API. |
michael@0 | 71 | * |
michael@0 | 72 | * This list must be kept in sync with LANGUAGES_3, with corresponding |
michael@0 | 73 | * entries matched. |
michael@0 | 74 | * |
michael@0 | 75 | * This table should be terminated with a NULL entry, followed by a |
michael@0 | 76 | * second list, and another NULL entry. The first list is visible to |
michael@0 | 77 | * user code when this array is returned by API. The second list |
michael@0 | 78 | * contains codes we support, but do not expose through user API. |
michael@0 | 79 | * |
michael@0 | 80 | * Notes |
michael@0 | 81 | * |
michael@0 | 82 | * Tables updated per http://lcweb.loc.gov/standards/iso639-2/ to |
michael@0 | 83 | * include the revisions up to 2001/7/27 *CWB* |
michael@0 | 84 | * |
michael@0 | 85 | * The 3 character codes are the terminology codes like RFC 3066. This |
michael@0 | 86 | * is compatible with prior ICU codes |
michael@0 | 87 | * |
michael@0 | 88 | * "in" "iw" "ji" "jw" & "sh" have been withdrawn but are still in the |
michael@0 | 89 | * table but now at the end of the table because 3 character codes are |
michael@0 | 90 | * duplicates. This avoids bad searches going from 3 to 2 character |
michael@0 | 91 | * codes. |
michael@0 | 92 | * |
michael@0 | 93 | * The range qaa-qtz is reserved for local use |
michael@0 | 94 | */ |
michael@0 | 95 | /* Generated using org.unicode.cldr.icu.GenerateISO639LanguageTables */ |
michael@0 | 96 | /* ISO639 table version is 20130531 */ |
michael@0 | 97 | static const char * const LANGUAGES[] = { |
michael@0 | 98 | "aa", "ab", "ace", "ach", "ada", "ady", "ae", "af", |
michael@0 | 99 | "afa", "afh", "agq", "ain", "ak", "akk", "ale", "alg", |
michael@0 | 100 | "alt", "am", "an", "ang", "anp", "apa", "ar", "arc", |
michael@0 | 101 | "arn", "arp", "art", "arw", "as", "asa", "ast", "ath", |
michael@0 | 102 | "aus", "av", "awa", "ay", "az", |
michael@0 | 103 | "ba", "bad", "bai", "bal", "ban", "bas", "bat", "bax", |
michael@0 | 104 | "bbj", "be", "bej", "bem", "ber", "bez", "bfd", "bg", |
michael@0 | 105 | "bh", "bho", "bi", "bik", "bin", "bkm", "bla", "bm", |
michael@0 | 106 | "bn", "bnt", "bo", "br", "bra", "brx", "bs", "bss", |
michael@0 | 107 | "btk", "bua", "bug", "bum", "byn", "byv", |
michael@0 | 108 | "ca", "cad", "cai", "car", "cau", "cay", "cch", "ce", |
michael@0 | 109 | "ceb", "cel", "cgg", "ch", "chb", "chg", "chk", "chm", |
michael@0 | 110 | "chn", "cho", "chp", "chr", "chy", "ckb", "cmc", "co", |
michael@0 | 111 | "cop", "cpe", "cpf", "cpp", "cr", "crh", "crp", "cs", |
michael@0 | 112 | "csb", "cu", "cus", "cv", "cy", |
michael@0 | 113 | "da", "dak", "dar", "dav", "day", "de", "del", "den", |
michael@0 | 114 | "dgr", "din", "dje", "doi", "dra", "dsb", "dua", "dum", |
michael@0 | 115 | "dv", "dyo", "dyu", "dz", "dzg", |
michael@0 | 116 | "ebu", "ee", "efi", "egy", "eka", "el", "elx", "en", |
michael@0 | 117 | "enm", "eo", "es", "et", "eu", "ewo", |
michael@0 | 118 | "fa", "fan", "fat", "ff", "fi", "fil", "fiu", "fj", |
michael@0 | 119 | "fo", "fon", "fr", "frm", "fro", "frr", "frs", "fur", |
michael@0 | 120 | "fy", |
michael@0 | 121 | "ga", "gaa", "gay", "gba", "gd", "gem", "gez", "gil", |
michael@0 | 122 | "gl", "gmh", "gn", "goh", "gon", "gor", "got", "grb", |
michael@0 | 123 | "grc", "gsw", "gu", "guz", "gv", "gwi", |
michael@0 | 124 | "ha", "hai", "haw", "he", "hi", "hil", "him", "hit", |
michael@0 | 125 | "hmn", "ho", "hr", "hsb", "ht", "hu", "hup", "hy", |
michael@0 | 126 | "hz", |
michael@0 | 127 | "ia", "iba", "ibb", "id", "ie", "ig", "ii", "ijo", |
michael@0 | 128 | "ik", "ilo", "inc", "ine", "inh", "io", "ira", "iro", |
michael@0 | 129 | "is", "it", "iu", |
michael@0 | 130 | "ja", "jbo", "jgo", "jmc", "jpr", "jrb", "jv", |
michael@0 | 131 | "ka", "kaa", "kab", "kac", "kaj", "kam", "kar", "kaw", |
michael@0 | 132 | "kbd", "kbl", "kcg", "kde", "kea", "kfo", "kg", "kha", |
michael@0 | 133 | "khi", "kho", "khq", "ki", "kj", "kk", "kkj", "kl", |
michael@0 | 134 | "kln", "km", "kmb", "kn", "ko", "kok", "kos", "kpe", |
michael@0 | 135 | "kr", "krc", "krl", "kro", "kru", "ks", "ksb", "ksf", |
michael@0 | 136 | "ksh", "ku", "kum", "kut", "kv", "kw", "ky", |
michael@0 | 137 | "la", "lad", "lag", "lah", "lam", "lb", "lez", "lg", |
michael@0 | 138 | "li", "lkt", "ln", "lo", "lol", "loz", "lt", "lu", |
michael@0 | 139 | "lua", "lui", "lun", "luo", "lus", "luy", "lv", |
michael@0 | 140 | "mad", "maf", "mag", "mai", "mak", "man", "map", "mas", |
michael@0 | 141 | "mde", "mdf", "mdr", "men", "mer", "mfe", "mg", "mga", |
michael@0 | 142 | "mgh", "mgo", "mh", "mi", "mic", "min", "mis", "mk", |
michael@0 | 143 | "mkh", "ml", "mn", "mnc", "mni", "mno", "mo", "moh", |
michael@0 | 144 | "mos", "mr", "ms", "mt", "mua", "mul", "mun", "mus", |
michael@0 | 145 | "mwl", "mwr", "my", "mye", "myn", "myv", |
michael@0 | 146 | "na", "nah", "nai", "nap", "naq", "nb", "nd", "nds", |
michael@0 | 147 | "ne", "new", "ng", "nia", "nic", "niu", "nl", "nmg", |
michael@0 | 148 | "nn", "nnh", "no", "nog", "non", "nqo", "nr", "nso", |
michael@0 | 149 | "nub", "nus", "nv", "nwc", "ny", "nym", "nyn", "nyo", |
michael@0 | 150 | "nzi", |
michael@0 | 151 | "oc", "oj", "om", "or", "os", "osa", "ota", "oto", |
michael@0 | 152 | "pa", "paa", "pag", "pal", "pam", "pap", "pau", "peo", |
michael@0 | 153 | "phi", "phn", "pi", "pl", "pon", "pra", "pro", "ps", |
michael@0 | 154 | "pt", |
michael@0 | 155 | "qu", |
michael@0 | 156 | "raj", "rap", "rar", "rm", "rn", "ro", "roa", "rof", |
michael@0 | 157 | "rom", "ru", "rup", "rw", "rwk", |
michael@0 | 158 | "sa", "sad", "sah", "sai", "sal", "sam", "saq", "sas", |
michael@0 | 159 | "sat", "sba", "sbp", "sc", "scn", "sco", "sd", "se", |
michael@0 | 160 | "see", "seh", "sel", "sem", "ses", "sg", "sga", "sgn", |
michael@0 | 161 | "shi", "shn", "shu", "si", "sid", "sio", "sit", |
michael@0 | 162 | "sk", "sl", "sla", "sm", "sma", "smi", "smj", "smn", |
michael@0 | 163 | "sms", "sn", "snk", "so", "sog", "son", "sq", "sr", |
michael@0 | 164 | "srn", "srr", "ss", "ssa", "ssy", "st", "su", "suk", |
michael@0 | 165 | "sus", "sux", "sv", "sw", "swb", "swc", "syc", "syr", |
michael@0 | 166 | "ta", "tai", "te", "tem", "teo", "ter", "tet", "tg", |
michael@0 | 167 | "th", "ti", "tig", "tiv", "tk", "tkl", "tl", "tlh", |
michael@0 | 168 | "tli", "tmh", "tn", "to", "tog", "tpi", "tr", "trv", |
michael@0 | 169 | "ts", "tsi", "tt", "tum", "tup", "tut", "tvl", "tw", |
michael@0 | 170 | "twq", "ty", "tyv", "tzm", |
michael@0 | 171 | "udm", "ug", "uga", "uk", "umb", "und", "ur", "uz", |
michael@0 | 172 | "vai", "ve", "vi", "vo", "vot", "vun", |
michael@0 | 173 | "wa", "wae", "wak", "wal", "war", "was", "wen", "wo", |
michael@0 | 174 | "xal", "xh", "xog", |
michael@0 | 175 | "yao", "yap", "yav", "ybb", "yi", "yo", "ypk", "yue", |
michael@0 | 176 | "za", "zap", "zbl", "zen", "zgh", "zh", "znd", "zu", |
michael@0 | 177 | "zun", "zxx", "zza", |
michael@0 | 178 | NULL, |
michael@0 | 179 | "in", "iw", "ji", "jw", "sh", /* obsolete language codes */ |
michael@0 | 180 | NULL |
michael@0 | 181 | }; |
michael@0 | 182 | |
michael@0 | 183 | static const char* const DEPRECATED_LANGUAGES[]={ |
michael@0 | 184 | "in", "iw", "ji", "jw", NULL, NULL |
michael@0 | 185 | }; |
michael@0 | 186 | static const char* const REPLACEMENT_LANGUAGES[]={ |
michael@0 | 187 | "id", "he", "yi", "jv", NULL, NULL |
michael@0 | 188 | }; |
michael@0 | 189 | |
michael@0 | 190 | /** |
michael@0 | 191 | * Table of 3-letter language codes. |
michael@0 | 192 | * |
michael@0 | 193 | * This is a lookup table used to convert 3-letter language codes to |
michael@0 | 194 | * their 2-letter equivalent, where possible. It must be kept in sync |
michael@0 | 195 | * with LANGUAGES. For all valid i, LANGUAGES[i] must refer to the |
michael@0 | 196 | * same language as LANGUAGES_3[i]. The commented-out lines are |
michael@0 | 197 | * copied from LANGUAGES to make eyeballing this baby easier. |
michael@0 | 198 | * |
michael@0 | 199 | * Where a 3-letter language code has no 2-letter equivalent, the |
michael@0 | 200 | * 3-letter code occupies both LANGUAGES[i] and LANGUAGES_3[i]. |
michael@0 | 201 | * |
michael@0 | 202 | * This table should be terminated with a NULL entry, followed by a |
michael@0 | 203 | * second list, and another NULL entry. The two lists correspond to |
michael@0 | 204 | * the two lists in LANGUAGES. |
michael@0 | 205 | */ |
michael@0 | 206 | /* Generated using org.unicode.cldr.icu.GenerateISO639LanguageTables */ |
michael@0 | 207 | /* ISO639 table version is 20130531 */ |
michael@0 | 208 | static const char * const LANGUAGES_3[] = { |
michael@0 | 209 | "aar", "abk", "ace", "ach", "ada", "ady", "ave", "afr", |
michael@0 | 210 | "afa", "afh", "agq", "ain", "aka", "akk", "ale", "alg", |
michael@0 | 211 | "alt", "amh", "arg", "ang", "anp", "apa", "ara", "arc", |
michael@0 | 212 | "arn", "arp", "art", "arw", "asm", "asa", "ast", "ath", |
michael@0 | 213 | "aus", "ava", "awa", "aym", "aze", |
michael@0 | 214 | "bak", "bad", "bai", "bal", "ban", "bas", "bat", "bax", |
michael@0 | 215 | "bbj", "bel", "bej", "bem", "ber", "bez", "bfd", "bul", |
michael@0 | 216 | "bih", "bho", "bis", "bik", "bin", "bkm", "bla", "bam", |
michael@0 | 217 | "ben", "bnt", "bod", "bre", "bra", "brx", "bos", "bss", |
michael@0 | 218 | "btk", "bua", "bug", "bum", "byn", "byv", |
michael@0 | 219 | "cat", "cad", "cai", "car", "cau", "cay", "cch", "che", |
michael@0 | 220 | "ceb", "cel", "cgg", "cha", "chb", "chg", "chk", "chm", |
michael@0 | 221 | "chn", "cho", "chp", "chr", "chy", "ckb", "cmc", "cos", |
michael@0 | 222 | "cop", "cpe", "cpf", "cpp", "cre", "crh", "crp", "ces", |
michael@0 | 223 | "csb", "chu", "cus", "chv", "cym", |
michael@0 | 224 | "dan", "dak", "dar", "dav", "day", "deu", "del", "den", |
michael@0 | 225 | "dgr", "din", "dje", "doi", "dra", "dsb", "dua", "dum", |
michael@0 | 226 | "div", "dyo", "dyu", "dzo", "dzg", |
michael@0 | 227 | "ebu", "ewe", "efi", "egy", "eka", "ell", "elx", "eng", |
michael@0 | 228 | "enm", "epo", "spa", "est", "eus", "ewo", |
michael@0 | 229 | "fas", "fan", "fat", "ful", "fin", "fil", "fiu", "fij", |
michael@0 | 230 | "fao", "fon", "fra", "frm", "fro", "frr", "frs", "fur", |
michael@0 | 231 | "fry", |
michael@0 | 232 | "gle", "gaa", "gay", "gba", "gla", "gem", "gez", "gil", |
michael@0 | 233 | "glg", "gmh", "grn", "goh", "gon", "gor", "got", "grb", |
michael@0 | 234 | "grc", "gsw", "guj", "guz", "glv", "gwi", |
michael@0 | 235 | "hau", "hai", "haw", "heb", "hin", "hil", "him", "hit", |
michael@0 | 236 | "hmn", "hmo", "hrv", "hsb", "hat", "hun", "hup", "hye", |
michael@0 | 237 | "her", |
michael@0 | 238 | "ina", "iba", "ibb", "ind", "ile", "ibo", "iii", "ijo", |
michael@0 | 239 | "ipk", "ilo", "inc", "ine", "inh", "ido", "ira", "iro", |
michael@0 | 240 | "isl", "ita", "iku", |
michael@0 | 241 | "jpn", "jbo", "jgo", "jmc", "jpr", "jrb", "jav", |
michael@0 | 242 | "kat", "kaa", "kab", "kac", "kaj", "kam", "kar", "kaw", |
michael@0 | 243 | "kbd", "kbl", "kcg", "kde", "kea", "kfo", "kon", "kha", |
michael@0 | 244 | "khi", "kho", "khq", "kik", "kua", "kaz", "kkj", "kal", |
michael@0 | 245 | "kln", "khm", "kmb", "kan", "kor", "kok", "kos", "kpe", |
michael@0 | 246 | "kau", "krc", "krl", "kro", "kru", "kas", "ksb", "ksf", |
michael@0 | 247 | "ksh", "kur", "kum", "kut", "kom", "cor", "kir", |
michael@0 | 248 | "lat", "lad", "lag", "lah", "lam", "ltz", "lez", "lug", |
michael@0 | 249 | "lim", "lkt", "lin", "lao", "lol", "loz", "lit", "lub", |
michael@0 | 250 | "lua", "lui", "lun", "luo", "lus", "luy", "lav", |
michael@0 | 251 | "mad", "maf", "mag", "mai", "mak", "man", "map", "mas", |
michael@0 | 252 | "mde", "mdf", "mdr", "men", "mer", "mfe", "mlg", "mga", |
michael@0 | 253 | "mgh", "mgo", "mah", "mri", "mic", "min", "mis", "mkd", |
michael@0 | 254 | "mkh", "mal", "mon", "mnc", "mni", "mno", "mol", "moh", |
michael@0 | 255 | "mos", "mar", "msa", "mlt", "mua", "mul", "mun", "mus", |
michael@0 | 256 | "mwl", "mwr", "mya", "mye", "myn", "myv", |
michael@0 | 257 | "nau", "nah", "nai", "nap", "naq", "nob", "nde", "nds", |
michael@0 | 258 | "nep", "new", "ndo", "nia", "nic", "niu", "nld", "nmg", |
michael@0 | 259 | "nno", "nnh", "nor", "nog", "non", "nqo", "nbl", "nso", |
michael@0 | 260 | "nub", "nus", "nav", "nwc", "nya", "nym", "nyn", "nyo", |
michael@0 | 261 | "nzi", |
michael@0 | 262 | "oci", "oji", "orm", "ori", "oss", "osa", "ota", "oto", |
michael@0 | 263 | "pan", "paa", "pag", "pal", "pam", "pap", "pau", "peo", |
michael@0 | 264 | "phi", "phn", "pli", "pol", "pon", "pra", "pro", "pus", |
michael@0 | 265 | "por", |
michael@0 | 266 | "que", |
michael@0 | 267 | "raj", "rap", "rar", "roh", "run", "ron", "roa", "rof", |
michael@0 | 268 | "rom", "rus", "rup", "kin", "rwk", |
michael@0 | 269 | "san", "sad", "sah", "sai", "sal", "sam", "saq", "sas", |
michael@0 | 270 | "sat", "sba", "sbp", "srd", "scn", "sco", "snd", "sme", |
michael@0 | 271 | "see", "seh", "sel", "sem", "ses", "sag", "sga", "sgn", |
michael@0 | 272 | "shi", "shn", "shu", "sin", "sid", "sio", "sit", |
michael@0 | 273 | "slk", "slv", "sla", "smo", "sma", "smi", "smj", "smn", |
michael@0 | 274 | "sms", "sna", "snk", "som", "sog", "son", "sqi", "srp", |
michael@0 | 275 | "srn", "srr", "ssw", "ssa", "ssy", "sot", "sun", "suk", |
michael@0 | 276 | "sus", "sux", "swe", "swa", "swb", "swc", "syc", "syr", |
michael@0 | 277 | "tam", "tai", "tel", "tem", "teo", "ter", "tet", "tgk", |
michael@0 | 278 | "tha", "tir", "tig", "tiv", "tuk", "tkl", "tgl", "tlh", |
michael@0 | 279 | "tli", "tmh", "tsn", "ton", "tog", "tpi", "tur", "trv", |
michael@0 | 280 | "tso", "tsi", "tat", "tum", "tup", "tut", "tvl", "twi", |
michael@0 | 281 | "twq", "tah", "tyv", "tzm", |
michael@0 | 282 | "udm", "uig", "uga", "ukr", "umb", "und", "urd", "uzb", |
michael@0 | 283 | "vai", "ven", "vie", "vol", "vot", "vun", |
michael@0 | 284 | "wln", "wae", "wak", "wal", "war", "was", "wen", "wol", |
michael@0 | 285 | "xal", "xho", "xog", |
michael@0 | 286 | "yao", "yap", "yav", "ybb", "yid", "yor", "ypk", "yue", |
michael@0 | 287 | "zha", "zap", "zbl", "zen", "zgh", "zho", "znd", "zul", |
michael@0 | 288 | "zun", "zxx", "zza", |
michael@0 | 289 | NULL, |
michael@0 | 290 | /* "in", "iw", "ji", "jw", "sh", */ |
michael@0 | 291 | "ind", "heb", "yid", "jaw", "srp", |
michael@0 | 292 | NULL |
michael@0 | 293 | }; |
michael@0 | 294 | |
michael@0 | 295 | /** |
michael@0 | 296 | * Table of 2-letter country codes. |
michael@0 | 297 | * |
michael@0 | 298 | * This list must be in sorted order. This list is returned directly |
michael@0 | 299 | * to the user by some API. |
michael@0 | 300 | * |
michael@0 | 301 | * This list must be kept in sync with COUNTRIES_3, with corresponding |
michael@0 | 302 | * entries matched. |
michael@0 | 303 | * |
michael@0 | 304 | * This table should be terminated with a NULL entry, followed by a |
michael@0 | 305 | * second list, and another NULL entry. The first list is visible to |
michael@0 | 306 | * user code when this array is returned by API. The second list |
michael@0 | 307 | * contains codes we support, but do not expose through user API. |
michael@0 | 308 | * |
michael@0 | 309 | * Notes: |
michael@0 | 310 | * |
michael@0 | 311 | * ZR(ZAR) is now CD(COD) and FX(FXX) is PS(PSE) as per |
michael@0 | 312 | * http://www.evertype.com/standards/iso3166/iso3166-1-en.html added |
michael@0 | 313 | * new codes keeping the old ones for compatibility updated to include |
michael@0 | 314 | * 1999/12/03 revisions *CWB* |
michael@0 | 315 | * |
michael@0 | 316 | * RO(ROM) is now RO(ROU) according to |
michael@0 | 317 | * http://www.iso.org/iso/en/prods-services/iso3166ma/03updates-on-iso-3166/nlv3e-rou.html |
michael@0 | 318 | */ |
michael@0 | 319 | static const char * const COUNTRIES[] = { |
michael@0 | 320 | "AD", "AE", "AF", "AG", "AI", "AL", "AM", |
michael@0 | 321 | "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", |
michael@0 | 322 | "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", |
michael@0 | 323 | "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", |
michael@0 | 324 | "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", |
michael@0 | 325 | "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", |
michael@0 | 326 | "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", |
michael@0 | 327 | "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", |
michael@0 | 328 | "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", |
michael@0 | 329 | "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", |
michael@0 | 330 | "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", |
michael@0 | 331 | "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", |
michael@0 | 332 | "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", |
michael@0 | 333 | "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", |
michael@0 | 334 | "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", |
michael@0 | 335 | "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", |
michael@0 | 336 | "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", |
michael@0 | 337 | "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", |
michael@0 | 338 | "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", |
michael@0 | 339 | "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", |
michael@0 | 340 | "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", |
michael@0 | 341 | "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", |
michael@0 | 342 | "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", |
michael@0 | 343 | "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", |
michael@0 | 344 | "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", |
michael@0 | 345 | "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", |
michael@0 | 346 | "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", |
michael@0 | 347 | "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", |
michael@0 | 348 | "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", |
michael@0 | 349 | "WS", "YE", "YT", "ZA", "ZM", "ZW", |
michael@0 | 350 | NULL, |
michael@0 | 351 | "AN", "BU", "CS", "FX", "RO", "SU", "TP", "YD", "YU", "ZR", /* obsolete country codes */ |
michael@0 | 352 | NULL |
michael@0 | 353 | }; |
michael@0 | 354 | |
michael@0 | 355 | static const char* const DEPRECATED_COUNTRIES[] = { |
michael@0 | 356 | "AN", "BU", "CS", "DD", "DY", "FX", "HV", "NH", "RH", "SU", "TP", "UK", "VD", "YD", "YU", "ZR", NULL, NULL /* deprecated country list */ |
michael@0 | 357 | }; |
michael@0 | 358 | static const char* const REPLACEMENT_COUNTRIES[] = { |
michael@0 | 359 | /* "AN", "BU", "CS", "DD", "DY", "FX", "HV", "NH", "RH", "SU", "TP", "UK", "VD", "YD", "YU", "ZR" */ |
michael@0 | 360 | "CW", "MM", "RS", "DE", "BJ", "FR", "BF", "VU", "ZW", "RU", "TL", "GB", "VN", "YE", "RS", "CD", NULL, NULL /* replacement country codes */ |
michael@0 | 361 | }; |
michael@0 | 362 | |
michael@0 | 363 | /** |
michael@0 | 364 | * Table of 3-letter country codes. |
michael@0 | 365 | * |
michael@0 | 366 | * This is a lookup table used to convert 3-letter country codes to |
michael@0 | 367 | * their 2-letter equivalent. It must be kept in sync with COUNTRIES. |
michael@0 | 368 | * For all valid i, COUNTRIES[i] must refer to the same country as |
michael@0 | 369 | * COUNTRIES_3[i]. The commented-out lines are copied from COUNTRIES |
michael@0 | 370 | * to make eyeballing this baby easier. |
michael@0 | 371 | * |
michael@0 | 372 | * This table should be terminated with a NULL entry, followed by a |
michael@0 | 373 | * second list, and another NULL entry. The two lists correspond to |
michael@0 | 374 | * the two lists in COUNTRIES. |
michael@0 | 375 | */ |
michael@0 | 376 | static const char * const COUNTRIES_3[] = { |
michael@0 | 377 | /* "AD", "AE", "AF", "AG", "AI", "AL", "AM", */ |
michael@0 | 378 | "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", |
michael@0 | 379 | /* "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", */ |
michael@0 | 380 | "AGO", "ATA", "ARG", "ASM", "AUT", "AUS", "ABW", "ALA", "AZE", |
michael@0 | 381 | /* "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", */ |
michael@0 | 382 | "BIH", "BRB", "BGD", "BEL", "BFA", "BGR", "BHR", "BDI", |
michael@0 | 383 | /* "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", */ |
michael@0 | 384 | "BEN", "BLM", "BMU", "BRN", "BOL", "BES", "BRA", "BHS", "BTN", "BVT", |
michael@0 | 385 | /* "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", */ |
michael@0 | 386 | "BWA", "BLR", "BLZ", "CAN", "CCK", "COD", "CAF", "COG", |
michael@0 | 387 | /* "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", */ |
michael@0 | 388 | "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI", |
michael@0 | 389 | /* "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", */ |
michael@0 | 390 | "CUB", "CPV", "CUW", "CXR", "CYP", "CZE", "DEU", "DJI", "DNK", |
michael@0 | 391 | /* "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", */ |
michael@0 | 392 | "DMA", "DOM", "DZA", "ECU", "EST", "EGY", "ESH", "ERI", |
michael@0 | 393 | /* "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", */ |
michael@0 | 394 | "ESP", "ETH", "FIN", "FJI", "FLK", "FSM", "FRO", "FRA", |
michael@0 | 395 | /* "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", */ |
michael@0 | 396 | "GAB", "GBR", "GRD", "GEO", "GUF", "GGY", "GHA", "GIB", "GRL", |
michael@0 | 397 | /* "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", */ |
michael@0 | 398 | "GMB", "GIN", "GLP", "GNQ", "GRC", "SGS", "GTM", "GUM", |
michael@0 | 399 | /* "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", */ |
michael@0 | 400 | "GNB", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN", |
michael@0 | 401 | /* "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS" */ |
michael@0 | 402 | "IDN", "IRL", "ISR", "IMN", "IND", "IOT", "IRQ", "IRN", "ISL", |
michael@0 | 403 | /* "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", */ |
michael@0 | 404 | "ITA", "JEY", "JAM", "JOR", "JPN", "KEN", "KGZ", "KHM", "KIR", |
michael@0 | 405 | /* "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", */ |
michael@0 | 406 | "COM", "KNA", "PRK", "KOR", "KWT", "CYM", "KAZ", "LAO", |
michael@0 | 407 | /* "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", */ |
michael@0 | 408 | "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", "LUX", |
michael@0 | 409 | /* "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", */ |
michael@0 | 410 | "LVA", "LBY", "MAR", "MCO", "MDA", "MNE", "MAF", "MDG", "MHL", "MKD", |
michael@0 | 411 | /* "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", */ |
michael@0 | 412 | "MLI", "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", |
michael@0 | 413 | /* "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", */ |
michael@0 | 414 | "MLT", "MUS", "MDV", "MWI", "MEX", "MYS", "MOZ", "NAM", |
michael@0 | 415 | /* "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", */ |
michael@0 | 416 | "NCL", "NER", "NFK", "NGA", "NIC", "NLD", "NOR", "NPL", |
michael@0 | 417 | /* "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", */ |
michael@0 | 418 | "NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", "PNG", |
michael@0 | 419 | /* "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", */ |
michael@0 | 420 | "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", |
michael@0 | 421 | /* "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", */ |
michael@0 | 422 | "PLW", "PRY", "QAT", "REU", "ROU", "SRB", "RUS", "RWA", "SAU", |
michael@0 | 423 | /* "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", */ |
michael@0 | 424 | "SLB", "SYC", "SDN", "SWE", "SGP", "SHN", "SVN", "SJM", |
michael@0 | 425 | /* "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", */ |
michael@0 | 426 | "SVK", "SLE", "SMR", "SEN", "SOM", "SUR", "SSD", "STP", "SLV", |
michael@0 | 427 | /* "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", */ |
michael@0 | 428 | "SXM", "SYR", "SWZ", "TCA", "TCD", "ATF", "TGO", "THA", "TJK", |
michael@0 | 429 | /* "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", */ |
michael@0 | 430 | "TKL", "TLS", "TKM", "TUN", "TON", "TUR", "TTO", "TUV", |
michael@0 | 431 | /* "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", */ |
michael@0 | 432 | "TWN", "TZA", "UKR", "UGA", "UMI", "USA", "URY", "UZB", |
michael@0 | 433 | /* "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", */ |
michael@0 | 434 | "VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF", |
michael@0 | 435 | /* "WS", "YE", "YT", "ZA", "ZM", "ZW", */ |
michael@0 | 436 | "WSM", "YEM", "MYT", "ZAF", "ZMB", "ZWE", |
michael@0 | 437 | NULL, |
michael@0 | 438 | /* "AN", "BU", "CS", "FX", "RO", "SU", "TP", "YD", "YU", "ZR" */ |
michael@0 | 439 | "ANT", "BUR", "SCG", "FXX", "ROM", "SUN", "TMP", "YMD", "YUG", "ZAR", |
michael@0 | 440 | NULL |
michael@0 | 441 | }; |
michael@0 | 442 | |
michael@0 | 443 | typedef struct CanonicalizationMap { |
michael@0 | 444 | const char *id; /* input ID */ |
michael@0 | 445 | const char *canonicalID; /* canonicalized output ID */ |
michael@0 | 446 | const char *keyword; /* keyword, or NULL if none */ |
michael@0 | 447 | const char *value; /* keyword value, or NULL if kw==NULL */ |
michael@0 | 448 | } CanonicalizationMap; |
michael@0 | 449 | |
michael@0 | 450 | /** |
michael@0 | 451 | * A map to canonicalize locale IDs. This handles a variety of |
michael@0 | 452 | * different semantic kinds of transformations. |
michael@0 | 453 | */ |
michael@0 | 454 | static const CanonicalizationMap CANONICALIZE_MAP[] = { |
michael@0 | 455 | { "", "en_US_POSIX", NULL, NULL }, /* .NET name */ |
michael@0 | 456 | { "c", "en_US_POSIX", NULL, NULL }, /* POSIX name */ |
michael@0 | 457 | { "posix", "en_US_POSIX", NULL, NULL }, /* POSIX name (alias of C) */ |
michael@0 | 458 | { "art_LOJBAN", "jbo", NULL, NULL }, /* registered name */ |
michael@0 | 459 | { "az_AZ_CYRL", "az_Cyrl_AZ", NULL, NULL }, /* .NET name */ |
michael@0 | 460 | { "az_AZ_LATN", "az_Latn_AZ", NULL, NULL }, /* .NET name */ |
michael@0 | 461 | { "ca_ES_PREEURO", "ca_ES", "currency", "ESP" }, |
michael@0 | 462 | { "de__PHONEBOOK", "de", "collation", "phonebook" }, /* Old ICU name */ |
michael@0 | 463 | { "de_AT_PREEURO", "de_AT", "currency", "ATS" }, |
michael@0 | 464 | { "de_DE_PREEURO", "de_DE", "currency", "DEM" }, |
michael@0 | 465 | { "de_LU_PREEURO", "de_LU", "currency", "LUF" }, |
michael@0 | 466 | { "el_GR_PREEURO", "el_GR", "currency", "GRD" }, |
michael@0 | 467 | { "en_BE_PREEURO", "en_BE", "currency", "BEF" }, |
michael@0 | 468 | { "en_IE_PREEURO", "en_IE", "currency", "IEP" }, |
michael@0 | 469 | { "es__TRADITIONAL", "es", "collation", "traditional" }, /* Old ICU name */ |
michael@0 | 470 | { "es_ES_PREEURO", "es_ES", "currency", "ESP" }, |
michael@0 | 471 | { "eu_ES_PREEURO", "eu_ES", "currency", "ESP" }, |
michael@0 | 472 | { "fi_FI_PREEURO", "fi_FI", "currency", "FIM" }, |
michael@0 | 473 | { "fr_BE_PREEURO", "fr_BE", "currency", "BEF" }, |
michael@0 | 474 | { "fr_FR_PREEURO", "fr_FR", "currency", "FRF" }, |
michael@0 | 475 | { "fr_LU_PREEURO", "fr_LU", "currency", "LUF" }, |
michael@0 | 476 | { "ga_IE_PREEURO", "ga_IE", "currency", "IEP" }, |
michael@0 | 477 | { "gl_ES_PREEURO", "gl_ES", "currency", "ESP" }, |
michael@0 | 478 | { "hi__DIRECT", "hi", "collation", "direct" }, /* Old ICU name */ |
michael@0 | 479 | { "it_IT_PREEURO", "it_IT", "currency", "ITL" }, |
michael@0 | 480 | { "ja_JP_TRADITIONAL", "ja_JP", "calendar", "japanese" }, /* Old ICU name */ |
michael@0 | 481 | { "nb_NO_NY", "nn_NO", NULL, NULL }, /* "markus said this was ok" :-) */ |
michael@0 | 482 | { "nl_BE_PREEURO", "nl_BE", "currency", "BEF" }, |
michael@0 | 483 | { "nl_NL_PREEURO", "nl_NL", "currency", "NLG" }, |
michael@0 | 484 | { "pt_PT_PREEURO", "pt_PT", "currency", "PTE" }, |
michael@0 | 485 | { "sr_SP_CYRL", "sr_Cyrl_RS", NULL, NULL }, /* .NET name */ |
michael@0 | 486 | { "sr_SP_LATN", "sr_Latn_RS", NULL, NULL }, /* .NET name */ |
michael@0 | 487 | { "sr_YU_CYRILLIC", "sr_Cyrl_RS", NULL, NULL }, /* Linux name */ |
michael@0 | 488 | { "th_TH_TRADITIONAL", "th_TH", "calendar", "buddhist" }, /* Old ICU name */ |
michael@0 | 489 | { "uz_UZ_CYRILLIC", "uz_Cyrl_UZ", NULL, NULL }, /* Linux name */ |
michael@0 | 490 | { "uz_UZ_CYRL", "uz_Cyrl_UZ", NULL, NULL }, /* .NET name */ |
michael@0 | 491 | { "uz_UZ_LATN", "uz_Latn_UZ", NULL, NULL }, /* .NET name */ |
michael@0 | 492 | { "zh_CHS", "zh_Hans", NULL, NULL }, /* .NET name */ |
michael@0 | 493 | { "zh_CHT", "zh_Hant", NULL, NULL }, /* .NET name */ |
michael@0 | 494 | { "zh_GAN", "gan", NULL, NULL }, /* registered name */ |
michael@0 | 495 | { "zh_GUOYU", "zh", NULL, NULL }, /* registered name */ |
michael@0 | 496 | { "zh_HAKKA", "hak", NULL, NULL }, /* registered name */ |
michael@0 | 497 | { "zh_MIN_NAN", "nan", NULL, NULL }, /* registered name */ |
michael@0 | 498 | { "zh_WUU", "wuu", NULL, NULL }, /* registered name */ |
michael@0 | 499 | { "zh_XIANG", "hsn", NULL, NULL }, /* registered name */ |
michael@0 | 500 | { "zh_YUE", "yue", NULL, NULL }, /* registered name */ |
michael@0 | 501 | }; |
michael@0 | 502 | |
michael@0 | 503 | typedef struct VariantMap { |
michael@0 | 504 | const char *variant; /* input ID */ |
michael@0 | 505 | const char *keyword; /* keyword, or NULL if none */ |
michael@0 | 506 | const char *value; /* keyword value, or NULL if kw==NULL */ |
michael@0 | 507 | } VariantMap; |
michael@0 | 508 | |
michael@0 | 509 | static const VariantMap VARIANT_MAP[] = { |
michael@0 | 510 | { "EURO", "currency", "EUR" }, |
michael@0 | 511 | { "PINYIN", "collation", "pinyin" }, /* Solaris variant */ |
michael@0 | 512 | { "STROKE", "collation", "stroke" } /* Solaris variant */ |
michael@0 | 513 | }; |
michael@0 | 514 | |
michael@0 | 515 | /* ### BCP47 Conversion *******************************************/ |
michael@0 | 516 | /* Test if the locale id has BCP47 u extension and does not have '@' */ |
michael@0 | 517 | #define _hasBCP47Extension(id) (id && uprv_strstr(id, "@") == NULL && getShortestSubtagLength(localeID) == 1) |
michael@0 | 518 | /* Converts the BCP47 id to Unicode id. Does nothing to id if conversion fails */ |
michael@0 | 519 | #define _ConvertBCP47(finalID, id, buffer, length,err) \ |
michael@0 | 520 | if (uloc_forLanguageTag(id, buffer, length, NULL, err) <= 0 || U_FAILURE(*err)) { \ |
michael@0 | 521 | finalID=id; \ |
michael@0 | 522 | } else { \ |
michael@0 | 523 | finalID=buffer; \ |
michael@0 | 524 | } |
michael@0 | 525 | /* Gets the size of the shortest subtag in the given localeID. */ |
michael@0 | 526 | static int32_t getShortestSubtagLength(const char *localeID) { |
michael@0 | 527 | int32_t localeIDLength = uprv_strlen(localeID); |
michael@0 | 528 | int32_t length = localeIDLength; |
michael@0 | 529 | int32_t tmpLength = 0; |
michael@0 | 530 | int32_t i; |
michael@0 | 531 | UBool reset = TRUE; |
michael@0 | 532 | |
michael@0 | 533 | for (i = 0; i < localeIDLength; i++) { |
michael@0 | 534 | if (localeID[i] != '_' && localeID[i] != '-') { |
michael@0 | 535 | if (reset) { |
michael@0 | 536 | tmpLength = 0; |
michael@0 | 537 | reset = FALSE; |
michael@0 | 538 | } |
michael@0 | 539 | tmpLength++; |
michael@0 | 540 | } else { |
michael@0 | 541 | if (tmpLength != 0 && tmpLength < length) { |
michael@0 | 542 | length = tmpLength; |
michael@0 | 543 | } |
michael@0 | 544 | reset = TRUE; |
michael@0 | 545 | } |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | return length; |
michael@0 | 549 | } |
michael@0 | 550 | |
michael@0 | 551 | /* ### Keywords **************************************************/ |
michael@0 | 552 | |
michael@0 | 553 | #define ULOC_KEYWORD_BUFFER_LEN 25 |
michael@0 | 554 | #define ULOC_MAX_NO_KEYWORDS 25 |
michael@0 | 555 | |
michael@0 | 556 | U_CAPI const char * U_EXPORT2 |
michael@0 | 557 | locale_getKeywordsStart(const char *localeID) { |
michael@0 | 558 | const char *result = NULL; |
michael@0 | 559 | if((result = uprv_strchr(localeID, '@')) != NULL) { |
michael@0 | 560 | return result; |
michael@0 | 561 | } |
michael@0 | 562 | #if (U_CHARSET_FAMILY == U_EBCDIC_FAMILY) |
michael@0 | 563 | else { |
michael@0 | 564 | /* We do this because the @ sign is variant, and the @ sign used on one |
michael@0 | 565 | EBCDIC machine won't be compiled the same way on other EBCDIC based |
michael@0 | 566 | machines. */ |
michael@0 | 567 | static const uint8_t ebcdicSigns[] = { 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 }; |
michael@0 | 568 | const uint8_t *charToFind = ebcdicSigns; |
michael@0 | 569 | while(*charToFind) { |
michael@0 | 570 | if((result = uprv_strchr(localeID, *charToFind)) != NULL) { |
michael@0 | 571 | return result; |
michael@0 | 572 | } |
michael@0 | 573 | charToFind++; |
michael@0 | 574 | } |
michael@0 | 575 | } |
michael@0 | 576 | #endif |
michael@0 | 577 | return NULL; |
michael@0 | 578 | } |
michael@0 | 579 | |
michael@0 | 580 | /** |
michael@0 | 581 | * @param buf buffer of size [ULOC_KEYWORD_BUFFER_LEN] |
michael@0 | 582 | * @param keywordName incoming name to be canonicalized |
michael@0 | 583 | * @param status return status (keyword too long) |
michael@0 | 584 | * @return length of the keyword name |
michael@0 | 585 | */ |
michael@0 | 586 | static int32_t locale_canonKeywordName(char *buf, const char *keywordName, UErrorCode *status) |
michael@0 | 587 | { |
michael@0 | 588 | int32_t i; |
michael@0 | 589 | int32_t keywordNameLen = (int32_t)uprv_strlen(keywordName); |
michael@0 | 590 | |
michael@0 | 591 | if(keywordNameLen >= ULOC_KEYWORD_BUFFER_LEN) { |
michael@0 | 592 | /* keyword name too long for internal buffer */ |
michael@0 | 593 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 594 | return 0; |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | /* normalize the keyword name */ |
michael@0 | 598 | for(i = 0; i < keywordNameLen; i++) { |
michael@0 | 599 | buf[i] = uprv_tolower(keywordName[i]); |
michael@0 | 600 | } |
michael@0 | 601 | buf[i] = 0; |
michael@0 | 602 | |
michael@0 | 603 | return keywordNameLen; |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | typedef struct { |
michael@0 | 607 | char keyword[ULOC_KEYWORD_BUFFER_LEN]; |
michael@0 | 608 | int32_t keywordLen; |
michael@0 | 609 | const char *valueStart; |
michael@0 | 610 | int32_t valueLen; |
michael@0 | 611 | } KeywordStruct; |
michael@0 | 612 | |
michael@0 | 613 | static int32_t U_CALLCONV |
michael@0 | 614 | compareKeywordStructs(const void * /*context*/, const void *left, const void *right) { |
michael@0 | 615 | const char* leftString = ((const KeywordStruct *)left)->keyword; |
michael@0 | 616 | const char* rightString = ((const KeywordStruct *)right)->keyword; |
michael@0 | 617 | return uprv_strcmp(leftString, rightString); |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | /** |
michael@0 | 621 | * Both addKeyword and addValue must already be in canonical form. |
michael@0 | 622 | * Either both addKeyword and addValue are NULL, or neither is NULL. |
michael@0 | 623 | * If they are not NULL they must be zero terminated. |
michael@0 | 624 | * If addKeyword is not NULL is must have length small enough to fit in KeywordStruct.keyword. |
michael@0 | 625 | */ |
michael@0 | 626 | static int32_t |
michael@0 | 627 | _getKeywords(const char *localeID, |
michael@0 | 628 | char prev, |
michael@0 | 629 | char *keywords, int32_t keywordCapacity, |
michael@0 | 630 | char *values, int32_t valuesCapacity, int32_t *valLen, |
michael@0 | 631 | UBool valuesToo, |
michael@0 | 632 | const char* addKeyword, |
michael@0 | 633 | const char* addValue, |
michael@0 | 634 | UErrorCode *status) |
michael@0 | 635 | { |
michael@0 | 636 | KeywordStruct keywordList[ULOC_MAX_NO_KEYWORDS]; |
michael@0 | 637 | |
michael@0 | 638 | int32_t maxKeywords = ULOC_MAX_NO_KEYWORDS; |
michael@0 | 639 | int32_t numKeywords = 0; |
michael@0 | 640 | const char* pos = localeID; |
michael@0 | 641 | const char* equalSign = NULL; |
michael@0 | 642 | const char* semicolon = NULL; |
michael@0 | 643 | int32_t i = 0, j, n; |
michael@0 | 644 | int32_t keywordsLen = 0; |
michael@0 | 645 | int32_t valuesLen = 0; |
michael@0 | 646 | |
michael@0 | 647 | if(prev == '@') { /* start of keyword definition */ |
michael@0 | 648 | /* we will grab pairs, trim spaces, lowercase keywords, sort and return */ |
michael@0 | 649 | do { |
michael@0 | 650 | UBool duplicate = FALSE; |
michael@0 | 651 | /* skip leading spaces */ |
michael@0 | 652 | while(*pos == ' ') { |
michael@0 | 653 | pos++; |
michael@0 | 654 | } |
michael@0 | 655 | if (!*pos) { /* handle trailing "; " */ |
michael@0 | 656 | break; |
michael@0 | 657 | } |
michael@0 | 658 | if(numKeywords == maxKeywords) { |
michael@0 | 659 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 660 | return 0; |
michael@0 | 661 | } |
michael@0 | 662 | equalSign = uprv_strchr(pos, '='); |
michael@0 | 663 | semicolon = uprv_strchr(pos, ';'); |
michael@0 | 664 | /* lack of '=' [foo@currency] is illegal */ |
michael@0 | 665 | /* ';' before '=' [foo@currency;collation=pinyin] is illegal */ |
michael@0 | 666 | if(!equalSign || (semicolon && semicolon<equalSign)) { |
michael@0 | 667 | *status = U_INVALID_FORMAT_ERROR; |
michael@0 | 668 | return 0; |
michael@0 | 669 | } |
michael@0 | 670 | /* need to normalize both keyword and keyword name */ |
michael@0 | 671 | if(equalSign - pos >= ULOC_KEYWORD_BUFFER_LEN) { |
michael@0 | 672 | /* keyword name too long for internal buffer */ |
michael@0 | 673 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 674 | return 0; |
michael@0 | 675 | } |
michael@0 | 676 | for(i = 0, n = 0; i < equalSign - pos; ++i) { |
michael@0 | 677 | if (pos[i] != ' ') { |
michael@0 | 678 | keywordList[numKeywords].keyword[n++] = uprv_tolower(pos[i]); |
michael@0 | 679 | } |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | /* zero-length keyword is an error. */ |
michael@0 | 683 | if (n == 0) { |
michael@0 | 684 | *status = U_INVALID_FORMAT_ERROR; |
michael@0 | 685 | return 0; |
michael@0 | 686 | } |
michael@0 | 687 | |
michael@0 | 688 | keywordList[numKeywords].keyword[n] = 0; |
michael@0 | 689 | keywordList[numKeywords].keywordLen = n; |
michael@0 | 690 | /* now grab the value part. First we skip the '=' */ |
michael@0 | 691 | equalSign++; |
michael@0 | 692 | /* then we leading spaces */ |
michael@0 | 693 | while(*equalSign == ' ') { |
michael@0 | 694 | equalSign++; |
michael@0 | 695 | } |
michael@0 | 696 | |
michael@0 | 697 | /* Premature end or zero-length value */ |
michael@0 | 698 | if (!equalSign || equalSign == semicolon) { |
michael@0 | 699 | *status = U_INVALID_FORMAT_ERROR; |
michael@0 | 700 | return 0; |
michael@0 | 701 | } |
michael@0 | 702 | |
michael@0 | 703 | keywordList[numKeywords].valueStart = equalSign; |
michael@0 | 704 | |
michael@0 | 705 | pos = semicolon; |
michael@0 | 706 | i = 0; |
michael@0 | 707 | if(pos) { |
michael@0 | 708 | while(*(pos - i - 1) == ' ') { |
michael@0 | 709 | i++; |
michael@0 | 710 | } |
michael@0 | 711 | keywordList[numKeywords].valueLen = (int32_t)(pos - equalSign - i); |
michael@0 | 712 | pos++; |
michael@0 | 713 | } else { |
michael@0 | 714 | i = (int32_t)uprv_strlen(equalSign); |
michael@0 | 715 | while(i && equalSign[i-1] == ' ') { |
michael@0 | 716 | i--; |
michael@0 | 717 | } |
michael@0 | 718 | keywordList[numKeywords].valueLen = i; |
michael@0 | 719 | } |
michael@0 | 720 | /* If this is a duplicate keyword, then ignore it */ |
michael@0 | 721 | for (j=0; j<numKeywords; ++j) { |
michael@0 | 722 | if (uprv_strcmp(keywordList[j].keyword, keywordList[numKeywords].keyword) == 0) { |
michael@0 | 723 | duplicate = TRUE; |
michael@0 | 724 | break; |
michael@0 | 725 | } |
michael@0 | 726 | } |
michael@0 | 727 | if (!duplicate) { |
michael@0 | 728 | ++numKeywords; |
michael@0 | 729 | } |
michael@0 | 730 | } while(pos); |
michael@0 | 731 | |
michael@0 | 732 | /* Handle addKeyword/addValue. */ |
michael@0 | 733 | if (addKeyword != NULL) { |
michael@0 | 734 | UBool duplicate = FALSE; |
michael@0 | 735 | U_ASSERT(addValue != NULL); |
michael@0 | 736 | /* Search for duplicate; if found, do nothing. Explicit keyword |
michael@0 | 737 | overrides addKeyword. */ |
michael@0 | 738 | for (j=0; j<numKeywords; ++j) { |
michael@0 | 739 | if (uprv_strcmp(keywordList[j].keyword, addKeyword) == 0) { |
michael@0 | 740 | duplicate = TRUE; |
michael@0 | 741 | break; |
michael@0 | 742 | } |
michael@0 | 743 | } |
michael@0 | 744 | if (!duplicate) { |
michael@0 | 745 | if (numKeywords == maxKeywords) { |
michael@0 | 746 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 747 | return 0; |
michael@0 | 748 | } |
michael@0 | 749 | uprv_strcpy(keywordList[numKeywords].keyword, addKeyword); |
michael@0 | 750 | keywordList[numKeywords].keywordLen = (int32_t)uprv_strlen(addKeyword); |
michael@0 | 751 | keywordList[numKeywords].valueStart = addValue; |
michael@0 | 752 | keywordList[numKeywords].valueLen = (int32_t)uprv_strlen(addValue); |
michael@0 | 753 | ++numKeywords; |
michael@0 | 754 | } |
michael@0 | 755 | } else { |
michael@0 | 756 | U_ASSERT(addValue == NULL); |
michael@0 | 757 | } |
michael@0 | 758 | |
michael@0 | 759 | /* now we have a list of keywords */ |
michael@0 | 760 | /* we need to sort it */ |
michael@0 | 761 | uprv_sortArray(keywordList, numKeywords, sizeof(KeywordStruct), compareKeywordStructs, NULL, FALSE, status); |
michael@0 | 762 | |
michael@0 | 763 | /* Now construct the keyword part */ |
michael@0 | 764 | for(i = 0; i < numKeywords; i++) { |
michael@0 | 765 | if(keywordsLen + keywordList[i].keywordLen + 1< keywordCapacity) { |
michael@0 | 766 | uprv_strcpy(keywords+keywordsLen, keywordList[i].keyword); |
michael@0 | 767 | if(valuesToo) { |
michael@0 | 768 | keywords[keywordsLen + keywordList[i].keywordLen] = '='; |
michael@0 | 769 | } else { |
michael@0 | 770 | keywords[keywordsLen + keywordList[i].keywordLen] = 0; |
michael@0 | 771 | } |
michael@0 | 772 | } |
michael@0 | 773 | keywordsLen += keywordList[i].keywordLen + 1; |
michael@0 | 774 | if(valuesToo) { |
michael@0 | 775 | if(keywordsLen + keywordList[i].valueLen < keywordCapacity) { |
michael@0 | 776 | uprv_strncpy(keywords+keywordsLen, keywordList[i].valueStart, keywordList[i].valueLen); |
michael@0 | 777 | } |
michael@0 | 778 | keywordsLen += keywordList[i].valueLen; |
michael@0 | 779 | |
michael@0 | 780 | if(i < numKeywords - 1) { |
michael@0 | 781 | if(keywordsLen < keywordCapacity) { |
michael@0 | 782 | keywords[keywordsLen] = ';'; |
michael@0 | 783 | } |
michael@0 | 784 | keywordsLen++; |
michael@0 | 785 | } |
michael@0 | 786 | } |
michael@0 | 787 | if(values) { |
michael@0 | 788 | if(valuesLen + keywordList[i].valueLen + 1< valuesCapacity) { |
michael@0 | 789 | uprv_strcpy(values+valuesLen, keywordList[i].valueStart); |
michael@0 | 790 | values[valuesLen + keywordList[i].valueLen] = 0; |
michael@0 | 791 | } |
michael@0 | 792 | valuesLen += keywordList[i].valueLen + 1; |
michael@0 | 793 | } |
michael@0 | 794 | } |
michael@0 | 795 | if(values) { |
michael@0 | 796 | values[valuesLen] = 0; |
michael@0 | 797 | if(valLen) { |
michael@0 | 798 | *valLen = valuesLen; |
michael@0 | 799 | } |
michael@0 | 800 | } |
michael@0 | 801 | return u_terminateChars(keywords, keywordCapacity, keywordsLen, status); |
michael@0 | 802 | } else { |
michael@0 | 803 | return 0; |
michael@0 | 804 | } |
michael@0 | 805 | } |
michael@0 | 806 | |
michael@0 | 807 | U_CFUNC int32_t |
michael@0 | 808 | locale_getKeywords(const char *localeID, |
michael@0 | 809 | char prev, |
michael@0 | 810 | char *keywords, int32_t keywordCapacity, |
michael@0 | 811 | char *values, int32_t valuesCapacity, int32_t *valLen, |
michael@0 | 812 | UBool valuesToo, |
michael@0 | 813 | UErrorCode *status) { |
michael@0 | 814 | return _getKeywords(localeID, prev, keywords, keywordCapacity, |
michael@0 | 815 | values, valuesCapacity, valLen, valuesToo, |
michael@0 | 816 | NULL, NULL, status); |
michael@0 | 817 | } |
michael@0 | 818 | |
michael@0 | 819 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 820 | uloc_getKeywordValue(const char* localeID, |
michael@0 | 821 | const char* keywordName, |
michael@0 | 822 | char* buffer, int32_t bufferCapacity, |
michael@0 | 823 | UErrorCode* status) |
michael@0 | 824 | { |
michael@0 | 825 | const char* startSearchHere = NULL; |
michael@0 | 826 | const char* nextSeparator = NULL; |
michael@0 | 827 | char keywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN]; |
michael@0 | 828 | char localeKeywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN]; |
michael@0 | 829 | int32_t i = 0; |
michael@0 | 830 | int32_t result = 0; |
michael@0 | 831 | |
michael@0 | 832 | if(status && U_SUCCESS(*status) && localeID) { |
michael@0 | 833 | char tempBuffer[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 834 | const char* tmpLocaleID; |
michael@0 | 835 | |
michael@0 | 836 | if (_hasBCP47Extension(localeID)) { |
michael@0 | 837 | _ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), status); |
michael@0 | 838 | } else { |
michael@0 | 839 | tmpLocaleID=localeID; |
michael@0 | 840 | } |
michael@0 | 841 | |
michael@0 | 842 | startSearchHere = uprv_strchr(tmpLocaleID, '@'); /* TODO: REVISIT: shouldn't this be locale_getKeywordsStart ? */ |
michael@0 | 843 | if(startSearchHere == NULL) { |
michael@0 | 844 | /* no keywords, return at once */ |
michael@0 | 845 | return 0; |
michael@0 | 846 | } |
michael@0 | 847 | |
michael@0 | 848 | locale_canonKeywordName(keywordNameBuffer, keywordName, status); |
michael@0 | 849 | if(U_FAILURE(*status)) { |
michael@0 | 850 | return 0; |
michael@0 | 851 | } |
michael@0 | 852 | |
michael@0 | 853 | /* find the first keyword */ |
michael@0 | 854 | while(startSearchHere) { |
michael@0 | 855 | startSearchHere++; |
michael@0 | 856 | /* skip leading spaces (allowed?) */ |
michael@0 | 857 | while(*startSearchHere == ' ') { |
michael@0 | 858 | startSearchHere++; |
michael@0 | 859 | } |
michael@0 | 860 | nextSeparator = uprv_strchr(startSearchHere, '='); |
michael@0 | 861 | /* need to normalize both keyword and keyword name */ |
michael@0 | 862 | if(!nextSeparator) { |
michael@0 | 863 | break; |
michael@0 | 864 | } |
michael@0 | 865 | if(nextSeparator - startSearchHere >= ULOC_KEYWORD_BUFFER_LEN) { |
michael@0 | 866 | /* keyword name too long for internal buffer */ |
michael@0 | 867 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 868 | return 0; |
michael@0 | 869 | } |
michael@0 | 870 | for(i = 0; i < nextSeparator - startSearchHere; i++) { |
michael@0 | 871 | localeKeywordNameBuffer[i] = uprv_tolower(startSearchHere[i]); |
michael@0 | 872 | } |
michael@0 | 873 | /* trim trailing spaces */ |
michael@0 | 874 | while(startSearchHere[i-1] == ' ') { |
michael@0 | 875 | i--; |
michael@0 | 876 | U_ASSERT(i>=0); |
michael@0 | 877 | } |
michael@0 | 878 | localeKeywordNameBuffer[i] = 0; |
michael@0 | 879 | |
michael@0 | 880 | startSearchHere = uprv_strchr(nextSeparator, ';'); |
michael@0 | 881 | |
michael@0 | 882 | if(uprv_strcmp(keywordNameBuffer, localeKeywordNameBuffer) == 0) { |
michael@0 | 883 | nextSeparator++; |
michael@0 | 884 | while(*nextSeparator == ' ') { |
michael@0 | 885 | nextSeparator++; |
michael@0 | 886 | } |
michael@0 | 887 | /* we actually found the keyword. Copy the value */ |
michael@0 | 888 | if(startSearchHere && startSearchHere - nextSeparator < bufferCapacity) { |
michael@0 | 889 | while(*(startSearchHere-1) == ' ') { |
michael@0 | 890 | startSearchHere--; |
michael@0 | 891 | } |
michael@0 | 892 | uprv_strncpy(buffer, nextSeparator, startSearchHere - nextSeparator); |
michael@0 | 893 | result = u_terminateChars(buffer, bufferCapacity, (int32_t)(startSearchHere - nextSeparator), status); |
michael@0 | 894 | } else if(!startSearchHere && (int32_t)uprv_strlen(nextSeparator) < bufferCapacity) { /* last item in string */ |
michael@0 | 895 | i = (int32_t)uprv_strlen(nextSeparator); |
michael@0 | 896 | while(nextSeparator[i - 1] == ' ') { |
michael@0 | 897 | i--; |
michael@0 | 898 | } |
michael@0 | 899 | uprv_strncpy(buffer, nextSeparator, i); |
michael@0 | 900 | result = u_terminateChars(buffer, bufferCapacity, i, status); |
michael@0 | 901 | } else { |
michael@0 | 902 | /* give a bigger buffer, please */ |
michael@0 | 903 | *status = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 904 | if(startSearchHere) { |
michael@0 | 905 | result = (int32_t)(startSearchHere - nextSeparator); |
michael@0 | 906 | } else { |
michael@0 | 907 | result = (int32_t)uprv_strlen(nextSeparator); |
michael@0 | 908 | } |
michael@0 | 909 | } |
michael@0 | 910 | return result; |
michael@0 | 911 | } |
michael@0 | 912 | } |
michael@0 | 913 | } |
michael@0 | 914 | return 0; |
michael@0 | 915 | } |
michael@0 | 916 | |
michael@0 | 917 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 918 | uloc_setKeywordValue(const char* keywordName, |
michael@0 | 919 | const char* keywordValue, |
michael@0 | 920 | char* buffer, int32_t bufferCapacity, |
michael@0 | 921 | UErrorCode* status) |
michael@0 | 922 | { |
michael@0 | 923 | /* TODO: sorting. removal. */ |
michael@0 | 924 | int32_t keywordNameLen; |
michael@0 | 925 | int32_t keywordValueLen; |
michael@0 | 926 | int32_t bufLen; |
michael@0 | 927 | int32_t needLen = 0; |
michael@0 | 928 | int32_t foundValueLen; |
michael@0 | 929 | int32_t keywordAtEnd = 0; /* is the keyword at the end of the string? */ |
michael@0 | 930 | char keywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN]; |
michael@0 | 931 | char localeKeywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN]; |
michael@0 | 932 | int32_t i = 0; |
michael@0 | 933 | int32_t rc; |
michael@0 | 934 | char* nextSeparator = NULL; |
michael@0 | 935 | char* nextEqualsign = NULL; |
michael@0 | 936 | char* startSearchHere = NULL; |
michael@0 | 937 | char* keywordStart = NULL; |
michael@0 | 938 | char *insertHere = NULL; |
michael@0 | 939 | if(U_FAILURE(*status)) { |
michael@0 | 940 | return -1; |
michael@0 | 941 | } |
michael@0 | 942 | if(bufferCapacity>1) { |
michael@0 | 943 | bufLen = (int32_t)uprv_strlen(buffer); |
michael@0 | 944 | } else { |
michael@0 | 945 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 946 | return 0; |
michael@0 | 947 | } |
michael@0 | 948 | if(bufferCapacity<bufLen) { |
michael@0 | 949 | /* The capacity is less than the length?! Is this NULL terminated? */ |
michael@0 | 950 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 951 | return 0; |
michael@0 | 952 | } |
michael@0 | 953 | if(keywordValue && !*keywordValue) { |
michael@0 | 954 | keywordValue = NULL; |
michael@0 | 955 | } |
michael@0 | 956 | if(keywordValue) { |
michael@0 | 957 | keywordValueLen = (int32_t)uprv_strlen(keywordValue); |
michael@0 | 958 | } else { |
michael@0 | 959 | keywordValueLen = 0; |
michael@0 | 960 | } |
michael@0 | 961 | keywordNameLen = locale_canonKeywordName(keywordNameBuffer, keywordName, status); |
michael@0 | 962 | if(U_FAILURE(*status)) { |
michael@0 | 963 | return 0; |
michael@0 | 964 | } |
michael@0 | 965 | startSearchHere = (char*)locale_getKeywordsStart(buffer); |
michael@0 | 966 | if(startSearchHere == NULL || (startSearchHere[1]==0)) { |
michael@0 | 967 | if(!keywordValue) { /* no keywords = nothing to remove */ |
michael@0 | 968 | return bufLen; |
michael@0 | 969 | } |
michael@0 | 970 | |
michael@0 | 971 | needLen = bufLen+1+keywordNameLen+1+keywordValueLen; |
michael@0 | 972 | if(startSearchHere) { /* had a single @ */ |
michael@0 | 973 | needLen--; /* already had the @ */ |
michael@0 | 974 | /* startSearchHere points at the @ */ |
michael@0 | 975 | } else { |
michael@0 | 976 | startSearchHere=buffer+bufLen; |
michael@0 | 977 | } |
michael@0 | 978 | if(needLen >= bufferCapacity) { |
michael@0 | 979 | *status = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 980 | return needLen; /* no change */ |
michael@0 | 981 | } |
michael@0 | 982 | *startSearchHere = '@'; |
michael@0 | 983 | startSearchHere++; |
michael@0 | 984 | uprv_strcpy(startSearchHere, keywordNameBuffer); |
michael@0 | 985 | startSearchHere += keywordNameLen; |
michael@0 | 986 | *startSearchHere = '='; |
michael@0 | 987 | startSearchHere++; |
michael@0 | 988 | uprv_strcpy(startSearchHere, keywordValue); |
michael@0 | 989 | startSearchHere+=keywordValueLen; |
michael@0 | 990 | return needLen; |
michael@0 | 991 | } /* end shortcut - no @ */ |
michael@0 | 992 | |
michael@0 | 993 | keywordStart = startSearchHere; |
michael@0 | 994 | /* search for keyword */ |
michael@0 | 995 | while(keywordStart) { |
michael@0 | 996 | keywordStart++; |
michael@0 | 997 | /* skip leading spaces (allowed?) */ |
michael@0 | 998 | while(*keywordStart == ' ') { |
michael@0 | 999 | keywordStart++; |
michael@0 | 1000 | } |
michael@0 | 1001 | nextEqualsign = uprv_strchr(keywordStart, '='); |
michael@0 | 1002 | /* need to normalize both keyword and keyword name */ |
michael@0 | 1003 | if(!nextEqualsign) { |
michael@0 | 1004 | break; |
michael@0 | 1005 | } |
michael@0 | 1006 | if(nextEqualsign - keywordStart >= ULOC_KEYWORD_BUFFER_LEN) { |
michael@0 | 1007 | /* keyword name too long for internal buffer */ |
michael@0 | 1008 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 1009 | return 0; |
michael@0 | 1010 | } |
michael@0 | 1011 | for(i = 0; i < nextEqualsign - keywordStart; i++) { |
michael@0 | 1012 | localeKeywordNameBuffer[i] = uprv_tolower(keywordStart[i]); |
michael@0 | 1013 | } |
michael@0 | 1014 | /* trim trailing spaces */ |
michael@0 | 1015 | while(keywordStart[i-1] == ' ') { |
michael@0 | 1016 | i--; |
michael@0 | 1017 | } |
michael@0 | 1018 | U_ASSERT(i>=0 && i<ULOC_KEYWORD_BUFFER_LEN); |
michael@0 | 1019 | localeKeywordNameBuffer[i] = 0; |
michael@0 | 1020 | |
michael@0 | 1021 | nextSeparator = uprv_strchr(nextEqualsign, ';'); |
michael@0 | 1022 | rc = uprv_strcmp(keywordNameBuffer, localeKeywordNameBuffer); |
michael@0 | 1023 | if(rc == 0) { |
michael@0 | 1024 | nextEqualsign++; |
michael@0 | 1025 | while(*nextEqualsign == ' ') { |
michael@0 | 1026 | nextEqualsign++; |
michael@0 | 1027 | } |
michael@0 | 1028 | /* we actually found the keyword. Change the value */ |
michael@0 | 1029 | if (nextSeparator) { |
michael@0 | 1030 | keywordAtEnd = 0; |
michael@0 | 1031 | foundValueLen = (int32_t)(nextSeparator - nextEqualsign); |
michael@0 | 1032 | } else { |
michael@0 | 1033 | keywordAtEnd = 1; |
michael@0 | 1034 | foundValueLen = (int32_t)uprv_strlen(nextEqualsign); |
michael@0 | 1035 | } |
michael@0 | 1036 | if(keywordValue) { /* adding a value - not removing */ |
michael@0 | 1037 | if(foundValueLen == keywordValueLen) { |
michael@0 | 1038 | uprv_strncpy(nextEqualsign, keywordValue, keywordValueLen); |
michael@0 | 1039 | return bufLen; /* no change in size */ |
michael@0 | 1040 | } else if(foundValueLen > keywordValueLen) { |
michael@0 | 1041 | int32_t delta = foundValueLen - keywordValueLen; |
michael@0 | 1042 | if(nextSeparator) { /* RH side */ |
michael@0 | 1043 | uprv_memmove(nextSeparator - delta, nextSeparator, bufLen-(nextSeparator-buffer)); |
michael@0 | 1044 | } |
michael@0 | 1045 | uprv_strncpy(nextEqualsign, keywordValue, keywordValueLen); |
michael@0 | 1046 | bufLen -= delta; |
michael@0 | 1047 | buffer[bufLen]=0; |
michael@0 | 1048 | return bufLen; |
michael@0 | 1049 | } else { /* FVL < KVL */ |
michael@0 | 1050 | int32_t delta = keywordValueLen - foundValueLen; |
michael@0 | 1051 | if((bufLen+delta) >= bufferCapacity) { |
michael@0 | 1052 | *status = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 1053 | return bufLen+delta; |
michael@0 | 1054 | } |
michael@0 | 1055 | if(nextSeparator) { /* RH side */ |
michael@0 | 1056 | uprv_memmove(nextSeparator+delta,nextSeparator, bufLen-(nextSeparator-buffer)); |
michael@0 | 1057 | } |
michael@0 | 1058 | uprv_strncpy(nextEqualsign, keywordValue, keywordValueLen); |
michael@0 | 1059 | bufLen += delta; |
michael@0 | 1060 | buffer[bufLen]=0; |
michael@0 | 1061 | return bufLen; |
michael@0 | 1062 | } |
michael@0 | 1063 | } else { /* removing a keyword */ |
michael@0 | 1064 | if(keywordAtEnd) { |
michael@0 | 1065 | /* zero out the ';' or '@' just before startSearchhere */ |
michael@0 | 1066 | keywordStart[-1] = 0; |
michael@0 | 1067 | return (int32_t)((keywordStart-buffer)-1); /* (string length without keyword) minus separator */ |
michael@0 | 1068 | } else { |
michael@0 | 1069 | uprv_memmove(keywordStart, nextSeparator+1, bufLen-((nextSeparator+1)-buffer)); |
michael@0 | 1070 | keywordStart[bufLen-((nextSeparator+1)-buffer)]=0; |
michael@0 | 1071 | return (int32_t)(bufLen-((nextSeparator+1)-keywordStart)); |
michael@0 | 1072 | } |
michael@0 | 1073 | } |
michael@0 | 1074 | } else if(rc<0){ /* end match keyword */ |
michael@0 | 1075 | /* could insert at this location. */ |
michael@0 | 1076 | insertHere = keywordStart; |
michael@0 | 1077 | } |
michael@0 | 1078 | keywordStart = nextSeparator; |
michael@0 | 1079 | } /* end loop searching */ |
michael@0 | 1080 | |
michael@0 | 1081 | if(!keywordValue) { |
michael@0 | 1082 | return bufLen; /* removal of non-extant keyword - no change */ |
michael@0 | 1083 | } |
michael@0 | 1084 | |
michael@0 | 1085 | /* we know there is at least one keyword. */ |
michael@0 | 1086 | needLen = bufLen+1+keywordNameLen+1+keywordValueLen; |
michael@0 | 1087 | if(needLen >= bufferCapacity) { |
michael@0 | 1088 | *status = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 1089 | return needLen; /* no change */ |
michael@0 | 1090 | } |
michael@0 | 1091 | |
michael@0 | 1092 | if(insertHere) { |
michael@0 | 1093 | uprv_memmove(insertHere+(1+keywordNameLen+1+keywordValueLen), insertHere, bufLen-(insertHere-buffer)); |
michael@0 | 1094 | keywordStart = insertHere; |
michael@0 | 1095 | } else { |
michael@0 | 1096 | keywordStart = buffer+bufLen; |
michael@0 | 1097 | *keywordStart = ';'; |
michael@0 | 1098 | keywordStart++; |
michael@0 | 1099 | } |
michael@0 | 1100 | uprv_strncpy(keywordStart, keywordNameBuffer, keywordNameLen); |
michael@0 | 1101 | keywordStart += keywordNameLen; |
michael@0 | 1102 | *keywordStart = '='; |
michael@0 | 1103 | keywordStart++; |
michael@0 | 1104 | uprv_strncpy(keywordStart, keywordValue, keywordValueLen); /* terminates. */ |
michael@0 | 1105 | keywordStart+=keywordValueLen; |
michael@0 | 1106 | if(insertHere) { |
michael@0 | 1107 | *keywordStart = ';'; |
michael@0 | 1108 | keywordStart++; |
michael@0 | 1109 | } |
michael@0 | 1110 | buffer[needLen]=0; |
michael@0 | 1111 | return needLen; |
michael@0 | 1112 | } |
michael@0 | 1113 | |
michael@0 | 1114 | /* ### ID parsing implementation **************************************************/ |
michael@0 | 1115 | |
michael@0 | 1116 | #define _isPrefixLetter(a) ((a=='x')||(a=='X')||(a=='i')||(a=='I')) |
michael@0 | 1117 | |
michael@0 | 1118 | /*returns TRUE if one of the special prefixes is here (s=string) |
michael@0 | 1119 | 'x-' or 'i-' */ |
michael@0 | 1120 | #define _isIDPrefix(s) (_isPrefixLetter(s[0])&&_isIDSeparator(s[1])) |
michael@0 | 1121 | |
michael@0 | 1122 | /* Dot terminates it because of POSIX form where dot precedes the codepage |
michael@0 | 1123 | * except for variant |
michael@0 | 1124 | */ |
michael@0 | 1125 | #define _isTerminator(a) ((a==0)||(a=='.')||(a=='@')) |
michael@0 | 1126 | |
michael@0 | 1127 | static char* _strnchr(const char* str, int32_t len, char c) { |
michael@0 | 1128 | U_ASSERT(str != 0 && len >= 0); |
michael@0 | 1129 | while (len-- != 0) { |
michael@0 | 1130 | char d = *str; |
michael@0 | 1131 | if (d == c) { |
michael@0 | 1132 | return (char*) str; |
michael@0 | 1133 | } else if (d == 0) { |
michael@0 | 1134 | break; |
michael@0 | 1135 | } |
michael@0 | 1136 | ++str; |
michael@0 | 1137 | } |
michael@0 | 1138 | return NULL; |
michael@0 | 1139 | } |
michael@0 | 1140 | |
michael@0 | 1141 | /** |
michael@0 | 1142 | * Lookup 'key' in the array 'list'. The array 'list' should contain |
michael@0 | 1143 | * a NULL entry, followed by more entries, and a second NULL entry. |
michael@0 | 1144 | * |
michael@0 | 1145 | * The 'list' param should be LANGUAGES, LANGUAGES_3, COUNTRIES, or |
michael@0 | 1146 | * COUNTRIES_3. |
michael@0 | 1147 | */ |
michael@0 | 1148 | static int16_t _findIndex(const char* const* list, const char* key) |
michael@0 | 1149 | { |
michael@0 | 1150 | const char* const* anchor = list; |
michael@0 | 1151 | int32_t pass = 0; |
michael@0 | 1152 | |
michael@0 | 1153 | /* Make two passes through two NULL-terminated arrays at 'list' */ |
michael@0 | 1154 | while (pass++ < 2) { |
michael@0 | 1155 | while (*list) { |
michael@0 | 1156 | if (uprv_strcmp(key, *list) == 0) { |
michael@0 | 1157 | return (int16_t)(list - anchor); |
michael@0 | 1158 | } |
michael@0 | 1159 | list++; |
michael@0 | 1160 | } |
michael@0 | 1161 | ++list; /* skip final NULL *CWB*/ |
michael@0 | 1162 | } |
michael@0 | 1163 | return -1; |
michael@0 | 1164 | } |
michael@0 | 1165 | |
michael@0 | 1166 | /* count the length of src while copying it to dest; return strlen(src) */ |
michael@0 | 1167 | static inline int32_t |
michael@0 | 1168 | _copyCount(char *dest, int32_t destCapacity, const char *src) { |
michael@0 | 1169 | const char *anchor; |
michael@0 | 1170 | char c; |
michael@0 | 1171 | |
michael@0 | 1172 | anchor=src; |
michael@0 | 1173 | for(;;) { |
michael@0 | 1174 | if((c=*src)==0) { |
michael@0 | 1175 | return (int32_t)(src-anchor); |
michael@0 | 1176 | } |
michael@0 | 1177 | if(destCapacity<=0) { |
michael@0 | 1178 | return (int32_t)((src-anchor)+uprv_strlen(src)); |
michael@0 | 1179 | } |
michael@0 | 1180 | ++src; |
michael@0 | 1181 | *dest++=c; |
michael@0 | 1182 | --destCapacity; |
michael@0 | 1183 | } |
michael@0 | 1184 | } |
michael@0 | 1185 | |
michael@0 | 1186 | U_CFUNC const char* |
michael@0 | 1187 | uloc_getCurrentCountryID(const char* oldID){ |
michael@0 | 1188 | int32_t offset = _findIndex(DEPRECATED_COUNTRIES, oldID); |
michael@0 | 1189 | if (offset >= 0) { |
michael@0 | 1190 | return REPLACEMENT_COUNTRIES[offset]; |
michael@0 | 1191 | } |
michael@0 | 1192 | return oldID; |
michael@0 | 1193 | } |
michael@0 | 1194 | U_CFUNC const char* |
michael@0 | 1195 | uloc_getCurrentLanguageID(const char* oldID){ |
michael@0 | 1196 | int32_t offset = _findIndex(DEPRECATED_LANGUAGES, oldID); |
michael@0 | 1197 | if (offset >= 0) { |
michael@0 | 1198 | return REPLACEMENT_LANGUAGES[offset]; |
michael@0 | 1199 | } |
michael@0 | 1200 | return oldID; |
michael@0 | 1201 | } |
michael@0 | 1202 | /* |
michael@0 | 1203 | * the internal functions _getLanguage(), _getCountry(), _getVariant() |
michael@0 | 1204 | * avoid duplicating code to handle the earlier locale ID pieces |
michael@0 | 1205 | * in the functions for the later ones by |
michael@0 | 1206 | * setting the *pEnd pointer to where they stopped parsing |
michael@0 | 1207 | * |
michael@0 | 1208 | * TODO try to use this in Locale |
michael@0 | 1209 | */ |
michael@0 | 1210 | U_CFUNC int32_t |
michael@0 | 1211 | ulocimp_getLanguage(const char *localeID, |
michael@0 | 1212 | char *language, int32_t languageCapacity, |
michael@0 | 1213 | const char **pEnd) { |
michael@0 | 1214 | int32_t i=0; |
michael@0 | 1215 | int32_t offset; |
michael@0 | 1216 | char lang[4]={ 0, 0, 0, 0 }; /* temporary buffer to hold language code for searching */ |
michael@0 | 1217 | |
michael@0 | 1218 | /* if it starts with i- or x- then copy that prefix */ |
michael@0 | 1219 | if(_isIDPrefix(localeID)) { |
michael@0 | 1220 | if(i<languageCapacity) { |
michael@0 | 1221 | language[i]=(char)uprv_tolower(*localeID); |
michael@0 | 1222 | } |
michael@0 | 1223 | if(i<languageCapacity) { |
michael@0 | 1224 | language[i+1]='-'; |
michael@0 | 1225 | } |
michael@0 | 1226 | i+=2; |
michael@0 | 1227 | localeID+=2; |
michael@0 | 1228 | } |
michael@0 | 1229 | |
michael@0 | 1230 | /* copy the language as far as possible and count its length */ |
michael@0 | 1231 | while(!_isTerminator(*localeID) && !_isIDSeparator(*localeID)) { |
michael@0 | 1232 | if(i<languageCapacity) { |
michael@0 | 1233 | language[i]=(char)uprv_tolower(*localeID); |
michael@0 | 1234 | } |
michael@0 | 1235 | if(i<3) { |
michael@0 | 1236 | U_ASSERT(i>=0); |
michael@0 | 1237 | lang[i]=(char)uprv_tolower(*localeID); |
michael@0 | 1238 | } |
michael@0 | 1239 | i++; |
michael@0 | 1240 | localeID++; |
michael@0 | 1241 | } |
michael@0 | 1242 | |
michael@0 | 1243 | if(i==3) { |
michael@0 | 1244 | /* convert 3 character code to 2 character code if possible *CWB*/ |
michael@0 | 1245 | offset=_findIndex(LANGUAGES_3, lang); |
michael@0 | 1246 | if(offset>=0) { |
michael@0 | 1247 | i=_copyCount(language, languageCapacity, LANGUAGES[offset]); |
michael@0 | 1248 | } |
michael@0 | 1249 | } |
michael@0 | 1250 | |
michael@0 | 1251 | if(pEnd!=NULL) { |
michael@0 | 1252 | *pEnd=localeID; |
michael@0 | 1253 | } |
michael@0 | 1254 | return i; |
michael@0 | 1255 | } |
michael@0 | 1256 | |
michael@0 | 1257 | U_CFUNC int32_t |
michael@0 | 1258 | ulocimp_getScript(const char *localeID, |
michael@0 | 1259 | char *script, int32_t scriptCapacity, |
michael@0 | 1260 | const char **pEnd) |
michael@0 | 1261 | { |
michael@0 | 1262 | int32_t idLen = 0; |
michael@0 | 1263 | |
michael@0 | 1264 | if (pEnd != NULL) { |
michael@0 | 1265 | *pEnd = localeID; |
michael@0 | 1266 | } |
michael@0 | 1267 | |
michael@0 | 1268 | /* copy the second item as far as possible and count its length */ |
michael@0 | 1269 | while(!_isTerminator(localeID[idLen]) && !_isIDSeparator(localeID[idLen]) |
michael@0 | 1270 | && uprv_isASCIILetter(localeID[idLen])) { |
michael@0 | 1271 | idLen++; |
michael@0 | 1272 | } |
michael@0 | 1273 | |
michael@0 | 1274 | /* If it's exactly 4 characters long, then it's a script and not a country. */ |
michael@0 | 1275 | if (idLen == 4) { |
michael@0 | 1276 | int32_t i; |
michael@0 | 1277 | if (pEnd != NULL) { |
michael@0 | 1278 | *pEnd = localeID+idLen; |
michael@0 | 1279 | } |
michael@0 | 1280 | if(idLen > scriptCapacity) { |
michael@0 | 1281 | idLen = scriptCapacity; |
michael@0 | 1282 | } |
michael@0 | 1283 | if (idLen >= 1) { |
michael@0 | 1284 | script[0]=(char)uprv_toupper(*(localeID++)); |
michael@0 | 1285 | } |
michael@0 | 1286 | for (i = 1; i < idLen; i++) { |
michael@0 | 1287 | script[i]=(char)uprv_tolower(*(localeID++)); |
michael@0 | 1288 | } |
michael@0 | 1289 | } |
michael@0 | 1290 | else { |
michael@0 | 1291 | idLen = 0; |
michael@0 | 1292 | } |
michael@0 | 1293 | return idLen; |
michael@0 | 1294 | } |
michael@0 | 1295 | |
michael@0 | 1296 | U_CFUNC int32_t |
michael@0 | 1297 | ulocimp_getCountry(const char *localeID, |
michael@0 | 1298 | char *country, int32_t countryCapacity, |
michael@0 | 1299 | const char **pEnd) |
michael@0 | 1300 | { |
michael@0 | 1301 | int32_t idLen=0; |
michael@0 | 1302 | char cnty[ULOC_COUNTRY_CAPACITY]={ 0, 0, 0, 0 }; |
michael@0 | 1303 | int32_t offset; |
michael@0 | 1304 | |
michael@0 | 1305 | /* copy the country as far as possible and count its length */ |
michael@0 | 1306 | while(!_isTerminator(localeID[idLen]) && !_isIDSeparator(localeID[idLen])) { |
michael@0 | 1307 | if(idLen<(ULOC_COUNTRY_CAPACITY-1)) { /*CWB*/ |
michael@0 | 1308 | cnty[idLen]=(char)uprv_toupper(localeID[idLen]); |
michael@0 | 1309 | } |
michael@0 | 1310 | idLen++; |
michael@0 | 1311 | } |
michael@0 | 1312 | |
michael@0 | 1313 | /* the country should be either length 2 or 3 */ |
michael@0 | 1314 | if (idLen == 2 || idLen == 3) { |
michael@0 | 1315 | UBool gotCountry = FALSE; |
michael@0 | 1316 | /* convert 3 character code to 2 character code if possible *CWB*/ |
michael@0 | 1317 | if(idLen==3) { |
michael@0 | 1318 | offset=_findIndex(COUNTRIES_3, cnty); |
michael@0 | 1319 | if(offset>=0) { |
michael@0 | 1320 | idLen=_copyCount(country, countryCapacity, COUNTRIES[offset]); |
michael@0 | 1321 | gotCountry = TRUE; |
michael@0 | 1322 | } |
michael@0 | 1323 | } |
michael@0 | 1324 | if (!gotCountry) { |
michael@0 | 1325 | int32_t i = 0; |
michael@0 | 1326 | for (i = 0; i < idLen; i++) { |
michael@0 | 1327 | if (i < countryCapacity) { |
michael@0 | 1328 | country[i]=(char)uprv_toupper(localeID[i]); |
michael@0 | 1329 | } |
michael@0 | 1330 | } |
michael@0 | 1331 | } |
michael@0 | 1332 | localeID+=idLen; |
michael@0 | 1333 | } else { |
michael@0 | 1334 | idLen = 0; |
michael@0 | 1335 | } |
michael@0 | 1336 | |
michael@0 | 1337 | if(pEnd!=NULL) { |
michael@0 | 1338 | *pEnd=localeID; |
michael@0 | 1339 | } |
michael@0 | 1340 | |
michael@0 | 1341 | return idLen; |
michael@0 | 1342 | } |
michael@0 | 1343 | |
michael@0 | 1344 | /** |
michael@0 | 1345 | * @param needSeparator if true, then add leading '_' if any variants |
michael@0 | 1346 | * are added to 'variant' |
michael@0 | 1347 | */ |
michael@0 | 1348 | static int32_t |
michael@0 | 1349 | _getVariantEx(const char *localeID, |
michael@0 | 1350 | char prev, |
michael@0 | 1351 | char *variant, int32_t variantCapacity, |
michael@0 | 1352 | UBool needSeparator) { |
michael@0 | 1353 | int32_t i=0; |
michael@0 | 1354 | |
michael@0 | 1355 | /* get one or more variant tags and separate them with '_' */ |
michael@0 | 1356 | if(_isIDSeparator(prev)) { |
michael@0 | 1357 | /* get a variant string after a '-' or '_' */ |
michael@0 | 1358 | while(!_isTerminator(*localeID)) { |
michael@0 | 1359 | if (needSeparator) { |
michael@0 | 1360 | if (i<variantCapacity) { |
michael@0 | 1361 | variant[i] = '_'; |
michael@0 | 1362 | } |
michael@0 | 1363 | ++i; |
michael@0 | 1364 | needSeparator = FALSE; |
michael@0 | 1365 | } |
michael@0 | 1366 | if(i<variantCapacity) { |
michael@0 | 1367 | variant[i]=(char)uprv_toupper(*localeID); |
michael@0 | 1368 | if(variant[i]=='-') { |
michael@0 | 1369 | variant[i]='_'; |
michael@0 | 1370 | } |
michael@0 | 1371 | } |
michael@0 | 1372 | i++; |
michael@0 | 1373 | localeID++; |
michael@0 | 1374 | } |
michael@0 | 1375 | } |
michael@0 | 1376 | |
michael@0 | 1377 | /* if there is no variant tag after a '-' or '_' then look for '@' */ |
michael@0 | 1378 | if(i==0) { |
michael@0 | 1379 | if(prev=='@') { |
michael@0 | 1380 | /* keep localeID */ |
michael@0 | 1381 | } else if((localeID=locale_getKeywordsStart(localeID))!=NULL) { |
michael@0 | 1382 | ++localeID; /* point after the '@' */ |
michael@0 | 1383 | } else { |
michael@0 | 1384 | return 0; |
michael@0 | 1385 | } |
michael@0 | 1386 | while(!_isTerminator(*localeID)) { |
michael@0 | 1387 | if (needSeparator) { |
michael@0 | 1388 | if (i<variantCapacity) { |
michael@0 | 1389 | variant[i] = '_'; |
michael@0 | 1390 | } |
michael@0 | 1391 | ++i; |
michael@0 | 1392 | needSeparator = FALSE; |
michael@0 | 1393 | } |
michael@0 | 1394 | if(i<variantCapacity) { |
michael@0 | 1395 | variant[i]=(char)uprv_toupper(*localeID); |
michael@0 | 1396 | if(variant[i]=='-' || variant[i]==',') { |
michael@0 | 1397 | variant[i]='_'; |
michael@0 | 1398 | } |
michael@0 | 1399 | } |
michael@0 | 1400 | i++; |
michael@0 | 1401 | localeID++; |
michael@0 | 1402 | } |
michael@0 | 1403 | } |
michael@0 | 1404 | |
michael@0 | 1405 | return i; |
michael@0 | 1406 | } |
michael@0 | 1407 | |
michael@0 | 1408 | static int32_t |
michael@0 | 1409 | _getVariant(const char *localeID, |
michael@0 | 1410 | char prev, |
michael@0 | 1411 | char *variant, int32_t variantCapacity) { |
michael@0 | 1412 | return _getVariantEx(localeID, prev, variant, variantCapacity, FALSE); |
michael@0 | 1413 | } |
michael@0 | 1414 | |
michael@0 | 1415 | /** |
michael@0 | 1416 | * Delete ALL instances of a variant from the given list of one or |
michael@0 | 1417 | * more variants. Example: "FOO_EURO_BAR_EURO" => "FOO_BAR". |
michael@0 | 1418 | * @param variants the source string of one or more variants, |
michael@0 | 1419 | * separated by '_'. This will be MODIFIED IN PLACE. Not zero |
michael@0 | 1420 | * terminated; if it is, trailing zero will NOT be maintained. |
michael@0 | 1421 | * @param variantsLen length of variants |
michael@0 | 1422 | * @param toDelete variant to delete, without separators, e.g. "EURO" |
michael@0 | 1423 | * or "PREEURO"; not zero terminated |
michael@0 | 1424 | * @param toDeleteLen length of toDelete |
michael@0 | 1425 | * @return number of characters deleted from variants |
michael@0 | 1426 | */ |
michael@0 | 1427 | static int32_t |
michael@0 | 1428 | _deleteVariant(char* variants, int32_t variantsLen, |
michael@0 | 1429 | const char* toDelete, int32_t toDeleteLen) |
michael@0 | 1430 | { |
michael@0 | 1431 | int32_t delta = 0; /* number of chars deleted */ |
michael@0 | 1432 | for (;;) { |
michael@0 | 1433 | UBool flag = FALSE; |
michael@0 | 1434 | if (variantsLen < toDeleteLen) { |
michael@0 | 1435 | return delta; |
michael@0 | 1436 | } |
michael@0 | 1437 | if (uprv_strncmp(variants, toDelete, toDeleteLen) == 0 && |
michael@0 | 1438 | (variantsLen == toDeleteLen || |
michael@0 | 1439 | (flag=(variants[toDeleteLen] == '_')))) |
michael@0 | 1440 | { |
michael@0 | 1441 | int32_t d = toDeleteLen + (flag?1:0); |
michael@0 | 1442 | variantsLen -= d; |
michael@0 | 1443 | delta += d; |
michael@0 | 1444 | if (variantsLen > 0) { |
michael@0 | 1445 | uprv_memmove(variants, variants+d, variantsLen); |
michael@0 | 1446 | } |
michael@0 | 1447 | } else { |
michael@0 | 1448 | char* p = _strnchr(variants, variantsLen, '_'); |
michael@0 | 1449 | if (p == NULL) { |
michael@0 | 1450 | return delta; |
michael@0 | 1451 | } |
michael@0 | 1452 | ++p; |
michael@0 | 1453 | variantsLen -= (int32_t)(p - variants); |
michael@0 | 1454 | variants = p; |
michael@0 | 1455 | } |
michael@0 | 1456 | } |
michael@0 | 1457 | } |
michael@0 | 1458 | |
michael@0 | 1459 | /* Keyword enumeration */ |
michael@0 | 1460 | |
michael@0 | 1461 | typedef struct UKeywordsContext { |
michael@0 | 1462 | char* keywords; |
michael@0 | 1463 | char* current; |
michael@0 | 1464 | } UKeywordsContext; |
michael@0 | 1465 | |
michael@0 | 1466 | static void U_CALLCONV |
michael@0 | 1467 | uloc_kw_closeKeywords(UEnumeration *enumerator) { |
michael@0 | 1468 | uprv_free(((UKeywordsContext *)enumerator->context)->keywords); |
michael@0 | 1469 | uprv_free(enumerator->context); |
michael@0 | 1470 | uprv_free(enumerator); |
michael@0 | 1471 | } |
michael@0 | 1472 | |
michael@0 | 1473 | static int32_t U_CALLCONV |
michael@0 | 1474 | uloc_kw_countKeywords(UEnumeration *en, UErrorCode * /*status*/) { |
michael@0 | 1475 | char *kw = ((UKeywordsContext *)en->context)->keywords; |
michael@0 | 1476 | int32_t result = 0; |
michael@0 | 1477 | while(*kw) { |
michael@0 | 1478 | result++; |
michael@0 | 1479 | kw += uprv_strlen(kw)+1; |
michael@0 | 1480 | } |
michael@0 | 1481 | return result; |
michael@0 | 1482 | } |
michael@0 | 1483 | |
michael@0 | 1484 | static const char* U_CALLCONV |
michael@0 | 1485 | uloc_kw_nextKeyword(UEnumeration* en, |
michael@0 | 1486 | int32_t* resultLength, |
michael@0 | 1487 | UErrorCode* /*status*/) { |
michael@0 | 1488 | const char* result = ((UKeywordsContext *)en->context)->current; |
michael@0 | 1489 | int32_t len = 0; |
michael@0 | 1490 | if(*result) { |
michael@0 | 1491 | len = (int32_t)uprv_strlen(((UKeywordsContext *)en->context)->current); |
michael@0 | 1492 | ((UKeywordsContext *)en->context)->current += len+1; |
michael@0 | 1493 | } else { |
michael@0 | 1494 | result = NULL; |
michael@0 | 1495 | } |
michael@0 | 1496 | if (resultLength) { |
michael@0 | 1497 | *resultLength = len; |
michael@0 | 1498 | } |
michael@0 | 1499 | return result; |
michael@0 | 1500 | } |
michael@0 | 1501 | |
michael@0 | 1502 | static void U_CALLCONV |
michael@0 | 1503 | uloc_kw_resetKeywords(UEnumeration* en, |
michael@0 | 1504 | UErrorCode* /*status*/) { |
michael@0 | 1505 | ((UKeywordsContext *)en->context)->current = ((UKeywordsContext *)en->context)->keywords; |
michael@0 | 1506 | } |
michael@0 | 1507 | |
michael@0 | 1508 | static const UEnumeration gKeywordsEnum = { |
michael@0 | 1509 | NULL, |
michael@0 | 1510 | NULL, |
michael@0 | 1511 | uloc_kw_closeKeywords, |
michael@0 | 1512 | uloc_kw_countKeywords, |
michael@0 | 1513 | uenum_unextDefault, |
michael@0 | 1514 | uloc_kw_nextKeyword, |
michael@0 | 1515 | uloc_kw_resetKeywords |
michael@0 | 1516 | }; |
michael@0 | 1517 | |
michael@0 | 1518 | U_CAPI UEnumeration* U_EXPORT2 |
michael@0 | 1519 | uloc_openKeywordList(const char *keywordList, int32_t keywordListSize, UErrorCode* status) |
michael@0 | 1520 | { |
michael@0 | 1521 | UKeywordsContext *myContext = NULL; |
michael@0 | 1522 | UEnumeration *result = NULL; |
michael@0 | 1523 | |
michael@0 | 1524 | if(U_FAILURE(*status)) { |
michael@0 | 1525 | return NULL; |
michael@0 | 1526 | } |
michael@0 | 1527 | result = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); |
michael@0 | 1528 | /* Null pointer test */ |
michael@0 | 1529 | if (result == NULL) { |
michael@0 | 1530 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1531 | return NULL; |
michael@0 | 1532 | } |
michael@0 | 1533 | uprv_memcpy(result, &gKeywordsEnum, sizeof(UEnumeration)); |
michael@0 | 1534 | myContext = static_cast<UKeywordsContext *>(uprv_malloc(sizeof(UKeywordsContext))); |
michael@0 | 1535 | if (myContext == NULL) { |
michael@0 | 1536 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1537 | uprv_free(result); |
michael@0 | 1538 | return NULL; |
michael@0 | 1539 | } |
michael@0 | 1540 | myContext->keywords = (char *)uprv_malloc(keywordListSize+1); |
michael@0 | 1541 | uprv_memcpy(myContext->keywords, keywordList, keywordListSize); |
michael@0 | 1542 | myContext->keywords[keywordListSize] = 0; |
michael@0 | 1543 | myContext->current = myContext->keywords; |
michael@0 | 1544 | result->context = myContext; |
michael@0 | 1545 | return result; |
michael@0 | 1546 | } |
michael@0 | 1547 | |
michael@0 | 1548 | U_CAPI UEnumeration* U_EXPORT2 |
michael@0 | 1549 | uloc_openKeywords(const char* localeID, |
michael@0 | 1550 | UErrorCode* status) |
michael@0 | 1551 | { |
michael@0 | 1552 | int32_t i=0; |
michael@0 | 1553 | char keywords[256]; |
michael@0 | 1554 | int32_t keywordsCapacity = 256; |
michael@0 | 1555 | char tempBuffer[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 1556 | const char* tmpLocaleID; |
michael@0 | 1557 | |
michael@0 | 1558 | if(status==NULL || U_FAILURE(*status)) { |
michael@0 | 1559 | return 0; |
michael@0 | 1560 | } |
michael@0 | 1561 | |
michael@0 | 1562 | if (_hasBCP47Extension(localeID)) { |
michael@0 | 1563 | _ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), status); |
michael@0 | 1564 | } else { |
michael@0 | 1565 | if (localeID==NULL) { |
michael@0 | 1566 | localeID=uloc_getDefault(); |
michael@0 | 1567 | } |
michael@0 | 1568 | tmpLocaleID=localeID; |
michael@0 | 1569 | } |
michael@0 | 1570 | |
michael@0 | 1571 | /* Skip the language */ |
michael@0 | 1572 | ulocimp_getLanguage(tmpLocaleID, NULL, 0, &tmpLocaleID); |
michael@0 | 1573 | if(_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1574 | const char *scriptID; |
michael@0 | 1575 | /* Skip the script if available */ |
michael@0 | 1576 | ulocimp_getScript(tmpLocaleID+1, NULL, 0, &scriptID); |
michael@0 | 1577 | if(scriptID != tmpLocaleID+1) { |
michael@0 | 1578 | /* Found optional script */ |
michael@0 | 1579 | tmpLocaleID = scriptID; |
michael@0 | 1580 | } |
michael@0 | 1581 | /* Skip the Country */ |
michael@0 | 1582 | if (_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1583 | ulocimp_getCountry(tmpLocaleID+1, NULL, 0, &tmpLocaleID); |
michael@0 | 1584 | if(_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1585 | _getVariant(tmpLocaleID+1, *tmpLocaleID, NULL, 0); |
michael@0 | 1586 | } |
michael@0 | 1587 | } |
michael@0 | 1588 | } |
michael@0 | 1589 | |
michael@0 | 1590 | /* keywords are located after '@' */ |
michael@0 | 1591 | if((tmpLocaleID = locale_getKeywordsStart(tmpLocaleID)) != NULL) { |
michael@0 | 1592 | i=locale_getKeywords(tmpLocaleID+1, '@', keywords, keywordsCapacity, NULL, 0, NULL, FALSE, status); |
michael@0 | 1593 | } |
michael@0 | 1594 | |
michael@0 | 1595 | if(i) { |
michael@0 | 1596 | return uloc_openKeywordList(keywords, i, status); |
michael@0 | 1597 | } else { |
michael@0 | 1598 | return NULL; |
michael@0 | 1599 | } |
michael@0 | 1600 | } |
michael@0 | 1601 | |
michael@0 | 1602 | |
michael@0 | 1603 | /* bit-flags for 'options' parameter of _canonicalize */ |
michael@0 | 1604 | #define _ULOC_STRIP_KEYWORDS 0x2 |
michael@0 | 1605 | #define _ULOC_CANONICALIZE 0x1 |
michael@0 | 1606 | |
michael@0 | 1607 | #define OPTION_SET(options, mask) ((options & mask) != 0) |
michael@0 | 1608 | |
michael@0 | 1609 | static const char i_default[] = {'i', '-', 'd', 'e', 'f', 'a', 'u', 'l', 't'}; |
michael@0 | 1610 | #define I_DEFAULT_LENGTH (sizeof i_default / sizeof i_default[0]) |
michael@0 | 1611 | |
michael@0 | 1612 | /** |
michael@0 | 1613 | * Canonicalize the given localeID, to level 1 or to level 2, |
michael@0 | 1614 | * depending on the options. To specify level 1, pass in options=0. |
michael@0 | 1615 | * To specify level 2, pass in options=_ULOC_CANONICALIZE. |
michael@0 | 1616 | * |
michael@0 | 1617 | * This is the code underlying uloc_getName and uloc_canonicalize. |
michael@0 | 1618 | */ |
michael@0 | 1619 | static int32_t |
michael@0 | 1620 | _canonicalize(const char* localeID, |
michael@0 | 1621 | char* result, |
michael@0 | 1622 | int32_t resultCapacity, |
michael@0 | 1623 | uint32_t options, |
michael@0 | 1624 | UErrorCode* err) { |
michael@0 | 1625 | int32_t j, len, fieldCount=0, scriptSize=0, variantSize=0, nameCapacity; |
michael@0 | 1626 | char localeBuffer[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 1627 | char tempBuffer[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 1628 | const char* origLocaleID; |
michael@0 | 1629 | const char* tmpLocaleID; |
michael@0 | 1630 | const char* keywordAssign = NULL; |
michael@0 | 1631 | const char* separatorIndicator = NULL; |
michael@0 | 1632 | const char* addKeyword = NULL; |
michael@0 | 1633 | const char* addValue = NULL; |
michael@0 | 1634 | char* name; |
michael@0 | 1635 | char* variant = NULL; /* pointer into name, or NULL */ |
michael@0 | 1636 | |
michael@0 | 1637 | if (U_FAILURE(*err)) { |
michael@0 | 1638 | return 0; |
michael@0 | 1639 | } |
michael@0 | 1640 | |
michael@0 | 1641 | if (_hasBCP47Extension(localeID)) { |
michael@0 | 1642 | _ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), err); |
michael@0 | 1643 | } else { |
michael@0 | 1644 | if (localeID==NULL) { |
michael@0 | 1645 | localeID=uloc_getDefault(); |
michael@0 | 1646 | } |
michael@0 | 1647 | tmpLocaleID=localeID; |
michael@0 | 1648 | } |
michael@0 | 1649 | |
michael@0 | 1650 | origLocaleID=tmpLocaleID; |
michael@0 | 1651 | |
michael@0 | 1652 | /* if we are doing a full canonicalization, then put results in |
michael@0 | 1653 | localeBuffer, if necessary; otherwise send them to result. */ |
michael@0 | 1654 | if (/*OPTION_SET(options, _ULOC_CANONICALIZE) &&*/ |
michael@0 | 1655 | (result == NULL || resultCapacity < (int32_t)sizeof(localeBuffer))) { |
michael@0 | 1656 | name = localeBuffer; |
michael@0 | 1657 | nameCapacity = (int32_t)sizeof(localeBuffer); |
michael@0 | 1658 | } else { |
michael@0 | 1659 | name = result; |
michael@0 | 1660 | nameCapacity = resultCapacity; |
michael@0 | 1661 | } |
michael@0 | 1662 | |
michael@0 | 1663 | /* get all pieces, one after another, and separate with '_' */ |
michael@0 | 1664 | len=ulocimp_getLanguage(tmpLocaleID, name, nameCapacity, &tmpLocaleID); |
michael@0 | 1665 | |
michael@0 | 1666 | if(len == I_DEFAULT_LENGTH && uprv_strncmp(origLocaleID, i_default, len) == 0) { |
michael@0 | 1667 | const char *d = uloc_getDefault(); |
michael@0 | 1668 | |
michael@0 | 1669 | len = (int32_t)uprv_strlen(d); |
michael@0 | 1670 | |
michael@0 | 1671 | if (name != NULL) { |
michael@0 | 1672 | uprv_strncpy(name, d, len); |
michael@0 | 1673 | } |
michael@0 | 1674 | } else if(_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1675 | const char *scriptID; |
michael@0 | 1676 | |
michael@0 | 1677 | ++fieldCount; |
michael@0 | 1678 | if(len<nameCapacity) { |
michael@0 | 1679 | name[len]='_'; |
michael@0 | 1680 | } |
michael@0 | 1681 | ++len; |
michael@0 | 1682 | |
michael@0 | 1683 | scriptSize=ulocimp_getScript(tmpLocaleID+1, |
michael@0 | 1684 | (len<nameCapacity ? name+len : NULL), nameCapacity-len, &scriptID); |
michael@0 | 1685 | if(scriptSize > 0) { |
michael@0 | 1686 | /* Found optional script */ |
michael@0 | 1687 | tmpLocaleID = scriptID; |
michael@0 | 1688 | ++fieldCount; |
michael@0 | 1689 | len+=scriptSize; |
michael@0 | 1690 | if (_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1691 | /* If there is something else, then we add the _ */ |
michael@0 | 1692 | if(len<nameCapacity) { |
michael@0 | 1693 | name[len]='_'; |
michael@0 | 1694 | } |
michael@0 | 1695 | ++len; |
michael@0 | 1696 | } |
michael@0 | 1697 | } |
michael@0 | 1698 | |
michael@0 | 1699 | if (_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1700 | const char *cntryID; |
michael@0 | 1701 | int32_t cntrySize = ulocimp_getCountry(tmpLocaleID+1, |
michael@0 | 1702 | (len<nameCapacity ? name+len : NULL), nameCapacity-len, &cntryID); |
michael@0 | 1703 | if (cntrySize > 0) { |
michael@0 | 1704 | /* Found optional country */ |
michael@0 | 1705 | tmpLocaleID = cntryID; |
michael@0 | 1706 | len+=cntrySize; |
michael@0 | 1707 | } |
michael@0 | 1708 | if(_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1709 | /* If there is something else, then we add the _ if we found country before. */ |
michael@0 | 1710 | if (cntrySize >= 0 && ! _isIDSeparator(*(tmpLocaleID+1)) ) { |
michael@0 | 1711 | ++fieldCount; |
michael@0 | 1712 | if(len<nameCapacity) { |
michael@0 | 1713 | name[len]='_'; |
michael@0 | 1714 | } |
michael@0 | 1715 | ++len; |
michael@0 | 1716 | } |
michael@0 | 1717 | |
michael@0 | 1718 | variantSize = _getVariant(tmpLocaleID+1, *tmpLocaleID, |
michael@0 | 1719 | (len<nameCapacity ? name+len : NULL), nameCapacity-len); |
michael@0 | 1720 | if (variantSize > 0) { |
michael@0 | 1721 | variant = len<nameCapacity ? name+len : NULL; |
michael@0 | 1722 | len += variantSize; |
michael@0 | 1723 | tmpLocaleID += variantSize + 1; /* skip '_' and variant */ |
michael@0 | 1724 | } |
michael@0 | 1725 | } |
michael@0 | 1726 | } |
michael@0 | 1727 | } |
michael@0 | 1728 | |
michael@0 | 1729 | /* Copy POSIX-style charset specifier, if any [mr.utf8] */ |
michael@0 | 1730 | if (!OPTION_SET(options, _ULOC_CANONICALIZE) && *tmpLocaleID == '.') { |
michael@0 | 1731 | UBool done = FALSE; |
michael@0 | 1732 | do { |
michael@0 | 1733 | char c = *tmpLocaleID; |
michael@0 | 1734 | switch (c) { |
michael@0 | 1735 | case 0: |
michael@0 | 1736 | case '@': |
michael@0 | 1737 | done = TRUE; |
michael@0 | 1738 | break; |
michael@0 | 1739 | default: |
michael@0 | 1740 | if (len<nameCapacity) { |
michael@0 | 1741 | name[len] = c; |
michael@0 | 1742 | } |
michael@0 | 1743 | ++len; |
michael@0 | 1744 | ++tmpLocaleID; |
michael@0 | 1745 | break; |
michael@0 | 1746 | } |
michael@0 | 1747 | } while (!done); |
michael@0 | 1748 | } |
michael@0 | 1749 | |
michael@0 | 1750 | /* Scan ahead to next '@' and determine if it is followed by '=' and/or ';' |
michael@0 | 1751 | After this, tmpLocaleID either points to '@' or is NULL */ |
michael@0 | 1752 | if ((tmpLocaleID=locale_getKeywordsStart(tmpLocaleID))!=NULL) { |
michael@0 | 1753 | keywordAssign = uprv_strchr(tmpLocaleID, '='); |
michael@0 | 1754 | separatorIndicator = uprv_strchr(tmpLocaleID, ';'); |
michael@0 | 1755 | } |
michael@0 | 1756 | |
michael@0 | 1757 | /* Copy POSIX-style variant, if any [mr@FOO] */ |
michael@0 | 1758 | if (!OPTION_SET(options, _ULOC_CANONICALIZE) && |
michael@0 | 1759 | tmpLocaleID != NULL && keywordAssign == NULL) { |
michael@0 | 1760 | for (;;) { |
michael@0 | 1761 | char c = *tmpLocaleID; |
michael@0 | 1762 | if (c == 0) { |
michael@0 | 1763 | break; |
michael@0 | 1764 | } |
michael@0 | 1765 | if (len<nameCapacity) { |
michael@0 | 1766 | name[len] = c; |
michael@0 | 1767 | } |
michael@0 | 1768 | ++len; |
michael@0 | 1769 | ++tmpLocaleID; |
michael@0 | 1770 | } |
michael@0 | 1771 | } |
michael@0 | 1772 | |
michael@0 | 1773 | if (OPTION_SET(options, _ULOC_CANONICALIZE)) { |
michael@0 | 1774 | /* Handle @FOO variant if @ is present and not followed by = */ |
michael@0 | 1775 | if (tmpLocaleID!=NULL && keywordAssign==NULL) { |
michael@0 | 1776 | int32_t posixVariantSize; |
michael@0 | 1777 | /* Add missing '_' if needed */ |
michael@0 | 1778 | if (fieldCount < 2 || (fieldCount < 3 && scriptSize > 0)) { |
michael@0 | 1779 | do { |
michael@0 | 1780 | if(len<nameCapacity) { |
michael@0 | 1781 | name[len]='_'; |
michael@0 | 1782 | } |
michael@0 | 1783 | ++len; |
michael@0 | 1784 | ++fieldCount; |
michael@0 | 1785 | } while(fieldCount<2); |
michael@0 | 1786 | } |
michael@0 | 1787 | posixVariantSize = _getVariantEx(tmpLocaleID+1, '@', name+len, nameCapacity-len, |
michael@0 | 1788 | (UBool)(variantSize > 0)); |
michael@0 | 1789 | if (posixVariantSize > 0) { |
michael@0 | 1790 | if (variant == NULL) { |
michael@0 | 1791 | variant = name+len; |
michael@0 | 1792 | } |
michael@0 | 1793 | len += posixVariantSize; |
michael@0 | 1794 | variantSize += posixVariantSize; |
michael@0 | 1795 | } |
michael@0 | 1796 | } |
michael@0 | 1797 | |
michael@0 | 1798 | /* Handle generic variants first */ |
michael@0 | 1799 | if (variant) { |
michael@0 | 1800 | for (j=0; j<(int32_t)(sizeof(VARIANT_MAP)/sizeof(VARIANT_MAP[0])); j++) { |
michael@0 | 1801 | const char* variantToCompare = VARIANT_MAP[j].variant; |
michael@0 | 1802 | int32_t n = (int32_t)uprv_strlen(variantToCompare); |
michael@0 | 1803 | int32_t variantLen = _deleteVariant(variant, uprv_min(variantSize, (nameCapacity-len)), variantToCompare, n); |
michael@0 | 1804 | len -= variantLen; |
michael@0 | 1805 | if (variantLen > 0) { |
michael@0 | 1806 | if (len > 0 && name[len-1] == '_') { /* delete trailing '_' */ |
michael@0 | 1807 | --len; |
michael@0 | 1808 | } |
michael@0 | 1809 | addKeyword = VARIANT_MAP[j].keyword; |
michael@0 | 1810 | addValue = VARIANT_MAP[j].value; |
michael@0 | 1811 | break; |
michael@0 | 1812 | } |
michael@0 | 1813 | } |
michael@0 | 1814 | if (len > 0 && len <= nameCapacity && name[len-1] == '_') { /* delete trailing '_' */ |
michael@0 | 1815 | --len; |
michael@0 | 1816 | } |
michael@0 | 1817 | } |
michael@0 | 1818 | |
michael@0 | 1819 | /* Look up the ID in the canonicalization map */ |
michael@0 | 1820 | for (j=0; j<(int32_t)(sizeof(CANONICALIZE_MAP)/sizeof(CANONICALIZE_MAP[0])); j++) { |
michael@0 | 1821 | const char* id = CANONICALIZE_MAP[j].id; |
michael@0 | 1822 | int32_t n = (int32_t)uprv_strlen(id); |
michael@0 | 1823 | if (len == n && uprv_strncmp(name, id, n) == 0) { |
michael@0 | 1824 | if (n == 0 && tmpLocaleID != NULL) { |
michael@0 | 1825 | break; /* Don't remap "" if keywords present */ |
michael@0 | 1826 | } |
michael@0 | 1827 | len = _copyCount(name, nameCapacity, CANONICALIZE_MAP[j].canonicalID); |
michael@0 | 1828 | if (CANONICALIZE_MAP[j].keyword) { |
michael@0 | 1829 | addKeyword = CANONICALIZE_MAP[j].keyword; |
michael@0 | 1830 | addValue = CANONICALIZE_MAP[j].value; |
michael@0 | 1831 | } |
michael@0 | 1832 | break; |
michael@0 | 1833 | } |
michael@0 | 1834 | } |
michael@0 | 1835 | } |
michael@0 | 1836 | |
michael@0 | 1837 | if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) { |
michael@0 | 1838 | if (tmpLocaleID!=NULL && keywordAssign!=NULL && |
michael@0 | 1839 | (!separatorIndicator || separatorIndicator > keywordAssign)) { |
michael@0 | 1840 | if(len<nameCapacity) { |
michael@0 | 1841 | name[len]='@'; |
michael@0 | 1842 | } |
michael@0 | 1843 | ++len; |
michael@0 | 1844 | ++fieldCount; |
michael@0 | 1845 | len += _getKeywords(tmpLocaleID+1, '@', (len<nameCapacity ? name+len : NULL), nameCapacity-len, |
michael@0 | 1846 | NULL, 0, NULL, TRUE, addKeyword, addValue, err); |
michael@0 | 1847 | } else if (addKeyword != NULL) { |
michael@0 | 1848 | U_ASSERT(addValue != NULL && len < nameCapacity); |
michael@0 | 1849 | /* inelegant but works -- later make _getKeywords do this? */ |
michael@0 | 1850 | len += _copyCount(name+len, nameCapacity-len, "@"); |
michael@0 | 1851 | len += _copyCount(name+len, nameCapacity-len, addKeyword); |
michael@0 | 1852 | len += _copyCount(name+len, nameCapacity-len, "="); |
michael@0 | 1853 | len += _copyCount(name+len, nameCapacity-len, addValue); |
michael@0 | 1854 | } |
michael@0 | 1855 | } |
michael@0 | 1856 | |
michael@0 | 1857 | if (U_SUCCESS(*err) && result != NULL && name == localeBuffer) { |
michael@0 | 1858 | uprv_strncpy(result, localeBuffer, (len > resultCapacity) ? resultCapacity : len); |
michael@0 | 1859 | } |
michael@0 | 1860 | |
michael@0 | 1861 | return u_terminateChars(result, resultCapacity, len, err); |
michael@0 | 1862 | } |
michael@0 | 1863 | |
michael@0 | 1864 | /* ### ID parsing API **************************************************/ |
michael@0 | 1865 | |
michael@0 | 1866 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 1867 | uloc_getParent(const char* localeID, |
michael@0 | 1868 | char* parent, |
michael@0 | 1869 | int32_t parentCapacity, |
michael@0 | 1870 | UErrorCode* err) |
michael@0 | 1871 | { |
michael@0 | 1872 | const char *lastUnderscore; |
michael@0 | 1873 | int32_t i; |
michael@0 | 1874 | |
michael@0 | 1875 | if (U_FAILURE(*err)) |
michael@0 | 1876 | return 0; |
michael@0 | 1877 | |
michael@0 | 1878 | if (localeID == NULL) |
michael@0 | 1879 | localeID = uloc_getDefault(); |
michael@0 | 1880 | |
michael@0 | 1881 | lastUnderscore=uprv_strrchr(localeID, '_'); |
michael@0 | 1882 | if(lastUnderscore!=NULL) { |
michael@0 | 1883 | i=(int32_t)(lastUnderscore-localeID); |
michael@0 | 1884 | } else { |
michael@0 | 1885 | i=0; |
michael@0 | 1886 | } |
michael@0 | 1887 | |
michael@0 | 1888 | if(i>0 && parent != localeID) { |
michael@0 | 1889 | uprv_memcpy(parent, localeID, uprv_min(i, parentCapacity)); |
michael@0 | 1890 | } |
michael@0 | 1891 | return u_terminateChars(parent, parentCapacity, i, err); |
michael@0 | 1892 | } |
michael@0 | 1893 | |
michael@0 | 1894 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 1895 | uloc_getLanguage(const char* localeID, |
michael@0 | 1896 | char* language, |
michael@0 | 1897 | int32_t languageCapacity, |
michael@0 | 1898 | UErrorCode* err) |
michael@0 | 1899 | { |
michael@0 | 1900 | /* uloc_getLanguage will return a 2 character iso-639 code if one exists. *CWB*/ |
michael@0 | 1901 | int32_t i=0; |
michael@0 | 1902 | |
michael@0 | 1903 | if (err==NULL || U_FAILURE(*err)) { |
michael@0 | 1904 | return 0; |
michael@0 | 1905 | } |
michael@0 | 1906 | |
michael@0 | 1907 | if(localeID==NULL) { |
michael@0 | 1908 | localeID=uloc_getDefault(); |
michael@0 | 1909 | } |
michael@0 | 1910 | |
michael@0 | 1911 | i=ulocimp_getLanguage(localeID, language, languageCapacity, NULL); |
michael@0 | 1912 | return u_terminateChars(language, languageCapacity, i, err); |
michael@0 | 1913 | } |
michael@0 | 1914 | |
michael@0 | 1915 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 1916 | uloc_getScript(const char* localeID, |
michael@0 | 1917 | char* script, |
michael@0 | 1918 | int32_t scriptCapacity, |
michael@0 | 1919 | UErrorCode* err) |
michael@0 | 1920 | { |
michael@0 | 1921 | int32_t i=0; |
michael@0 | 1922 | |
michael@0 | 1923 | if(err==NULL || U_FAILURE(*err)) { |
michael@0 | 1924 | return 0; |
michael@0 | 1925 | } |
michael@0 | 1926 | |
michael@0 | 1927 | if(localeID==NULL) { |
michael@0 | 1928 | localeID=uloc_getDefault(); |
michael@0 | 1929 | } |
michael@0 | 1930 | |
michael@0 | 1931 | /* skip the language */ |
michael@0 | 1932 | ulocimp_getLanguage(localeID, NULL, 0, &localeID); |
michael@0 | 1933 | if(_isIDSeparator(*localeID)) { |
michael@0 | 1934 | i=ulocimp_getScript(localeID+1, script, scriptCapacity, NULL); |
michael@0 | 1935 | } |
michael@0 | 1936 | return u_terminateChars(script, scriptCapacity, i, err); |
michael@0 | 1937 | } |
michael@0 | 1938 | |
michael@0 | 1939 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 1940 | uloc_getCountry(const char* localeID, |
michael@0 | 1941 | char* country, |
michael@0 | 1942 | int32_t countryCapacity, |
michael@0 | 1943 | UErrorCode* err) |
michael@0 | 1944 | { |
michael@0 | 1945 | int32_t i=0; |
michael@0 | 1946 | |
michael@0 | 1947 | if(err==NULL || U_FAILURE(*err)) { |
michael@0 | 1948 | return 0; |
michael@0 | 1949 | } |
michael@0 | 1950 | |
michael@0 | 1951 | if(localeID==NULL) { |
michael@0 | 1952 | localeID=uloc_getDefault(); |
michael@0 | 1953 | } |
michael@0 | 1954 | |
michael@0 | 1955 | /* Skip the language */ |
michael@0 | 1956 | ulocimp_getLanguage(localeID, NULL, 0, &localeID); |
michael@0 | 1957 | if(_isIDSeparator(*localeID)) { |
michael@0 | 1958 | const char *scriptID; |
michael@0 | 1959 | /* Skip the script if available */ |
michael@0 | 1960 | ulocimp_getScript(localeID+1, NULL, 0, &scriptID); |
michael@0 | 1961 | if(scriptID != localeID+1) { |
michael@0 | 1962 | /* Found optional script */ |
michael@0 | 1963 | localeID = scriptID; |
michael@0 | 1964 | } |
michael@0 | 1965 | if(_isIDSeparator(*localeID)) { |
michael@0 | 1966 | i=ulocimp_getCountry(localeID+1, country, countryCapacity, NULL); |
michael@0 | 1967 | } |
michael@0 | 1968 | } |
michael@0 | 1969 | return u_terminateChars(country, countryCapacity, i, err); |
michael@0 | 1970 | } |
michael@0 | 1971 | |
michael@0 | 1972 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 1973 | uloc_getVariant(const char* localeID, |
michael@0 | 1974 | char* variant, |
michael@0 | 1975 | int32_t variantCapacity, |
michael@0 | 1976 | UErrorCode* err) |
michael@0 | 1977 | { |
michael@0 | 1978 | char tempBuffer[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 1979 | const char* tmpLocaleID; |
michael@0 | 1980 | int32_t i=0; |
michael@0 | 1981 | |
michael@0 | 1982 | if(err==NULL || U_FAILURE(*err)) { |
michael@0 | 1983 | return 0; |
michael@0 | 1984 | } |
michael@0 | 1985 | |
michael@0 | 1986 | if (_hasBCP47Extension(localeID)) { |
michael@0 | 1987 | _ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), err); |
michael@0 | 1988 | } else { |
michael@0 | 1989 | if (localeID==NULL) { |
michael@0 | 1990 | localeID=uloc_getDefault(); |
michael@0 | 1991 | } |
michael@0 | 1992 | tmpLocaleID=localeID; |
michael@0 | 1993 | } |
michael@0 | 1994 | |
michael@0 | 1995 | /* Skip the language */ |
michael@0 | 1996 | ulocimp_getLanguage(tmpLocaleID, NULL, 0, &tmpLocaleID); |
michael@0 | 1997 | if(_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 1998 | const char *scriptID; |
michael@0 | 1999 | /* Skip the script if available */ |
michael@0 | 2000 | ulocimp_getScript(tmpLocaleID+1, NULL, 0, &scriptID); |
michael@0 | 2001 | if(scriptID != tmpLocaleID+1) { |
michael@0 | 2002 | /* Found optional script */ |
michael@0 | 2003 | tmpLocaleID = scriptID; |
michael@0 | 2004 | } |
michael@0 | 2005 | /* Skip the Country */ |
michael@0 | 2006 | if (_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 2007 | const char *cntryID; |
michael@0 | 2008 | ulocimp_getCountry(tmpLocaleID+1, NULL, 0, &cntryID); |
michael@0 | 2009 | if (cntryID != tmpLocaleID+1) { |
michael@0 | 2010 | /* Found optional country */ |
michael@0 | 2011 | tmpLocaleID = cntryID; |
michael@0 | 2012 | } |
michael@0 | 2013 | if(_isIDSeparator(*tmpLocaleID)) { |
michael@0 | 2014 | /* If there was no country ID, skip a possible extra IDSeparator */ |
michael@0 | 2015 | if (tmpLocaleID != cntryID && _isIDSeparator(tmpLocaleID[1])) { |
michael@0 | 2016 | tmpLocaleID++; |
michael@0 | 2017 | } |
michael@0 | 2018 | i=_getVariant(tmpLocaleID+1, *tmpLocaleID, variant, variantCapacity); |
michael@0 | 2019 | } |
michael@0 | 2020 | } |
michael@0 | 2021 | } |
michael@0 | 2022 | |
michael@0 | 2023 | /* removed by weiv. We don't want to handle POSIX variants anymore. Use canonicalization function */ |
michael@0 | 2024 | /* if we do not have a variant tag yet then try a POSIX variant after '@' */ |
michael@0 | 2025 | /* |
michael@0 | 2026 | if(!haveVariant && (localeID=uprv_strrchr(localeID, '@'))!=NULL) { |
michael@0 | 2027 | i=_getVariant(localeID+1, '@', variant, variantCapacity); |
michael@0 | 2028 | } |
michael@0 | 2029 | */ |
michael@0 | 2030 | return u_terminateChars(variant, variantCapacity, i, err); |
michael@0 | 2031 | } |
michael@0 | 2032 | |
michael@0 | 2033 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 2034 | uloc_getName(const char* localeID, |
michael@0 | 2035 | char* name, |
michael@0 | 2036 | int32_t nameCapacity, |
michael@0 | 2037 | UErrorCode* err) |
michael@0 | 2038 | { |
michael@0 | 2039 | return _canonicalize(localeID, name, nameCapacity, 0, err); |
michael@0 | 2040 | } |
michael@0 | 2041 | |
michael@0 | 2042 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 2043 | uloc_getBaseName(const char* localeID, |
michael@0 | 2044 | char* name, |
michael@0 | 2045 | int32_t nameCapacity, |
michael@0 | 2046 | UErrorCode* err) |
michael@0 | 2047 | { |
michael@0 | 2048 | return _canonicalize(localeID, name, nameCapacity, _ULOC_STRIP_KEYWORDS, err); |
michael@0 | 2049 | } |
michael@0 | 2050 | |
michael@0 | 2051 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 2052 | uloc_canonicalize(const char* localeID, |
michael@0 | 2053 | char* name, |
michael@0 | 2054 | int32_t nameCapacity, |
michael@0 | 2055 | UErrorCode* err) |
michael@0 | 2056 | { |
michael@0 | 2057 | return _canonicalize(localeID, name, nameCapacity, _ULOC_CANONICALIZE, err); |
michael@0 | 2058 | } |
michael@0 | 2059 | |
michael@0 | 2060 | U_CAPI const char* U_EXPORT2 |
michael@0 | 2061 | uloc_getISO3Language(const char* localeID) |
michael@0 | 2062 | { |
michael@0 | 2063 | int16_t offset; |
michael@0 | 2064 | char lang[ULOC_LANG_CAPACITY]; |
michael@0 | 2065 | UErrorCode err = U_ZERO_ERROR; |
michael@0 | 2066 | |
michael@0 | 2067 | if (localeID == NULL) |
michael@0 | 2068 | { |
michael@0 | 2069 | localeID = uloc_getDefault(); |
michael@0 | 2070 | } |
michael@0 | 2071 | uloc_getLanguage(localeID, lang, ULOC_LANG_CAPACITY, &err); |
michael@0 | 2072 | if (U_FAILURE(err)) |
michael@0 | 2073 | return ""; |
michael@0 | 2074 | offset = _findIndex(LANGUAGES, lang); |
michael@0 | 2075 | if (offset < 0) |
michael@0 | 2076 | return ""; |
michael@0 | 2077 | return LANGUAGES_3[offset]; |
michael@0 | 2078 | } |
michael@0 | 2079 | |
michael@0 | 2080 | U_CAPI const char* U_EXPORT2 |
michael@0 | 2081 | uloc_getISO3Country(const char* localeID) |
michael@0 | 2082 | { |
michael@0 | 2083 | int16_t offset; |
michael@0 | 2084 | char cntry[ULOC_LANG_CAPACITY]; |
michael@0 | 2085 | UErrorCode err = U_ZERO_ERROR; |
michael@0 | 2086 | |
michael@0 | 2087 | if (localeID == NULL) |
michael@0 | 2088 | { |
michael@0 | 2089 | localeID = uloc_getDefault(); |
michael@0 | 2090 | } |
michael@0 | 2091 | uloc_getCountry(localeID, cntry, ULOC_LANG_CAPACITY, &err); |
michael@0 | 2092 | if (U_FAILURE(err)) |
michael@0 | 2093 | return ""; |
michael@0 | 2094 | offset = _findIndex(COUNTRIES, cntry); |
michael@0 | 2095 | if (offset < 0) |
michael@0 | 2096 | return ""; |
michael@0 | 2097 | |
michael@0 | 2098 | return COUNTRIES_3[offset]; |
michael@0 | 2099 | } |
michael@0 | 2100 | |
michael@0 | 2101 | U_CAPI uint32_t U_EXPORT2 |
michael@0 | 2102 | uloc_getLCID(const char* localeID) |
michael@0 | 2103 | { |
michael@0 | 2104 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 2105 | char langID[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 2106 | |
michael@0 | 2107 | uloc_getLanguage(localeID, langID, sizeof(langID), &status); |
michael@0 | 2108 | if (U_FAILURE(status)) { |
michael@0 | 2109 | return 0; |
michael@0 | 2110 | } |
michael@0 | 2111 | |
michael@0 | 2112 | if (uprv_strchr(localeID, '@')) { |
michael@0 | 2113 | // uprv_convertToLCID does not support keywords other than collation. |
michael@0 | 2114 | // Remove all keywords except collation. |
michael@0 | 2115 | int32_t len; |
michael@0 | 2116 | char collVal[ULOC_KEYWORDS_CAPACITY]; |
michael@0 | 2117 | char tmpLocaleID[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 2118 | |
michael@0 | 2119 | len = uloc_getKeywordValue(localeID, "collation", collVal, |
michael@0 | 2120 | sizeof(collVal)/sizeof(collVal[0]) - 1, &status); |
michael@0 | 2121 | |
michael@0 | 2122 | if (U_SUCCESS(status) && len > 0) { |
michael@0 | 2123 | collVal[len] = 0; |
michael@0 | 2124 | |
michael@0 | 2125 | len = uloc_getBaseName(localeID, tmpLocaleID, |
michael@0 | 2126 | sizeof(tmpLocaleID)/sizeof(tmpLocaleID[0]) - 1, &status); |
michael@0 | 2127 | |
michael@0 | 2128 | if (U_SUCCESS(status)) { |
michael@0 | 2129 | tmpLocaleID[len] = 0; |
michael@0 | 2130 | |
michael@0 | 2131 | len = uloc_setKeywordValue("collation", collVal, tmpLocaleID, |
michael@0 | 2132 | sizeof(tmpLocaleID)/sizeof(tmpLocaleID[0]) - len - 1, &status); |
michael@0 | 2133 | |
michael@0 | 2134 | if (U_SUCCESS(status)) { |
michael@0 | 2135 | tmpLocaleID[len] = 0; |
michael@0 | 2136 | return uprv_convertToLCID(langID, tmpLocaleID, &status); |
michael@0 | 2137 | } |
michael@0 | 2138 | } |
michael@0 | 2139 | } |
michael@0 | 2140 | |
michael@0 | 2141 | // fall through - all keywords are simply ignored |
michael@0 | 2142 | status = U_ZERO_ERROR; |
michael@0 | 2143 | } |
michael@0 | 2144 | |
michael@0 | 2145 | return uprv_convertToLCID(langID, localeID, &status); |
michael@0 | 2146 | } |
michael@0 | 2147 | |
michael@0 | 2148 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 2149 | uloc_getLocaleForLCID(uint32_t hostid, char *locale, int32_t localeCapacity, |
michael@0 | 2150 | UErrorCode *status) |
michael@0 | 2151 | { |
michael@0 | 2152 | return uprv_convertToPosix(hostid, locale, localeCapacity, status); |
michael@0 | 2153 | } |
michael@0 | 2154 | |
michael@0 | 2155 | /* ### Default locale **************************************************/ |
michael@0 | 2156 | |
michael@0 | 2157 | U_CAPI const char* U_EXPORT2 |
michael@0 | 2158 | uloc_getDefault() |
michael@0 | 2159 | { |
michael@0 | 2160 | return locale_get_default(); |
michael@0 | 2161 | } |
michael@0 | 2162 | |
michael@0 | 2163 | U_CAPI void U_EXPORT2 |
michael@0 | 2164 | uloc_setDefault(const char* newDefaultLocale, |
michael@0 | 2165 | UErrorCode* err) |
michael@0 | 2166 | { |
michael@0 | 2167 | if (U_FAILURE(*err)) |
michael@0 | 2168 | return; |
michael@0 | 2169 | /* the error code isn't currently used for anything by this function*/ |
michael@0 | 2170 | |
michael@0 | 2171 | /* propagate change to C++ */ |
michael@0 | 2172 | locale_set_default(newDefaultLocale); |
michael@0 | 2173 | } |
michael@0 | 2174 | |
michael@0 | 2175 | /** |
michael@0 | 2176 | * Returns a list of all 2-letter language codes defined in ISO 639. This is a pointer |
michael@0 | 2177 | * to an array of pointers to arrays of char. All of these pointers are owned |
michael@0 | 2178 | * by ICU-- do not delete them, and do not write through them. The array is |
michael@0 | 2179 | * terminated with a null pointer. |
michael@0 | 2180 | */ |
michael@0 | 2181 | U_CAPI const char* const* U_EXPORT2 |
michael@0 | 2182 | uloc_getISOLanguages() |
michael@0 | 2183 | { |
michael@0 | 2184 | return LANGUAGES; |
michael@0 | 2185 | } |
michael@0 | 2186 | |
michael@0 | 2187 | /** |
michael@0 | 2188 | * Returns a list of all 2-letter country codes defined in ISO 639. This is a |
michael@0 | 2189 | * pointer to an array of pointers to arrays of char. All of these pointers are |
michael@0 | 2190 | * owned by ICU-- do not delete them, and do not write through them. The array is |
michael@0 | 2191 | * terminated with a null pointer. |
michael@0 | 2192 | */ |
michael@0 | 2193 | U_CAPI const char* const* U_EXPORT2 |
michael@0 | 2194 | uloc_getISOCountries() |
michael@0 | 2195 | { |
michael@0 | 2196 | return COUNTRIES; |
michael@0 | 2197 | } |
michael@0 | 2198 | |
michael@0 | 2199 | |
michael@0 | 2200 | /* this function to be moved into cstring.c later */ |
michael@0 | 2201 | static char gDecimal = 0; |
michael@0 | 2202 | |
michael@0 | 2203 | static /* U_CAPI */ |
michael@0 | 2204 | double |
michael@0 | 2205 | /* U_EXPORT2 */ |
michael@0 | 2206 | _uloc_strtod(const char *start, char **end) { |
michael@0 | 2207 | char *decimal; |
michael@0 | 2208 | char *myEnd; |
michael@0 | 2209 | char buf[30]; |
michael@0 | 2210 | double rv; |
michael@0 | 2211 | if (!gDecimal) { |
michael@0 | 2212 | char rep[5]; |
michael@0 | 2213 | /* For machines that decide to change the decimal on you, |
michael@0 | 2214 | and try to be too smart with localization. |
michael@0 | 2215 | This normally should be just a '.'. */ |
michael@0 | 2216 | sprintf(rep, "%+1.1f", 1.0); |
michael@0 | 2217 | gDecimal = rep[2]; |
michael@0 | 2218 | } |
michael@0 | 2219 | |
michael@0 | 2220 | if(gDecimal == '.') { |
michael@0 | 2221 | return uprv_strtod(start, end); /* fall through to OS */ |
michael@0 | 2222 | } else { |
michael@0 | 2223 | uprv_strncpy(buf, start, 29); |
michael@0 | 2224 | buf[29]=0; |
michael@0 | 2225 | decimal = uprv_strchr(buf, '.'); |
michael@0 | 2226 | if(decimal) { |
michael@0 | 2227 | *decimal = gDecimal; |
michael@0 | 2228 | } else { |
michael@0 | 2229 | return uprv_strtod(start, end); /* no decimal point */ |
michael@0 | 2230 | } |
michael@0 | 2231 | rv = uprv_strtod(buf, &myEnd); |
michael@0 | 2232 | if(end) { |
michael@0 | 2233 | *end = (char*)(start+(myEnd-buf)); /* cast away const (to follow uprv_strtod API.) */ |
michael@0 | 2234 | } |
michael@0 | 2235 | return rv; |
michael@0 | 2236 | } |
michael@0 | 2237 | } |
michael@0 | 2238 | |
michael@0 | 2239 | typedef struct { |
michael@0 | 2240 | float q; |
michael@0 | 2241 | int32_t dummy; /* to avoid uninitialized memory copy from qsort */ |
michael@0 | 2242 | char *locale; |
michael@0 | 2243 | } _acceptLangItem; |
michael@0 | 2244 | |
michael@0 | 2245 | static int32_t U_CALLCONV |
michael@0 | 2246 | uloc_acceptLanguageCompare(const void * /*context*/, const void *a, const void *b) |
michael@0 | 2247 | { |
michael@0 | 2248 | const _acceptLangItem *aa = (const _acceptLangItem*)a; |
michael@0 | 2249 | const _acceptLangItem *bb = (const _acceptLangItem*)b; |
michael@0 | 2250 | |
michael@0 | 2251 | int32_t rc = 0; |
michael@0 | 2252 | if(bb->q < aa->q) { |
michael@0 | 2253 | rc = -1; /* A > B */ |
michael@0 | 2254 | } else if(bb->q > aa->q) { |
michael@0 | 2255 | rc = 1; /* A < B */ |
michael@0 | 2256 | } else { |
michael@0 | 2257 | rc = 0; /* A = B */ |
michael@0 | 2258 | } |
michael@0 | 2259 | |
michael@0 | 2260 | if(rc==0) { |
michael@0 | 2261 | rc = uprv_stricmp(aa->locale, bb->locale); |
michael@0 | 2262 | } |
michael@0 | 2263 | |
michael@0 | 2264 | #if defined(ULOC_DEBUG) |
michael@0 | 2265 | /* fprintf(stderr, "a:[%s:%g], b:[%s:%g] -> %d\n", |
michael@0 | 2266 | aa->locale, aa->q, |
michael@0 | 2267 | bb->locale, bb->q, |
michael@0 | 2268 | rc);*/ |
michael@0 | 2269 | #endif |
michael@0 | 2270 | |
michael@0 | 2271 | return rc; |
michael@0 | 2272 | } |
michael@0 | 2273 | |
michael@0 | 2274 | /* |
michael@0 | 2275 | mt-mt, ja;q=0.76, en-us;q=0.95, en;q=0.92, en-gb;q=0.89, fr;q=0.87, iu-ca;q=0.84, iu;q=0.82, ja-jp;q=0.79, mt;q=0.97, de-de;q=0.74, de;q=0.71, es;q=0.68, it-it;q=0.66, it;q=0.63, vi-vn;q=0.61, vi;q=0.58, nl-nl;q=0.55, nl;q=0.53 |
michael@0 | 2276 | */ |
michael@0 | 2277 | |
michael@0 | 2278 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 2279 | uloc_acceptLanguageFromHTTP(char *result, int32_t resultAvailable, UAcceptResult *outResult, |
michael@0 | 2280 | const char *httpAcceptLanguage, |
michael@0 | 2281 | UEnumeration* availableLocales, |
michael@0 | 2282 | UErrorCode *status) |
michael@0 | 2283 | { |
michael@0 | 2284 | _acceptLangItem *j; |
michael@0 | 2285 | _acceptLangItem smallBuffer[30]; |
michael@0 | 2286 | char **strs; |
michael@0 | 2287 | char tmp[ULOC_FULLNAME_CAPACITY +1]; |
michael@0 | 2288 | int32_t n = 0; |
michael@0 | 2289 | const char *itemEnd; |
michael@0 | 2290 | const char *paramEnd; |
michael@0 | 2291 | const char *s; |
michael@0 | 2292 | const char *t; |
michael@0 | 2293 | int32_t res; |
michael@0 | 2294 | int32_t i; |
michael@0 | 2295 | int32_t l = (int32_t)uprv_strlen(httpAcceptLanguage); |
michael@0 | 2296 | int32_t jSize; |
michael@0 | 2297 | char *tempstr; /* Use for null pointer check */ |
michael@0 | 2298 | |
michael@0 | 2299 | j = smallBuffer; |
michael@0 | 2300 | jSize = sizeof(smallBuffer)/sizeof(smallBuffer[0]); |
michael@0 | 2301 | if(U_FAILURE(*status)) { |
michael@0 | 2302 | return -1; |
michael@0 | 2303 | } |
michael@0 | 2304 | |
michael@0 | 2305 | for(s=httpAcceptLanguage;s&&*s;) { |
michael@0 | 2306 | while(isspace(*s)) /* eat space at the beginning */ |
michael@0 | 2307 | s++; |
michael@0 | 2308 | itemEnd=uprv_strchr(s,','); |
michael@0 | 2309 | paramEnd=uprv_strchr(s,';'); |
michael@0 | 2310 | if(!itemEnd) { |
michael@0 | 2311 | itemEnd = httpAcceptLanguage+l; /* end of string */ |
michael@0 | 2312 | } |
michael@0 | 2313 | if(paramEnd && paramEnd<itemEnd) { |
michael@0 | 2314 | /* semicolon (;) is closer than end (,) */ |
michael@0 | 2315 | t = paramEnd+1; |
michael@0 | 2316 | if(*t=='q') { |
michael@0 | 2317 | t++; |
michael@0 | 2318 | } |
michael@0 | 2319 | while(isspace(*t)) { |
michael@0 | 2320 | t++; |
michael@0 | 2321 | } |
michael@0 | 2322 | if(*t=='=') { |
michael@0 | 2323 | t++; |
michael@0 | 2324 | } |
michael@0 | 2325 | while(isspace(*t)) { |
michael@0 | 2326 | t++; |
michael@0 | 2327 | } |
michael@0 | 2328 | j[n].q = (float)_uloc_strtod(t,NULL); |
michael@0 | 2329 | } else { |
michael@0 | 2330 | /* no semicolon - it's 1.0 */ |
michael@0 | 2331 | j[n].q = 1.0f; |
michael@0 | 2332 | paramEnd = itemEnd; |
michael@0 | 2333 | } |
michael@0 | 2334 | j[n].dummy=0; |
michael@0 | 2335 | /* eat spaces prior to semi */ |
michael@0 | 2336 | for(t=(paramEnd-1);(paramEnd>s)&&isspace(*t);t--) |
michael@0 | 2337 | ; |
michael@0 | 2338 | /* Check for null pointer from uprv_strndup */ |
michael@0 | 2339 | tempstr = uprv_strndup(s,(int32_t)((t+1)-s)); |
michael@0 | 2340 | if (tempstr == NULL) { |
michael@0 | 2341 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 2342 | return -1; |
michael@0 | 2343 | } |
michael@0 | 2344 | j[n].locale = tempstr; |
michael@0 | 2345 | uloc_canonicalize(j[n].locale,tmp,sizeof(tmp)/sizeof(tmp[0]),status); |
michael@0 | 2346 | if(strcmp(j[n].locale,tmp)) { |
michael@0 | 2347 | uprv_free(j[n].locale); |
michael@0 | 2348 | j[n].locale=uprv_strdup(tmp); |
michael@0 | 2349 | } |
michael@0 | 2350 | #if defined(ULOC_DEBUG) |
michael@0 | 2351 | /*fprintf(stderr,"%d: s <%s> q <%g>\n", n, j[n].locale, j[n].q);*/ |
michael@0 | 2352 | #endif |
michael@0 | 2353 | n++; |
michael@0 | 2354 | s = itemEnd; |
michael@0 | 2355 | while(*s==',') { /* eat duplicate commas */ |
michael@0 | 2356 | s++; |
michael@0 | 2357 | } |
michael@0 | 2358 | if(n>=jSize) { |
michael@0 | 2359 | if(j==smallBuffer) { /* overflowed the small buffer. */ |
michael@0 | 2360 | j = static_cast<_acceptLangItem *>(uprv_malloc(sizeof(j[0])*(jSize*2))); |
michael@0 | 2361 | if(j!=NULL) { |
michael@0 | 2362 | uprv_memcpy(j,smallBuffer,sizeof(j[0])*jSize); |
michael@0 | 2363 | } |
michael@0 | 2364 | #if defined(ULOC_DEBUG) |
michael@0 | 2365 | fprintf(stderr,"malloced at size %d\n", jSize); |
michael@0 | 2366 | #endif |
michael@0 | 2367 | } else { |
michael@0 | 2368 | j = static_cast<_acceptLangItem *>(uprv_realloc(j, sizeof(j[0])*jSize*2)); |
michael@0 | 2369 | #if defined(ULOC_DEBUG) |
michael@0 | 2370 | fprintf(stderr,"re-alloced at size %d\n", jSize); |
michael@0 | 2371 | #endif |
michael@0 | 2372 | } |
michael@0 | 2373 | jSize *= 2; |
michael@0 | 2374 | if(j==NULL) { |
michael@0 | 2375 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 2376 | return -1; |
michael@0 | 2377 | } |
michael@0 | 2378 | } |
michael@0 | 2379 | } |
michael@0 | 2380 | uprv_sortArray(j, n, sizeof(j[0]), uloc_acceptLanguageCompare, NULL, TRUE, status); |
michael@0 | 2381 | if(U_FAILURE(*status)) { |
michael@0 | 2382 | if(j != smallBuffer) { |
michael@0 | 2383 | #if defined(ULOC_DEBUG) |
michael@0 | 2384 | fprintf(stderr,"freeing j %p\n", j); |
michael@0 | 2385 | #endif |
michael@0 | 2386 | uprv_free(j); |
michael@0 | 2387 | } |
michael@0 | 2388 | return -1; |
michael@0 | 2389 | } |
michael@0 | 2390 | strs = static_cast<char **>(uprv_malloc((size_t)(sizeof(strs[0])*n))); |
michael@0 | 2391 | /* Check for null pointer */ |
michael@0 | 2392 | if (strs == NULL) { |
michael@0 | 2393 | uprv_free(j); /* Free to avoid memory leak */ |
michael@0 | 2394 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 2395 | return -1; |
michael@0 | 2396 | } |
michael@0 | 2397 | for(i=0;i<n;i++) { |
michael@0 | 2398 | #if defined(ULOC_DEBUG) |
michael@0 | 2399 | /*fprintf(stderr,"%d: s <%s> q <%g>\n", i, j[i].locale, j[i].q);*/ |
michael@0 | 2400 | #endif |
michael@0 | 2401 | strs[i]=j[i].locale; |
michael@0 | 2402 | } |
michael@0 | 2403 | res = uloc_acceptLanguage(result, resultAvailable, outResult, |
michael@0 | 2404 | (const char**)strs, n, availableLocales, status); |
michael@0 | 2405 | for(i=0;i<n;i++) { |
michael@0 | 2406 | uprv_free(strs[i]); |
michael@0 | 2407 | } |
michael@0 | 2408 | uprv_free(strs); |
michael@0 | 2409 | if(j != smallBuffer) { |
michael@0 | 2410 | #if defined(ULOC_DEBUG) |
michael@0 | 2411 | fprintf(stderr,"freeing j %p\n", j); |
michael@0 | 2412 | #endif |
michael@0 | 2413 | uprv_free(j); |
michael@0 | 2414 | } |
michael@0 | 2415 | return res; |
michael@0 | 2416 | } |
michael@0 | 2417 | |
michael@0 | 2418 | |
michael@0 | 2419 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 2420 | uloc_acceptLanguage(char *result, int32_t resultAvailable, |
michael@0 | 2421 | UAcceptResult *outResult, const char **acceptList, |
michael@0 | 2422 | int32_t acceptListCount, |
michael@0 | 2423 | UEnumeration* availableLocales, |
michael@0 | 2424 | UErrorCode *status) |
michael@0 | 2425 | { |
michael@0 | 2426 | int32_t i,j; |
michael@0 | 2427 | int32_t len; |
michael@0 | 2428 | int32_t maxLen=0; |
michael@0 | 2429 | char tmp[ULOC_FULLNAME_CAPACITY+1]; |
michael@0 | 2430 | const char *l; |
michael@0 | 2431 | char **fallbackList; |
michael@0 | 2432 | if(U_FAILURE(*status)) { |
michael@0 | 2433 | return -1; |
michael@0 | 2434 | } |
michael@0 | 2435 | fallbackList = static_cast<char **>(uprv_malloc((size_t)(sizeof(fallbackList[0])*acceptListCount))); |
michael@0 | 2436 | if(fallbackList==NULL) { |
michael@0 | 2437 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 2438 | return -1; |
michael@0 | 2439 | } |
michael@0 | 2440 | for(i=0;i<acceptListCount;i++) { |
michael@0 | 2441 | #if defined(ULOC_DEBUG) |
michael@0 | 2442 | fprintf(stderr,"%02d: %s\n", i, acceptList[i]); |
michael@0 | 2443 | #endif |
michael@0 | 2444 | while((l=uenum_next(availableLocales, NULL, status))) { |
michael@0 | 2445 | #if defined(ULOC_DEBUG) |
michael@0 | 2446 | fprintf(stderr," %s\n", l); |
michael@0 | 2447 | #endif |
michael@0 | 2448 | len = (int32_t)uprv_strlen(l); |
michael@0 | 2449 | if(!uprv_strcmp(acceptList[i], l)) { |
michael@0 | 2450 | if(outResult) { |
michael@0 | 2451 | *outResult = ULOC_ACCEPT_VALID; |
michael@0 | 2452 | } |
michael@0 | 2453 | #if defined(ULOC_DEBUG) |
michael@0 | 2454 | fprintf(stderr, "MATCH! %s\n", l); |
michael@0 | 2455 | #endif |
michael@0 | 2456 | if(len>0) { |
michael@0 | 2457 | uprv_strncpy(result, l, uprv_min(len, resultAvailable)); |
michael@0 | 2458 | } |
michael@0 | 2459 | for(j=0;j<i;j++) { |
michael@0 | 2460 | uprv_free(fallbackList[j]); |
michael@0 | 2461 | } |
michael@0 | 2462 | uprv_free(fallbackList); |
michael@0 | 2463 | return u_terminateChars(result, resultAvailable, len, status); |
michael@0 | 2464 | } |
michael@0 | 2465 | if(len>maxLen) { |
michael@0 | 2466 | maxLen = len; |
michael@0 | 2467 | } |
michael@0 | 2468 | } |
michael@0 | 2469 | uenum_reset(availableLocales, status); |
michael@0 | 2470 | /* save off parent info */ |
michael@0 | 2471 | if(uloc_getParent(acceptList[i], tmp, sizeof(tmp)/sizeof(tmp[0]), status)!=0) { |
michael@0 | 2472 | fallbackList[i] = uprv_strdup(tmp); |
michael@0 | 2473 | } else { |
michael@0 | 2474 | fallbackList[i]=0; |
michael@0 | 2475 | } |
michael@0 | 2476 | } |
michael@0 | 2477 | |
michael@0 | 2478 | for(maxLen--;maxLen>0;maxLen--) { |
michael@0 | 2479 | for(i=0;i<acceptListCount;i++) { |
michael@0 | 2480 | if(fallbackList[i] && ((int32_t)uprv_strlen(fallbackList[i])==maxLen)) { |
michael@0 | 2481 | #if defined(ULOC_DEBUG) |
michael@0 | 2482 | fprintf(stderr,"Try: [%s]", fallbackList[i]); |
michael@0 | 2483 | #endif |
michael@0 | 2484 | while((l=uenum_next(availableLocales, NULL, status))) { |
michael@0 | 2485 | #if defined(ULOC_DEBUG) |
michael@0 | 2486 | fprintf(stderr," %s\n", l); |
michael@0 | 2487 | #endif |
michael@0 | 2488 | len = (int32_t)uprv_strlen(l); |
michael@0 | 2489 | if(!uprv_strcmp(fallbackList[i], l)) { |
michael@0 | 2490 | if(outResult) { |
michael@0 | 2491 | *outResult = ULOC_ACCEPT_FALLBACK; |
michael@0 | 2492 | } |
michael@0 | 2493 | #if defined(ULOC_DEBUG) |
michael@0 | 2494 | fprintf(stderr, "fallback MATCH! %s\n", l); |
michael@0 | 2495 | #endif |
michael@0 | 2496 | if(len>0) { |
michael@0 | 2497 | uprv_strncpy(result, l, uprv_min(len, resultAvailable)); |
michael@0 | 2498 | } |
michael@0 | 2499 | for(j=0;j<acceptListCount;j++) { |
michael@0 | 2500 | uprv_free(fallbackList[j]); |
michael@0 | 2501 | } |
michael@0 | 2502 | uprv_free(fallbackList); |
michael@0 | 2503 | return u_terminateChars(result, resultAvailable, len, status); |
michael@0 | 2504 | } |
michael@0 | 2505 | } |
michael@0 | 2506 | uenum_reset(availableLocales, status); |
michael@0 | 2507 | |
michael@0 | 2508 | if(uloc_getParent(fallbackList[i], tmp, sizeof(tmp)/sizeof(tmp[0]), status)!=0) { |
michael@0 | 2509 | uprv_free(fallbackList[i]); |
michael@0 | 2510 | fallbackList[i] = uprv_strdup(tmp); |
michael@0 | 2511 | } else { |
michael@0 | 2512 | uprv_free(fallbackList[i]); |
michael@0 | 2513 | fallbackList[i]=0; |
michael@0 | 2514 | } |
michael@0 | 2515 | } |
michael@0 | 2516 | } |
michael@0 | 2517 | if(outResult) { |
michael@0 | 2518 | *outResult = ULOC_ACCEPT_FAILED; |
michael@0 | 2519 | } |
michael@0 | 2520 | } |
michael@0 | 2521 | for(i=0;i<acceptListCount;i++) { |
michael@0 | 2522 | uprv_free(fallbackList[i]); |
michael@0 | 2523 | } |
michael@0 | 2524 | uprv_free(fallbackList); |
michael@0 | 2525 | return -1; |
michael@0 | 2526 | } |
michael@0 | 2527 | |
michael@0 | 2528 | /*eof*/ |