]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
3 | * | |
729e4ab9 | 4 | * Copyright (C) 1999-2010, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ****************************************************************************** | |
729e4ab9 | 8 | * file name: udata.cpp |
b75a7d8f A |
9 | * encoding: US-ASCII |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 1999oct25 | |
14 | * created by: Markus W. Scherer | |
15 | */ | |
16 | ||
729e4ab9 A |
17 | #include "unicode/utypes.h" /* U_LINUX */ |
18 | ||
19 | #ifdef U_LINUX | |
20 | /* if gcc | |
21 | #define ATTRIBUTE_WEAK __attribute__ ((weak)) | |
22 | might have to #include some other header | |
23 | */ | |
24 | #endif | |
25 | ||
b75a7d8f | 26 | #include "unicode/putil.h" |
b75a7d8f A |
27 | #include "unicode/udata.h" |
28 | #include "unicode/uversion.h" | |
729e4ab9 A |
29 | #include "charstr.h" |
30 | #include "cmemory.h" | |
31 | #include "cstring.h" | |
374ca955 | 32 | #include "putilimp.h" |
729e4ab9 A |
33 | #include "ucln_cmn.h" |
34 | #include "ucmndata.h" | |
b75a7d8f | 35 | #include "udatamem.h" |
729e4ab9 | 36 | #include "uhash.h" |
b75a7d8f | 37 | #include "umapfile.h" |
729e4ab9 | 38 | #include "umutex.h" |
b75a7d8f A |
39 | |
40 | /*********************************************************************** | |
41 | * | |
42 | * Notes on the organization of the ICU data implementation | |
43 | * | |
44 | * All of the public API is defined in udata.h | |
45 | * | |
46 | * The implementation is split into several files... | |
47 | * | |
48 | * - udata.c (this file) contains higher level code that knows about | |
49 | * the search paths for locating data, caching opened data, etc. | |
50 | * | |
51 | * - umapfile.c contains the low level platform-specific code for actually loading | |
52 | * (memory mapping, file reading, whatever) data into memory. | |
53 | * | |
54 | * - ucmndata.c deals with the tables of contents of ICU data items within | |
55 | * an ICU common format data file. The implementation includes | |
56 | * an abstract interface and support for multiple TOC formats. | |
57 | * All knowledge of any specific TOC format is encapsulated here. | |
58 | * | |
59 | * - udatamem.c has code for managing UDataMemory structs. These are little | |
60 | * descriptor objects for blocks of memory holding ICU data of | |
61 | * various types. | |
62 | */ | |
63 | ||
64 | /* configuration ---------------------------------------------------------- */ | |
65 | ||
66 | /* If you are excruciatingly bored turn this on .. */ | |
67 | /* #define UDATA_DEBUG 1 */ | |
68 | ||
b75a7d8f A |
69 | #if defined(UDATA_DEBUG) |
70 | # include <stdio.h> | |
71 | #endif | |
72 | ||
729e4ab9 A |
73 | #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) |
74 | ||
75 | U_NAMESPACE_USE | |
76 | ||
77 | /* | |
78 | * Forwards | |
79 | */ | |
80 | static UDataMemory *udata_findCachedData(const char *path); | |
b75a7d8f A |
81 | |
82 | /*********************************************************************** | |
83 | * | |
84 | * static (Global) data | |
85 | * | |
86 | ************************************************************************/ | |
b75a7d8f | 87 | |
729e4ab9 A |
88 | /* |
89 | * Pointers to the common ICU data. | |
90 | * | |
91 | * We store multiple pointers to ICU data packages and iterate through them | |
92 | * when looking for a data item. | |
93 | * | |
94 | * It is possible to combine this with dependency inversion: | |
95 | * One or more data package libraries may export | |
96 | * functions that each return a pointer to their piece of the ICU data, | |
97 | * and this file would import them as weak functions, without a | |
98 | * strong linker dependency from the common library on the data library. | |
99 | * | |
100 | * Then we can have applications depend on only that part of ICU's data | |
101 | * that they really need, reducing the size of binaries that take advantage | |
102 | * of this. | |
103 | */ | |
104 | static UDataMemory *gCommonICUDataArray[10] = { NULL }; | |
105 | ||
106 | static UBool gHaveTriedToLoadCommonData = FALSE; /* See extendICUData(). */ | |
b75a7d8f A |
107 | |
108 | static UHashtable *gCommonDataCache = NULL; /* Global hash table of opened ICU data files. */ | |
109 | ||
73c04bcf | 110 | static UDataFileAccess gDataFileAccess = UDATA_DEFAULT_ACCESS; |
b75a7d8f | 111 | |
374ca955 A |
112 | static UBool U_CALLCONV |
113 | udata_cleanup(void) | |
b75a7d8f | 114 | { |
729e4ab9 A |
115 | int32_t i; |
116 | ||
b75a7d8f A |
117 | if (gCommonDataCache) { /* Delete the cache of user data mappings. */ |
118 | uhash_close(gCommonDataCache); /* Table owns the contents, and will delete them. */ | |
119 | gCommonDataCache = NULL; /* Cleanup is not thread safe. */ | |
120 | } | |
121 | ||
729e4ab9 A |
122 | for (i = 0; i < LENGTHOF(gCommonICUDataArray) && gCommonICUDataArray[i] != NULL; ++i) { |
123 | udata_close(gCommonICUDataArray[i]); | |
124 | gCommonICUDataArray[i] = NULL; | |
b75a7d8f | 125 | } |
729e4ab9 | 126 | gHaveTriedToLoadCommonData = FALSE; |
b75a7d8f A |
127 | |
128 | return TRUE; /* Everything was cleaned up */ | |
129 | } | |
130 | ||
729e4ab9 A |
131 | static UBool U_CALLCONV |
132 | findCommonICUDataByName(const char *inBasename) | |
133 | { | |
134 | UBool found = FALSE; | |
135 | int32_t i; | |
136 | ||
137 | UDataMemory *pData = udata_findCachedData(inBasename); | |
138 | if (pData == NULL) | |
139 | return FALSE; | |
140 | ||
141 | for (i = 0; i < LENGTHOF(gCommonICUDataArray); ++i) { | |
142 | if ((gCommonICUDataArray[i] != NULL) && (gCommonICUDataArray[i]->pHeader == pData->pHeader)) { | |
143 | /* The data pointer is already in the array. */ | |
144 | found = TRUE; | |
145 | break; | |
146 | } | |
147 | } | |
b75a7d8f | 148 | |
729e4ab9 A |
149 | return found; |
150 | } | |
b75a7d8f A |
151 | |
152 | ||
153 | /* | |
154 | * setCommonICUData. Set a UDataMemory to be the global ICU Data | |
155 | */ | |
729e4ab9 | 156 | static UBool |
b75a7d8f | 157 | setCommonICUData(UDataMemory *pData, /* The new common data. Belongs to caller, we copy it. */ |
b75a7d8f A |
158 | UBool warn, /* If true, set USING_DEFAULT warning if ICUData was */ |
159 | /* changed by another thread before we got to it. */ | |
160 | UErrorCode *pErr) | |
161 | { | |
162 | UDataMemory *newCommonData = UDataMemory_createNewInstance(pErr); | |
729e4ab9 A |
163 | int32_t i; |
164 | UBool didUpdate = FALSE; | |
b75a7d8f | 165 | if (U_FAILURE(*pErr)) { |
729e4ab9 | 166 | return FALSE; |
b75a7d8f A |
167 | } |
168 | ||
169 | /* For the assignment, other threads must cleanly see either the old */ | |
170 | /* or the new, not some partially initialized new. The old can not be */ | |
171 | /* deleted - someone may still have a pointer to it lying around in */ | |
172 | /* their locals. */ | |
173 | UDatamemory_assign(newCommonData, pData); | |
174 | umtx_lock(NULL); | |
729e4ab9 A |
175 | for (i = 0; i < LENGTHOF(gCommonICUDataArray); ++i) { |
176 | if (gCommonICUDataArray[i] == NULL) { | |
177 | gCommonICUDataArray[i] = newCommonData; | |
178 | ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); | |
179 | didUpdate = TRUE; | |
180 | break; | |
181 | } else if (gCommonICUDataArray[i]->pHeader == pData->pHeader) { | |
182 | /* The same data pointer is already in the array. */ | |
183 | break; | |
b75a7d8f | 184 | } |
b75a7d8f A |
185 | } |
186 | umtx_unlock(NULL); | |
729e4ab9 A |
187 | |
188 | if (i == LENGTHOF(gCommonICUDataArray) && warn) { | |
189 | *pErr = U_USING_DEFAULT_WARNING; | |
190 | } | |
191 | if (!didUpdate) { | |
192 | uprv_free(newCommonData); | |
193 | } | |
194 | return didUpdate; | |
195 | } | |
196 | ||
197 | static UBool | |
198 | setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) { | |
199 | UDataMemory tData; | |
200 | UDataMemory_init(&tData); | |
201 | tData.pHeader = (const DataHeader *)pData; | |
202 | udata_checkCommonData(&tData, pErrorCode); | |
203 | return setCommonICUData(&tData, FALSE, pErrorCode); | |
b75a7d8f A |
204 | } |
205 | ||
206 | static const char * | |
207 | findBasename(const char *path) { | |
208 | const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR); | |
209 | if(basename==NULL) { | |
210 | return path; | |
211 | } else { | |
212 | return basename+1; | |
213 | } | |
214 | } | |
215 | ||
374ca955 | 216 | #ifdef UDATA_DEBUG |
b75a7d8f A |
217 | static const char * |
218 | packageNameFromPath(const char *path) | |
219 | { | |
220 | if((path == NULL) || (*path == 0)) { | |
221 | return U_ICUDATA_NAME; | |
222 | } | |
223 | ||
224 | path = findBasename(path); | |
225 | ||
226 | if((path == NULL) || (*path == 0)) { | |
227 | return U_ICUDATA_NAME; | |
228 | } | |
229 | ||
230 | return path; | |
231 | } | |
374ca955 | 232 | #endif |
b75a7d8f A |
233 | |
234 | /*----------------------------------------------------------------------* | |
235 | * * | |
236 | * Cache for common data * | |
237 | * Functions for looking up or adding entries to a cache of * | |
238 | * data that has been previously opened. Avoids a potentially * | |
239 | * expensive operation of re-opening the data for subsequent * | |
240 | * uses. * | |
241 | * * | |
242 | * Data remains cached for the duration of the process. * | |
243 | * * | |
244 | *----------------------------------------------------------------------*/ | |
245 | ||
246 | typedef struct DataCacheElement { | |
247 | char *name; | |
248 | UDataMemory *item; | |
249 | } DataCacheElement; | |
250 | ||
251 | ||
252 | ||
253 | /* | |
254 | * Deleter function for DataCacheElements. | |
255 | * udata cleanup function closes the hash table; hash table in turn calls back to | |
256 | * here for each entry. | |
257 | */ | |
73c04bcf | 258 | static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) { |
b75a7d8f A |
259 | DataCacheElement *p = (DataCacheElement *)pDCEl; |
260 | udata_close(p->item); /* unmaps storage */ | |
261 | uprv_free(p->name); /* delete the hash key string. */ | |
262 | uprv_free(pDCEl); /* delete 'this' */ | |
263 | } | |
264 | ||
265 | /* udata_getCacheHashTable() | |
266 | * Get the hash table used to store the data cache entries. | |
267 | * Lazy create it if it doesn't yet exist. | |
268 | */ | |
269 | static UHashtable *udata_getHashTable() { | |
374ca955 A |
270 | UErrorCode err = U_ZERO_ERROR; |
271 | UBool cacheIsInitialized; | |
272 | UHashtable *tHT = NULL; | |
b75a7d8f | 273 | |
46f4442e | 274 | UMTX_CHECK(NULL, (gCommonDataCache != NULL), cacheIsInitialized); |
374ca955 A |
275 | |
276 | if (cacheIsInitialized) { | |
b75a7d8f A |
277 | return gCommonDataCache; |
278 | } | |
374ca955 | 279 | |
73c04bcf | 280 | tHT = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err); |
46f4442e A |
281 | /* Check for null pointer. */ |
282 | if (tHT == NULL) { | |
283 | return NULL; /* TODO: Handle this error better. */ | |
284 | } | |
374ca955 A |
285 | uhash_setValueDeleter(tHT, DataCacheElement_deleter); |
286 | ||
b75a7d8f A |
287 | umtx_lock(NULL); |
288 | if (gCommonDataCache == NULL) { | |
374ca955 A |
289 | gCommonDataCache = tHT; |
290 | tHT = NULL; | |
291 | ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); | |
b75a7d8f A |
292 | } |
293 | umtx_unlock(NULL); | |
374ca955 A |
294 | if (tHT != NULL) { |
295 | uhash_close(tHT); | |
296 | } | |
b75a7d8f A |
297 | |
298 | if (U_FAILURE(err)) { | |
299 | return NULL; /* TODO: handle this error better. */ | |
300 | } | |
301 | return gCommonDataCache; | |
302 | } | |
303 | ||
304 | ||
305 | ||
306 | static UDataMemory *udata_findCachedData(const char *path) | |
307 | { | |
308 | UHashtable *htable; | |
309 | UDataMemory *retVal = NULL; | |
310 | DataCacheElement *el; | |
311 | const char *baseName; | |
312 | ||
313 | baseName = findBasename(path); /* Cache remembers only the base name, not the full path. */ | |
314 | htable = udata_getHashTable(); | |
315 | umtx_lock(NULL); | |
316 | el = (DataCacheElement *)uhash_get(htable, baseName); | |
317 | umtx_unlock(NULL); | |
318 | if (el != NULL) { | |
319 | retVal = el->item; | |
320 | } | |
321 | #ifdef UDATA_DEBUG | |
322 | fprintf(stderr, "Cache: [%s] -> %p\n", baseName, retVal); | |
323 | #endif | |
324 | return retVal; | |
325 | } | |
326 | ||
327 | ||
328 | static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) { | |
329 | DataCacheElement *newElement; | |
330 | const char *baseName; | |
331 | int32_t nameLen; | |
332 | UHashtable *htable; | |
729e4ab9 | 333 | DataCacheElement *oldValue = NULL; |
374ca955 | 334 | UErrorCode subErr = U_ZERO_ERROR; |
b75a7d8f A |
335 | |
336 | if (U_FAILURE(*pErr)) { | |
337 | return NULL; | |
338 | } | |
339 | ||
340 | /* Create a new DataCacheElement - the thingy we store in the hash table - | |
341 | * and copy the supplied path and UDataMemoryItems into it. | |
342 | */ | |
729e4ab9 | 343 | newElement = (DataCacheElement *)uprv_malloc(sizeof(DataCacheElement)); |
b75a7d8f A |
344 | if (newElement == NULL) { |
345 | *pErr = U_MEMORY_ALLOCATION_ERROR; | |
346 | return NULL; | |
347 | } | |
348 | newElement->item = UDataMemory_createNewInstance(pErr); | |
349 | if (U_FAILURE(*pErr)) { | |
73c04bcf | 350 | uprv_free(newElement); |
b75a7d8f A |
351 | return NULL; |
352 | } | |
353 | UDatamemory_assign(newElement->item, item); | |
354 | ||
355 | baseName = findBasename(path); | |
356 | nameLen = (int32_t)uprv_strlen(baseName); | |
729e4ab9 | 357 | newElement->name = (char *)uprv_malloc(nameLen+1); |
b75a7d8f A |
358 | if (newElement->name == NULL) { |
359 | *pErr = U_MEMORY_ALLOCATION_ERROR; | |
73c04bcf A |
360 | uprv_free(newElement->item); |
361 | uprv_free(newElement); | |
b75a7d8f A |
362 | return NULL; |
363 | } | |
364 | uprv_strcpy(newElement->name, baseName); | |
365 | ||
366 | /* Stick the new DataCacheElement into the hash table. | |
367 | */ | |
368 | htable = udata_getHashTable(); | |
369 | umtx_lock(NULL); | |
729e4ab9 | 370 | oldValue = (DataCacheElement *)uhash_get(htable, path); |
b75a7d8f | 371 | if (oldValue != NULL) { |
374ca955 A |
372 | subErr = U_USING_DEFAULT_WARNING; |
373 | } | |
b75a7d8f A |
374 | else { |
375 | uhash_put( | |
376 | htable, | |
377 | newElement->name, /* Key */ | |
378 | newElement, /* Value */ | |
374ca955 | 379 | &subErr); |
b75a7d8f A |
380 | } |
381 | umtx_unlock(NULL); | |
382 | ||
383 | #ifdef UDATA_DEBUG | |
374ca955 A |
384 | fprintf(stderr, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement->name, |
385 | newElement->item, u_errorName(subErr), newElement->item->vFuncs); | |
b75a7d8f A |
386 | #endif |
387 | ||
374ca955 A |
388 | if (subErr == U_USING_DEFAULT_WARNING || U_FAILURE(subErr)) { |
389 | *pErr = subErr; /* copy sub err unto fillin ONLY if something happens. */ | |
b75a7d8f A |
390 | uprv_free(newElement->name); |
391 | uprv_free(newElement->item); | |
392 | uprv_free(newElement); | |
729e4ab9 | 393 | return oldValue ? oldValue->item : NULL; |
b75a7d8f A |
394 | } |
395 | ||
396 | return newElement->item; | |
397 | } | |
398 | ||
b75a7d8f A |
399 | /*----------------------------------------------------------------------*============== |
400 | * * | |
401 | * Path management. Could be shared with other tools/etc if need be * | |
402 | * later on. * | |
403 | * * | |
404 | *----------------------------------------------------------------------*/ | |
405 | ||
374ca955 A |
406 | #define U_DATA_PATHITER_BUFSIZ 128 /* Size of local buffer for paths */ |
407 | /* Overflow causes malloc of larger buf */ | |
b75a7d8f | 408 | |
729e4ab9 A |
409 | U_NAMESPACE_BEGIN |
410 | ||
411 | class UDataPathIterator | |
b75a7d8f | 412 | { |
729e4ab9 A |
413 | public: |
414 | UDataPathIterator(const char *path, const char *pkg, | |
415 | const char *item, const char *suffix, UBool doCheckLastFour, | |
416 | UErrorCode *pErrorCode); | |
417 | const char *next(UErrorCode *pErrorCode); | |
418 | ||
419 | private: | |
b75a7d8f A |
420 | const char *path; /* working path (u_icudata_Dir) */ |
421 | const char *nextPath; /* path following this one */ | |
422 | const char *basename; /* item's basename (icudt22e_mt.res)*/ | |
423 | const char *suffix; /* item suffix (can be null) */ | |
424 | ||
374ca955 A |
425 | uint32_t basenameLen; /* length of basename */ |
426 | ||
729e4ab9 A |
427 | CharString itemPath; /* path passed in with item name */ |
428 | CharString pathBuffer; /* output path for this it'ion */ | |
429 | CharString packageStub; /* example: "/icudt28b". Will ignore that leaf in set paths. */ | |
b75a7d8f | 430 | |
374ca955 A |
431 | UBool checkLastFour; /* if TRUE then allow paths such as '/foo/myapp.dat' |
432 | * to match, checks last 4 chars of suffix with | |
433 | * last 4 of path, then previous chars. */ | |
729e4ab9 | 434 | }; |
b75a7d8f | 435 | |
729e4ab9 | 436 | /** |
b75a7d8f A |
437 | * @param iter The iterator to be initialized. Its current state does not matter. |
438 | * @param path The full pathname to be iterated over. If NULL, defaults to U_ICUDATA_NAME | |
374ca955 | 439 | * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leave directories such as /icudt28l |
b75a7d8f | 440 | * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat |
374ca955 A |
441 | * @param suffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly. |
442 | * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2. | |
443 | * '/blarg/stuff.dat' would also be found. | |
b75a7d8f | 444 | */ |
729e4ab9 A |
445 | UDataPathIterator::UDataPathIterator(const char *inPath, const char *pkg, |
446 | const char *item, const char *inSuffix, UBool doCheckLastFour, | |
447 | UErrorCode *pErrorCode) | |
b75a7d8f A |
448 | { |
449 | #ifdef UDATA_DEBUG | |
729e4ab9 | 450 | fprintf(stderr, "SUFFIX1=%s PATH=%s\n", inSuffix, inPath); |
b75a7d8f A |
451 | #endif |
452 | /** Path **/ | |
729e4ab9 A |
453 | if(inPath == NULL) { |
454 | path = u_getDataDirectory(); | |
b75a7d8f | 455 | } else { |
729e4ab9 | 456 | path = inPath; |
b75a7d8f A |
457 | } |
458 | ||
374ca955 | 459 | /** Package **/ |
729e4ab9 A |
460 | if(pkg != NULL) { |
461 | packageStub.append(U_FILE_SEP_CHAR, *pErrorCode).append(pkg, *pErrorCode); | |
374ca955 | 462 | #ifdef UDATA_DEBUG |
729e4ab9 | 463 | fprintf(stderr, "STUB=%s [%d]\n", packageStub.data(), packageStub.length()); |
374ca955 A |
464 | #endif |
465 | } | |
466 | ||
b75a7d8f | 467 | /** Item **/ |
729e4ab9 A |
468 | basename = findBasename(item); |
469 | basenameLen = (int32_t)uprv_strlen(basename); | |
b75a7d8f | 470 | |
b75a7d8f | 471 | /** Item path **/ |
729e4ab9 A |
472 | if(basename == item) { |
473 | nextPath = path; | |
474 | } else { | |
475 | itemPath.append(item, (int32_t)(basename-item), *pErrorCode); | |
476 | nextPath = itemPath.data(); | |
b75a7d8f A |
477 | } |
478 | #ifdef UDATA_DEBUG | |
729e4ab9 | 479 | fprintf(stderr, "SUFFIX=%s [%p]\n", inSuffix, inSuffix); |
b75a7d8f | 480 | #endif |
729e4ab9 | 481 | |
374ca955 | 482 | /** Suffix **/ |
729e4ab9 A |
483 | if(inSuffix != NULL) { |
484 | suffix = inSuffix; | |
b75a7d8f | 485 | } else { |
729e4ab9 | 486 | suffix = ""; |
374ca955 A |
487 | } |
488 | ||
729e4ab9 A |
489 | checkLastFour = doCheckLastFour; |
490 | ||
491 | /* pathBuffer will hold the output path strings returned by this iterator */ | |
492 | ||
b75a7d8f A |
493 | #ifdef UDATA_DEBUG |
494 | fprintf(stderr, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n", | |
495 | iter, | |
496 | item, | |
729e4ab9 A |
497 | path, |
498 | basename, | |
499 | suffix, | |
500 | itemPath.data(), | |
501 | nextPath, | |
502 | checkLastFour?"TRUE":"false"); | |
b75a7d8f | 503 | #endif |
b75a7d8f A |
504 | } |
505 | ||
506 | /** | |
507 | * Get the next path on the list. | |
508 | * | |
509 | * @param iter The Iter to be used | |
510 | * @param len If set, pointer to the length of the returned path, for convenience. | |
511 | * @return Pointer to the next path segment, or NULL if there are no more. | |
512 | */ | |
729e4ab9 | 513 | const char *UDataPathIterator::next(UErrorCode *pErrorCode) |
b75a7d8f | 514 | { |
729e4ab9 A |
515 | if(U_FAILURE(*pErrorCode)) { |
516 | return NULL; | |
517 | } | |
518 | ||
519 | const char *currentPath = NULL; | |
520 | int32_t pathLen = 0; | |
b75a7d8f A |
521 | const char *pathBasename; |
522 | ||
b75a7d8f A |
523 | do |
524 | { | |
729e4ab9 | 525 | if( nextPath == NULL ) { |
73c04bcf | 526 | break; |
b75a7d8f | 527 | } |
729e4ab9 A |
528 | currentPath = nextPath; |
529 | ||
530 | if(nextPath == itemPath.data()) { /* we were processing item's path. */ | |
531 | nextPath = path; /* start with regular path next tm. */ | |
532 | pathLen = (int32_t)uprv_strlen(currentPath); | |
b75a7d8f A |
533 | } else { |
534 | /* fix up next for next time */ | |
729e4ab9 A |
535 | nextPath = uprv_strchr(currentPath, U_PATH_SEP_CHAR); |
536 | if(nextPath == NULL) { | |
b75a7d8f | 537 | /* segment: entire path */ |
729e4ab9 | 538 | pathLen = (int32_t)uprv_strlen(currentPath); |
b75a7d8f A |
539 | } else { |
540 | /* segment: until next segment */ | |
729e4ab9 A |
541 | pathLen = (int32_t)(nextPath - currentPath); |
542 | /* skip divider */ | |
543 | nextPath ++; | |
b75a7d8f A |
544 | } |
545 | } | |
546 | ||
547 | if(pathLen == 0) { | |
548 | continue; | |
549 | } | |
550 | ||
551 | #ifdef UDATA_DEBUG | |
729e4ab9 | 552 | fprintf(stderr, "rest of path (IDD) = %s\n", currentPath); |
b75a7d8f A |
553 | fprintf(stderr, " "); |
554 | { | |
374ca955 | 555 | uint32_t qqq; |
b75a7d8f A |
556 | for(qqq=0;qqq<pathLen;qqq++) |
557 | { | |
558 | fprintf(stderr, " "); | |
559 | } | |
560 | ||
561 | fprintf(stderr, "^\n"); | |
562 | } | |
563 | #endif | |
729e4ab9 | 564 | pathBuffer.clear().append(currentPath, pathLen, *pErrorCode); |
b75a7d8f A |
565 | |
566 | /* check for .dat files */ | |
729e4ab9 | 567 | pathBasename = findBasename(pathBuffer.data()); |
b75a7d8f | 568 | |
729e4ab9 | 569 | if(checkLastFour == TRUE && |
b75a7d8f | 570 | (pathLen>=4) && |
729e4ab9 A |
571 | uprv_strncmp(pathBuffer.data() +(pathLen-4), suffix, 4)==0 && /* suffix matches */ |
572 | uprv_strncmp(findBasename(pathBuffer.data()), basename, basenameLen)==0 && /* base matches */ | |
573 | uprv_strlen(pathBasename)==(basenameLen+4)) { /* base+suffix = full len */ | |
574 | ||
b75a7d8f | 575 | #ifdef UDATA_DEBUG |
729e4ab9 | 576 | fprintf(stderr, "Have %s file on the path: %s\n", suffix, pathBuffer.data()); |
b75a7d8f A |
577 | #endif |
578 | /* do nothing */ | |
579 | } | |
580 | else | |
581 | { /* regular dir path */ | |
729e4ab9 | 582 | if(pathBuffer[pathLen-1] != U_FILE_SEP_CHAR) { |
b75a7d8f | 583 | if((pathLen>=4) && |
729e4ab9 | 584 | uprv_strncmp(pathBuffer.data()+(pathLen-4), ".dat", 4) == 0) |
b75a7d8f A |
585 | { |
586 | #ifdef UDATA_DEBUG | |
729e4ab9 | 587 | fprintf(stderr, "skipping non-directory .dat file %s\n", pathBuffer.data()); |
b75a7d8f A |
588 | #endif |
589 | continue; | |
590 | } | |
374ca955 A |
591 | |
592 | /* Check if it is a directory with the same name as our package */ | |
729e4ab9 A |
593 | if(!packageStub.isEmpty() && |
594 | (pathLen > packageStub.length()) && | |
595 | !uprv_strcmp(pathBuffer.data() + pathLen - packageStub.length(), packageStub.data())) { | |
374ca955 | 596 | #ifdef UDATA_DEBUG |
729e4ab9 | 597 | fprintf(stderr, "Found stub %s (will add package %s of len %d)\n", packageStub.data(), basename, basenameLen); |
374ca955 | 598 | #endif |
729e4ab9 | 599 | pathBuffer.truncate(pathLen - packageStub.length()); |
374ca955 | 600 | } |
729e4ab9 A |
601 | pathBuffer.append(U_FILE_SEP_CHAR, *pErrorCode); |
602 | } | |
b75a7d8f | 603 | |
729e4ab9 A |
604 | /* + basename */ |
605 | pathBuffer.append(packageStub.data()+1, packageStub.length()-1, *pErrorCode); | |
b75a7d8f | 606 | |
729e4ab9 | 607 | if(*suffix) /* tack on suffix */ |
b75a7d8f | 608 | { |
729e4ab9 | 609 | pathBuffer.append(suffix, *pErrorCode); |
b75a7d8f | 610 | } |
b75a7d8f A |
611 | } |
612 | ||
b75a7d8f | 613 | #ifdef UDATA_DEBUG |
729e4ab9 | 614 | fprintf(stderr, " --> %s\n", pathBuffer.data()); |
b75a7d8f A |
615 | #endif |
616 | ||
729e4ab9 | 617 | return pathBuffer.data(); |
b75a7d8f | 618 | |
729e4ab9 | 619 | } while(path); |
b75a7d8f A |
620 | |
621 | /* fell way off the end */ | |
622 | return NULL; | |
623 | } | |
624 | ||
729e4ab9 | 625 | U_NAMESPACE_END |
374ca955 | 626 | |
b75a7d8f A |
627 | /* ==================================================================================*/ |
628 | ||
629 | ||
630 | /*----------------------------------------------------------------------* | |
631 | * * | |
632 | * Add a static reference to the common data library * | |
633 | * Unless overridden by an explicit udata_setCommonData, this will be * | |
634 | * our common data. * | |
635 | * * | |
636 | *----------------------------------------------------------------------*/ | |
729e4ab9 | 637 | extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT; |
b75a7d8f | 638 | |
729e4ab9 A |
639 | /* |
640 | * This would be a good place for weak-linkage declarations of | |
641 | * partial-data-library access functions where each returns a pointer | |
642 | * to its data package, if it is linked in. | |
643 | */ | |
644 | /* | |
645 | extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK; | |
646 | extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK; | |
647 | */ | |
b75a7d8f A |
648 | |
649 | /*----------------------------------------------------------------------* | |
650 | * * | |
651 | * openCommonData Attempt to open a common format (.dat) file * | |
652 | * Map it into memory (if it's not there already) * | |
653 | * and return a UDataMemory object for it. * | |
654 | * * | |
655 | * If the requested data is already open and cached * | |
656 | * just return the cached UDataMem object. * | |
657 | * * | |
658 | *----------------------------------------------------------------------*/ | |
659 | static UDataMemory * | |
73c04bcf | 660 | openCommonData(const char *path, /* Path from OpenChoice? */ |
729e4ab9 | 661 | int32_t commonDataIndex, /* ICU Data (index >= 0) if path == NULL */ |
b75a7d8f A |
662 | UErrorCode *pErrorCode) |
663 | { | |
664 | UDataMemory tData; | |
b75a7d8f | 665 | const char *pathBuffer; |
b75a7d8f A |
666 | const char *inBasename; |
667 | ||
668 | if (U_FAILURE(*pErrorCode)) { | |
669 | return NULL; | |
670 | } | |
671 | ||
672 | UDataMemory_init(&tData); | |
673 | ||
674 | /* ??????? TODO revisit this */ | |
729e4ab9 | 675 | if (commonDataIndex >= 0) { |
b75a7d8f | 676 | /* "mini-cache" for common ICU data */ |
729e4ab9 A |
677 | if(commonDataIndex >= LENGTHOF(gCommonICUDataArray)) { |
678 | return NULL; | |
b75a7d8f | 679 | } |
729e4ab9 A |
680 | if(gCommonICUDataArray[commonDataIndex] == NULL) { |
681 | int32_t i; | |
682 | for(i = 0; i < commonDataIndex; ++i) { | |
683 | if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) { | |
684 | /* The linked-in data is already in the list. */ | |
685 | return NULL; | |
686 | } | |
687 | } | |
b75a7d8f | 688 | |
729e4ab9 A |
689 | /* Add the linked-in data to the list. */ |
690 | /* | |
691 | * This is where we would check and call weakly linked partial-data-library | |
692 | * access functions. | |
693 | */ | |
694 | /* | |
695 | if (uprv_getICUData_collation) { | |
696 | setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode); | |
697 | } | |
698 | if (uprv_getICUData_conversion) { | |
699 | setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode); | |
700 | } | |
701 | */ | |
702 | setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, FALSE, pErrorCode); | |
703 | } | |
704 | return gCommonICUDataArray[commonDataIndex]; | |
b75a7d8f A |
705 | } |
706 | ||
707 | ||
708 | /* request is NOT for ICU Data. */ | |
709 | ||
710 | /* Find the base name portion of the supplied path. */ | |
711 | /* inBasename will be left pointing somewhere within the original path string. */ | |
712 | inBasename = findBasename(path); | |
713 | #ifdef UDATA_DEBUG | |
714 | fprintf(stderr, "inBasename = %s\n", inBasename); | |
715 | #endif | |
716 | ||
717 | if(*inBasename==0) { | |
718 | /* no basename. This will happen if the original path was a directory name, */ | |
719 | /* like "a/b/c/". (Fallback to separate files will still work.) */ | |
720 | #ifdef UDATA_DEBUG | |
721 | fprintf(stderr, "ocd: no basename in %s, bailing.\n", path); | |
722 | #endif | |
723 | *pErrorCode=U_FILE_ACCESS_ERROR; | |
724 | return NULL; | |
725 | } | |
726 | ||
727 | /* Is the requested common data file already open and cached? */ | |
728 | /* Note that the cache is keyed by the base name only. The rest of the path, */ | |
729 | /* if any, is not considered. */ | |
730 | { | |
731 | UDataMemory *dataToReturn = udata_findCachedData(inBasename); | |
732 | if (dataToReturn != NULL) { | |
733 | return dataToReturn; | |
734 | } | |
735 | } | |
736 | ||
737 | /* Requested item is not in the cache. | |
738 | * Hunt it down, trying all the path locations | |
739 | */ | |
740 | ||
729e4ab9 | 741 | UDataPathIterator iter(u_getDataDirectory(), inBasename, path, ".dat", TRUE, pErrorCode); |
b75a7d8f | 742 | |
729e4ab9 | 743 | while((UDataMemory_isLoaded(&tData)==FALSE) && (pathBuffer = iter.next(pErrorCode)) != NULL) |
b75a7d8f A |
744 | { |
745 | #ifdef UDATA_DEBUG | |
746 | fprintf(stderr, "ocd: trying path %s - ", pathBuffer); | |
747 | #endif | |
748 | uprv_mapFile(&tData, pathBuffer); | |
749 | #ifdef UDATA_DEBUG | |
750 | fprintf(stderr, "%s\n", UDataMemory_isLoaded(&tData)?"LOADED":"not loaded"); | |
751 | #endif | |
752 | } | |
753 | ||
754 | #if defined(OS390_STUBDATA) && defined(OS390BATCH) | |
755 | if (!UDataMemory_isLoaded(&tData)) { | |
756 | char ourPathBuffer[1024]; | |
757 | /* One more chance, for extendCommonData() */ | |
758 | uprv_strncpy(ourPathBuffer, path, 1019); | |
759 | ourPathBuffer[1019]=0; | |
760 | uprv_strcat(ourPathBuffer, ".dat"); | |
761 | uprv_mapFile(&tData, ourPathBuffer); | |
762 | } | |
763 | #endif | |
764 | ||
765 | if (!UDataMemory_isLoaded(&tData)) { | |
766 | /* no common data */ | |
767 | *pErrorCode=U_FILE_ACCESS_ERROR; | |
768 | return NULL; | |
769 | } | |
770 | ||
771 | /* we have mapped a file, check its header */ | |
772 | udata_checkCommonData(&tData, pErrorCode); | |
773 | ||
774 | ||
775 | /* Cache the UDataMemory struct for this .dat file, | |
776 | * so we won't need to hunt it down and map it again next time | |
777 | * something is needed from it. */ | |
778 | return udata_cacheDataItem(inBasename, &tData, pErrorCode); | |
779 | } | |
780 | ||
781 | ||
b75a7d8f A |
782 | /*----------------------------------------------------------------------* |
783 | * * | |
784 | * extendICUData If the full set of ICU data was not loaded at * | |
785 | * program startup, load it now. This function will * | |
786 | * be called when the lookup of an ICU data item in * | |
787 | * the common ICU data fails. * | |
788 | * * | |
b75a7d8f A |
789 | * return true if new data is loaded, false otherwise.* |
790 | * * | |
791 | *----------------------------------------------------------------------*/ | |
729e4ab9 | 792 | static UBool extendICUData(UErrorCode *pErr) |
b75a7d8f | 793 | { |
b75a7d8f A |
794 | UDataMemory *pData; |
795 | UDataMemory copyPData; | |
729e4ab9 A |
796 | UBool didUpdate = FALSE; |
797 | ||
798 | /* | |
799 | * There is a chance for a race condition here. | |
800 | * Normally, ICU data is loaded from a DLL or via mmap() and | |
801 | * setCommonICUData() will detect if the same address is set twice. | |
802 | * If ICU is built with data loading via fread() then the address will | |
803 | * be different each time the common data is loaded and we may add | |
804 | * multiple copies of the data. | |
805 | * In this case, use a mutex to prevent the race. | |
806 | * Use a specific mutex to avoid nested locks of the global mutex. | |
807 | */ | |
808 | #if MAP_IMPLEMENTATION==MAP_STDIO | |
809 | static UMTX extendICUDataMutex = NULL; | |
810 | umtx_lock(&extendICUDataMutex); | |
811 | #endif | |
812 | if(!gHaveTriedToLoadCommonData) { | |
813 | /* See if we can explicitly open a .dat file for the ICUData. */ | |
814 | pData = openCommonData( | |
815 | U_ICUDATA_NAME, /* "icudt20l" , for example. */ | |
816 | -1, /* Pretend we're not opening ICUData */ | |
817 | pErr); | |
818 | ||
819 | /* How about if there is no pData, eh... */ | |
820 | ||
821 | UDataMemory_init(©PData); | |
822 | if(pData != NULL) { | |
823 | UDatamemory_assign(©PData, pData); | |
824 | copyPData.map = 0; /* The mapping for this data is owned by the hash table */ | |
825 | copyPData.mapAddr = 0; /* which will unmap it when ICU is shut down. */ | |
826 | /* CommonICUData is also unmapped when ICU is shut down.*/ | |
827 | /* To avoid unmapping the data twice, zero out the map */ | |
828 | /* fields in the UDataMemory that we're assigning */ | |
829 | /* to CommonICUData. */ | |
830 | ||
831 | didUpdate = | |
832 | setCommonICUData(©PData,/* The new common data. */ | |
833 | FALSE, /* No warnings if write didn't happen */ | |
834 | pErr); /* setCommonICUData honors errors; NOP if error set */ | |
835 | } | |
b75a7d8f | 836 | |
729e4ab9 | 837 | gHaveTriedToLoadCommonData = TRUE; |
b75a7d8f A |
838 | } |
839 | ||
729e4ab9 A |
840 | didUpdate = findCommonICUDataByName(U_ICUDATA_NAME); /* Return 'true' when a racing writes out the extended */ |
841 | /* data after another thread has failed to see it (in openCommonData), so */ | |
842 | /* extended data can be examined. */ | |
843 | /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */ | |
b75a7d8f | 844 | |
729e4ab9 A |
845 | #if MAP_IMPLEMENTATION==MAP_STDIO |
846 | umtx_unlock(&extendICUDataMutex); | |
847 | #endif | |
848 | return didUpdate; /* Return true if ICUData pointer was updated. */ | |
b75a7d8f A |
849 | /* (Could potentialy have been done by another thread racing */ |
850 | /* us through here, but that's fine, we still return true */ | |
851 | /* so that current thread will also examine extended data. */ | |
852 | } | |
853 | ||
b75a7d8f A |
854 | /*----------------------------------------------------------------------* |
855 | * * | |
856 | * udata_setCommonData * | |
857 | * * | |
858 | *----------------------------------------------------------------------*/ | |
859 | U_CAPI void U_EXPORT2 | |
860 | udata_setCommonData(const void *data, UErrorCode *pErrorCode) { | |
861 | UDataMemory dataMemory; | |
862 | ||
863 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
864 | return; | |
865 | } | |
866 | ||
867 | if(data==NULL) { | |
868 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
869 | return; | |
870 | } | |
871 | ||
b75a7d8f A |
872 | /* set the data pointer and test for validity */ |
873 | UDataMemory_init(&dataMemory); | |
874 | UDataMemory_setData(&dataMemory, data); | |
875 | udata_checkCommonData(&dataMemory, pErrorCode); | |
876 | if (U_FAILURE(*pErrorCode)) {return;} | |
877 | ||
878 | /* we have good data */ | |
879 | /* Set it up as the ICU Common Data. */ | |
729e4ab9 | 880 | setCommonICUData(&dataMemory, TRUE, pErrorCode); |
b75a7d8f A |
881 | } |
882 | ||
b75a7d8f A |
883 | /*--------------------------------------------------------------------------- |
884 | * | |
885 | * udata_setAppData | |
886 | * | |
887 | *---------------------------------------------------------------------------- */ | |
888 | U_CAPI void U_EXPORT2 | |
889 | udata_setAppData(const char *path, const void *data, UErrorCode *err) | |
890 | { | |
891 | UDataMemory udm; | |
892 | ||
893 | if(err==NULL || U_FAILURE(*err)) { | |
894 | return; | |
895 | } | |
896 | if(data==NULL) { | |
897 | *err=U_ILLEGAL_ARGUMENT_ERROR; | |
898 | return; | |
899 | } | |
900 | ||
901 | UDataMemory_init(&udm); | |
729e4ab9 | 902 | UDataMemory_setData(&udm, data); |
b75a7d8f A |
903 | udata_checkCommonData(&udm, err); |
904 | udata_cacheDataItem(path, &udm, err); | |
905 | } | |
906 | ||
907 | /*----------------------------------------------------------------------------* | |
908 | * * | |
909 | * checkDataItem Given a freshly located/loaded data item, either * | |
910 | * an entry in a common file or a separately loaded file, * | |
911 | * sanity check its header, and see if the data is * | |
912 | * acceptable to the app. * | |
913 | * If the data is good, create and return a UDataMemory * | |
914 | * object that can be returned to the application. * | |
915 | * Return NULL on any sort of failure. * | |
916 | * * | |
917 | *----------------------------------------------------------------------------*/ | |
918 | static UDataMemory * | |
919 | checkDataItem | |
920 | ( | |
921 | const DataHeader *pHeader, /* The data item to be checked. */ | |
922 | UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */ | |
923 | void *context, /* pass-thru param for above. */ | |
924 | const char *type, /* pass-thru param for above. */ | |
925 | const char *name, /* pass-thru param for above. */ | |
926 | UErrorCode *nonFatalErr, /* Error code if this data was not acceptable */ | |
927 | /* but openChoice should continue with */ | |
928 | /* trying to get data from fallback path. */ | |
929 | UErrorCode *fatalErr /* Bad error, caller should return immediately */ | |
930 | ) | |
931 | { | |
932 | UDataMemory *rDataMem = NULL; /* the new UDataMemory, to be returned. */ | |
933 | ||
934 | if (U_FAILURE(*fatalErr)) { | |
935 | return NULL; | |
936 | } | |
937 | ||
938 | if(pHeader->dataHeader.magic1==0xda && | |
939 | pHeader->dataHeader.magic2==0x27 && | |
b75a7d8f | 940 | (isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info)) |
374ca955 | 941 | ) { |
b75a7d8f A |
942 | rDataMem=UDataMemory_createNewInstance(fatalErr); |
943 | if (U_FAILURE(*fatalErr)) { | |
944 | return NULL; | |
945 | } | |
946 | rDataMem->pHeader = pHeader; | |
947 | } else { | |
948 | /* the data is not acceptable, look further */ | |
949 | /* If we eventually find something good, this errorcode will be */ | |
950 | /* cleared out. */ | |
951 | *nonFatalErr=U_INVALID_FORMAT_ERROR; | |
952 | } | |
953 | return rDataMem; | |
954 | } | |
955 | ||
73c04bcf A |
956 | /** |
957 | * @return 0 if not loaded, 1 if loaded or err | |
958 | */ | |
959 | static UDataMemory *doLoadFromIndividualFiles(const char *pkgName, | |
960 | const char *dataPath, const char *tocEntryPathSuffix, | |
961 | /* following arguments are the same as doOpenChoice itself */ | |
962 | const char *path, const char *type, const char *name, | |
963 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
964 | UErrorCode *subErrorCode, | |
965 | UErrorCode *pErrorCode) | |
966 | { | |
73c04bcf A |
967 | const char *pathBuffer; |
968 | UDataMemory dataMemory; | |
969 | UDataMemory *pEntryData; | |
970 | ||
73c04bcf A |
971 | /* look in ind. files: package\nam.typ ========================= */ |
972 | /* init path iterator for individual files */ | |
729e4ab9 | 973 | UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, FALSE, pErrorCode); |
73c04bcf | 974 | |
729e4ab9 | 975 | while((pathBuffer = iter.next(pErrorCode))) |
73c04bcf A |
976 | { |
977 | #ifdef UDATA_DEBUG | |
978 | fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer); | |
979 | #endif | |
980 | if(uprv_mapFile(&dataMemory, pathBuffer)) | |
981 | { | |
982 | pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); | |
983 | if (pEntryData != NULL) { | |
984 | /* Data is good. | |
985 | * Hand off ownership of the backing memory to the user's UDataMemory. | |
986 | * and return it. */ | |
987 | pEntryData->mapAddr = dataMemory.mapAddr; | |
988 | pEntryData->map = dataMemory.map; | |
989 | ||
990 | #ifdef UDATA_DEBUG | |
991 | fprintf(stderr, "** Mapped file: %s\n", pathBuffer); | |
992 | #endif | |
729e4ab9 | 993 | return pEntryData; |
73c04bcf A |
994 | } |
995 | ||
996 | /* the data is not acceptable, or some error occured. Either way, unmap the memory */ | |
997 | udata_close(&dataMemory); | |
998 | ||
999 | /* If we had a nasty error, bail out completely. */ | |
1000 | if (U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1001 | return NULL; |
73c04bcf A |
1002 | } |
1003 | ||
1004 | /* Otherwise remember that we found data but didn't like it for some reason */ | |
1005 | *subErrorCode=U_INVALID_FORMAT_ERROR; | |
1006 | } | |
1007 | #ifdef UDATA_DEBUG | |
1008 | fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded"); | |
1009 | #endif | |
1010 | } | |
729e4ab9 | 1011 | return NULL; |
73c04bcf A |
1012 | } |
1013 | ||
1014 | /** | |
1015 | * @return 0 if not loaded, 1 if loaded or err | |
1016 | */ | |
729e4ab9 A |
1017 | static UDataMemory *doLoadFromCommonData(UBool isICUData, const char * /*pkgName*/, |
1018 | const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName, | |
73c04bcf A |
1019 | /* following arguments are the same as doOpenChoice itself */ |
1020 | const char *path, const char *type, const char *name, | |
1021 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
1022 | UErrorCode *subErrorCode, | |
1023 | UErrorCode *pErrorCode) | |
1024 | { | |
73c04bcf A |
1025 | UDataMemory *pEntryData; |
1026 | const DataHeader *pHeader; | |
1027 | UDataMemory *pCommonData; | |
729e4ab9 A |
1028 | int32_t commonDataIndex; |
1029 | UBool checkedExtendedICUData = FALSE; | |
73c04bcf A |
1030 | /* try to get common data. The loop is for platforms such as the 390 that do |
1031 | * not initially load the full set of ICU data. If the lookup of an ICU data item | |
1032 | * fails, the full (but slower to load) set is loaded, the and the loop repeats, | |
1033 | * trying the lookup again. Once the full set of ICU data is loaded, the loop wont | |
1034 | * repeat because the full set will be checked the first time through. | |
1035 | * | |
1036 | * The loop also handles the fallback to a .dat file if the application linked | |
1037 | * to the stub data library rather than a real library. | |
1038 | */ | |
729e4ab9 A |
1039 | for (commonDataIndex = isICUData ? 0 : -1;;) { |
1040 | pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/ | |
73c04bcf | 1041 | |
729e4ab9 | 1042 | if(U_SUCCESS(*subErrorCode) && pCommonData!=NULL) { |
73c04bcf A |
1043 | int32_t length; |
1044 | ||
1045 | /* look up the data piece in the common data */ | |
1046 | pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &length, subErrorCode); | |
1047 | #ifdef UDATA_DEBUG | |
1048 | fprintf(stderr, "%s: pHeader=%p - %s\n", tocEntryName, pHeader, u_errorName(*subErrorCode)); | |
1049 | #endif | |
b75a7d8f | 1050 | |
73c04bcf A |
1051 | if(pHeader!=NULL) { |
1052 | pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); | |
1053 | #ifdef UDATA_DEBUG | |
1054 | fprintf(stderr, "pEntryData=%p\n", pEntryData); | |
1055 | #endif | |
1056 | if (U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1057 | return NULL; |
73c04bcf A |
1058 | } |
1059 | if (pEntryData != NULL) { | |
1060 | pEntryData->length = length; | |
729e4ab9 | 1061 | return pEntryData; |
73c04bcf A |
1062 | } |
1063 | } | |
1064 | } | |
1065 | /* Data wasn't found. If we were looking for an ICUData item and there is | |
1066 | * more data available, load it and try again, | |
1067 | * otherwise break out of this loop. */ | |
729e4ab9 A |
1068 | if (!isICUData) { |
1069 | return NULL; | |
1070 | } else if (pCommonData != NULL) { | |
1071 | ++commonDataIndex; /* try the next data package */ | |
1072 | } else if ((!checkedExtendedICUData) && extendICUData(subErrorCode)) { | |
1073 | checkedExtendedICUData = TRUE; | |
1074 | /* try this data package slot again: it changed from NULL to non-NULL */ | |
1075 | } else { | |
1076 | return NULL; | |
73c04bcf A |
1077 | } |
1078 | } | |
73c04bcf | 1079 | } |
b75a7d8f A |
1080 | |
1081 | /* | |
1082 | * A note on the ownership of Mapped Memory | |
1083 | * | |
1084 | * For common format files, ownership resides with the UDataMemory object | |
1085 | * that lives in the cache of opened common data. These UDataMemorys are private | |
1086 | * to the udata implementation, and are never seen directly by users. | |
1087 | * | |
1088 | * The UDataMemory objects returned to users will have the address of some desired | |
1089 | * data within the mapped region, but they wont have the mapping info itself, and thus | |
1090 | * won't cause anything to be removed from memory when they are closed. | |
1091 | * | |
1092 | * For individual data files, the UDataMemory returned to the user holds the | |
1093 | * information necessary to unmap the data on close. If the user independently | |
1094 | * opens the same data file twice, two completely independent mappings will be made. | |
1095 | * (There is no cache of opened data items from individual files, only a cache of | |
1096 | * opened Common Data files, that is, files containing a collection of data items.) | |
1097 | * | |
1098 | * For common data passed in from the user via udata_setAppData() or | |
1099 | * udata_setCommonData(), ownership remains with the user. | |
1100 | * | |
1101 | * UDataMemory objects themselves, as opposed to the memory they describe, | |
1102 | * can be anywhere - heap, stack/local or global. | |
1103 | * They have a flag to indicate when they're heap allocated and thus | |
1104 | * must be deleted when closed. | |
1105 | */ | |
1106 | ||
1107 | ||
1108 | /*----------------------------------------------------------------------------* | |
1109 | * * | |
1110 | * main data loading functions * | |
1111 | * * | |
1112 | *----------------------------------------------------------------------------*/ | |
1113 | static UDataMemory * | |
1114 | doOpenChoice(const char *path, const char *type, const char *name, | |
1115 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
1116 | UErrorCode *pErrorCode) | |
1117 | { | |
374ca955 | 1118 | UDataMemory *retVal = NULL; |
b75a7d8f | 1119 | |
b75a7d8f A |
1120 | const char *dataPath; |
1121 | ||
374ca955 A |
1122 | int32_t tocEntrySuffixIndex; |
1123 | const char *tocEntryPathSuffix; | |
73c04bcf | 1124 | UErrorCode subErrorCode=U_ZERO_ERROR; |
374ca955 A |
1125 | const char *treeChar; |
1126 | ||
1127 | UBool isICUData = FALSE; | |
1128 | ||
73c04bcf A |
1129 | |
1130 | /* Is this path ICU data? */ | |
374ca955 | 1131 | if(path == NULL || |
73c04bcf A |
1132 | !strcmp(path, U_ICUDATA_ALIAS) || /* "ICUDATA" */ |
1133 | !uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING, /* "icudt26e-" */ | |
1134 | uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)) || | |
1135 | !uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING, /* "ICUDATA-" */ | |
374ca955 A |
1136 | uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING))) { |
1137 | isICUData = TRUE; | |
1138 | } | |
1139 | ||
73c04bcf | 1140 | #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */ |
374ca955 | 1141 | /* remap from alternate path char to the main one */ |
729e4ab9 | 1142 | CharString altSepPath; |
374ca955 | 1143 | if(path) { |
729e4ab9 A |
1144 | if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR) != NULL) { |
1145 | altSepPath.append(path, *pErrorCode); | |
1146 | char *p; | |
1147 | while((p=uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR))) { | |
374ca955 A |
1148 | *p = U_FILE_SEP_CHAR; |
1149 | } | |
1150 | #if defined (UDATA_DEBUG) | |
1151 | fprintf(stderr, "Changed path from [%s] to [%s]\n", path, altSepPath.s); | |
1152 | #endif | |
729e4ab9 | 1153 | path = altSepPath.data(); |
374ca955 A |
1154 | } |
1155 | } | |
1156 | #endif | |
1157 | ||
729e4ab9 A |
1158 | CharString tocEntryName; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */ |
1159 | CharString tocEntryPath; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */ | |
374ca955 | 1160 | |
729e4ab9 A |
1161 | CharString pkgName; |
1162 | CharString treeName; | |
374ca955 | 1163 | |
73c04bcf | 1164 | /* ======= Set up strings */ |
374ca955 | 1165 | if(path==NULL) { |
729e4ab9 | 1166 | pkgName.append(U_ICUDATA_NAME, *pErrorCode); |
374ca955 A |
1167 | } else { |
1168 | const char *pkg; | |
1169 | const char *first; | |
1170 | pkg = uprv_strrchr(path, U_FILE_SEP_CHAR); | |
1171 | first = uprv_strchr(path, U_FILE_SEP_CHAR); | |
1172 | if(uprv_pathIsAbsolute(path) || (pkg != first)) { /* more than one slash in the path- not a tree name */ | |
1173 | /* see if this is an /absolute/path/to/package path */ | |
1174 | if(pkg) { | |
729e4ab9 | 1175 | pkgName.append(pkg+1, *pErrorCode); |
374ca955 | 1176 | } else { |
729e4ab9 | 1177 | pkgName.append(path, *pErrorCode); |
374ca955 A |
1178 | } |
1179 | } else { | |
1180 | treeChar = uprv_strchr(path, U_TREE_SEPARATOR); | |
1181 | if(treeChar) { | |
729e4ab9 | 1182 | treeName.append(treeChar+1, *pErrorCode); /* following '-' */ |
73c04bcf | 1183 | if(isICUData) { |
729e4ab9 | 1184 | pkgName.append(U_ICUDATA_NAME, *pErrorCode); |
73c04bcf | 1185 | } else { |
729e4ab9 | 1186 | pkgName.append(path, (int32_t)(treeChar-path), *pErrorCode); |
73c04bcf A |
1187 | if (first == NULL) { |
1188 | /* | |
1189 | This user data has no path, but there is a tree name. | |
1190 | Look up the correct path from the data cache later. | |
1191 | */ | |
729e4ab9 | 1192 | path = pkgName.data(); |
73c04bcf | 1193 | } |
374ca955 A |
1194 | } |
1195 | } else { | |
73c04bcf | 1196 | if(isICUData) { |
729e4ab9 | 1197 | pkgName.append(U_ICUDATA_NAME, *pErrorCode); |
73c04bcf | 1198 | } else { |
729e4ab9 | 1199 | pkgName.append(path, *pErrorCode); |
374ca955 A |
1200 | } |
1201 | } | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | #ifdef UDATA_DEBUG | |
729e4ab9 | 1206 | fprintf(stderr, " P=%s T=%s\n", pkgName.data(), treeName.data()); |
374ca955 A |
1207 | #endif |
1208 | ||
73c04bcf A |
1209 | /* setting up the entry name and file name |
1210 | * Make up a full name by appending the type to the supplied | |
b75a7d8f A |
1211 | * name, assuming that a type was supplied. |
1212 | */ | |
1213 | ||
1214 | /* prepend the package */ | |
729e4ab9 A |
1215 | tocEntryName.append(pkgName, *pErrorCode); |
1216 | tocEntryPath.append(pkgName, *pErrorCode); | |
1217 | tocEntrySuffixIndex = tocEntryName.length(); | |
b75a7d8f | 1218 | |
729e4ab9 A |
1219 | if(!treeName.isEmpty()) { |
1220 | tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode); | |
1221 | tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode); | |
374ca955 | 1222 | } |
b75a7d8f | 1223 | |
729e4ab9 A |
1224 | tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(name, *pErrorCode); |
1225 | tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(name, *pErrorCode); | |
b75a7d8f | 1226 | if(type!=NULL && *type!=0) { |
729e4ab9 A |
1227 | tocEntryName.append(".", *pErrorCode).append(type, *pErrorCode); |
1228 | tocEntryPath.append(".", *pErrorCode).append(type, *pErrorCode); | |
b75a7d8f | 1229 | } |
729e4ab9 | 1230 | tocEntryPathSuffix = tocEntryPath.data()+tocEntrySuffixIndex; /* suffix starts here */ |
b75a7d8f A |
1231 | |
1232 | #ifdef UDATA_DEBUG | |
729e4ab9 A |
1233 | fprintf(stderr, " tocEntryName = %s\n", tocEntryName.data()); |
1234 | fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.data()); | |
1235 | #endif | |
b75a7d8f | 1236 | |
b75a7d8f | 1237 | if(path == NULL) { |
73c04bcf | 1238 | path = COMMON_DATA_NAME; /* "icudt26e" */ |
b75a7d8f A |
1239 | } |
1240 | ||
1241 | /************************ Begin loop looking for ind. files ***************/ | |
1242 | #ifdef UDATA_DEBUG | |
729e4ab9 | 1243 | fprintf(stderr, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path)); |
b75a7d8f A |
1244 | #endif |
1245 | ||
b75a7d8f | 1246 | /* End of dealing with a null basename */ |
b75a7d8f A |
1247 | dataPath = u_getDataDirectory(); |
1248 | ||
73c04bcf A |
1249 | /**** COMMON PACKAGE - only if packages are first. */ |
1250 | if(gDataFileAccess == UDATA_PACKAGES_FIRST) { | |
374ca955 | 1251 | #ifdef UDATA_DEBUG |
73c04bcf | 1252 | fprintf(stderr, "Trying packages (UDATA_PACKAGES_FIRST)\n"); |
374ca955 | 1253 | #endif |
73c04bcf A |
1254 | /* #2 */ |
1255 | retVal = doLoadFromCommonData(isICUData, | |
729e4ab9 | 1256 | pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(), |
73c04bcf A |
1257 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
1258 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1259 | return retVal; |
b75a7d8f | 1260 | } |
73c04bcf | 1261 | } |
729e4ab9 | 1262 | |
73c04bcf A |
1263 | /**** INDIVIDUAL FILES */ |
1264 | if((gDataFileAccess==UDATA_PACKAGES_FIRST) || | |
1265 | (gDataFileAccess==UDATA_FILES_FIRST)) { | |
374ca955 | 1266 | #ifdef UDATA_DEBUG |
73c04bcf | 1267 | fprintf(stderr, "Trying individual files\n"); |
374ca955 | 1268 | #endif |
73c04bcf A |
1269 | /* Check to make sure that there is a dataPath to iterate over */ |
1270 | if ((dataPath && *dataPath) || !isICUData) { | |
729e4ab9 | 1271 | retVal = doLoadFromIndividualFiles(pkgName.data(), dataPath, tocEntryPathSuffix, |
73c04bcf A |
1272 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
1273 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1274 | return retVal; |
374ca955 | 1275 | } |
374ca955 | 1276 | } |
b75a7d8f A |
1277 | } |
1278 | ||
73c04bcf A |
1279 | /**** COMMON PACKAGE */ |
1280 | if((gDataFileAccess==UDATA_ONLY_PACKAGES) || | |
1281 | (gDataFileAccess==UDATA_FILES_FIRST)) { | |
b75a7d8f | 1282 | #ifdef UDATA_DEBUG |
73c04bcf | 1283 | fprintf(stderr, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n"); |
b75a7d8f | 1284 | #endif |
729e4ab9 A |
1285 | retVal = doLoadFromCommonData(isICUData, |
1286 | pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(), | |
73c04bcf A |
1287 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
1288 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1289 | return retVal; |
73c04bcf A |
1290 | } |
1291 | } | |
1292 | ||
1293 | /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to | |
1294 | try a DLL (static or setCommonData/etc) load. | |
1295 | If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */ | |
1296 | if(gDataFileAccess==UDATA_NO_FILES) { | |
b75a7d8f | 1297 | #ifdef UDATA_DEBUG |
73c04bcf | 1298 | fprintf(stderr, "Trying common data (UDATA_NO_FILES)\n"); |
b75a7d8f | 1299 | #endif |
729e4ab9 A |
1300 | retVal = doLoadFromCommonData(isICUData, |
1301 | pkgName.data(), "", tocEntryPathSuffix, tocEntryName.data(), | |
73c04bcf A |
1302 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
1303 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1304 | return retVal; |
b75a7d8f A |
1305 | } |
1306 | } | |
1307 | ||
1308 | /* data not found */ | |
1309 | if(U_SUCCESS(*pErrorCode)) { | |
73c04bcf | 1310 | if(U_SUCCESS(subErrorCode)) { |
b75a7d8f A |
1311 | /* file not found */ |
1312 | *pErrorCode=U_FILE_ACCESS_ERROR; | |
1313 | } else { | |
1314 | /* entry point not found or rejected */ | |
73c04bcf | 1315 | *pErrorCode=subErrorCode; |
b75a7d8f A |
1316 | } |
1317 | } | |
374ca955 | 1318 | return retVal; |
b75a7d8f A |
1319 | } |
1320 | ||
1321 | ||
1322 | ||
1323 | /* API ---------------------------------------------------------------------- */ | |
1324 | ||
1325 | U_CAPI UDataMemory * U_EXPORT2 | |
1326 | udata_open(const char *path, const char *type, const char *name, | |
1327 | UErrorCode *pErrorCode) { | |
1328 | #ifdef UDATA_DEBUG | |
374ca955 | 1329 | fprintf(stderr, "udata_open(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type); |
b75a7d8f A |
1330 | fflush(stderr); |
1331 | #endif | |
1332 | ||
1333 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
1334 | return NULL; | |
1335 | } else if(name==NULL || *name==0) { | |
1336 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1337 | return NULL; | |
1338 | } else { | |
1339 | return doOpenChoice(path, type, name, NULL, NULL, pErrorCode); | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | ||
1344 | ||
1345 | U_CAPI UDataMemory * U_EXPORT2 | |
1346 | udata_openChoice(const char *path, const char *type, const char *name, | |
1347 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
1348 | UErrorCode *pErrorCode) { | |
1349 | #ifdef UDATA_DEBUG | |
374ca955 | 1350 | fprintf(stderr, "udata_openChoice(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type); |
b75a7d8f A |
1351 | #endif |
1352 | ||
1353 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
1354 | return NULL; | |
1355 | } else if(name==NULL || *name==0 || isAcceptable==NULL) { | |
1356 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1357 | return NULL; | |
1358 | } else { | |
1359 | return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode); | |
1360 | } | |
1361 | } | |
1362 | ||
1363 | ||
1364 | ||
1365 | U_CAPI void U_EXPORT2 | |
1366 | udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) { | |
1367 | if(pInfo!=NULL) { | |
1368 | if(pData!=NULL && pData->pHeader!=NULL) { | |
1369 | const UDataInfo *info=&pData->pHeader->info; | |
374ca955 A |
1370 | uint16_t dataInfoSize=udata_getInfoSize(info); |
1371 | if(pInfo->size>dataInfoSize) { | |
1372 | pInfo->size=dataInfoSize; | |
1373 | } | |
1374 | uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2); | |
1375 | if(info->isBigEndian!=U_IS_BIG_ENDIAN) { | |
1376 | /* opposite endianness */ | |
1377 | uint16_t x=info->reservedWord; | |
1378 | pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8)); | |
b75a7d8f | 1379 | } |
b75a7d8f A |
1380 | } else { |
1381 | pInfo->size=0; | |
1382 | } | |
1383 | } | |
1384 | } | |
73c04bcf A |
1385 | |
1386 | ||
729e4ab9 | 1387 | U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/) |
73c04bcf A |
1388 | { |
1389 | gDataFileAccess = access; | |
1390 | } |