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