modified dll/win32/kernel32/misc/lang.c
[reactos.git] / reactos / lib / 3rdparty / icu4ros / icu / source / common / locutil.cpp
1 /**
2 *******************************************************************************
3 * Copyright (C) 2002-2006, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 *******************************************************************************
8 */
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_SERVICE || !UCONFIG_NO_TRANSLITERATION
12
13 #include "unicode/resbund.h"
14 #include "cmemory.h"
15 #include "ustrfmt.h"
16 #include "locutil.h"
17 #include "charstr.h"
18 #include "ucln_cmn.h"
19 #include "uassert.h"
20 #include "umutex.h"
21
22 // see LocaleUtility::getAvailableLocaleNames
23 static U_NAMESPACE_QUALIFIER Hashtable * LocaleUtility_cache = NULL;
24
25 #define UNDERSCORE_CHAR ((UChar)0x005f)
26 #define AT_SIGN_CHAR ((UChar)64)
27 #define PERIOD_CHAR ((UChar)46)
28
29 /*
30 ******************************************************************
31 */
32
33 /**
34 * Release all static memory held by Locale Utility.
35 */
36 U_CDECL_BEGIN
37 static UBool U_CALLCONV service_cleanup(void) {
38 if (LocaleUtility_cache) {
39 delete LocaleUtility_cache;
40 LocaleUtility_cache = NULL;
41 }
42 return TRUE;
43 }
44 U_CDECL_END
45
46 U_NAMESPACE_BEGIN
47
48 UnicodeString&
49 LocaleUtility::canonicalLocaleString(const UnicodeString* id, UnicodeString& result)
50 {
51 if (id == NULL) {
52 result.setToBogus();
53 } else {
54 // Fix case only (no other changes) up to the first '@' or '.' or
55 // end of string, whichever comes first. In 3.0 I changed this to
56 // stop at first '@' or '.'. It used to run out to the end of
57 // string. My fix makes the tests pass but is probably
58 // structurally incorrect. See below. [alan 3.0]
59
60 // TODO: Doug, you might want to revise this...
61 result = *id;
62 int32_t i = 0;
63 int32_t end = result.indexOf(AT_SIGN_CHAR);
64 int32_t n = result.indexOf(PERIOD_CHAR);
65 if (n >= 0 && n < end) {
66 end = n;
67 }
68 if (end < 0) {
69 end = result.length();
70 }
71 n = result.indexOf(UNDERSCORE_CHAR);
72 if (n < 0) {
73 n = end;
74 }
75 for (; i < n; ++i) {
76 UChar c = result.charAt(i);
77 if (c >= 0x0041 && c <= 0x005a) {
78 c += 0x20;
79 result.setCharAt(i, c);
80 }
81 }
82 for (n = end; i < n; ++i) {
83 UChar c = result.charAt(i);
84 if (c >= 0x0061 && c <= 0x007a) {
85 c -= 0x20;
86 result.setCharAt(i, c);
87 }
88 }
89 }
90 return result;
91
92 #if 0
93 // This code does a proper full level 2 canonicalization of id.
94 // It's nasty to go from UChar to char to char to UChar -- but
95 // that's what you have to do to use the uloc_canonicalize
96 // function on UnicodeStrings.
97
98 // I ended up doing the alternate fix (see above) not for
99 // performance reasons, although performance will certainly be
100 // better, but because doing a full level 2 canonicalization
101 // causes some tests to fail. [alan 3.0]
102
103 // TODO: Doug, you might want to revisit this...
104 result.setToBogus();
105 if (id != 0) {
106 int32_t buflen = id->length() + 8; // space for NUL
107 char* buf = (char*) uprv_malloc(buflen);
108 char* canon = (buf == 0) ? 0 : (char*) uprv_malloc(buflen);
109 if (buf != 0 && canon != 0) {
110 U_ASSERT(id->extract(0, INT32_MAX, buf, buflen) < buflen);
111 UErrorCode ec = U_ZERO_ERROR;
112 uloc_canonicalize(buf, canon, buflen, &ec);
113 if (U_SUCCESS(ec)) {
114 result = UnicodeString(canon);
115 }
116 }
117 uprv_free(buf);
118 uprv_free(canon);
119 }
120 return result;
121 #endif
122 }
123
124 Locale&
125 LocaleUtility::initLocaleFromName(const UnicodeString& id, Locale& result)
126 {
127 enum { BUFLEN = 128 }; // larger than ever needed
128
129 if (id.isBogus() || id.length() >= BUFLEN) {
130 result.setToBogus();
131 } else {
132 /*
133 * We need to convert from a UnicodeString to char * in order to
134 * create a Locale.
135 *
136 * Problem: Locale ID strings may contain '@' which is a variant
137 * character and cannot be handled by invariant-character conversion.
138 *
139 * Hack: Since ICU code can handle locale IDs with multiple encodings
140 * of '@' (at least for EBCDIC; it's not known to be a problem for
141 * ASCII-based systems),
142 * we use regular invariant-character conversion for everything else
143 * and manually convert U+0040 into a compiler-char-constant '@'.
144 * While this compilation-time constant may not match the runtime
145 * encoding of '@', it should be one of the encodings which ICU
146 * recognizes.
147 *
148 * There should be only at most one '@' in a locale ID.
149 */
150 char buffer[BUFLEN];
151 int32_t prev, i;
152 prev = 0;
153 for(;;) {
154 i = id.indexOf((UChar)0x40, prev);
155 if(i < 0) {
156 // no @ between prev and the rest of the string
157 id.extract(prev, INT32_MAX, buffer + prev, BUFLEN - prev, US_INV);
158 break; // done
159 } else {
160 // normal invariant-character conversion for text between @s
161 id.extract(prev, i - prev, buffer + prev, BUFLEN - prev, US_INV);
162 // manually "convert" U+0040 at id[i] into '@' at buffer[i]
163 buffer[i] = '@';
164 prev = i + 1;
165 }
166 }
167 result = Locale::createFromName(buffer);
168 }
169 return result;
170 }
171
172 UnicodeString&
173 LocaleUtility::initNameFromLocale(const Locale& locale, UnicodeString& result)
174 {
175 if (locale.isBogus()) {
176 result.setToBogus();
177 } else {
178 result.append(UnicodeString(locale.getName(), -1, US_INV));
179 }
180 return result;
181 }
182
183 const Hashtable*
184 LocaleUtility::getAvailableLocaleNames(const UnicodeString& bundleID)
185 {
186 // LocaleUtility_cache is a hash-of-hashes. The top-level keys
187 // are path strings ('bundleID') passed to
188 // ures_openAvailableLocales. The top-level values are
189 // second-level hashes. The second-level keys are result strings
190 // from ures_openAvailableLocales. The second-level values are
191 // garbage ((void*)1 or other random pointer).
192
193 UErrorCode status = U_ZERO_ERROR;
194 Hashtable* cache;
195 umtx_lock(NULL);
196 cache = LocaleUtility_cache;
197 umtx_unlock(NULL);
198
199 if (cache == NULL) {
200 cache = new Hashtable(status);
201 if (cache == NULL || U_FAILURE(status)) {
202 return NULL; // catastrophic failure; e.g. out of memory
203 }
204 cache->setValueDeleter(uhash_deleteHashtable);
205 Hashtable* h; // set this to final LocaleUtility_cache value
206 umtx_lock(NULL);
207 h = LocaleUtility_cache;
208 if (h == NULL) {
209 LocaleUtility_cache = h = cache;
210 cache = NULL;
211 ucln_common_registerCleanup(UCLN_COMMON_SERVICE, service_cleanup);
212 }
213 umtx_unlock(NULL);
214 if(cache != NULL) {
215 delete cache;
216 }
217 cache = h;
218 }
219
220 U_ASSERT(cache != NULL);
221
222 Hashtable* htp;
223 umtx_lock(NULL);
224 htp = (Hashtable*) cache->get(bundleID);
225 umtx_unlock(NULL);
226
227 if (htp == NULL) {
228 htp = new Hashtable(status);
229 if (htp && U_SUCCESS(status)) {
230 CharString cbundleID(bundleID);
231 const char* path = (const char*) cbundleID;
232 if (*path == 0) path = NULL; // empty string => NULL
233 UEnumeration *uenum = ures_openAvailableLocales(path, &status);
234 for (;;) {
235 const UChar* id = uenum_unext(uenum, NULL, &status);
236 if (id == NULL) {
237 break;
238 }
239 htp->put(UnicodeString(id), (void*)htp, status);
240 }
241 uenum_close(uenum);
242 if (U_FAILURE(status)) {
243 delete htp;
244 return NULL;
245 }
246 umtx_lock(NULL);
247 cache->put(bundleID, (void*)htp, status);
248 umtx_unlock(NULL);
249 }
250 }
251 return htp;
252 }
253
254 UBool
255 LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child)
256 {
257 return child.indexOf(root) == 0 &&
258 (child.length() == root.length() ||
259 child.charAt(root.length()) == UNDERSCORE_CHAR);
260 }
261
262 U_NAMESPACE_END
263
264 /* !UCONFIG_NO_SERVICE */
265 #endif
266
267