]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
374ca955 | 3 | * Copyright (C) 1997-2004, International Business Machines Corporation and * |
b75a7d8f A |
4 | * others. All Rights Reserved. * |
5 | ****************************************************************************** | |
6 | * | |
7 | * File URESBUND.C | |
8 | * | |
9 | * Modification History: | |
10 | * | |
11 | * Date Name Description | |
12 | * 04/01/97 aliu Creation. | |
13 | * 06/14/99 stephen Removed functions taking a filename suffix. | |
14 | * 07/20/99 stephen Changed for UResourceBundle typedef'd to void* | |
15 | * 11/09/99 weiv Added ures_getLocale() | |
16 | * March 2000 weiv Total overhaul - using data in DLLs | |
17 | * 06/20/2000 helena OS/400 port changes; mostly typecast. | |
18 | * 06/24/02 weiv Added support for resource sharing | |
19 | ****************************************************************************** | |
20 | */ | |
21 | ||
22 | #include "unicode/ustring.h" | |
374ca955 | 23 | #include "unicode/ucnv.h" |
b75a7d8f | 24 | #include "uresimp.h" |
374ca955 | 25 | #include "ustr_imp.h" |
b75a7d8f A |
26 | #include "cwchar.h" |
27 | #include "ucln_cmn.h" | |
28 | #include "cmemory.h" | |
29 | #include "cstring.h" | |
30 | #include "uhash.h" | |
374ca955 A |
31 | #include "unicode/uenum.h" |
32 | #include "uenumimp.h" | |
33 | #include "ulocimp.h" | |
b75a7d8f | 34 | #include "umutex.h" |
374ca955 | 35 | #include "putilimp.h" |
b75a7d8f A |
36 | |
37 | ||
38 | /* | |
39 | Static cache for already opened resource bundles - mostly for keeping fallback info | |
40 | TODO: This cache should probably be removed when the deprecated code is | |
41 | completely removed. | |
42 | */ | |
43 | static UHashtable *cache = NULL; | |
44 | ||
45 | static UMTX resbMutex = NULL; | |
46 | ||
47 | /* INTERNAL: hashes an entry */ | |
48 | static int32_t U_EXPORT2 U_CALLCONV hashEntry(const UHashTok parm) { | |
49 | UResourceDataEntry *b = (UResourceDataEntry *)parm.pointer; | |
50 | UHashTok namekey, pathkey; | |
51 | namekey.pointer = b->fName; | |
52 | pathkey.pointer = b->fPath; | |
53 | return uhash_hashChars(namekey)+37*uhash_hashChars(pathkey); | |
54 | } | |
55 | ||
56 | /* INTERNAL: compares two entries */ | |
57 | static UBool U_EXPORT2 U_CALLCONV compareEntries(const UHashTok p1, const UHashTok p2) { | |
58 | UResourceDataEntry *b1 = (UResourceDataEntry *)p1.pointer; | |
59 | UResourceDataEntry *b2 = (UResourceDataEntry *)p2.pointer; | |
60 | UHashTok name1, name2, path1, path2; | |
61 | name1.pointer = b1->fName; | |
62 | name2.pointer = b2->fName; | |
63 | path1.pointer = b1->fPath; | |
64 | path2.pointer = b2->fPath; | |
65 | return (UBool)(uhash_compareChars(name1, name2) & | |
66 | uhash_compareChars(path1, path2)); | |
67 | } | |
68 | ||
69 | ||
70 | /** | |
71 | * Internal function, gets parts of locale name according | |
72 | * to the position of '_' character | |
73 | */ | |
74 | static UBool chopLocale(char *name) { | |
75 | char *i = uprv_strrchr(name, '_'); | |
76 | ||
77 | if(i != NULL) { | |
78 | *i = '\0'; | |
79 | return TRUE; | |
80 | } | |
81 | ||
82 | return FALSE; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Internal function | |
87 | */ | |
88 | static void entryIncrease(UResourceDataEntry *entry) { | |
89 | umtx_lock(&resbMutex); | |
90 | entry->fCountExisting++; | |
91 | while(entry->fParent != NULL) { | |
92 | entry = entry->fParent; | |
93 | entry->fCountExisting++; | |
94 | } | |
95 | umtx_unlock(&resbMutex); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Internal function. Tries to find a resource in given Resource | |
100 | * Bundle, as well as in its parents | |
101 | */ | |
102 | static const ResourceData *getFallbackData(const UResourceBundle* resBundle, const char* * resTag, UResourceDataEntry* *realData, Resource *res, UErrorCode *status) { | |
103 | UResourceDataEntry *resB = resBundle->fData; | |
104 | int32_t indexR = -1; | |
105 | int32_t i = 0; | |
106 | *res = RES_BOGUS; | |
107 | if(resB != NULL) { | |
108 | if(resB->fBogus == U_ZERO_ERROR) { /* if this resource is real, */ | |
109 | *res = res_getTableItemByKey(&(resB->fData), resB->fData.rootRes, &indexR, resTag); /* try to get data from there */ | |
110 | i++; | |
111 | } | |
112 | if(resBundle->fHasFallback == TRUE) { | |
113 | while(*res == RES_BOGUS && resB->fParent != NULL) { /* Otherwise, we'll look in parents */ | |
114 | resB = resB->fParent; | |
115 | if(resB->fBogus == U_ZERO_ERROR) { | |
116 | i++; | |
117 | *res = res_getTableItemByKey(&(resB->fData), resB->fData.rootRes, &indexR, resTag); | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | if(*res != RES_BOGUS) { /* If the resource is found in parents, we need to adjust the error */ | |
123 | if(i>1) { | |
124 | if(uprv_strcmp(resB->fName, uloc_getDefault())==0 || uprv_strcmp(resB->fName, kRootLocaleName)==0) { | |
125 | *status = U_USING_DEFAULT_WARNING; | |
126 | } else { | |
127 | *status = U_USING_FALLBACK_WARNING; | |
128 | } | |
129 | } | |
130 | *realData = resB; | |
131 | return (&(resB->fData)); | |
132 | } else { /* If resource is not found, we need to give an error */ | |
133 | *status = U_MISSING_RESOURCE_ERROR; | |
134 | return NULL; | |
135 | } | |
136 | } else { | |
137 | *status = U_MISSING_RESOURCE_ERROR; | |
138 | return NULL; | |
139 | } | |
140 | } | |
141 | ||
b75a7d8f A |
142 | /* Works just like ucnv_flushCache() */ |
143 | /* TODO: figure out why fCountExisting may not go to zero. Do not make this function public yet. */ | |
144 | static int32_t ures_flushCache() | |
145 | { | |
146 | UResourceDataEntry *resB = NULL; | |
147 | int32_t pos = -1; | |
148 | int32_t rbDeletedNum = 0; | |
149 | const UHashElement *e; | |
150 | ||
151 | /*if shared data hasn't even been lazy evaluated yet | |
152 | * return 0 | |
153 | */ | |
154 | umtx_lock(&resbMutex); | |
155 | if (cache == NULL) { | |
156 | umtx_unlock(&resbMutex); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | /*creates an enumeration to iterate through every element in the table */ | |
161 | while ((e = uhash_nextElement(cache, &pos)) != NULL) | |
162 | { | |
163 | resB = (UResourceDataEntry *) e->value.pointer; | |
164 | /* Deletes only if reference counter == 0 | |
165 | * Don't worry about the children of this node. | |
166 | * Those will eventually get deleted too, if not already. | |
167 | * Don't worry about the parents of this node. | |
168 | * Those will eventually get deleted too, if not already. | |
169 | */ | |
170 | /* DONE: figure out why fCountExisting may not go to zero. Do not make this function public yet. */ | |
171 | /* 04/05/2002 [weiv] fCountExisting should now be accurate. If it's not zero, that means that */ | |
172 | /* some resource bundles are still open somewhere. */ | |
173 | ||
374ca955 | 174 | /*U_ASSERT(resB->fCountExisting == 0);*/ |
b75a7d8f A |
175 | if (resB->fCountExisting == 0) { |
176 | rbDeletedNum++; | |
177 | uhash_removeElement(cache, e); | |
178 | if(resB->fBogus == U_ZERO_ERROR) { | |
179 | res_unload(&(resB->fData)); | |
180 | } | |
181 | if(resB->fName != NULL) { | |
182 | uprv_free(resB->fName); | |
183 | } | |
184 | if(resB->fPath != NULL) { | |
185 | uprv_free(resB->fPath); | |
186 | } | |
187 | uprv_free(resB); | |
188 | } | |
189 | } | |
190 | umtx_unlock(&resbMutex); | |
191 | ||
192 | return rbDeletedNum; | |
193 | } | |
194 | ||
374ca955 | 195 | static UBool U_CALLCONV ures_cleanup(void) |
b75a7d8f A |
196 | { |
197 | if (cache != NULL) { | |
198 | ures_flushCache(); | |
199 | if (cache != NULL && uhash_count(cache) == 0) { | |
200 | uhash_close(cache); | |
201 | cache = NULL; | |
b75a7d8f A |
202 | } |
203 | } | |
374ca955 A |
204 | if (cache == NULL && resbMutex != NULL) { |
205 | umtx_destroy(&resbMutex); | |
206 | } | |
b75a7d8f A |
207 | return (cache == NULL); |
208 | } | |
209 | ||
374ca955 A |
210 | /** INTERNAL: Initializes the cache for resources */ |
211 | static void initCache(UErrorCode *status) { | |
212 | UBool makeCache = FALSE; | |
213 | umtx_lock(&resbMutex); | |
214 | makeCache = (cache == NULL); | |
215 | umtx_unlock(&resbMutex); | |
216 | if(makeCache) { | |
217 | UHashtable *newCache = uhash_open(hashEntry, compareEntries, status); | |
218 | if (U_FAILURE(*status)) { | |
219 | return; | |
220 | } | |
221 | umtx_lock(&resbMutex); | |
222 | if(cache == NULL) { | |
223 | cache = newCache; | |
224 | newCache = NULL; | |
225 | ucln_common_registerCleanup(UCLN_COMMON_URES, ures_cleanup); | |
226 | } | |
227 | umtx_unlock(&resbMutex); | |
228 | if(newCache != NULL) { | |
229 | uhash_close(newCache); | |
230 | } | |
231 | } | |
b75a7d8f A |
232 | } |
233 | ||
b75a7d8f A |
234 | /** INTERNAL: sets the name (locale) of the resource bundle to given name */ |
235 | ||
236 | static void setEntryName(UResourceDataEntry *res, char *name, UErrorCode *status) { | |
237 | if(res->fName != NULL) { | |
238 | uprv_free(res->fName); | |
239 | } | |
240 | res->fName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(name)+1); | |
241 | if(res->fName == NULL) { | |
242 | *status = U_MEMORY_ALLOCATION_ERROR; | |
243 | } else { | |
244 | uprv_strcpy(res->fName, name); | |
245 | } | |
246 | } | |
247 | ||
248 | /** | |
249 | * INTERNAL: Inits and opens an entry from a data DLL. | |
374ca955 | 250 | * CAUTION: resbMutex must be locked when calling this function. |
b75a7d8f A |
251 | */ |
252 | static UResourceDataEntry *init_entry(const char *localeID, const char *path, UErrorCode *status) { | |
253 | UResourceDataEntry *r = NULL; | |
254 | UResourceDataEntry find; | |
255 | int32_t hashValue; | |
256 | char name[96]; | |
257 | const char *myPath = NULL; | |
258 | char aliasName[100] = { 0 }; | |
259 | int32_t aliasLen = 0; | |
260 | UBool isAlias = FALSE; | |
261 | UHashTok hashkey; | |
262 | ||
263 | if(U_FAILURE(*status)) { | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | /* here we try to deduce the right locale name */ | |
268 | if(localeID == NULL) { /* if localeID is NULL, we're trying to open default locale */ | |
269 | uprv_strcpy(name, uloc_getDefault()); | |
270 | } else if(uprv_strlen(localeID) == 0) { /* if localeID is "" then we try to open root locale */ | |
271 | uprv_strcpy(name, kRootLocaleName); | |
272 | } else { /* otherwise, we'll open what we're given */ | |
273 | uprv_strcpy(name, localeID); | |
274 | } | |
275 | ||
276 | if(path != NULL) { /* if we actually have path, we'll use it */ | |
374ca955 | 277 | myPath = path; |
b75a7d8f A |
278 | } |
279 | ||
280 | find.fName = name; | |
281 | find.fPath = (char *)myPath; | |
282 | ||
283 | /* calculate the hash value of the entry */ | |
284 | hashkey.pointer = (void *)&find; | |
285 | hashValue = hashEntry(hashkey); | |
286 | ||
287 | /* check to see if we already have this entry */ | |
288 | r = (UResourceDataEntry *)uhash_get(cache, &find); | |
289 | ||
290 | if(r != NULL) { /* if the entry is already in the hash table */ | |
291 | r->fCountExisting++; /* we just increase it's reference count */ | |
292 | /* if the resource has a warning */ | |
293 | /* we don't want to overwrite a status with no error */ | |
294 | if(r->fBogus != U_ZERO_ERROR) { | |
295 | *status = r->fBogus; /* set the returning status */ | |
296 | } | |
297 | } else { /* otherwise, we'll try to construct a new entry */ | |
298 | UBool result = FALSE; | |
299 | ||
300 | r = (UResourceDataEntry *) uprv_malloc(sizeof(UResourceDataEntry)); | |
301 | ||
302 | if(r == NULL) { | |
303 | *status = U_MEMORY_ALLOCATION_ERROR; | |
304 | return NULL; | |
305 | } | |
306 | r->fCountExisting = 1; | |
307 | ||
308 | r->fName = NULL; | |
309 | setEntryName(r, name, status); | |
310 | ||
311 | r->fPath = NULL; | |
312 | if(myPath != NULL && !U_FAILURE(*status)) { | |
313 | r->fPath = (char *)uprv_malloc(sizeof(char)*uprv_strlen(myPath)+1); | |
314 | if(r->fPath == NULL) { | |
315 | *status = U_MEMORY_ALLOCATION_ERROR; | |
316 | } else { | |
317 | uprv_strcpy(r->fPath, myPath); | |
318 | } | |
319 | } | |
320 | ||
321 | r->fHashKey = hashValue; | |
322 | r->fParent = NULL; | |
323 | r->fData.data = NULL; | |
324 | r->fData.pRoot = NULL; | |
325 | r->fData.rootRes = 0; | |
326 | r->fBogus = U_ZERO_ERROR; | |
327 | ||
328 | /* this is the acutal loading - returns bool true/false */ | |
329 | result = res_load(&(r->fData), r->fPath, r->fName, status); | |
330 | ||
331 | if (result == FALSE || U_FAILURE(*status)) { | |
332 | /* we have no such entry in dll, so it will always use fallback */ | |
333 | *status = U_USING_FALLBACK_WARNING; | |
334 | r->fBogus = U_USING_FALLBACK_WARNING; | |
335 | } else { /* if we have a regular entry */ | |
336 | /* We might be able to do this a wee bit more efficiently (we could check whether the aliased data) */ | |
337 | /* is already in the cache), but it's good the way it is */ | |
338 | /* handle the alias by trying to get out the %%Alias tag.*/ | |
339 | /* We'll try to get alias string from the bundle */ | |
340 | Resource aliasres = res_getResource(&(r->fData), "%%ALIAS"); | |
341 | const UChar *alias = res_getString(&(r->fData), aliasres, &aliasLen); | |
342 | if(alias != NULL && aliasLen > 0) { /* if there is actual alias - unload and load new data */ | |
343 | u_UCharsToChars(alias, aliasName, aliasLen+1); | |
344 | isAlias = TRUE; | |
345 | res_unload(&(r->fData)); | |
346 | result = res_load(&(r->fData), r->fPath, aliasName, status); | |
347 | if (result == FALSE || U_FAILURE(*status)) { | |
348 | /* we couldn't load aliased data - so we have no data */ | |
349 | *status = U_USING_FALLBACK_WARNING; | |
350 | r->fBogus = U_USING_FALLBACK_WARNING; | |
351 | } | |
352 | setEntryName(r, aliasName, status); | |
353 | } | |
354 | } | |
355 | ||
356 | { | |
357 | UResourceDataEntry *oldR = NULL; | |
358 | if((oldR = (UResourceDataEntry *)uhash_get(cache, r)) == NULL) { /* if the data is not cached */ | |
359 | /* just insert it in the cache */ | |
360 | uhash_put(cache, (void *)r, r, status); | |
361 | } else { | |
362 | /* somebody have already inserted it while we were working, discard newly opened data */ | |
363 | /* Also, we could get here IF we opened an alias */ | |
364 | uprv_free(r->fName); | |
365 | if(r->fPath != NULL) { | |
366 | uprv_free(r->fPath); | |
367 | } | |
368 | res_unload(&(r->fData)); | |
369 | uprv_free(r); | |
370 | r = oldR; | |
371 | r->fCountExisting++; | |
372 | } | |
373 | } | |
374 | ||
375 | } | |
376 | return r; | |
377 | } | |
378 | ||
379 | /* INTERNAL: */ | |
374ca955 | 380 | /* CAUTION: resbMutex must be locked when calling this function! */ |
b75a7d8f A |
381 | static UResourceDataEntry *findFirstExisting(const char* path, char* name, UBool *isRoot, UBool *hasChopped, UBool *isDefault, UErrorCode* status) { |
382 | UResourceDataEntry *r = NULL; | |
383 | UBool hasRealData = FALSE; | |
384 | const char *defaultLoc = uloc_getDefault(); | |
385 | UErrorCode intStatus = U_ZERO_ERROR; | |
386 | *hasChopped = TRUE; /* we're starting with a fresh name */ | |
387 | ||
388 | while(*hasChopped && !hasRealData) { | |
389 | r = init_entry(name, path, &intStatus); | |
390 | *isDefault = (UBool)(uprv_strncmp(name, defaultLoc, uprv_strlen(name)) == 0); | |
391 | hasRealData = (UBool)(r->fBogus == U_ZERO_ERROR); | |
392 | if(!hasRealData) { | |
393 | /* this entry is not real. We will discard it. */ | |
394 | /* However, the parent line for this entry is */ | |
395 | /* not to be used - as there might be parent */ | |
396 | /* lines in cache from previous openings that */ | |
397 | /* are not updated yet. */ | |
398 | r->fCountExisting--; | |
399 | /*entryCloseInt(r);*/ | |
400 | r = NULL; | |
401 | *status = U_USING_FALLBACK_WARNING; | |
402 | } else { | |
403 | uprv_strcpy(name, r->fName); /* this is needed for supporting aliases */ | |
404 | } | |
405 | ||
406 | *isRoot = (UBool)(uprv_strcmp(name, kRootLocaleName) == 0); | |
407 | ||
408 | /*Fallback data stuff*/ | |
409 | *hasChopped = chopLocale(name); | |
410 | } | |
411 | return r; | |
412 | } | |
413 | ||
374ca955 A |
414 | static void ures_setIsStackObject( UResourceBundle* resB, UBool state) { |
415 | if(state) { | |
416 | resB->fMagic1 = 0; | |
417 | resB->fMagic2 = 0; | |
418 | } else { | |
419 | resB->fMagic1 = MAGIC1; | |
420 | resB->fMagic2 = MAGIC2; | |
421 | } | |
422 | } | |
423 | ||
424 | static UBool ures_isStackObject(const UResourceBundle* resB) { | |
425 | return((resB->fMagic1 == MAGIC1 && resB->fMagic2 == MAGIC2)?FALSE:TRUE); | |
426 | } | |
427 | ||
428 | ||
429 | U_CFUNC void ures_initStackObject(UResourceBundle* resB) { | |
430 | uprv_memset(resB, 0, sizeof(UResourceBundle)); | |
431 | ures_setIsStackObject(resB, TRUE); | |
432 | } | |
433 | ||
b75a7d8f A |
434 | static UResourceDataEntry *entryOpen(const char* path, const char* localeID, UErrorCode* status) { |
435 | UErrorCode intStatus = U_ZERO_ERROR; | |
436 | UErrorCode parentStatus = U_ZERO_ERROR; | |
437 | UResourceDataEntry *r = NULL; | |
438 | UResourceDataEntry *t1 = NULL; | |
439 | UResourceDataEntry *t2 = NULL; | |
440 | UBool isDefault = FALSE; | |
441 | UBool isRoot = FALSE; | |
442 | UBool hasRealData = FALSE; | |
443 | UBool hasChopped = TRUE; | |
444 | char name[96]; | |
445 | ||
446 | if(U_FAILURE(*status)) { | |
447 | return NULL; | |
448 | } | |
449 | ||
450 | initCache(status); | |
451 | ||
452 | uprv_strcpy(name, localeID); | |
453 | ||
454 | umtx_lock(&resbMutex); | |
455 | { /* umtx_lock */ | |
456 | /* We're going to skip all the locales that do not have any data */ | |
457 | r = findFirstExisting(path, name, &isRoot, &hasChopped, &isDefault, &intStatus); | |
458 | ||
459 | if(r != NULL) { /* if there is one real locale, we can look for parents. */ | |
460 | t1 = r; | |
461 | hasRealData = TRUE; | |
462 | while (hasChopped && !isRoot && t1->fParent == NULL) { | |
463 | /* insert regular parents */ | |
464 | t2 = init_entry(name, r->fPath, &parentStatus); | |
465 | t1->fParent = t2; | |
466 | t1 = t2; | |
467 | hasChopped = chopLocale(name); | |
468 | } | |
469 | } | |
470 | ||
471 | /* we could have reached this point without having any real data */ | |
472 | /* if that is the case, we need to chain in the default locale */ | |
473 | if(r==NULL && !isDefault && !isRoot /*&& t1->fParent == NULL*/) { | |
474 | /* insert default locale */ | |
475 | uprv_strcpy(name, uloc_getDefault()); | |
476 | r = findFirstExisting(path, name, &isRoot, &hasChopped, &isDefault, &intStatus); | |
477 | intStatus = U_USING_DEFAULT_WARNING; | |
478 | if(r != NULL) { /* the default locale exists */ | |
479 | t1 = r; | |
480 | hasRealData = TRUE; | |
481 | isDefault = TRUE; | |
482 | while (hasChopped && t1->fParent == NULL) { | |
483 | /* insert chopped defaults */ | |
484 | t2 = init_entry(name, r->fPath, &parentStatus); | |
485 | t1->fParent = t2; | |
486 | t1 = t2; | |
487 | hasChopped = chopLocale(name); | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
492 | /* we could still have r == NULL at this point - maybe even default locale is not */ | |
493 | /* present */ | |
494 | if(r == NULL) { | |
495 | uprv_strcpy(name, kRootLocaleName); | |
496 | r = findFirstExisting(path, name, &isRoot, &hasChopped, &isDefault, &intStatus); | |
497 | if(r != NULL) { | |
498 | t1 = r; | |
499 | intStatus = U_USING_DEFAULT_WARNING; | |
500 | hasRealData = TRUE; | |
501 | } else { /* we don't even have the root locale */ | |
502 | *status = U_MISSING_RESOURCE_ERROR; | |
503 | } | |
504 | } else if(!isRoot && uprv_strcmp(t1->fName, kRootLocaleName) != 0 && t1->fParent == NULL) { | |
505 | /* insert root locale */ | |
506 | t2 = init_entry(kRootLocaleName, r->fPath, &parentStatus); | |
507 | if(!hasRealData) { | |
508 | r->fBogus = U_USING_DEFAULT_WARNING; | |
509 | } | |
510 | hasRealData = (UBool)((t2->fBogus == U_ZERO_ERROR) | hasRealData); | |
511 | t1->fParent = t2; | |
512 | t1 = t2; | |
513 | } | |
514 | ||
515 | while(r != NULL && !isRoot && t1->fParent != NULL) { | |
516 | t1->fParent->fCountExisting++; | |
517 | t1 = t1->fParent; | |
518 | hasRealData = (UBool)((t1->fBogus == U_ZERO_ERROR) | hasRealData); | |
519 | } | |
520 | } /* umtx_lock */ | |
521 | umtx_unlock(&resbMutex); | |
522 | ||
523 | if(U_SUCCESS(*status)) { | |
524 | if(U_SUCCESS(parentStatus)) { | |
525 | if(intStatus != U_ZERO_ERROR) { | |
526 | *status = intStatus; | |
527 | } | |
528 | return r; | |
529 | } else { | |
530 | *status = parentStatus; | |
531 | return NULL; | |
532 | } | |
533 | } else { | |
534 | return NULL; | |
535 | } | |
536 | } | |
537 | ||
538 | ||
539 | /** | |
540 | * Functions to create and destroy resource bundles. | |
374ca955 | 541 | * CAUTION: resbMutex must be locked when calling this function. |
b75a7d8f A |
542 | */ |
543 | /* INTERNAL: */ | |
544 | static void entryCloseInt(UResourceDataEntry *resB) { | |
545 | UResourceDataEntry *p = resB; | |
546 | ||
547 | while(resB != NULL) { | |
548 | p = resB->fParent; | |
549 | resB->fCountExisting--; | |
550 | ||
551 | /* Entries are left in the cache. TODO: add ures_cacheFlush() to force a flush | |
552 | of the cache. */ | |
553 | /* | |
554 | if(resB->fCountExisting <= 0) { | |
555 | uhash_remove(cache, resB); | |
556 | if(resB->fBogus == U_ZERO_ERROR) { | |
557 | res_unload(&(resB->fData)); | |
558 | } | |
559 | if(resB->fName != NULL) { | |
560 | uprv_free(resB->fName); | |
561 | } | |
562 | if(resB->fPath != NULL) { | |
563 | uprv_free(resB->fPath); | |
564 | } | |
565 | uprv_free(resB); | |
566 | } | |
567 | */ | |
568 | ||
569 | resB = p; | |
570 | } | |
571 | } | |
572 | ||
573 | /** | |
574 | * API: closes a resource bundle and cleans up. | |
575 | */ | |
576 | ||
577 | static void entryClose(UResourceDataEntry *resB) { | |
578 | umtx_lock(&resbMutex); | |
579 | entryCloseInt(resB); | |
580 | umtx_unlock(&resbMutex); | |
581 | } | |
582 | ||
583 | static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, | |
584 | const char *key, int32_t index, UResourceDataEntry *realData, | |
585 | const UResourceBundle *parent, int32_t noAlias, | |
586 | UResourceBundle *resB, UErrorCode *status) | |
587 | { | |
588 | if(status == NULL || U_FAILURE(*status)) { | |
589 | return resB; | |
590 | } | |
591 | if(RES_GET_TYPE(r) == URES_ALIAS) { /* This is an alias, need to exchange with real data */ | |
592 | if(noAlias < URES_MAX_ALIAS_LEVEL) { | |
593 | int32_t len = 0; | |
594 | const UChar *alias = res_getAlias(rdata, r, &len); | |
595 | if(len > 0) { | |
596 | /* we have an alias, now let's cut it up */ | |
374ca955 | 597 | char stackAlias[200]; |
b75a7d8f | 598 | char *chAlias = NULL, *path = NULL, *locale = NULL, *keyPath = NULL; |
374ca955 A |
599 | int32_t capacity; |
600 | ||
601 | /* | |
602 | * Allocate enough space for both the char * version | |
603 | * of the alias and parent->fResPath. | |
604 | * | |
605 | * We do this so that res_findResource() can modify the path, | |
606 | * which allows us to remove redundant _res_findResource() variants | |
607 | * in uresdata.c. | |
608 | * res_findResource() now NUL-terminates each segment so that table keys | |
609 | * can always be compared with strcmp() instead of strncmp(). | |
610 | * Saves code there and simplifies testing and code coverage. | |
611 | * | |
612 | * markus 2003oct17 | |
613 | */ | |
614 | ++len; /* count the terminating NUL */ | |
615 | if(parent != NULL && parent->fResPath != NULL) { | |
616 | capacity = uprv_strlen(parent->fResPath) + 1; | |
617 | } else { | |
618 | capacity = 0; | |
619 | } | |
620 | if(capacity < len) { | |
621 | capacity = len; | |
622 | } | |
623 | if(capacity <= sizeof(stackAlias)) { | |
624 | capacity = sizeof(stackAlias); | |
625 | chAlias = stackAlias; | |
626 | } else { | |
627 | chAlias = (char *)uprv_malloc(capacity); | |
628 | /* test for NULL */ | |
629 | if(chAlias == NULL) { | |
630 | *status = U_MEMORY_ALLOCATION_ERROR; | |
631 | return NULL; | |
b75a7d8f | 632 | } |
374ca955 | 633 | } |
b75a7d8f | 634 | u_UCharsToChars(alias, chAlias, len); |
b75a7d8f A |
635 | |
636 | if(*chAlias == RES_PATH_SEPARATOR) { | |
637 | /* there is a path included */ | |
638 | locale = uprv_strchr(chAlias+1, RES_PATH_SEPARATOR); | |
374ca955 A |
639 | if(locale == NULL) { |
640 | locale = uprv_strchr(chAlias, 0); /* avoid locale == NULL to make code below work */ | |
641 | } else { | |
642 | *locale = 0; | |
643 | locale++; | |
644 | } | |
b75a7d8f A |
645 | path = chAlias+1; |
646 | if(uprv_strcmp(path, "ICUDATA") == 0) { /* want ICU data */ | |
647 | path = NULL; | |
648 | } | |
649 | } else { | |
650 | /* no path, start with a locale */ | |
651 | locale = chAlias; | |
652 | path = realData->fPath; | |
653 | } | |
654 | keyPath = uprv_strchr(locale, RES_PATH_SEPARATOR); | |
655 | if(keyPath) { | |
656 | *keyPath = 0; | |
657 | keyPath++; | |
658 | } | |
659 | { | |
660 | /* got almost everything, let's try to open */ | |
661 | /* first, open the bundle with real data */ | |
662 | UResourceBundle *result = resB; | |
663 | const char* temp = NULL; | |
664 | UErrorCode intStatus = U_ZERO_ERROR; | |
665 | UResourceBundle *mainRes = ures_openDirect(path, locale, &intStatus); | |
666 | if(U_SUCCESS(intStatus)) { | |
667 | if(keyPath == NULL) { | |
668 | /* no key path. This means that we are going to | |
669 | * to use the corresponding resource from | |
670 | * another bundle | |
671 | */ | |
672 | /* first, we are going to get a corresponding parent | |
673 | * resource to the one we are searching. | |
674 | */ | |
374ca955 | 675 | char *aKey = parent->fResPath; |
b75a7d8f | 676 | if(aKey) { |
374ca955 A |
677 | uprv_strcpy(chAlias, aKey); /* allocated large enough above */ |
678 | aKey = chAlias; | |
b75a7d8f A |
679 | r = res_findResource(&(mainRes->fResData), mainRes->fRes, &aKey, &temp); |
680 | } else { | |
681 | r = mainRes->fRes; | |
682 | } | |
683 | if(key) { | |
374ca955 A |
684 | /* we need to make keyPath from parent's fResPath and |
685 | * current key, if there is a key associated | |
686 | */ | |
687 | len = uprv_strlen(key) + 1; | |
688 | if(len > capacity) { | |
689 | capacity = len; | |
690 | if(chAlias == stackAlias) { | |
691 | chAlias = (char *)uprv_malloc(capacity); | |
692 | } else { | |
693 | chAlias = (char *)uprv_realloc(chAlias, capacity); | |
694 | } | |
695 | if(chAlias == NULL) { | |
696 | ures_close(mainRes); | |
697 | *status = U_MEMORY_ALLOCATION_ERROR; | |
698 | return NULL; | |
699 | } | |
700 | } | |
701 | uprv_memcpy(chAlias, key, len); | |
702 | aKey = chAlias; | |
b75a7d8f A |
703 | r = res_findResource(&(mainRes->fResData), r, &aKey, &temp); |
704 | } else if(index != -1) { | |
705 | /* if there is no key, but there is an index, try to get by the index */ | |
706 | /* here we have either a table or an array, so get the element */ | |
374ca955 A |
707 | if(RES_GET_TYPE(r) == URES_TABLE || RES_GET_TYPE(r) == URES_TABLE32) { |
708 | r = res_getTableItemByIndex(&(mainRes->fResData), r, index, (const char **)&aKey); | |
b75a7d8f A |
709 | } else { /* array */ |
710 | r = res_getArrayItem(&(mainRes->fResData), r, index); | |
711 | } | |
712 | } | |
713 | if(r != RES_BOGUS) { | |
714 | result = init_resb_result(&(mainRes->fResData), r, key, -1, mainRes->fData, parent, noAlias+1, resB, status); | |
715 | } else { | |
716 | *status = U_MISSING_RESOURCE_ERROR; | |
717 | result = resB; | |
718 | } | |
719 | } else { | |
720 | /* this one is a bit trickier. | |
721 | * we start finding keys, but after we resolve one alias, the path might continue. | |
722 | * Consider: | |
723 | * aliastest:alias { "testtypes/anotheralias/Sequence" } | |
724 | * anotheralias:alias { "/ICUDATA/sh/CollationElements" } | |
725 | * aliastest resource should finally have the sequence, not collation elements. | |
726 | */ | |
727 | result = mainRes; | |
728 | while(*keyPath && U_SUCCESS(*status)) { | |
374ca955 | 729 | r = res_findResource(&(result->fResData), result->fRes, &keyPath, &temp); |
b75a7d8f A |
730 | if(r == RES_BOGUS) { |
731 | *status = U_MISSING_RESOURCE_ERROR; | |
732 | result = resB; | |
733 | break; | |
734 | } | |
735 | resB = init_resb_result(&(result->fResData), r, key, -1, result->fData, parent, noAlias+1, resB, status); | |
736 | result = resB; | |
737 | } | |
738 | } | |
739 | } else { /* we failed to open the resource we're aliasing to */ | |
740 | *status = intStatus; | |
741 | } | |
374ca955 A |
742 | if(chAlias != stackAlias) { |
743 | uprv_free(chAlias); | |
744 | } | |
745 | if(mainRes != result) { | |
746 | ures_close(mainRes); | |
747 | } | |
b75a7d8f A |
748 | return result; |
749 | } | |
750 | } else { | |
751 | /* bad alias, should be an error */ | |
752 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
753 | return resB; | |
754 | } | |
755 | } else { | |
756 | *status = U_TOO_MANY_ALIASES_ERROR; | |
757 | return resB; | |
758 | } | |
759 | } | |
760 | if(resB == NULL) { | |
761 | resB = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); | |
762 | /* test for NULL */ | |
763 | if (resB == NULL) { | |
764 | *status = U_MEMORY_ALLOCATION_ERROR; | |
765 | return NULL; | |
766 | } | |
767 | ures_setIsStackObject(resB, FALSE); | |
768 | resB->fResPath = NULL; | |
374ca955 | 769 | resB->fResPathLen = 0; |
b75a7d8f A |
770 | } else { |
771 | if(resB->fData != NULL) { | |
772 | entryClose(resB->fData); | |
773 | } | |
774 | if(resB->fVersion != NULL) { | |
775 | uprv_free(resB->fVersion); | |
776 | } | |
777 | if(ures_isStackObject(resB) != FALSE) { | |
778 | ures_initStackObject(resB); | |
779 | } | |
374ca955 A |
780 | if(parent != resB) { |
781 | ures_freeResPath(resB); | |
782 | } | |
b75a7d8f A |
783 | } |
784 | resB->fData = realData; | |
785 | entryIncrease(resB->fData); | |
786 | resB->fHasFallback = FALSE; | |
787 | resB->fIsTopLevel = FALSE; | |
788 | resB->fIndex = -1; | |
789 | resB->fKey = key; | |
374ca955 A |
790 | resB->fParentRes = parent; |
791 | resB->fTopLevelData = parent->fTopLevelData; | |
792 | if(parent->fResPath && parent != resB) { | |
b75a7d8f A |
793 | ures_appendResPath(resB, parent->fResPath, parent->fResPathLen); |
794 | } | |
795 | if(key != NULL) { | |
796 | ures_appendResPath(resB, key, uprv_strlen(key)); | |
797 | ures_appendResPath(resB, RES_PATH_SEPARATOR_S, 1); | |
798 | } else { | |
799 | char buf[256]; | |
800 | int32_t len = T_CString_integerToString(buf, index, 10); | |
801 | ures_appendResPath(resB, buf, len); | |
802 | ures_appendResPath(resB, RES_PATH_SEPARATOR_S, 1); | |
803 | } | |
804 | ||
805 | resB->fVersion = NULL; | |
806 | resB->fRes = r; | |
807 | /*resB->fParent = parent->fRes;*/ | |
808 | resB->fResData.data = rdata->data; | |
809 | resB->fResData.pRoot = rdata->pRoot; | |
810 | resB->fResData.rootRes = rdata->rootRes; | |
811 | resB->fSize = res_countArrayItems(&(resB->fResData), resB->fRes); | |
812 | return resB; | |
813 | } | |
814 | ||
815 | UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status) { | |
816 | UBool isStackObject; | |
817 | if(U_FAILURE(*status) || r == original) { | |
818 | return r; | |
819 | } | |
820 | if(original != NULL) { | |
821 | if(r == NULL) { | |
822 | isStackObject = FALSE; | |
823 | r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); | |
824 | /* test for NULL */ | |
825 | if (r == NULL) { | |
826 | *status = U_MEMORY_ALLOCATION_ERROR; | |
827 | return NULL; | |
828 | } | |
829 | } else { | |
830 | isStackObject = ures_isStackObject(r); | |
831 | if(U_FAILURE(*status)) { | |
832 | return r; | |
833 | } | |
834 | ures_close(r); | |
835 | if(isStackObject == FALSE) { | |
836 | r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); | |
837 | /* test for NULL */ | |
838 | if (r == NULL) { | |
839 | *status = U_MEMORY_ALLOCATION_ERROR; | |
840 | return NULL; | |
841 | } | |
842 | } | |
843 | } | |
844 | uprv_memcpy(r, original, sizeof(UResourceBundle)); | |
845 | r->fResPath = NULL; | |
374ca955 | 846 | r->fResPathLen = 0; |
b75a7d8f A |
847 | if(original->fResPath) { |
848 | ures_appendResPath(r, original->fResPath, original->fResPathLen); | |
849 | } | |
850 | ures_setIsStackObject(r, isStackObject); | |
851 | if(r->fData != NULL) { | |
852 | entryIncrease(r->fData); | |
853 | } | |
854 | return r; | |
855 | } else { | |
856 | return r; | |
857 | } | |
858 | } | |
859 | ||
860 | /** | |
861 | * Functions to retrieve data from resource bundles. | |
862 | */ | |
863 | ||
864 | U_CAPI const UChar* U_EXPORT2 ures_getString(const UResourceBundle* resB, int32_t* len, UErrorCode* status) { | |
865 | ||
866 | if (status==NULL || U_FAILURE(*status)) { | |
867 | return NULL; | |
868 | } | |
869 | if(resB == NULL) { | |
870 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
871 | return NULL; | |
872 | } | |
873 | ||
874 | switch(RES_GET_TYPE(resB->fRes)) { | |
875 | case URES_STRING: | |
876 | return res_getString(&(resB->fResData), resB->fRes, len); | |
877 | case URES_INT: | |
878 | case URES_INT_VECTOR: | |
879 | case URES_BINARY: | |
880 | case URES_ARRAY: | |
881 | case URES_TABLE: | |
374ca955 | 882 | case URES_TABLE32: |
b75a7d8f A |
883 | default: |
884 | *status = U_RESOURCE_TYPE_MISMATCH; | |
885 | } | |
886 | ||
887 | return NULL; | |
888 | } | |
889 | ||
890 | U_CAPI const uint8_t* U_EXPORT2 ures_getBinary(const UResourceBundle* resB, int32_t* len, | |
891 | UErrorCode* status) { | |
892 | if (status==NULL || U_FAILURE(*status)) { | |
893 | return NULL; | |
894 | } | |
895 | if(resB == NULL) { | |
896 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
897 | return NULL; | |
898 | } | |
899 | switch(RES_GET_TYPE(resB->fRes)) { | |
900 | case URES_BINARY: | |
901 | return res_getBinary(&(resB->fResData), resB->fRes, len); | |
902 | case URES_INT: | |
903 | case URES_STRING: | |
904 | case URES_INT_VECTOR: | |
905 | case URES_ARRAY: | |
906 | case URES_TABLE: | |
374ca955 | 907 | case URES_TABLE32: |
b75a7d8f A |
908 | default: |
909 | *status = U_RESOURCE_TYPE_MISMATCH; | |
910 | } | |
911 | ||
912 | return NULL; | |
913 | } | |
914 | ||
915 | U_CAPI const int32_t* U_EXPORT2 ures_getIntVector(const UResourceBundle* resB, int32_t* len, | |
916 | UErrorCode* status) { | |
917 | if (status==NULL || U_FAILURE(*status)) { | |
918 | return NULL; | |
919 | } | |
920 | if(resB == NULL) { | |
921 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
922 | return NULL; | |
923 | } | |
924 | switch(RES_GET_TYPE(resB->fRes)) { | |
925 | case URES_INT_VECTOR: | |
926 | return res_getIntVector(&(resB->fResData), resB->fRes, len); | |
927 | case URES_INT: | |
928 | case URES_STRING: | |
929 | case URES_ARRAY: | |
930 | case URES_BINARY: | |
931 | case URES_TABLE: | |
374ca955 | 932 | case URES_TABLE32: |
b75a7d8f A |
933 | default: |
934 | *status = U_RESOURCE_TYPE_MISMATCH; | |
935 | } | |
936 | ||
937 | return NULL; | |
938 | } | |
939 | ||
940 | /* this function returns a signed integer */ | |
941 | /* it performs sign extension */ | |
942 | U_CAPI int32_t U_EXPORT2 ures_getInt(const UResourceBundle* resB, UErrorCode *status) { | |
943 | if (status==NULL || U_FAILURE(*status)) { | |
944 | return 0xffffffff; | |
945 | } | |
946 | if(resB == NULL) { | |
947 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
948 | return 0xffffffff; | |
949 | } | |
950 | if(RES_GET_TYPE(resB->fRes) != URES_INT) { | |
951 | *status = U_RESOURCE_TYPE_MISMATCH; | |
952 | return 0xffffffff; | |
953 | } | |
954 | return RES_GET_INT(resB->fRes); | |
955 | } | |
956 | ||
957 | U_CAPI uint32_t U_EXPORT2 ures_getUInt(const UResourceBundle* resB, UErrorCode *status) { | |
958 | if (status==NULL || U_FAILURE(*status)) { | |
959 | return 0xffffffff; | |
960 | } | |
961 | if(resB == NULL) { | |
962 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
963 | return 0xffffffff; | |
964 | } | |
965 | if(RES_GET_TYPE(resB->fRes) != URES_INT) { | |
966 | *status = U_RESOURCE_TYPE_MISMATCH; | |
967 | return 0xffffffff; | |
968 | } | |
969 | return RES_GET_UINT(resB->fRes); | |
970 | } | |
971 | ||
972 | ||
374ca955 A |
973 | U_CAPI UResType U_EXPORT2 ures_getType(const UResourceBundle *resB) { |
974 | UResType type; | |
975 | ||
b75a7d8f A |
976 | if(resB == NULL) { |
977 | return URES_NONE; | |
978 | } | |
374ca955 A |
979 | type = (UResType) RES_GET_TYPE(resB->fRes); |
980 | return type == URES_TABLE32 ? URES_TABLE : type; | |
b75a7d8f A |
981 | } |
982 | ||
374ca955 | 983 | U_CAPI const char * U_EXPORT2 ures_getKey(const UResourceBundle *resB) { |
b75a7d8f A |
984 | if(resB == NULL) { |
985 | return NULL; | |
986 | } | |
987 | ||
988 | return(resB->fKey); | |
989 | } | |
990 | ||
374ca955 | 991 | U_CAPI int32_t U_EXPORT2 ures_getSize(const UResourceBundle *resB) { |
b75a7d8f A |
992 | if(resB == NULL) { |
993 | return 0; | |
994 | } | |
995 | ||
996 | return resB->fSize; | |
997 | } | |
998 | ||
999 | static const UChar* ures_getStringWithAlias(const UResourceBundle *resB, Resource r, int32_t sIndex, int32_t *len, UErrorCode *status) { | |
1000 | if(RES_GET_TYPE(r) == URES_ALIAS) { | |
1001 | const UChar* result = 0; | |
1002 | UResourceBundle *tempRes = ures_getByIndex(resB, sIndex, NULL, status); | |
1003 | result = ures_getString(tempRes, len, status); | |
1004 | ures_close(tempRes); | |
1005 | return result; | |
1006 | } else { | |
1007 | return res_getString(&(resB->fResData), r, len); | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | U_CAPI void U_EXPORT2 ures_resetIterator(UResourceBundle *resB){ | |
1012 | if(resB == NULL) { | |
1013 | return; | |
1014 | } | |
1015 | resB->fIndex = -1; | |
1016 | } | |
1017 | ||
374ca955 | 1018 | U_CAPI UBool U_EXPORT2 ures_hasNext(const UResourceBundle *resB) { |
b75a7d8f A |
1019 | if(resB == NULL) { |
1020 | return FALSE; | |
1021 | } | |
1022 | return (UBool)(resB->fIndex < resB->fSize-1); | |
1023 | } | |
1024 | ||
1025 | U_CAPI const UChar* U_EXPORT2 ures_getNextString(UResourceBundle *resB, int32_t* len, const char ** key, UErrorCode *status) { | |
1026 | Resource r = RES_BOGUS; | |
1027 | ||
1028 | if (status==NULL || U_FAILURE(*status)) { | |
1029 | return NULL; | |
1030 | } | |
1031 | if(resB == NULL) { | |
1032 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1033 | return NULL; | |
1034 | } | |
1035 | ||
1036 | if(resB->fIndex == resB->fSize-1) { | |
1037 | *status = U_INDEX_OUTOFBOUNDS_ERROR; | |
1038 | } else { | |
1039 | resB->fIndex++; | |
1040 | switch(RES_GET_TYPE(resB->fRes)) { | |
1041 | case URES_INT: | |
1042 | case URES_BINARY: | |
1043 | case URES_STRING: | |
1044 | return res_getString(&(resB->fResData), resB->fRes, len); | |
1045 | case URES_TABLE: | |
374ca955 | 1046 | case URES_TABLE32: |
b75a7d8f A |
1047 | r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, resB->fIndex, key); |
1048 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1049 | /* TODO: do the fallback */ | |
1050 | } | |
1051 | return ures_getStringWithAlias(resB, r, resB->fIndex, len, status); | |
1052 | case URES_ARRAY: | |
1053 | r = res_getArrayItem(&(resB->fResData), resB->fRes, resB->fIndex); | |
1054 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1055 | /* TODO: do the fallback */ | |
1056 | } | |
1057 | return ures_getStringWithAlias(resB, r, resB->fIndex, len, status); | |
1058 | case URES_ALIAS: | |
1059 | return ures_getStringWithAlias(resB, resB->fRes, resB->fIndex, len, status); | |
1060 | case URES_INT_VECTOR: | |
1061 | default: | |
1062 | return NULL; | |
1063 | break; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | return NULL; | |
1068 | } | |
1069 | ||
1070 | U_CAPI UResourceBundle* U_EXPORT2 ures_getNextResource(UResourceBundle *resB, UResourceBundle *fillIn, UErrorCode *status) { | |
1071 | const char *key = NULL; | |
1072 | Resource r = RES_BOGUS; | |
1073 | ||
1074 | if (status==NULL || U_FAILURE(*status)) { | |
1075 | /*return NULL;*/ | |
1076 | return fillIn; | |
1077 | } | |
1078 | if(resB == NULL) { | |
1079 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1080 | /*return NULL;*/ | |
1081 | return fillIn; | |
1082 | } | |
1083 | ||
1084 | if(resB->fIndex == resB->fSize-1) { | |
1085 | *status = U_INDEX_OUTOFBOUNDS_ERROR; | |
1086 | /*return NULL;*/ | |
1087 | } else { | |
1088 | resB->fIndex++; | |
1089 | switch(RES_GET_TYPE(resB->fRes)) { | |
1090 | case URES_INT: | |
1091 | case URES_BINARY: | |
1092 | case URES_STRING: | |
1093 | return ures_copyResb(fillIn, resB, status); | |
1094 | case URES_TABLE: | |
374ca955 | 1095 | case URES_TABLE32: |
b75a7d8f A |
1096 | r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, resB->fIndex, &key); |
1097 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1098 | /* TODO: do the fallback */ | |
1099 | } | |
1100 | return init_resb_result(&(resB->fResData), r, key, resB->fIndex, resB->fData, resB, 0, fillIn, status); | |
1101 | case URES_ARRAY: | |
1102 | r = res_getArrayItem(&(resB->fResData), resB->fRes, resB->fIndex); | |
1103 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1104 | /* TODO: do the fallback */ | |
1105 | } | |
1106 | return init_resb_result(&(resB->fResData), r, key, resB->fIndex, resB->fData, resB, 0, fillIn, status); | |
1107 | case URES_INT_VECTOR: | |
1108 | default: | |
1109 | /*return NULL;*/ | |
1110 | return fillIn; | |
1111 | } | |
1112 | } | |
1113 | /*return NULL;*/ | |
1114 | return fillIn; | |
1115 | } | |
1116 | ||
1117 | U_CAPI UResourceBundle* U_EXPORT2 ures_getByIndex(const UResourceBundle *resB, int32_t indexR, UResourceBundle *fillIn, UErrorCode *status) { | |
1118 | const char* key = NULL; | |
1119 | Resource r = RES_BOGUS; | |
1120 | ||
1121 | if (status==NULL || U_FAILURE(*status)) { | |
1122 | /*return NULL;*/ | |
1123 | return fillIn; | |
1124 | } | |
1125 | if(resB == NULL) { | |
1126 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1127 | /*return NULL;*/ | |
1128 | return fillIn; | |
1129 | } | |
1130 | ||
1131 | if(indexR >= 0 && resB->fSize > indexR) { | |
1132 | switch(RES_GET_TYPE(resB->fRes)) { | |
1133 | case URES_INT: | |
1134 | case URES_BINARY: | |
1135 | case URES_STRING: | |
1136 | return ures_copyResb(fillIn, resB, status); | |
1137 | case URES_TABLE: | |
374ca955 | 1138 | case URES_TABLE32: |
b75a7d8f A |
1139 | r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, indexR, &key); |
1140 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1141 | /* TODO: do the fallback */ | |
1142 | } | |
1143 | return init_resb_result(&(resB->fResData), r, key, indexR, resB->fData, resB, 0, fillIn, status); | |
1144 | case URES_ARRAY: | |
1145 | r = res_getArrayItem(&(resB->fResData), resB->fRes, indexR); | |
1146 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1147 | /* TODO: do the fallback */ | |
1148 | } | |
1149 | return init_resb_result(&(resB->fResData), r, key, indexR, resB->fData, resB, 0, fillIn, status); | |
1150 | case URES_INT_VECTOR: | |
1151 | default: | |
1152 | /*return NULL;*/ | |
1153 | return fillIn; | |
1154 | } | |
1155 | } else { | |
1156 | *status = U_MISSING_RESOURCE_ERROR; | |
1157 | } | |
1158 | /*return NULL;*/ | |
1159 | return fillIn; | |
1160 | } | |
1161 | ||
1162 | U_CAPI const UChar* U_EXPORT2 ures_getStringByIndex(const UResourceBundle *resB, int32_t indexS, int32_t* len, UErrorCode *status) { | |
1163 | const char* key = NULL; | |
1164 | Resource r = RES_BOGUS; | |
1165 | ||
1166 | if (status==NULL || U_FAILURE(*status)) { | |
1167 | return NULL; | |
1168 | } | |
1169 | if(resB == NULL) { | |
1170 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1171 | return NULL; | |
1172 | } | |
1173 | ||
1174 | if(indexS >= 0 && resB->fSize > indexS) { | |
1175 | switch(RES_GET_TYPE(resB->fRes)) { | |
1176 | case URES_INT: | |
1177 | case URES_BINARY: | |
1178 | case URES_STRING: | |
1179 | return res_getString(&(resB->fResData), resB->fRes, len); | |
1180 | case URES_TABLE: | |
374ca955 | 1181 | case URES_TABLE32: |
b75a7d8f A |
1182 | r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, indexS, &key); |
1183 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1184 | /* TODO: do the fallback */ | |
1185 | } | |
1186 | return ures_getStringWithAlias(resB, r, indexS, len, status); | |
1187 | case URES_ARRAY: | |
1188 | r = res_getArrayItem(&(resB->fResData), resB->fRes, indexS); | |
1189 | if(r == RES_BOGUS && resB->fHasFallback) { | |
1190 | /* TODO: do the fallback */ | |
1191 | } | |
1192 | return ures_getStringWithAlias(resB, r, indexS, len, status); | |
1193 | case URES_ALIAS: | |
1194 | return ures_getStringWithAlias(resB, resB->fRes, indexS, len, status); | |
1195 | ||
1196 | /*case URES_INT_VECTOR:*/ | |
1197 | /*default:*/ | |
1198 | /*return;*/ | |
1199 | } | |
1200 | } else { | |
1201 | *status = U_MISSING_RESOURCE_ERROR; | |
1202 | } | |
1203 | return NULL; | |
1204 | } | |
1205 | ||
1206 | /*U_CAPI const char *ures_getResPath(UResourceBundle *resB) { | |
1207 | return resB->fResPath; | |
1208 | }*/ | |
1209 | ||
1210 | U_CAPI UResourceBundle* U_EXPORT2 | |
1211 | ures_findResource(const char* path, UResourceBundle *fillIn, UErrorCode *status) | |
1212 | { | |
1213 | UResourceBundle *first = NULL; | |
1214 | UResourceBundle *result = fillIn; | |
1215 | char *packageName = NULL; | |
1216 | char *pathToResource = NULL; | |
1217 | char *locale = NULL, *localeEnd = NULL; | |
374ca955 A |
1218 | int32_t length; |
1219 | ||
b75a7d8f A |
1220 | if(status == NULL || U_FAILURE(*status)) { |
1221 | return result; | |
1222 | } | |
374ca955 A |
1223 | |
1224 | length = uprv_strlen(path)+1; | |
1225 | pathToResource = (char *)uprv_malloc(length*sizeof(char)); | |
b75a7d8f A |
1226 | /* test for NULL */ |
1227 | if(pathToResource == NULL) { | |
1228 | *status = U_MEMORY_ALLOCATION_ERROR; | |
1229 | return result; | |
374ca955 A |
1230 | } |
1231 | uprv_memcpy(pathToResource, path, length); | |
1232 | ||
b75a7d8f A |
1233 | locale = pathToResource; |
1234 | if(*pathToResource == RES_PATH_SEPARATOR) { /* there is a path specification */ | |
1235 | pathToResource++; | |
1236 | packageName = pathToResource; | |
1237 | pathToResource = uprv_strchr(pathToResource, RES_PATH_SEPARATOR); | |
1238 | if(pathToResource == NULL) { | |
1239 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1240 | } else { | |
1241 | *pathToResource = 0; | |
1242 | locale = pathToResource+1; | |
1243 | } | |
1244 | } | |
1245 | ||
374ca955 | 1246 | localeEnd = uprv_strchr(locale, RES_PATH_SEPARATOR); |
b75a7d8f A |
1247 | if(localeEnd != NULL) { |
1248 | *localeEnd = 0; | |
1249 | } | |
1250 | ||
1251 | first = ures_open(packageName, locale, status); | |
1252 | ||
1253 | if(U_SUCCESS(*status)) { | |
1254 | if(localeEnd) { | |
1255 | result = ures_findSubResource(first, localeEnd+1, fillIn, status); | |
1256 | } else { | |
1257 | result = ures_copyResb(fillIn, first, status); | |
1258 | } | |
1259 | ures_close(first); | |
1260 | } | |
1261 | uprv_free(pathToResource); | |
1262 | return result; | |
1263 | } | |
1264 | ||
1265 | U_CAPI UResourceBundle* U_EXPORT2 | |
374ca955 | 1266 | ures_findSubResource(const UResourceBundle *resB, char* path, UResourceBundle *fillIn, UErrorCode *status) |
b75a7d8f A |
1267 | { |
1268 | Resource res = RES_BOGUS; | |
1269 | UResourceBundle *result = fillIn; | |
b75a7d8f A |
1270 | const char *key; |
1271 | ||
1272 | if(status == NULL || U_FAILURE(*status)) { | |
1273 | return result; | |
1274 | } | |
1275 | ||
1276 | /* here we do looping and circular alias checking */ | |
374ca955 A |
1277 | /* this loop is here because aliasing is resolved on this level, not on res level */ |
1278 | /* so, when we encounter an alias, it is not an aggregate resource, so we return */ | |
1279 | do { | |
1280 | res = res_findResource(&(resB->fResData), resB->fRes, &path, &key); | |
1281 | if(res != RES_BOGUS) { | |
1282 | result = init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); | |
1283 | resB = result; | |
1284 | } else { | |
1285 | *status = U_MISSING_RESOURCE_ERROR; | |
1286 | break; | |
1287 | } | |
1288 | } while(uprv_strlen(path)); /* there is more stuff in the path */ | |
b75a7d8f | 1289 | |
374ca955 A |
1290 | return result; |
1291 | } | |
b75a7d8f | 1292 | |
374ca955 A |
1293 | U_CAPI UResourceBundle* U_EXPORT2 |
1294 | ures_getByKeyWithFallback(const UResourceBundle *resB, | |
1295 | const char* inKey, | |
1296 | UResourceBundle *fillIn, | |
1297 | UErrorCode *status) { | |
1298 | Resource res = RES_BOGUS; | |
1299 | /*UResourceDataEntry *realData = NULL;*/ | |
1300 | const char *key = inKey; | |
1301 | UResourceBundle *helper = NULL; | |
b75a7d8f | 1302 | |
374ca955 A |
1303 | if (status==NULL || U_FAILURE(*status)) { |
1304 | return fillIn; | |
1305 | } | |
1306 | if(resB == NULL) { | |
1307 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1308 | return fillIn; | |
1309 | } | |
1310 | ||
1311 | if(RES_GET_TYPE(resB->fRes) == URES_TABLE || RES_GET_TYPE(resB->fRes) == URES_TABLE32) { | |
1312 | int32_t t; | |
1313 | res = res_getTableItemByKey(&(resB->fResData), resB->fRes, &t, &key); | |
1314 | if(res == RES_BOGUS) { | |
1315 | UResourceDataEntry *dataEntry = resB->fData; | |
1316 | char path[256]; | |
1317 | char* myPath = path; | |
1318 | ||
1319 | while(res == RES_BOGUS && dataEntry->fParent != NULL) { /* Otherwise, we'll look in parents */ | |
1320 | dataEntry = dataEntry->fParent; | |
1321 | if(dataEntry->fBogus == U_ZERO_ERROR) { | |
1322 | uprv_strncpy(path, resB->fResPath, resB->fResPathLen); | |
1323 | uprv_strcpy(path+resB->fResPathLen, inKey); | |
1324 | myPath = path; | |
1325 | key = inKey; | |
1326 | do { | |
1327 | res = res_findResource(&(dataEntry->fData), dataEntry->fData.rootRes, &myPath, &key); | |
1328 | if (RES_GET_TYPE(res) == URES_ALIAS && *myPath) { | |
1329 | /* We hit an alias, but we didn't finish following the path. */ | |
1330 | helper = init_resb_result(&(dataEntry->fData), res, inKey, -1, dataEntry, resB, 0, helper, status); | |
1331 | dataEntry = helper->fData; | |
1332 | } | |
1333 | } while(uprv_strlen(myPath)); | |
1334 | } | |
1335 | } | |
1336 | /*const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status);*/ | |
1337 | if(res != RES_BOGUS) { | |
1338 | /* check if resB->fResPath gives the right name here */ | |
1339 | fillIn = init_resb_result(&(dataEntry->fData), res, inKey, -1, dataEntry, resB, 0, fillIn, status); | |
1340 | } else { | |
1341 | *status = U_MISSING_RESOURCE_ERROR; | |
1342 | } | |
1343 | } else { | |
1344 | fillIn = init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); | |
1345 | } | |
1346 | } | |
1347 | else { | |
1348 | *status = U_RESOURCE_TYPE_MISMATCH; | |
1349 | } | |
1350 | ures_close(helper); | |
1351 | return fillIn; | |
b75a7d8f A |
1352 | } |
1353 | ||
1354 | ||
1355 | U_CAPI UResourceBundle* U_EXPORT2 ures_getByKey(const UResourceBundle *resB, const char* inKey, UResourceBundle *fillIn, UErrorCode *status) { | |
1356 | Resource res = RES_BOGUS; | |
1357 | UResourceDataEntry *realData = NULL; | |
1358 | const char *key = inKey; | |
1359 | ||
1360 | if (status==NULL || U_FAILURE(*status)) { | |
1361 | return fillIn; | |
1362 | } | |
1363 | if(resB == NULL) { | |
1364 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1365 | return fillIn; | |
1366 | } | |
1367 | ||
374ca955 | 1368 | if(RES_GET_TYPE(resB->fRes) == URES_TABLE || RES_GET_TYPE(resB->fRes) == URES_TABLE32) { |
b75a7d8f A |
1369 | int32_t t; |
1370 | res = res_getTableItemByKey(&(resB->fResData), resB->fRes, &t, &key); | |
1371 | if(res == RES_BOGUS) { | |
1372 | key = inKey; | |
1373 | if(resB->fHasFallback == TRUE) { | |
1374 | const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); | |
1375 | if(U_SUCCESS(*status)) { | |
1376 | /* check if resB->fResPath gives the right name here */ | |
1377 | return init_resb_result(rd, res, key, -1, realData, resB, 0, fillIn, status); | |
1378 | } else { | |
1379 | *status = U_MISSING_RESOURCE_ERROR; | |
1380 | } | |
1381 | } else { | |
1382 | *status = U_MISSING_RESOURCE_ERROR; | |
1383 | } | |
1384 | } else { | |
1385 | return init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); | |
1386 | } | |
1387 | } | |
1388 | #if 0 | |
1389 | /* this is a kind of TODO item. If we have an array with an index table, we could do this. */ | |
1390 | /* not currently */ | |
1391 | else if(RES_GET_TYPE(resB->fRes) == URES_ARRAY && resB->fHasFallback == TRUE) { | |
1392 | /* here should go a first attempt to locate the key using index table */ | |
1393 | const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); | |
1394 | if(U_SUCCESS(*status)) { | |
1395 | return init_resb_result(rd, res, key, realData, resB, fillIn, status); | |
1396 | } else { | |
1397 | *status = U_MISSING_RESOURCE_ERROR; | |
1398 | } | |
1399 | } | |
1400 | #endif | |
1401 | else { | |
1402 | *status = U_RESOURCE_TYPE_MISMATCH; | |
1403 | } | |
1404 | return fillIn; | |
1405 | } | |
1406 | ||
1407 | U_CAPI const UChar* U_EXPORT2 ures_getStringByKey(const UResourceBundle *resB, const char* inKey, int32_t* len, UErrorCode *status) { | |
1408 | Resource res = RES_BOGUS; | |
1409 | UResourceDataEntry *realData = NULL; | |
1410 | const char* key = inKey; | |
1411 | ||
1412 | if (status==NULL || U_FAILURE(*status)) { | |
1413 | return NULL; | |
1414 | } | |
1415 | if(resB == NULL) { | |
1416 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1417 | return NULL; | |
1418 | } | |
1419 | ||
374ca955 | 1420 | if(RES_GET_TYPE(resB->fRes) == URES_TABLE || RES_GET_TYPE(resB->fRes) == URES_TABLE32) { |
b75a7d8f A |
1421 | int32_t t=0; |
1422 | ||
1423 | res = res_getTableItemByKey(&(resB->fResData), resB->fRes, &t, &key); | |
1424 | ||
1425 | if(res == RES_BOGUS) { | |
1426 | key = inKey; | |
1427 | if(resB->fHasFallback == TRUE) { | |
1428 | const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); | |
1429 | if(U_SUCCESS(*status)) { | |
1430 | switch (RES_GET_TYPE(res)) { | |
1431 | case URES_STRING: | |
1432 | case URES_TABLE: | |
374ca955 | 1433 | case URES_TABLE32: |
b75a7d8f A |
1434 | case URES_ARRAY: |
1435 | return res_getString(rd, res, len); | |
1436 | case URES_ALIAS: | |
1437 | { | |
1438 | const UChar* result = 0; | |
1439 | UResourceBundle *tempRes = ures_getByKey(resB, inKey, NULL, status); | |
1440 | result = ures_getString(tempRes, len, status); | |
1441 | ures_close(tempRes); | |
1442 | return result; | |
1443 | } | |
1444 | default: | |
1445 | *status = U_RESOURCE_TYPE_MISMATCH; | |
1446 | } | |
1447 | } else { | |
1448 | *status = U_MISSING_RESOURCE_ERROR; | |
1449 | } | |
1450 | } else { | |
1451 | *status = U_MISSING_RESOURCE_ERROR; | |
1452 | } | |
1453 | } else { | |
1454 | switch (RES_GET_TYPE(res)) { | |
1455 | case URES_STRING: | |
1456 | case URES_TABLE: | |
374ca955 | 1457 | case URES_TABLE32: |
b75a7d8f A |
1458 | case URES_ARRAY: |
1459 | return res_getString(&(resB->fResData), res, len); | |
1460 | case URES_ALIAS: | |
1461 | { | |
1462 | const UChar* result = 0; | |
1463 | UResourceBundle *tempRes = ures_getByKey(resB, inKey, NULL, status); | |
1464 | result = ures_getString(tempRes, len, status); | |
1465 | ures_close(tempRes); | |
1466 | return result; | |
1467 | } | |
1468 | default: | |
1469 | *status = U_RESOURCE_TYPE_MISMATCH; | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | #if 0 | |
1474 | /* this is a kind of TODO item. If we have an array with an index table, we could do this. */ | |
1475 | /* not currently */ | |
1476 | else if(RES_GET_TYPE(resB->fRes) == URES_ARRAY && resB->fHasFallback == TRUE) { | |
1477 | /* here should go a first attempt to locate the key using index table */ | |
1478 | const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); | |
1479 | if(U_SUCCESS(*status)) { | |
1480 | return res_getString(rd, res, len); | |
1481 | } else { | |
1482 | *status = U_MISSING_RESOURCE_ERROR; | |
1483 | } | |
1484 | } | |
1485 | #endif | |
1486 | else { | |
1487 | *status = U_RESOURCE_TYPE_MISMATCH; | |
1488 | } | |
1489 | return NULL; | |
1490 | } | |
1491 | ||
1492 | ||
1493 | /* TODO: clean from here down */ | |
1494 | ||
1495 | /** | |
1496 | * INTERNAL: Get the name of the first real locale (not placeholder) | |
1497 | * that has resource bundle data. | |
1498 | */ | |
1499 | U_CAPI const char* U_EXPORT2 | |
1500 | ures_getLocale(const UResourceBundle* resourceBundle, UErrorCode* status) | |
1501 | { | |
1502 | if (status==NULL || U_FAILURE(*status)) { | |
1503 | return NULL; | |
1504 | } | |
1505 | if (!resourceBundle) { | |
1506 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1507 | return NULL; | |
1508 | } else { | |
1509 | return resourceBundle->fData->fName; | |
1510 | } | |
1511 | } | |
1512 | ||
374ca955 A |
1513 | U_CAPI const char* U_EXPORT2 |
1514 | ures_getLocaleByType(const UResourceBundle* resourceBundle, | |
1515 | ULocDataLocaleType type, | |
1516 | UErrorCode* status) { | |
1517 | if (status==NULL || U_FAILURE(*status)) { | |
1518 | return NULL; | |
1519 | } | |
1520 | if (!resourceBundle) { | |
1521 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1522 | return NULL; | |
1523 | } else { | |
1524 | switch(type) { | |
1525 | case ULOC_ACTUAL_LOCALE: | |
1526 | return resourceBundle->fData->fName; | |
1527 | break; | |
1528 | case ULOC_VALID_LOCALE: | |
1529 | return resourceBundle->fTopLevelData->fName; | |
1530 | break; | |
1531 | case ULOC_REQUESTED_LOCALE: | |
1532 | return NULL; | |
1533 | break; | |
1534 | default: | |
1535 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1536 | return NULL; | |
1537 | } | |
1538 | } | |
1539 | } | |
1540 | ||
1541 | ||
b75a7d8f A |
1542 | /* |
1543 | U_CFUNC void ures_setResPath(UResourceBundle *resB, const char* toAdd) { | |
1544 | if(resB->fResPath == NULL) { | |
1545 | resB->fResPath = resB->fResBuf; | |
1546 | *(resB->fResPath) = 0; | |
1547 | } | |
1548 | resB->fResPathLen = uprv_strlen(toAdd); | |
1549 | if(RES_BUFSIZE <= resB->fResPathLen+1) { | |
1550 | if(resB->fResPath == resB->fResBuf) { | |
1551 | resB->fResPath = (char *)uprv_malloc((resB->fResPathLen+1)*sizeof(char)); | |
1552 | } else { | |
1553 | resB->fResPath = (char *)uprv_realloc(resB->fResPath, (resB->fResPathLen+1)*sizeof(char)); | |
1554 | } | |
1555 | } | |
1556 | uprv_strcpy(resB->fResPath, toAdd); | |
1557 | } | |
1558 | */ | |
1559 | U_CFUNC void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd) { | |
1560 | int32_t resPathLenOrig = resB->fResPathLen; | |
1561 | if(resB->fResPath == NULL) { | |
1562 | resB->fResPath = resB->fResBuf; | |
1563 | *(resB->fResPath) = 0; | |
1564 | resB->fResPathLen = 0; | |
1565 | } | |
1566 | resB->fResPathLen += lenToAdd; | |
1567 | if(RES_BUFSIZE <= resB->fResPathLen+1) { | |
1568 | if(resB->fResPath == resB->fResBuf) { | |
1569 | resB->fResPath = (char *)uprv_malloc((resB->fResPathLen+1)*sizeof(char)); | |
1570 | uprv_strcpy(resB->fResPath, resB->fResBuf); | |
1571 | } else { | |
1572 | resB->fResPath = (char *)uprv_realloc(resB->fResPath, (resB->fResPathLen+1)*sizeof(char)); | |
1573 | } | |
1574 | } | |
1575 | uprv_strcpy(resB->fResPath + resPathLenOrig, toAdd); | |
1576 | } | |
1577 | ||
1578 | U_CFUNC void ures_freeResPath(UResourceBundle *resB) { | |
1579 | if (resB->fResPath && resB->fResPath != resB->fResBuf) { | |
1580 | uprv_free(resB->fResPath); | |
1581 | } | |
1582 | resB->fResPath = NULL; | |
1583 | resB->fResPathLen = 0; | |
1584 | } | |
1585 | ||
374ca955 | 1586 | |
b75a7d8f A |
1587 | U_CFUNC const char* ures_getName(const UResourceBundle* resB) { |
1588 | if(resB == NULL) { | |
1589 | return NULL; | |
1590 | } | |
1591 | ||
1592 | return resB->fData->fName; | |
1593 | } | |
1594 | ||
1595 | U_CFUNC const char* ures_getPath(const UResourceBundle* resB) { | |
1596 | if(resB == NULL) { | |
1597 | return NULL; | |
1598 | } | |
1599 | ||
1600 | return resB->fData->fPath; | |
1601 | } | |
1602 | ||
1603 | /* OLD API implementation */ | |
1604 | ||
1605 | /** | |
1606 | * API: This function is used to open a resource bundle | |
1607 | * proper fallback chaining is executed while initialization. | |
1608 | * The result is stored in cache for later fallback search. | |
1609 | */ | |
1610 | U_CAPI void U_EXPORT2 | |
1611 | ures_openFillIn(UResourceBundle *r, const char* path, | |
1612 | const char* localeID, UErrorCode* status) { | |
1613 | if(r == NULL) { | |
1614 | *status = U_INTERNAL_PROGRAM_ERROR; | |
1615 | } else { | |
1616 | UResourceDataEntry *firstData; | |
1617 | r->fHasFallback = TRUE; | |
1618 | r->fIsTopLevel = TRUE; | |
1619 | r->fKey = NULL; | |
1620 | r->fVersion = NULL; | |
1621 | r->fIndex = -1; | |
1622 | if(r->fData != NULL) { | |
1623 | entryClose(r->fData); | |
1624 | } | |
1625 | if(r->fVersion != NULL) { | |
1626 | uprv_free(r->fVersion); | |
1627 | } | |
1628 | r->fData = entryOpen(path, localeID, status); | |
1629 | /* this is a quick fix to get regular data in bundle - until construction is cleaned up */ | |
1630 | firstData = r->fData; | |
1631 | while(firstData->fBogus != U_ZERO_ERROR && firstData->fParent != NULL) { | |
1632 | firstData = firstData->fParent; | |
1633 | } | |
1634 | r->fResData.data = firstData->fData.data; | |
1635 | r->fResData.pRoot = firstData->fData.pRoot; | |
1636 | r->fResData.rootRes = firstData->fData.rootRes; | |
1637 | r->fRes = r->fResData.rootRes; | |
1638 | r->fSize = res_countArrayItems(&(r->fResData), r->fRes); | |
1639 | /*r->fParent = RES_BOGUS;*/ | |
1640 | /*r->fResPath = NULL;*/ | |
374ca955 A |
1641 | r->fParentRes = NULL; |
1642 | r->fTopLevelData = r->fData; | |
1643 | ||
b75a7d8f | 1644 | ures_freeResPath(r); |
b75a7d8f A |
1645 | } |
1646 | } | |
374ca955 | 1647 | |
b75a7d8f A |
1648 | U_CAPI UResourceBundle* U_EXPORT2 |
1649 | ures_open(const char* path, | |
1650 | const char* localeID, | |
1651 | UErrorCode* status) | |
1652 | { | |
1653 | char canonLocaleID[100]; | |
1654 | UResourceDataEntry *hasData = NULL; | |
1655 | UResourceBundle *r; | |
1656 | int32_t length; | |
1657 | ||
1658 | if(status == NULL || U_FAILURE(*status)) { | |
1659 | return NULL; | |
1660 | } | |
1661 | ||
1662 | /* first "canonicalize" the locale ID */ | |
374ca955 | 1663 | length = uloc_getBaseName(localeID, canonLocaleID, sizeof(canonLocaleID), status); |
b75a7d8f A |
1664 | if(U_FAILURE(*status) || *status == U_STRING_NOT_TERMINATED_WARNING) { |
1665 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1666 | return NULL; | |
1667 | } | |
1668 | ||
1669 | r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); | |
1670 | if(r == NULL) { | |
1671 | *status = U_MEMORY_ALLOCATION_ERROR; | |
1672 | return NULL; | |
1673 | } | |
1674 | ||
1675 | r->fHasFallback = TRUE; | |
1676 | r->fIsTopLevel = TRUE; | |
1677 | ures_setIsStackObject(r, FALSE); | |
1678 | r->fKey = NULL; | |
1679 | r->fVersion = NULL; | |
1680 | r->fIndex = -1; | |
1681 | r->fData = entryOpen(path, canonLocaleID, status); | |
1682 | if(U_FAILURE(*status)) { | |
1683 | uprv_free(r); | |
1684 | return NULL; | |
1685 | } | |
374ca955 A |
1686 | r->fParentRes = NULL; |
1687 | r->fTopLevelData = r->fData; | |
b75a7d8f A |
1688 | |
1689 | hasData = r->fData; | |
1690 | while(hasData->fBogus != U_ZERO_ERROR) { | |
1691 | hasData = hasData->fParent; | |
1692 | if(hasData == NULL) { | |
1693 | /* This can happen only if fallback chain gets broken by an act of God */ | |
1694 | /* TODO: this unlikely to happen, consider removing it */ | |
1695 | entryClose(r->fData); | |
1696 | uprv_free(r); | |
1697 | *status = U_MISSING_RESOURCE_ERROR; | |
1698 | return NULL; | |
1699 | } | |
1700 | } | |
1701 | ||
1702 | r->fResData.data = hasData->fData.data; | |
1703 | r->fResData.pRoot = hasData->fData.pRoot; | |
1704 | r->fResData.rootRes = hasData->fData.rootRes; | |
1705 | r->fRes = r->fResData.rootRes; | |
1706 | /*r->fParent = RES_BOGUS;*/ | |
1707 | r->fSize = res_countArrayItems(&(r->fResData), r->fRes); | |
1708 | r->fResPath = NULL; | |
374ca955 | 1709 | r->fResPathLen = 0; |
b75a7d8f A |
1710 | /* |
1711 | if(r->fData->fPath != NULL) { | |
1712 | ures_setResPath(r, r->fData->fPath); | |
1713 | ures_appendResPath(r, RES_PATH_PACKAGE_S); | |
1714 | ures_appendResPath(r, r->fData->fName); | |
1715 | } else { | |
1716 | ures_setResPath(r, r->fData->fName); | |
1717 | } | |
1718 | */ | |
1719 | ||
1720 | ||
1721 | return r; | |
1722 | } | |
1723 | ||
b75a7d8f A |
1724 | /** |
1725 | * Opens a resource bundle without "canonicalizing" the locale name. No fallback will be performed | |
1726 | * or sought. However, alias substitution will happen! | |
1727 | */ | |
1728 | U_CAPI UResourceBundle* U_EXPORT2 | |
1729 | ures_openDirect(const char* path, const char* localeID, UErrorCode* status) { | |
1730 | UResourceBundle *r; | |
374ca955 | 1731 | UErrorCode subStatus = U_ZERO_ERROR; |
b75a7d8f A |
1732 | |
1733 | if(status == NULL || U_FAILURE(*status)) { | |
1734 | return NULL; | |
1735 | } | |
1736 | ||
1737 | r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); | |
1738 | if(r == NULL) { | |
1739 | *status = U_MEMORY_ALLOCATION_ERROR; | |
1740 | return NULL; | |
1741 | } | |
1742 | ||
1743 | r->fHasFallback = FALSE; | |
1744 | r->fIsTopLevel = TRUE; | |
1745 | ures_setIsStackObject(r, FALSE); | |
1746 | r->fIndex = -1; | |
374ca955 A |
1747 | r->fData = entryOpen(path, localeID, &subStatus); |
1748 | if(U_FAILURE(subStatus)) { | |
1749 | *status = subStatus; | |
b75a7d8f A |
1750 | uprv_free(r); |
1751 | return NULL; | |
1752 | } | |
374ca955 | 1753 | if(subStatus != U_ZERO_ERROR /*r->fData->fBogus != U_ZERO_ERROR*/) { |
b75a7d8f A |
1754 | /* we didn't find one we were looking for - so openDirect */ |
1755 | /* should fail */ | |
1756 | entryClose(r->fData); | |
1757 | uprv_free(r); | |
1758 | *status = U_MISSING_RESOURCE_ERROR; | |
1759 | return NULL; | |
1760 | } | |
1761 | ||
1762 | r->fKey = NULL; | |
1763 | r->fVersion = NULL; | |
1764 | r->fResData.data = r->fData->fData.data; | |
1765 | r->fResData.pRoot = r->fData->fData.pRoot; | |
1766 | r->fResData.rootRes = r->fData->fData.rootRes; | |
1767 | r->fRes = r->fResData.rootRes; | |
1768 | /*r->fParent = RES_BOGUS;*/ | |
1769 | r->fSize = res_countArrayItems(&(r->fResData), r->fRes); | |
1770 | r->fResPath = NULL; | |
374ca955 A |
1771 | r->fResPathLen = 0; |
1772 | r->fParentRes = NULL; | |
1773 | r->fTopLevelData = r->fData; | |
b75a7d8f | 1774 | |
374ca955 | 1775 | return r; |
b75a7d8f A |
1776 | } |
1777 | ||
1778 | /** | |
1779 | * API: Counts members. For arrays and tables, returns number of resources. | |
1780 | * For strings, returns 1. | |
1781 | */ | |
1782 | U_CAPI int32_t U_EXPORT2 | |
1783 | ures_countArrayItems(const UResourceBundle* resourceBundle, | |
1784 | const char* resourceKey, | |
1785 | UErrorCode* status) | |
1786 | { | |
1787 | UResourceBundle resData; | |
1788 | ures_initStackObject(&resData); | |
374ca955 A |
1789 | if (status==NULL || U_FAILURE(*status)) { |
1790 | return 0; | |
1791 | } | |
1792 | if(resourceBundle == NULL) { | |
1793 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
1794 | return 0; | |
1795 | } | |
b75a7d8f | 1796 | ures_getByKey(resourceBundle, resourceKey, &resData, status); |
374ca955 | 1797 | |
b75a7d8f | 1798 | if(resData.fResData.data != NULL) { |
374ca955 A |
1799 | int32_t result = res_countArrayItems(&resData.fResData, resData.fRes); |
1800 | ures_close(&resData); | |
1801 | return result; | |
b75a7d8f | 1802 | } else { |
374ca955 A |
1803 | *status = U_MISSING_RESOURCE_ERROR; |
1804 | ures_close(&resData); | |
1805 | return 0; | |
b75a7d8f A |
1806 | } |
1807 | } | |
1808 | ||
1809 | U_CAPI void U_EXPORT2 | |
1810 | ures_close(UResourceBundle* resB) | |
1811 | { | |
1812 | if(resB != NULL) { | |
1813 | if(resB->fData != NULL) { | |
1814 | entryClose(resB->fData); | |
1815 | } | |
1816 | if(resB->fVersion != NULL) { | |
1817 | uprv_free(resB->fVersion); | |
1818 | } | |
1819 | ures_freeResPath(resB); | |
1820 | ||
1821 | if(ures_isStackObject(resB) == FALSE) { | |
1822 | uprv_free(resB); | |
1823 | } | |
374ca955 A |
1824 | else { |
1825 | #if 0 /*U_DEBUG*/ | |
1826 | /* poison the data */ | |
1827 | uprv_memset(resB, -1, sizeof(UResourceBundle)); | |
1828 | #endif | |
1829 | } | |
b75a7d8f A |
1830 | } |
1831 | } | |
1832 | ||
1833 | U_CAPI const char* U_EXPORT2 | |
1834 | ures_getVersionNumber(const UResourceBundle* resourceBundle) | |
1835 | { | |
1836 | if (!resourceBundle) return NULL; | |
1837 | ||
1838 | if(resourceBundle->fVersion == NULL) { | |
1839 | ||
1840 | /* If the version ID has not been built yet, then do so. Retrieve */ | |
1841 | /* the minor version from the file. */ | |
1842 | UErrorCode status = U_ZERO_ERROR; | |
1843 | int32_t minor_len = 0; | |
1844 | int32_t len; | |
1845 | ||
1846 | const UChar* minor_version = ures_getStringByKey(resourceBundle, kVersionTag, &minor_len, &status); | |
374ca955 | 1847 | |
b75a7d8f A |
1848 | /* Determine the length of of the final version string. This is */ |
1849 | /* the length of the major part + the length of the separator */ | |
1850 | /* (==1) + the length of the minor part (+ 1 for the zero byte at */ | |
1851 | /* the end). */ | |
1852 | ||
1853 | len = (minor_len > 0) ? minor_len : 1; | |
1854 | ||
1855 | /* Allocate the string, and build it up. */ | |
1856 | /* + 1 for zero byte */ | |
1857 | ||
1858 | ||
1859 | ((UResourceBundle *)resourceBundle)->fVersion = (char *)uprv_malloc(1 + len); | |
374ca955 | 1860 | |
b75a7d8f A |
1861 | if(minor_len > 0) { |
1862 | u_UCharsToChars(minor_version, resourceBundle->fVersion , minor_len); | |
1863 | resourceBundle->fVersion[len] = '\0'; | |
1864 | } | |
1865 | else { | |
374ca955 | 1866 | uprv_strcpy(resourceBundle->fVersion, kDefaultMinorVersion); |
b75a7d8f A |
1867 | } |
1868 | } | |
1869 | ||
1870 | return resourceBundle->fVersion; | |
1871 | } | |
1872 | ||
1873 | U_CAPI void U_EXPORT2 ures_getVersion(const UResourceBundle* resB, UVersionInfo versionInfo) { | |
1874 | if (!resB) return; | |
1875 | ||
1876 | u_versionFromString(versionInfo, ures_getVersionNumber(resB)); | |
1877 | } | |
1878 | ||
374ca955 A |
1879 | /** Tree support functions *******************************/ |
1880 | #define INDEX_LOCALE_NAME "res_index" | |
1881 | #define INDEX_TAG "InstalledLocales" | |
1882 | #define DEFAULT_TAG "default" | |
1883 | ||
1884 | #if defined(URES_TREE_DEBUG) | |
1885 | #include <stdio.h> | |
1886 | #endif | |
1887 | ||
1888 | typedef struct ULocalesContext { | |
1889 | UResourceBundle installed; | |
1890 | UResourceBundle curr; | |
1891 | } ULocalesContext; | |
1892 | ||
1893 | static void U_CALLCONV | |
1894 | ures_loc_closeLocales(UEnumeration *enumerator) { | |
1895 | ULocalesContext *ctx = (ULocalesContext *)enumerator->context; | |
1896 | ures_close(&ctx->curr); | |
1897 | ures_close(&ctx->installed); | |
1898 | uprv_free(ctx); | |
1899 | uprv_free(enumerator); | |
1900 | } | |
1901 | ||
1902 | static int32_t U_CALLCONV | |
1903 | ures_loc_countLocales(UEnumeration *en, UErrorCode *status) { | |
1904 | ULocalesContext *ctx = (ULocalesContext *)en->context; | |
1905 | return ures_getSize(&ctx->installed); | |
1906 | } | |
1907 | ||
1908 | static const char* U_CALLCONV | |
1909 | ures_loc_nextLocale(UEnumeration* en, | |
1910 | int32_t* resultLength, | |
1911 | UErrorCode* status) { | |
1912 | ULocalesContext *ctx = (ULocalesContext *)en->context; | |
1913 | UResourceBundle *res = &(ctx->installed); | |
1914 | UResourceBundle *k = NULL; | |
1915 | const char *result = NULL; | |
1916 | int32_t len = 0; | |
1917 | if(ures_hasNext(res) && (k = ures_getNextResource(res, &ctx->curr, status))) { | |
1918 | result = ures_getKey(k); | |
1919 | len = uprv_strlen(result); | |
1920 | } | |
1921 | if (resultLength) { | |
1922 | *resultLength = len; | |
1923 | } | |
1924 | return result; | |
1925 | } | |
1926 | ||
1927 | static void U_CALLCONV | |
1928 | ures_loc_resetLocales(UEnumeration* en, | |
1929 | UErrorCode* status) { | |
1930 | UResourceBundle *res = &((ULocalesContext *)en->context)->installed; | |
1931 | ures_resetIterator(res); | |
1932 | } | |
1933 | ||
1934 | ||
1935 | static const UEnumeration gLocalesEnum = { | |
1936 | NULL, | |
1937 | NULL, | |
1938 | ures_loc_closeLocales, | |
1939 | ures_loc_countLocales, | |
1940 | uenum_unextDefault, | |
1941 | ures_loc_nextLocale, | |
1942 | ures_loc_resetLocales | |
1943 | }; | |
1944 | ||
1945 | ||
1946 | U_CAPI UEnumeration* U_EXPORT2 | |
1947 | ures_openAvailableLocales(const char *path, UErrorCode *status) | |
1948 | { | |
1949 | UResourceBundle *index = NULL; | |
1950 | UEnumeration *en = NULL; | |
1951 | ULocalesContext *myContext = NULL; | |
1952 | ||
1953 | if(U_FAILURE(*status)) { | |
1954 | return NULL; | |
1955 | } | |
1956 | myContext = uprv_malloc(sizeof(ULocalesContext)); | |
1957 | en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); | |
1958 | if(!en || !myContext) { | |
1959 | *status = U_MEMORY_ALLOCATION_ERROR; | |
1960 | uprv_free(en); | |
1961 | uprv_free(myContext); | |
1962 | return NULL; | |
1963 | } | |
1964 | uprv_memcpy(en, &gLocalesEnum, sizeof(UEnumeration)); | |
1965 | ||
1966 | ures_initStackObject(&myContext->installed); | |
1967 | ures_initStackObject(&myContext->curr); | |
1968 | index = ures_openDirect(path, INDEX_LOCALE_NAME, status); | |
1969 | ures_getByKey(index, INDEX_TAG, &myContext->installed, status); | |
1970 | if(U_SUCCESS(*status)) { | |
1971 | #if defined(URES_TREE_DEBUG) | |
1972 | fprintf(stderr, "Got %s::%s::[%s] : %s\n", | |
1973 | path, INDEX_LOCALE_NAME, INDEX_TAG, ures_getKey(&myContext->installed)); | |
1974 | #endif | |
1975 | en->context = myContext; | |
1976 | } else { | |
1977 | #if defined(URES_TREE_DEBUG) | |
1978 | fprintf(stderr, "%s open failed - %s\n", path, u_errorName(*status)); | |
1979 | #endif | |
1980 | ures_close(&myContext->installed); | |
1981 | uprv_free(myContext); | |
1982 | uprv_free(en); | |
1983 | en = NULL; | |
1984 | } | |
1985 | ||
1986 | ures_close(index); | |
1987 | ||
1988 | return en; | |
1989 | } | |
1990 | ||
1991 | U_CAPI int32_t U_EXPORT2 | |
1992 | ures_getFunctionalEquivalent(char *result, int32_t resultCapacity, | |
1993 | const char *path, const char *resName, const char *keyword, const char *locid, | |
1994 | UBool *isAvailable, UBool omitDefault, UErrorCode *status) | |
1995 | { | |
1996 | char kwVal[1024] = ""; /* value of keyword 'keyword' */ | |
1997 | char defVal[1024] = ""; /* default value for given locale */ | |
1998 | char defLoc[1024] = ""; /* default value for given locale */ | |
1999 | char base[1024] = ""; /* base locale */ | |
2000 | char found[1024]; | |
2001 | char parent[1024]; | |
2002 | char full[1024] = ""; | |
2003 | UResourceBundle bund1, bund2; | |
2004 | UResourceBundle *res = NULL; | |
2005 | UErrorCode subStatus = U_ZERO_ERROR; | |
2006 | int32_t length = 0; | |
2007 | if(U_FAILURE(*status)) return 0; | |
2008 | if(isAvailable) { | |
2009 | *isAvailable = TRUE; | |
2010 | } | |
2011 | uloc_getKeywordValue(locid, keyword, kwVal, 1024-1,&subStatus); | |
2012 | if(!uprv_strcmp(kwVal, DEFAULT_TAG)) { | |
2013 | kwVal[0]=0; | |
2014 | } | |
2015 | uloc_getBaseName(locid, base, 1024-1,&subStatus); | |
2016 | #if defined(URES_TREE_DEBUG) | |
2017 | fprintf(stderr, "getFunctionalEquivalent: \"%s\" [%s=%s] in %s - %s\n", | |
2018 | locid, keyword, kwVal, base, u_errorName(subStatus)); | |
2019 | #endif | |
2020 | ures_initStackObject(&bund1); | |
2021 | ures_initStackObject(&bund2); | |
2022 | ||
2023 | ||
2024 | uprv_strcpy(parent, base); | |
2025 | uprv_strcpy(found, base); | |
2026 | ||
2027 | if(U_FAILURE(subStatus)) { | |
2028 | *status = subStatus; | |
2029 | return 0; | |
2030 | } | |
2031 | ||
2032 | do { | |
2033 | subStatus = U_ZERO_ERROR; | |
2034 | res = ures_open(path, parent, &subStatus); | |
2035 | if(((subStatus == U_USING_FALLBACK_WARNING) || | |
2036 | (subStatus == U_USING_DEFAULT_WARNING)) && isAvailable) { | |
2037 | *isAvailable = FALSE; | |
2038 | } | |
2039 | isAvailable = NULL; /* only want to set this the first time around */ | |
2040 | ||
2041 | #if defined(URES_TREE_DEBUG) | |
2042 | fprintf(stderr, "%s;%s -> %s [%s]\n", path?path:"ICUDATA", parent, u_errorName(subStatus), ures_getLocale(res, &subStatus)); | |
2043 | #endif | |
2044 | if(U_FAILURE(subStatus)) { | |
2045 | *status = subStatus; | |
2046 | } else if(subStatus == U_ZERO_ERROR) { | |
2047 | ures_getByKey(res,resName,&bund1, &subStatus); | |
2048 | if(subStatus == U_ZERO_ERROR) { | |
2049 | const UChar *defUstr; | |
2050 | int32_t defLen; | |
2051 | /* look for default item */ | |
2052 | #if defined(URES_TREE_DEBUG) | |
2053 | fprintf(stderr, "%s;%s : loaded default -> %s\n", | |
2054 | path?path:"ICUDATA", parent, u_errorName(subStatus)); | |
2055 | #endif | |
2056 | defUstr = ures_getStringByKey(&bund1, DEFAULT_TAG, &defLen, &subStatus); | |
2057 | if(U_SUCCESS(subStatus) && defLen) { | |
2058 | u_UCharsToChars(defUstr, defVal, u_strlen(defUstr)); | |
2059 | #if defined(URES_TREE_DEBUG) | |
2060 | fprintf(stderr, "%s;%s -> default %s=%s, %s\n", | |
2061 | path?path:"ICUDATA", parent, keyword, defVal, u_errorName(subStatus)); | |
2062 | #endif | |
2063 | uprv_strcpy(defLoc, parent); | |
2064 | if(kwVal[0]==0) { | |
2065 | uprv_strcpy(kwVal, defVal); | |
2066 | #if defined(URES_TREE_DEBUG) | |
2067 | fprintf(stderr, "%s;%s -> kwVal = %s\n", | |
2068 | path?path:"ICUDATA", parent, keyword, kwVal); | |
2069 | #endif | |
2070 | } | |
2071 | } | |
2072 | } | |
2073 | } | |
2074 | ||
2075 | subStatus = U_ZERO_ERROR; | |
2076 | ||
2077 | uprv_strcpy(found, parent); | |
2078 | uloc_getParent(found,parent,1023,&subStatus); | |
2079 | ures_close(res); | |
2080 | } while(!defVal[0] && *found && U_SUCCESS(*status)); | |
2081 | ||
2082 | /* Now, see if we can find the kwVal collator.. start the search over.. */ | |
2083 | uprv_strcpy(parent, base); | |
2084 | uprv_strcpy(found, base); | |
2085 | ||
2086 | do { | |
2087 | subStatus = U_ZERO_ERROR; | |
2088 | res = ures_open(path, parent, &subStatus); | |
2089 | if((subStatus == U_USING_FALLBACK_WARNING) && isAvailable) { | |
2090 | *isAvailable = FALSE; | |
2091 | } | |
2092 | isAvailable = NULL; /* only want to set this the first time around */ | |
2093 | ||
2094 | #if defined(URES_TREE_DEBUG) | |
2095 | fprintf(stderr, "%s;%s -> %s (looking for %s)\n", | |
2096 | path?path:"ICUDATA", parent, u_errorName(subStatus), kwVal); | |
2097 | #endif | |
2098 | if(U_FAILURE(subStatus)) { | |
2099 | *status = subStatus; | |
2100 | } else if(subStatus == U_ZERO_ERROR) { | |
2101 | ures_getByKey(res,resName,&bund1, &subStatus); | |
2102 | #if defined(URES_TREE_DEBUG) | |
2103 | /**/ fprintf(stderr,"@%d [%s] %s\n", __LINE__, resName, u_errorName(subStatus)); | |
2104 | #endif | |
2105 | if(subStatus == U_ZERO_ERROR) { | |
2106 | ures_getByKey(&bund1, kwVal, &bund2, &subStatus); | |
2107 | #if defined(URES_TREE_DEBUG) | |
2108 | /**/ fprintf(stderr,"@%d [%s] %s\n", __LINE__, kwVal, u_errorName(subStatus)); | |
2109 | #endif | |
2110 | if(subStatus == U_ZERO_ERROR) { | |
2111 | #if defined(URES_TREE_DEBUG) | |
2112 | fprintf(stderr, "%s;%s -> full0 %s=%s, %s\n", | |
2113 | path?path:"ICUDATA", parent, keyword, kwVal, u_errorName(subStatus)); | |
2114 | #endif | |
2115 | uprv_strcpy(full, parent); | |
2116 | if(*full == 0) { | |
2117 | uprv_strcpy(full, "root"); | |
2118 | } | |
2119 | /* now, recalculate default kw if need be */ | |
2120 | if(uprv_strlen(defLoc) > uprv_strlen(full)) { | |
2121 | const UChar *defUstr; | |
2122 | int32_t defLen; | |
2123 | /* look for default item */ | |
2124 | #if defined(URES_TREE_DEBUG) | |
2125 | fprintf(stderr, "%s;%s -> recalculating Default0\n", | |
2126 | path?path:"ICUDATA", full); | |
2127 | #endif | |
2128 | defUstr = ures_getStringByKey(&bund1, DEFAULT_TAG, &defLen, &subStatus); | |
2129 | if(U_SUCCESS(subStatus) && defLen) { | |
2130 | u_UCharsToChars(defUstr, defVal, u_strlen(defUstr)); | |
2131 | #if defined(URES_TREE_DEBUG) | |
2132 | fprintf(stderr, "%s;%s -> default0 %s=%s, %s\n", | |
2133 | path?path:"ICUDATA", full, keyword, defVal, u_errorName(subStatus)); | |
2134 | #endif | |
2135 | uprv_strcpy(defLoc, full); | |
2136 | } | |
2137 | } /* end of recalculate default KW */ | |
2138 | #if defined(URES_TREE_DEBUG) | |
2139 | else { | |
2140 | fprintf(stderr, "No trim0, %s <= %s\n", defLoc, full); | |
2141 | } | |
2142 | #endif | |
2143 | } else { | |
2144 | #if defined(URES_TREE_DEBUG) | |
2145 | fprintf(stderr, "err=%s in %s looking for %s\n", | |
2146 | u_errorName(subStatus), parent, kwVal); | |
2147 | #endif | |
2148 | } | |
2149 | } | |
2150 | } | |
2151 | ||
2152 | subStatus = U_ZERO_ERROR; | |
2153 | ||
2154 | uprv_strcpy(found, parent); | |
2155 | uloc_getParent(found,parent,1023,&subStatus); | |
2156 | ures_close(res); | |
2157 | } while(!full[0] && *found && U_SUCCESS(*status)); | |
2158 | ||
2159 | if((full[0]==0) && uprv_strcmp(kwVal, defVal)) { | |
2160 | #if defined(URES_TREE_DEBUG) | |
2161 | fprintf(stderr, "Failed to locate kw %s - try default %s\n", kwVal, defVal); | |
2162 | #endif | |
2163 | uprv_strcpy(kwVal, defVal); | |
2164 | uprv_strcpy(parent, base); | |
2165 | uprv_strcpy(found, base); | |
2166 | ||
2167 | do { /* search for 'default' named item */ | |
2168 | subStatus = U_ZERO_ERROR; | |
2169 | res = ures_open(path, parent, &subStatus); | |
2170 | if((subStatus == U_USING_FALLBACK_WARNING) && isAvailable) { | |
2171 | *isAvailable = FALSE; | |
2172 | } | |
2173 | isAvailable = NULL; /* only want to set this the first time around */ | |
2174 | ||
2175 | #if defined(URES_TREE_DEBUG) | |
2176 | fprintf(stderr, "%s;%s -> %s (looking for default %s)\n", | |
2177 | path?path:"ICUDATA", parent, u_errorName(subStatus), kwVal); | |
2178 | #endif | |
2179 | if(U_FAILURE(subStatus)) { | |
2180 | *status = subStatus; | |
2181 | } else if(subStatus == U_ZERO_ERROR) { | |
2182 | ures_getByKey(res,resName,&bund1, &subStatus); | |
2183 | if(subStatus == U_ZERO_ERROR) { | |
2184 | ures_getByKey(&bund1, kwVal, &bund2, &subStatus); | |
2185 | if(subStatus == U_ZERO_ERROR) { | |
2186 | #if defined(URES_TREE_DEBUG) | |
2187 | fprintf(stderr, "%s;%s -> full1 %s=%s, %s\n", path?path:"ICUDATA", | |
2188 | parent, keyword, kwVal, u_errorName(subStatus)); | |
2189 | #endif | |
2190 | uprv_strcpy(full, parent); | |
2191 | if(*full == 0) { | |
2192 | uprv_strcpy(full, "root"); | |
2193 | } | |
2194 | ||
2195 | /* now, recalculate default kw if need be */ | |
2196 | if(uprv_strlen(defLoc) > uprv_strlen(full)) { | |
2197 | const UChar *defUstr; | |
2198 | int32_t defLen; | |
2199 | /* look for default item */ | |
2200 | #if defined(URES_TREE_DEBUG) | |
2201 | fprintf(stderr, "%s;%s -> recalculating Default1\n", | |
2202 | path?path:"ICUDATA", full); | |
2203 | #endif | |
2204 | defUstr = ures_getStringByKey(&bund1, DEFAULT_TAG, &defLen, &subStatus); | |
2205 | if(U_SUCCESS(subStatus) && defLen) { | |
2206 | u_UCharsToChars(defUstr, defVal, u_strlen(defUstr)); | |
2207 | #if defined(URES_TREE_DEBUG) | |
2208 | fprintf(stderr, "%s;%s -> default %s=%s, %s\n", | |
2209 | path?path:"ICUDATA", full, keyword, defVal, u_errorName(subStatus)); | |
2210 | #endif | |
2211 | uprv_strcpy(defLoc, full); | |
2212 | } | |
2213 | } /* end of recalculate default KW */ | |
2214 | #if defined(URES_TREE_DEBUG) | |
2215 | else { | |
2216 | fprintf(stderr, "No trim1, %s <= %s\n", defLoc, full); | |
2217 | } | |
2218 | #endif | |
2219 | } | |
2220 | } | |
2221 | } | |
2222 | subStatus = U_ZERO_ERROR; | |
2223 | ||
2224 | uprv_strcpy(found, parent); | |
2225 | uloc_getParent(found,parent,1023,&subStatus); | |
2226 | ures_close(res); | |
2227 | } while(!full[0] && *found && U_SUCCESS(*status)); | |
2228 | } | |
2229 | ||
2230 | if(U_SUCCESS(*status)) { | |
2231 | if(!full[0]) { | |
2232 | #if defined(URES_TREE_DEBUG) | |
2233 | fprintf(stderr, "Still could not load keyword %s=%s\n", keyword, kwVal); | |
2234 | #endif | |
2235 | *status = U_MISSING_RESOURCE_ERROR; | |
2236 | } else if(omitDefault) { | |
2237 | #if defined(URES_TREE_DEBUG) | |
2238 | fprintf(stderr,"Trim? full=%s, defLoc=%s, found=%s\n", full, defLoc, found); | |
2239 | #endif | |
2240 | if(uprv_strlen(defLoc) <= uprv_strlen(full)) { | |
2241 | /* found the keyword in a *child* of where the default tag was present. */ | |
2242 | if(!uprv_strcmp(kwVal, defVal)) { /* if the requested kw is default, */ | |
2243 | /* and the default is in or in an ancestor of the current locale */ | |
2244 | #if defined(URES_TREE_DEBUG) | |
2245 | fprintf(stderr, "Removing unneeded var %s=%s\n", keyword, kwVal); | |
2246 | #endif | |
2247 | kwVal[0]=0; | |
2248 | } | |
2249 | } | |
2250 | } | |
2251 | uprv_strcpy(found, full); | |
2252 | if(kwVal[0]) { | |
2253 | uprv_strcat(found, "@"); | |
2254 | uprv_strcat(found, keyword); | |
2255 | uprv_strcat(found, "="); | |
2256 | uprv_strcat(found, kwVal); | |
2257 | } else if(!omitDefault) { | |
2258 | uprv_strcat(found, "@"); | |
2259 | uprv_strcat(found, keyword); | |
2260 | uprv_strcat(found, "="); | |
2261 | uprv_strcat(found, defVal); | |
2262 | } | |
2263 | } | |
2264 | /* we found the default locale - no need to repeat it.*/ | |
2265 | ||
2266 | ures_close(&bund1); | |
2267 | ures_close(&bund2); | |
2268 | ||
2269 | length = uprv_strlen(found); | |
2270 | ||
2271 | if(U_SUCCESS(*status)) { | |
2272 | int32_t copyLength = uprv_min(length, resultCapacity); | |
2273 | if(copyLength>0) { | |
2274 | uprv_strncpy(result, found, copyLength); | |
2275 | } | |
2276 | if(length == 0) { | |
2277 | *status = U_MISSING_RESOURCE_ERROR; | |
2278 | } | |
2279 | } else { | |
2280 | length = 0; | |
2281 | result[0]=0; | |
2282 | } | |
2283 | return u_terminateChars(result, resultCapacity, length, status); | |
2284 | } | |
2285 | ||
2286 | U_CAPI UEnumeration* U_EXPORT2 | |
2287 | ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status) | |
2288 | { | |
2289 | #define VALUES_BUF_SIZE 2048 | |
2290 | #define VALUES_LIST_SIZE 512 | |
2291 | ||
2292 | char valuesBuf[VALUES_BUF_SIZE]; | |
2293 | int32_t valuesIndex = 0; | |
2294 | const char *valuesList[VALUES_LIST_SIZE]; | |
2295 | int32_t valuesCount = 0; | |
2296 | ||
2297 | const char *locale; | |
2298 | int32_t locLen; | |
2299 | ||
2300 | UEnumeration *locs = NULL; | |
2301 | ||
2302 | UResourceBundle item; | |
2303 | UResourceBundle subItem; | |
2304 | ||
2305 | ures_initStackObject(&item); | |
2306 | ures_initStackObject(&subItem); | |
2307 | locs = ures_openAvailableLocales(path, status); | |
2308 | ||
2309 | if(U_FAILURE(*status)) { | |
2310 | ures_close(&item); | |
2311 | ures_close(&subItem); | |
2312 | return NULL; | |
2313 | } | |
2314 | ||
2315 | valuesBuf[0]=0; | |
2316 | valuesBuf[1]=0; | |
2317 | ||
2318 | while((locale = uenum_next(locs, &locLen, status))) { | |
2319 | UResourceBundle *bund = NULL; | |
2320 | UResourceBundle *subPtr = NULL; | |
2321 | UErrorCode subStatus = U_ZERO_ERROR; /* don't fail if a bundle is unopenable */ | |
2322 | bund = ures_openDirect(path, locale, &subStatus); | |
2323 | ||
2324 | #if defined(URES_TREE_DEBUG) | |
2325 | if(!bund || U_FAILURE(subStatus)) { | |
2326 | fprintf(stderr, "%s-%s values: Can't open %s locale - skipping. (%s)\n", | |
2327 | path?path:"<ICUDATA>", keyword, locale, u_errorName(subStatus)); | |
2328 | } | |
2329 | #endif | |
2330 | ||
2331 | ures_getByKey(bund, keyword, &item, &subStatus); | |
2332 | ||
2333 | if(!bund || U_FAILURE(subStatus)) { | |
2334 | #if defined(URES_TREE_DEBUG) | |
2335 | fprintf(stderr, "%s-%s values: Can't find in %s - skipping. (%s)\n", | |
2336 | path?path:"<ICUDATA>", keyword, locale, u_errorName(subStatus)); | |
2337 | #endif | |
2338 | ures_close(bund); | |
2339 | bund = NULL; | |
2340 | continue; | |
2341 | } | |
2342 | ||
2343 | while((subPtr = ures_getNextResource(&item,&subItem,&subStatus)) | |
2344 | && U_SUCCESS(subStatus)) { | |
2345 | const char *k; | |
2346 | int32_t i; | |
2347 | k = ures_getKey(&subItem); | |
2348 | ||
2349 | #if defined(URES_TREE_DEBUG) | |
2350 | /* fprintf(stderr, "%s | %s | %s | %s\n", path?path:"<ICUDATA>", keyword, locale, k); */ | |
2351 | #endif | |
2352 | for(i=0;k&&i<valuesCount;i++) { | |
2353 | if(!uprv_strcmp(valuesList[i],k)) { | |
2354 | k = NULL; /* found duplicate */ | |
2355 | } | |
2356 | } | |
2357 | if(k && *k) { | |
2358 | int32_t kLen = uprv_strlen(k); | |
2359 | if(!uprv_strcmp(k,DEFAULT_TAG)) { | |
2360 | continue; /* don't need 'default'. */ | |
2361 | } | |
2362 | if((valuesCount >= (VALUES_LIST_SIZE-1)) || /* no more space in list .. */ | |
2363 | ((valuesIndex+kLen+1+1) >= VALUES_BUF_SIZE)) { /* no more space in buffer (string + 2 nulls) */ | |
2364 | *status = U_ILLEGAL_ARGUMENT_ERROR; /* out of space.. */ | |
2365 | } else { | |
2366 | uprv_strcpy(valuesBuf+valuesIndex, k); | |
2367 | valuesList[valuesCount++] = valuesBuf+valuesIndex; | |
2368 | valuesIndex += kLen; | |
2369 | #if defined(URES_TREE_DEBUG) | |
2370 | fprintf(stderr, "%s | %s | %s | [%s] (UNIQUE)\n", | |
2371 | path?path:"<ICUDATA>", keyword, locale, k); | |
2372 | #endif | |
2373 | valuesBuf[valuesIndex++] = 0; /* terminate */ | |
2374 | } | |
2375 | } | |
2376 | } | |
2377 | ures_close(bund); | |
2378 | } | |
2379 | valuesBuf[valuesIndex++] = 0; /* terminate */ | |
2380 | ||
2381 | ures_close(&item); | |
2382 | ures_close(&subItem); | |
2383 | uenum_close(locs); | |
2384 | #if defined(URES_TREE_DEBUG) | |
2385 | fprintf(stderr, "%s: size %d, #%d\n", u_errorName(*status), | |
2386 | valuesIndex, valuesCount); | |
2387 | #endif | |
2388 | return uloc_openKeywordList(valuesBuf, valuesIndex, status); | |
2389 | } | |
2390 | ||
b75a7d8f | 2391 | /* eof */ |