]>
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 | ||
0f5d89e8 A |
211 | #if U_PLATFORM_HAS_WINUWP_API == 0 |
212 | ||
729e4ab9 A |
213 | static UBool |
214 | setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) { | |
215 | UDataMemory tData; | |
216 | UDataMemory_init(&tData); | |
57a6839d | 217 | UDataMemory_setData(&tData, pData); |
729e4ab9 A |
218 | udata_checkCommonData(&tData, pErrorCode); |
219 | return setCommonICUData(&tData, FALSE, pErrorCode); | |
b75a7d8f A |
220 | } |
221 | ||
0f5d89e8 A |
222 | #endif |
223 | ||
b75a7d8f A |
224 | static const char * |
225 | findBasename(const char *path) { | |
226 | const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR); | |
227 | if(basename==NULL) { | |
228 | return path; | |
229 | } else { | |
230 | return basename+1; | |
231 | } | |
232 | } | |
233 | ||
374ca955 | 234 | #ifdef UDATA_DEBUG |
b75a7d8f A |
235 | static const char * |
236 | packageNameFromPath(const char *path) | |
237 | { | |
238 | if((path == NULL) || (*path == 0)) { | |
239 | return U_ICUDATA_NAME; | |
240 | } | |
241 | ||
242 | path = findBasename(path); | |
243 | ||
244 | if((path == NULL) || (*path == 0)) { | |
245 | return U_ICUDATA_NAME; | |
246 | } | |
247 | ||
248 | return path; | |
249 | } | |
374ca955 | 250 | #endif |
b75a7d8f A |
251 | |
252 | /*----------------------------------------------------------------------* | |
253 | * * | |
254 | * Cache for common data * | |
255 | * Functions for looking up or adding entries to a cache of * | |
256 | * data that has been previously opened. Avoids a potentially * | |
257 | * expensive operation of re-opening the data for subsequent * | |
258 | * uses. * | |
259 | * * | |
260 | * Data remains cached for the duration of the process. * | |
261 | * * | |
262 | *----------------------------------------------------------------------*/ | |
263 | ||
264 | typedef struct DataCacheElement { | |
265 | char *name; | |
266 | UDataMemory *item; | |
267 | } DataCacheElement; | |
268 | ||
269 | ||
270 | ||
271 | /* | |
272 | * Deleter function for DataCacheElements. | |
273 | * udata cleanup function closes the hash table; hash table in turn calls back to | |
274 | * here for each entry. | |
275 | */ | |
73c04bcf | 276 | static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) { |
b75a7d8f A |
277 | DataCacheElement *p = (DataCacheElement *)pDCEl; |
278 | udata_close(p->item); /* unmaps storage */ | |
279 | uprv_free(p->name); /* delete the hash key string. */ | |
280 | uprv_free(pDCEl); /* delete 'this' */ | |
281 | } | |
282 | ||
f3c0d7a5 | 283 | static void U_CALLCONV udata_initHashTable(UErrorCode &err) { |
57a6839d A |
284 | U_ASSERT(gCommonDataCache == NULL); |
285 | gCommonDataCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err); | |
286 | if (U_FAILURE(err)) { | |
f3c0d7a5 | 287 | return; |
b75a7d8f | 288 | } |
f3c0d7a5 A |
289 | U_ASSERT(gCommonDataCache != NULL); |
290 | uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter); | |
291 | ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); | |
57a6839d | 292 | } |
b75a7d8f | 293 | |
57a6839d A |
294 | /* udata_getCacheHashTable() |
295 | * Get the hash table used to store the data cache entries. | |
296 | * Lazy create it if it doesn't yet exist. | |
297 | */ | |
f3c0d7a5 A |
298 | static UHashtable *udata_getHashTable(UErrorCode &err) { |
299 | umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable, err); | |
b75a7d8f A |
300 | return gCommonDataCache; |
301 | } | |
302 | ||
303 | ||
304 | ||
f3c0d7a5 | 305 | static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err) |
b75a7d8f A |
306 | { |
307 | UHashtable *htable; | |
308 | UDataMemory *retVal = NULL; | |
309 | DataCacheElement *el; | |
310 | const char *baseName; | |
311 | ||
f3c0d7a5 A |
312 | htable = udata_getHashTable(err); |
313 | if (U_FAILURE(err)) { | |
314 | return NULL; | |
315 | } | |
316 | ||
b75a7d8f | 317 | baseName = findBasename(path); /* Cache remembers only the base name, not the full path. */ |
b75a7d8f A |
318 | umtx_lock(NULL); |
319 | el = (DataCacheElement *)uhash_get(htable, baseName); | |
320 | umtx_unlock(NULL); | |
321 | if (el != NULL) { | |
322 | retVal = el->item; | |
323 | } | |
324 | #ifdef UDATA_DEBUG | |
325 | fprintf(stderr, "Cache: [%s] -> %p\n", baseName, retVal); | |
326 | #endif | |
327 | return retVal; | |
328 | } | |
329 | ||
330 | ||
331 | static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) { | |
332 | DataCacheElement *newElement; | |
333 | const char *baseName; | |
334 | int32_t nameLen; | |
335 | UHashtable *htable; | |
729e4ab9 | 336 | DataCacheElement *oldValue = NULL; |
374ca955 | 337 | UErrorCode subErr = U_ZERO_ERROR; |
b75a7d8f | 338 | |
f3c0d7a5 | 339 | htable = udata_getHashTable(*pErr); |
b75a7d8f A |
340 | if (U_FAILURE(*pErr)) { |
341 | return NULL; | |
342 | } | |
343 | ||
344 | /* Create a new DataCacheElement - the thingy we store in the hash table - | |
345 | * and copy the supplied path and UDataMemoryItems into it. | |
346 | */ | |
729e4ab9 | 347 | newElement = (DataCacheElement *)uprv_malloc(sizeof(DataCacheElement)); |
b75a7d8f A |
348 | if (newElement == NULL) { |
349 | *pErr = U_MEMORY_ALLOCATION_ERROR; | |
350 | return NULL; | |
351 | } | |
352 | newElement->item = UDataMemory_createNewInstance(pErr); | |
353 | if (U_FAILURE(*pErr)) { | |
73c04bcf | 354 | uprv_free(newElement); |
b75a7d8f A |
355 | return NULL; |
356 | } | |
357 | UDatamemory_assign(newElement->item, item); | |
358 | ||
359 | baseName = findBasename(path); | |
360 | nameLen = (int32_t)uprv_strlen(baseName); | |
729e4ab9 | 361 | newElement->name = (char *)uprv_malloc(nameLen+1); |
b75a7d8f A |
362 | if (newElement->name == NULL) { |
363 | *pErr = U_MEMORY_ALLOCATION_ERROR; | |
73c04bcf A |
364 | uprv_free(newElement->item); |
365 | uprv_free(newElement); | |
b75a7d8f A |
366 | return NULL; |
367 | } | |
368 | uprv_strcpy(newElement->name, baseName); | |
369 | ||
370 | /* Stick the new DataCacheElement into the hash table. | |
371 | */ | |
b75a7d8f | 372 | umtx_lock(NULL); |
729e4ab9 | 373 | oldValue = (DataCacheElement *)uhash_get(htable, path); |
b75a7d8f | 374 | if (oldValue != NULL) { |
374ca955 A |
375 | subErr = U_USING_DEFAULT_WARNING; |
376 | } | |
b75a7d8f A |
377 | else { |
378 | uhash_put( | |
379 | htable, | |
380 | newElement->name, /* Key */ | |
381 | newElement, /* Value */ | |
374ca955 | 382 | &subErr); |
b75a7d8f A |
383 | } |
384 | umtx_unlock(NULL); | |
385 | ||
386 | #ifdef UDATA_DEBUG | |
374ca955 A |
387 | fprintf(stderr, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement->name, |
388 | newElement->item, u_errorName(subErr), newElement->item->vFuncs); | |
b75a7d8f A |
389 | #endif |
390 | ||
374ca955 A |
391 | if (subErr == U_USING_DEFAULT_WARNING || U_FAILURE(subErr)) { |
392 | *pErr = subErr; /* copy sub err unto fillin ONLY if something happens. */ | |
b75a7d8f A |
393 | uprv_free(newElement->name); |
394 | uprv_free(newElement->item); | |
395 | uprv_free(newElement); | |
729e4ab9 | 396 | return oldValue ? oldValue->item : NULL; |
b75a7d8f A |
397 | } |
398 | ||
399 | return newElement->item; | |
400 | } | |
401 | ||
b75a7d8f A |
402 | /*----------------------------------------------------------------------*============== |
403 | * * | |
404 | * Path management. Could be shared with other tools/etc if need be * | |
405 | * later on. * | |
406 | * * | |
407 | *----------------------------------------------------------------------*/ | |
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 | * * | |
f3c0d7a5 | 632 | * Add a static reference to the common data library * |
b75a7d8f A |
633 | * Unless overridden by an explicit udata_setCommonData, this will be * |
634 | * our common data. * | |
635 | * * | |
636 | *----------------------------------------------------------------------*/ | |
f3c0d7a5 | 637 | #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time |
729e4ab9 | 638 | extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT; |
f3c0d7a5 | 639 | #endif |
b75a7d8f | 640 | |
729e4ab9 A |
641 | /* |
642 | * This would be a good place for weak-linkage declarations of | |
643 | * partial-data-library access functions where each returns a pointer | |
644 | * to its data package, if it is linked in. | |
645 | */ | |
646 | /* | |
647 | extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK; | |
648 | extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK; | |
649 | */ | |
b75a7d8f A |
650 | |
651 | /*----------------------------------------------------------------------* | |
652 | * * | |
653 | * openCommonData Attempt to open a common format (.dat) file * | |
654 | * Map it into memory (if it's not there already) * | |
655 | * and return a UDataMemory object for it. * | |
656 | * * | |
657 | * If the requested data is already open and cached * | |
658 | * just return the cached UDataMem object. * | |
659 | * * | |
660 | *----------------------------------------------------------------------*/ | |
661 | static UDataMemory * | |
73c04bcf | 662 | openCommonData(const char *path, /* Path from OpenChoice? */ |
729e4ab9 | 663 | int32_t commonDataIndex, /* ICU Data (index >= 0) if path == NULL */ |
b75a7d8f A |
664 | UErrorCode *pErrorCode) |
665 | { | |
666 | UDataMemory tData; | |
b75a7d8f | 667 | const char *pathBuffer; |
b75a7d8f A |
668 | const char *inBasename; |
669 | ||
670 | if (U_FAILURE(*pErrorCode)) { | |
671 | return NULL; | |
672 | } | |
673 | ||
674 | UDataMemory_init(&tData); | |
675 | ||
676 | /* ??????? TODO revisit this */ | |
729e4ab9 | 677 | if (commonDataIndex >= 0) { |
b75a7d8f | 678 | /* "mini-cache" for common ICU data */ |
b331163b | 679 | if(commonDataIndex >= UPRV_LENGTHOF(gCommonICUDataArray)) { |
729e4ab9 | 680 | return NULL; |
b75a7d8f | 681 | } |
2ca993e8 A |
682 | { |
683 | Mutex lock; | |
684 | if(gCommonICUDataArray[commonDataIndex] != NULL) { | |
685 | return gCommonICUDataArray[commonDataIndex]; | |
686 | } | |
f3c0d7a5 | 687 | #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time |
729e4ab9 A |
688 | int32_t i; |
689 | for(i = 0; i < commonDataIndex; ++i) { | |
690 | if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) { | |
691 | /* The linked-in data is already in the list. */ | |
692 | return NULL; | |
693 | } | |
694 | } | |
f3c0d7a5 | 695 | #endif |
2ca993e8 | 696 | } |
b75a7d8f | 697 | |
2ca993e8 A |
698 | /* Add the linked-in data to the list. */ |
699 | /* | |
700 | * This is where we would check and call weakly linked partial-data-library | |
701 | * access functions. | |
702 | */ | |
703 | /* | |
704 | if (uprv_getICUData_collation) { | |
705 | setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode); | |
706 | } | |
707 | if (uprv_getICUData_conversion) { | |
708 | setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode); | |
709 | } | |
710 | */ | |
f3c0d7a5 | 711 | #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time |
2ca993e8 A |
712 | setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, FALSE, pErrorCode); |
713 | { | |
714 | Mutex lock; | |
715 | return gCommonICUDataArray[commonDataIndex]; | |
729e4ab9 | 716 | } |
f3c0d7a5 | 717 | #endif |
b75a7d8f A |
718 | } |
719 | ||
720 | ||
721 | /* request is NOT for ICU Data. */ | |
722 | ||
723 | /* Find the base name portion of the supplied path. */ | |
724 | /* inBasename will be left pointing somewhere within the original path string. */ | |
725 | inBasename = findBasename(path); | |
726 | #ifdef UDATA_DEBUG | |
727 | fprintf(stderr, "inBasename = %s\n", inBasename); | |
728 | #endif | |
729 | ||
730 | if(*inBasename==0) { | |
731 | /* no basename. This will happen if the original path was a directory name, */ | |
732 | /* like "a/b/c/". (Fallback to separate files will still work.) */ | |
733 | #ifdef UDATA_DEBUG | |
734 | fprintf(stderr, "ocd: no basename in %s, bailing.\n", path); | |
735 | #endif | |
f3c0d7a5 A |
736 | if (U_SUCCESS(*pErrorCode)) { |
737 | *pErrorCode=U_FILE_ACCESS_ERROR; | |
738 | } | |
b75a7d8f A |
739 | return NULL; |
740 | } | |
741 | ||
742 | /* Is the requested common data file already open and cached? */ | |
743 | /* Note that the cache is keyed by the base name only. The rest of the path, */ | |
744 | /* if any, is not considered. */ | |
f3c0d7a5 A |
745 | UDataMemory *dataToReturn = udata_findCachedData(inBasename, *pErrorCode); |
746 | if (dataToReturn != NULL || U_FAILURE(*pErrorCode)) { | |
747 | return dataToReturn; | |
b75a7d8f A |
748 | } |
749 | ||
750 | /* Requested item is not in the cache. | |
751 | * Hunt it down, trying all the path locations | |
752 | */ | |
753 | ||
729e4ab9 | 754 | UDataPathIterator iter(u_getDataDirectory(), inBasename, path, ".dat", TRUE, pErrorCode); |
b75a7d8f | 755 | |
729e4ab9 | 756 | while((UDataMemory_isLoaded(&tData)==FALSE) && (pathBuffer = iter.next(pErrorCode)) != NULL) |
b75a7d8f A |
757 | { |
758 | #ifdef UDATA_DEBUG | |
759 | fprintf(stderr, "ocd: trying path %s - ", pathBuffer); | |
760 | #endif | |
761 | uprv_mapFile(&tData, pathBuffer); | |
762 | #ifdef UDATA_DEBUG | |
763 | fprintf(stderr, "%s\n", UDataMemory_isLoaded(&tData)?"LOADED":"not loaded"); | |
764 | #endif | |
765 | } | |
766 | ||
767 | #if defined(OS390_STUBDATA) && defined(OS390BATCH) | |
768 | if (!UDataMemory_isLoaded(&tData)) { | |
769 | char ourPathBuffer[1024]; | |
770 | /* One more chance, for extendCommonData() */ | |
771 | uprv_strncpy(ourPathBuffer, path, 1019); | |
772 | ourPathBuffer[1019]=0; | |
773 | uprv_strcat(ourPathBuffer, ".dat"); | |
774 | uprv_mapFile(&tData, ourPathBuffer); | |
775 | } | |
776 | #endif | |
777 | ||
f3c0d7a5 A |
778 | if (U_FAILURE(*pErrorCode)) { |
779 | return NULL; | |
780 | } | |
b75a7d8f A |
781 | if (!UDataMemory_isLoaded(&tData)) { |
782 | /* no common data */ | |
783 | *pErrorCode=U_FILE_ACCESS_ERROR; | |
784 | return NULL; | |
785 | } | |
786 | ||
787 | /* we have mapped a file, check its header */ | |
788 | udata_checkCommonData(&tData, pErrorCode); | |
789 | ||
790 | ||
791 | /* Cache the UDataMemory struct for this .dat file, | |
792 | * so we won't need to hunt it down and map it again next time | |
793 | * something is needed from it. */ | |
794 | return udata_cacheDataItem(inBasename, &tData, pErrorCode); | |
795 | } | |
796 | ||
797 | ||
b75a7d8f A |
798 | /*----------------------------------------------------------------------* |
799 | * * | |
800 | * extendICUData If the full set of ICU data was not loaded at * | |
801 | * program startup, load it now. This function will * | |
802 | * be called when the lookup of an ICU data item in * | |
803 | * the common ICU data fails. * | |
804 | * * | |
b75a7d8f A |
805 | * return true if new data is loaded, false otherwise.* |
806 | * * | |
807 | *----------------------------------------------------------------------*/ | |
729e4ab9 | 808 | static UBool extendICUData(UErrorCode *pErr) |
b75a7d8f | 809 | { |
b75a7d8f A |
810 | UDataMemory *pData; |
811 | UDataMemory copyPData; | |
729e4ab9 A |
812 | UBool didUpdate = FALSE; |
813 | ||
814 | /* | |
815 | * There is a chance for a race condition here. | |
816 | * Normally, ICU data is loaded from a DLL or via mmap() and | |
817 | * setCommonICUData() will detect if the same address is set twice. | |
818 | * If ICU is built with data loading via fread() then the address will | |
819 | * be different each time the common data is loaded and we may add | |
820 | * multiple copies of the data. | |
821 | * In this case, use a mutex to prevent the race. | |
822 | * Use a specific mutex to avoid nested locks of the global mutex. | |
823 | */ | |
824 | #if MAP_IMPLEMENTATION==MAP_STDIO | |
51004dcb | 825 | static UMutex extendICUDataMutex = U_MUTEX_INITIALIZER; |
729e4ab9 A |
826 | umtx_lock(&extendICUDataMutex); |
827 | #endif | |
2ca993e8 | 828 | if(!umtx_loadAcquire(gHaveTriedToLoadCommonData)) { |
729e4ab9 A |
829 | /* See if we can explicitly open a .dat file for the ICUData. */ |
830 | pData = openCommonData( | |
831 | U_ICUDATA_NAME, /* "icudt20l" , for example. */ | |
832 | -1, /* Pretend we're not opening ICUData */ | |
833 | pErr); | |
834 | ||
835 | /* How about if there is no pData, eh... */ | |
836 | ||
837 | UDataMemory_init(©PData); | |
838 | if(pData != NULL) { | |
839 | UDatamemory_assign(©PData, pData); | |
840 | copyPData.map = 0; /* The mapping for this data is owned by the hash table */ | |
841 | copyPData.mapAddr = 0; /* which will unmap it when ICU is shut down. */ | |
842 | /* CommonICUData is also unmapped when ICU is shut down.*/ | |
843 | /* To avoid unmapping the data twice, zero out the map */ | |
844 | /* fields in the UDataMemory that we're assigning */ | |
845 | /* to CommonICUData. */ | |
846 | ||
4388f060 | 847 | didUpdate = /* no longer using this result */ |
729e4ab9 A |
848 | setCommonICUData(©PData,/* The new common data. */ |
849 | FALSE, /* No warnings if write didn't happen */ | |
850 | pErr); /* setCommonICUData honors errors; NOP if error set */ | |
851 | } | |
b75a7d8f | 852 | |
2ca993e8 | 853 | umtx_storeRelease(gHaveTriedToLoadCommonData, 1); |
b75a7d8f A |
854 | } |
855 | ||
f3c0d7a5 | 856 | didUpdate = findCommonICUDataByName(U_ICUDATA_NAME, *pErr); /* Return 'true' when a racing writes out the extended */ |
729e4ab9 A |
857 | /* data after another thread has failed to see it (in openCommonData), so */ |
858 | /* extended data can be examined. */ | |
859 | /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */ | |
b75a7d8f | 860 | |
729e4ab9 A |
861 | #if MAP_IMPLEMENTATION==MAP_STDIO |
862 | umtx_unlock(&extendICUDataMutex); | |
863 | #endif | |
864 | return didUpdate; /* Return true if ICUData pointer was updated. */ | |
b75a7d8f A |
865 | /* (Could potentialy have been done by another thread racing */ |
866 | /* us through here, but that's fine, we still return true */ | |
867 | /* so that current thread will also examine extended data. */ | |
868 | } | |
869 | ||
b75a7d8f A |
870 | /*----------------------------------------------------------------------* |
871 | * * | |
872 | * udata_setCommonData * | |
873 | * * | |
874 | *----------------------------------------------------------------------*/ | |
875 | U_CAPI void U_EXPORT2 | |
876 | udata_setCommonData(const void *data, UErrorCode *pErrorCode) { | |
877 | UDataMemory dataMemory; | |
878 | ||
879 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
880 | return; | |
881 | } | |
882 | ||
883 | if(data==NULL) { | |
884 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
885 | return; | |
886 | } | |
887 | ||
b75a7d8f A |
888 | /* set the data pointer and test for validity */ |
889 | UDataMemory_init(&dataMemory); | |
890 | UDataMemory_setData(&dataMemory, data); | |
891 | udata_checkCommonData(&dataMemory, pErrorCode); | |
892 | if (U_FAILURE(*pErrorCode)) {return;} | |
893 | ||
894 | /* we have good data */ | |
895 | /* Set it up as the ICU Common Data. */ | |
729e4ab9 | 896 | setCommonICUData(&dataMemory, TRUE, pErrorCode); |
b75a7d8f A |
897 | } |
898 | ||
b75a7d8f A |
899 | /*--------------------------------------------------------------------------- |
900 | * | |
901 | * udata_setAppData | |
902 | * | |
903 | *---------------------------------------------------------------------------- */ | |
904 | U_CAPI void U_EXPORT2 | |
905 | udata_setAppData(const char *path, const void *data, UErrorCode *err) | |
906 | { | |
907 | UDataMemory udm; | |
908 | ||
909 | if(err==NULL || U_FAILURE(*err)) { | |
910 | return; | |
911 | } | |
912 | if(data==NULL) { | |
913 | *err=U_ILLEGAL_ARGUMENT_ERROR; | |
914 | return; | |
915 | } | |
916 | ||
917 | UDataMemory_init(&udm); | |
729e4ab9 | 918 | UDataMemory_setData(&udm, data); |
b75a7d8f A |
919 | udata_checkCommonData(&udm, err); |
920 | udata_cacheDataItem(path, &udm, err); | |
921 | } | |
922 | ||
923 | /*----------------------------------------------------------------------------* | |
924 | * * | |
925 | * checkDataItem Given a freshly located/loaded data item, either * | |
926 | * an entry in a common file or a separately loaded file, * | |
927 | * sanity check its header, and see if the data is * | |
928 | * acceptable to the app. * | |
929 | * If the data is good, create and return a UDataMemory * | |
930 | * object that can be returned to the application. * | |
931 | * Return NULL on any sort of failure. * | |
932 | * * | |
933 | *----------------------------------------------------------------------------*/ | |
934 | static UDataMemory * | |
935 | checkDataItem | |
936 | ( | |
937 | const DataHeader *pHeader, /* The data item to be checked. */ | |
938 | UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */ | |
939 | void *context, /* pass-thru param for above. */ | |
940 | const char *type, /* pass-thru param for above. */ | |
941 | const char *name, /* pass-thru param for above. */ | |
942 | UErrorCode *nonFatalErr, /* Error code if this data was not acceptable */ | |
943 | /* but openChoice should continue with */ | |
944 | /* trying to get data from fallback path. */ | |
945 | UErrorCode *fatalErr /* Bad error, caller should return immediately */ | |
946 | ) | |
947 | { | |
948 | UDataMemory *rDataMem = NULL; /* the new UDataMemory, to be returned. */ | |
949 | ||
950 | if (U_FAILURE(*fatalErr)) { | |
951 | return NULL; | |
952 | } | |
953 | ||
954 | if(pHeader->dataHeader.magic1==0xda && | |
955 | pHeader->dataHeader.magic2==0x27 && | |
b75a7d8f | 956 | (isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info)) |
374ca955 | 957 | ) { |
b75a7d8f A |
958 | rDataMem=UDataMemory_createNewInstance(fatalErr); |
959 | if (U_FAILURE(*fatalErr)) { | |
960 | return NULL; | |
961 | } | |
962 | rDataMem->pHeader = pHeader; | |
963 | } else { | |
964 | /* the data is not acceptable, look further */ | |
965 | /* If we eventually find something good, this errorcode will be */ | |
966 | /* cleared out. */ | |
967 | *nonFatalErr=U_INVALID_FORMAT_ERROR; | |
968 | } | |
969 | return rDataMem; | |
970 | } | |
971 | ||
73c04bcf A |
972 | /** |
973 | * @return 0 if not loaded, 1 if loaded or err | |
974 | */ | |
975 | static UDataMemory *doLoadFromIndividualFiles(const char *pkgName, | |
976 | const char *dataPath, const char *tocEntryPathSuffix, | |
977 | /* following arguments are the same as doOpenChoice itself */ | |
978 | const char *path, const char *type, const char *name, | |
979 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
980 | UErrorCode *subErrorCode, | |
981 | UErrorCode *pErrorCode) | |
982 | { | |
73c04bcf A |
983 | const char *pathBuffer; |
984 | UDataMemory dataMemory; | |
985 | UDataMemory *pEntryData; | |
986 | ||
73c04bcf A |
987 | /* look in ind. files: package\nam.typ ========================= */ |
988 | /* init path iterator for individual files */ | |
729e4ab9 | 989 | UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, FALSE, pErrorCode); |
73c04bcf | 990 | |
0f5d89e8 | 991 | while((pathBuffer = iter.next(pErrorCode)) != NULL) |
73c04bcf A |
992 | { |
993 | #ifdef UDATA_DEBUG | |
994 | fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer); | |
995 | #endif | |
996 | if(uprv_mapFile(&dataMemory, pathBuffer)) | |
997 | { | |
998 | pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); | |
999 | if (pEntryData != NULL) { | |
1000 | /* Data is good. | |
1001 | * Hand off ownership of the backing memory to the user's UDataMemory. | |
1002 | * and return it. */ | |
1003 | pEntryData->mapAddr = dataMemory.mapAddr; | |
1004 | pEntryData->map = dataMemory.map; | |
1005 | ||
1006 | #ifdef UDATA_DEBUG | |
1007 | fprintf(stderr, "** Mapped file: %s\n", pathBuffer); | |
1008 | #endif | |
729e4ab9 | 1009 | return pEntryData; |
73c04bcf A |
1010 | } |
1011 | ||
1012 | /* the data is not acceptable, or some error occured. Either way, unmap the memory */ | |
1013 | udata_close(&dataMemory); | |
1014 | ||
1015 | /* If we had a nasty error, bail out completely. */ | |
1016 | if (U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1017 | return NULL; |
73c04bcf A |
1018 | } |
1019 | ||
1020 | /* Otherwise remember that we found data but didn't like it for some reason */ | |
1021 | *subErrorCode=U_INVALID_FORMAT_ERROR; | |
1022 | } | |
1023 | #ifdef UDATA_DEBUG | |
1024 | fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded"); | |
1025 | #endif | |
1026 | } | |
729e4ab9 | 1027 | return NULL; |
73c04bcf A |
1028 | } |
1029 | ||
1030 | /** | |
1031 | * @return 0 if not loaded, 1 if loaded or err | |
1032 | */ | |
729e4ab9 A |
1033 | static UDataMemory *doLoadFromCommonData(UBool isICUData, const char * /*pkgName*/, |
1034 | const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName, | |
73c04bcf A |
1035 | /* following arguments are the same as doOpenChoice itself */ |
1036 | const char *path, const char *type, const char *name, | |
1037 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
1038 | UErrorCode *subErrorCode, | |
1039 | UErrorCode *pErrorCode) | |
1040 | { | |
73c04bcf A |
1041 | UDataMemory *pEntryData; |
1042 | const DataHeader *pHeader; | |
1043 | UDataMemory *pCommonData; | |
729e4ab9 A |
1044 | int32_t commonDataIndex; |
1045 | UBool checkedExtendedICUData = FALSE; | |
73c04bcf A |
1046 | /* try to get common data. The loop is for platforms such as the 390 that do |
1047 | * not initially load the full set of ICU data. If the lookup of an ICU data item | |
1048 | * fails, the full (but slower to load) set is loaded, the and the loop repeats, | |
1049 | * trying the lookup again. Once the full set of ICU data is loaded, the loop wont | |
1050 | * repeat because the full set will be checked the first time through. | |
1051 | * | |
1052 | * The loop also handles the fallback to a .dat file if the application linked | |
1053 | * to the stub data library rather than a real library. | |
1054 | */ | |
729e4ab9 A |
1055 | for (commonDataIndex = isICUData ? 0 : -1;;) { |
1056 | pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/ | |
73c04bcf | 1057 | |
729e4ab9 | 1058 | if(U_SUCCESS(*subErrorCode) && pCommonData!=NULL) { |
73c04bcf A |
1059 | int32_t length; |
1060 | ||
1061 | /* look up the data piece in the common data */ | |
1062 | pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &length, subErrorCode); | |
1063 | #ifdef UDATA_DEBUG | |
1064 | fprintf(stderr, "%s: pHeader=%p - %s\n", tocEntryName, pHeader, u_errorName(*subErrorCode)); | |
1065 | #endif | |
b75a7d8f | 1066 | |
73c04bcf A |
1067 | if(pHeader!=NULL) { |
1068 | pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); | |
1069 | #ifdef UDATA_DEBUG | |
1070 | fprintf(stderr, "pEntryData=%p\n", pEntryData); | |
1071 | #endif | |
1072 | if (U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1073 | return NULL; |
73c04bcf A |
1074 | } |
1075 | if (pEntryData != NULL) { | |
1076 | pEntryData->length = length; | |
729e4ab9 | 1077 | return pEntryData; |
73c04bcf A |
1078 | } |
1079 | } | |
1080 | } | |
1081 | /* Data wasn't found. If we were looking for an ICUData item and there is | |
1082 | * more data available, load it and try again, | |
1083 | * otherwise break out of this loop. */ | |
729e4ab9 A |
1084 | if (!isICUData) { |
1085 | return NULL; | |
1086 | } else if (pCommonData != NULL) { | |
1087 | ++commonDataIndex; /* try the next data package */ | |
1088 | } else if ((!checkedExtendedICUData) && extendICUData(subErrorCode)) { | |
1089 | checkedExtendedICUData = TRUE; | |
1090 | /* try this data package slot again: it changed from NULL to non-NULL */ | |
1091 | } else { | |
1092 | return NULL; | |
73c04bcf A |
1093 | } |
1094 | } | |
73c04bcf | 1095 | } |
b75a7d8f | 1096 | |
b331163b A |
1097 | /* |
1098 | * Identify the Time Zone resources that are subject to special override data loading. | |
1099 | */ | |
1100 | static UBool isTimeZoneFile(const char *name, const char *type) { | |
1101 | return ((uprv_strcmp(type, "res") == 0) && | |
1102 | (uprv_strcmp(name, "zoneinfo64") == 0 || | |
1103 | uprv_strcmp(name, "timezoneTypes") == 0 || | |
1104 | uprv_strcmp(name, "windowsZones") == 0 || | |
1105 | uprv_strcmp(name, "metaZones") == 0)); | |
1106 | } | |
1107 | ||
b75a7d8f A |
1108 | /* |
1109 | * A note on the ownership of Mapped Memory | |
1110 | * | |
1111 | * For common format files, ownership resides with the UDataMemory object | |
1112 | * that lives in the cache of opened common data. These UDataMemorys are private | |
1113 | * to the udata implementation, and are never seen directly by users. | |
1114 | * | |
1115 | * The UDataMemory objects returned to users will have the address of some desired | |
1116 | * data within the mapped region, but they wont have the mapping info itself, and thus | |
1117 | * won't cause anything to be removed from memory when they are closed. | |
1118 | * | |
1119 | * For individual data files, the UDataMemory returned to the user holds the | |
1120 | * information necessary to unmap the data on close. If the user independently | |
1121 | * opens the same data file twice, two completely independent mappings will be made. | |
1122 | * (There is no cache of opened data items from individual files, only a cache of | |
1123 | * opened Common Data files, that is, files containing a collection of data items.) | |
1124 | * | |
1125 | * For common data passed in from the user via udata_setAppData() or | |
1126 | * udata_setCommonData(), ownership remains with the user. | |
1127 | * | |
1128 | * UDataMemory objects themselves, as opposed to the memory they describe, | |
1129 | * can be anywhere - heap, stack/local or global. | |
1130 | * They have a flag to indicate when they're heap allocated and thus | |
1131 | * must be deleted when closed. | |
1132 | */ | |
1133 | ||
1134 | ||
1135 | /*----------------------------------------------------------------------------* | |
1136 | * * | |
1137 | * main data loading functions * | |
1138 | * * | |
1139 | *----------------------------------------------------------------------------*/ | |
1140 | static UDataMemory * | |
1141 | doOpenChoice(const char *path, const char *type, const char *name, | |
1142 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
1143 | UErrorCode *pErrorCode) | |
1144 | { | |
374ca955 | 1145 | UDataMemory *retVal = NULL; |
b75a7d8f | 1146 | |
b75a7d8f A |
1147 | const char *dataPath; |
1148 | ||
374ca955 A |
1149 | int32_t tocEntrySuffixIndex; |
1150 | const char *tocEntryPathSuffix; | |
73c04bcf | 1151 | UErrorCode subErrorCode=U_ZERO_ERROR; |
374ca955 A |
1152 | const char *treeChar; |
1153 | ||
1154 | UBool isICUData = FALSE; | |
1155 | ||
73c04bcf A |
1156 | |
1157 | /* Is this path ICU data? */ | |
374ca955 | 1158 | if(path == NULL || |
73c04bcf A |
1159 | !strcmp(path, U_ICUDATA_ALIAS) || /* "ICUDATA" */ |
1160 | !uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING, /* "icudt26e-" */ | |
1161 | uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)) || | |
1162 | !uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING, /* "ICUDATA-" */ | |
374ca955 A |
1163 | uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING))) { |
1164 | isICUData = TRUE; | |
1165 | } | |
1166 | ||
73c04bcf | 1167 | #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */ |
374ca955 | 1168 | /* remap from alternate path char to the main one */ |
729e4ab9 | 1169 | CharString altSepPath; |
374ca955 | 1170 | if(path) { |
729e4ab9 A |
1171 | if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR) != NULL) { |
1172 | altSepPath.append(path, *pErrorCode); | |
1173 | char *p; | |
0f5d89e8 | 1174 | while ((p = uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR)) != NULL) { |
374ca955 A |
1175 | *p = U_FILE_SEP_CHAR; |
1176 | } | |
1177 | #if defined (UDATA_DEBUG) | |
1178 | fprintf(stderr, "Changed path from [%s] to [%s]\n", path, altSepPath.s); | |
1179 | #endif | |
729e4ab9 | 1180 | path = altSepPath.data(); |
374ca955 A |
1181 | } |
1182 | } | |
1183 | #endif | |
1184 | ||
729e4ab9 A |
1185 | CharString tocEntryName; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */ |
1186 | CharString tocEntryPath; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */ | |
374ca955 | 1187 | |
729e4ab9 A |
1188 | CharString pkgName; |
1189 | CharString treeName; | |
374ca955 | 1190 | |
73c04bcf | 1191 | /* ======= Set up strings */ |
374ca955 | 1192 | if(path==NULL) { |
729e4ab9 | 1193 | pkgName.append(U_ICUDATA_NAME, *pErrorCode); |
374ca955 A |
1194 | } else { |
1195 | const char *pkg; | |
1196 | const char *first; | |
1197 | pkg = uprv_strrchr(path, U_FILE_SEP_CHAR); | |
1198 | first = uprv_strchr(path, U_FILE_SEP_CHAR); | |
1199 | if(uprv_pathIsAbsolute(path) || (pkg != first)) { /* more than one slash in the path- not a tree name */ | |
1200 | /* see if this is an /absolute/path/to/package path */ | |
1201 | if(pkg) { | |
729e4ab9 | 1202 | pkgName.append(pkg+1, *pErrorCode); |
374ca955 | 1203 | } else { |
729e4ab9 | 1204 | pkgName.append(path, *pErrorCode); |
374ca955 A |
1205 | } |
1206 | } else { | |
1207 | treeChar = uprv_strchr(path, U_TREE_SEPARATOR); | |
1208 | if(treeChar) { | |
729e4ab9 | 1209 | treeName.append(treeChar+1, *pErrorCode); /* following '-' */ |
73c04bcf | 1210 | if(isICUData) { |
729e4ab9 | 1211 | pkgName.append(U_ICUDATA_NAME, *pErrorCode); |
73c04bcf | 1212 | } else { |
729e4ab9 | 1213 | pkgName.append(path, (int32_t)(treeChar-path), *pErrorCode); |
73c04bcf A |
1214 | if (first == NULL) { |
1215 | /* | |
1216 | This user data has no path, but there is a tree name. | |
1217 | Look up the correct path from the data cache later. | |
1218 | */ | |
729e4ab9 | 1219 | path = pkgName.data(); |
73c04bcf | 1220 | } |
374ca955 A |
1221 | } |
1222 | } else { | |
73c04bcf | 1223 | if(isICUData) { |
729e4ab9 | 1224 | pkgName.append(U_ICUDATA_NAME, *pErrorCode); |
73c04bcf | 1225 | } else { |
729e4ab9 | 1226 | pkgName.append(path, *pErrorCode); |
374ca955 A |
1227 | } |
1228 | } | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | #ifdef UDATA_DEBUG | |
729e4ab9 | 1233 | fprintf(stderr, " P=%s T=%s\n", pkgName.data(), treeName.data()); |
374ca955 A |
1234 | #endif |
1235 | ||
73c04bcf A |
1236 | /* setting up the entry name and file name |
1237 | * Make up a full name by appending the type to the supplied | |
b75a7d8f A |
1238 | * name, assuming that a type was supplied. |
1239 | */ | |
1240 | ||
1241 | /* prepend the package */ | |
729e4ab9 A |
1242 | tocEntryName.append(pkgName, *pErrorCode); |
1243 | tocEntryPath.append(pkgName, *pErrorCode); | |
1244 | tocEntrySuffixIndex = tocEntryName.length(); | |
b75a7d8f | 1245 | |
729e4ab9 A |
1246 | if(!treeName.isEmpty()) { |
1247 | tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode); | |
1248 | tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode); | |
374ca955 | 1249 | } |
b75a7d8f | 1250 | |
729e4ab9 A |
1251 | tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(name, *pErrorCode); |
1252 | tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(name, *pErrorCode); | |
b75a7d8f | 1253 | if(type!=NULL && *type!=0) { |
729e4ab9 A |
1254 | tocEntryName.append(".", *pErrorCode).append(type, *pErrorCode); |
1255 | tocEntryPath.append(".", *pErrorCode).append(type, *pErrorCode); | |
b75a7d8f | 1256 | } |
729e4ab9 | 1257 | tocEntryPathSuffix = tocEntryPath.data()+tocEntrySuffixIndex; /* suffix starts here */ |
b75a7d8f A |
1258 | |
1259 | #ifdef UDATA_DEBUG | |
729e4ab9 A |
1260 | fprintf(stderr, " tocEntryName = %s\n", tocEntryName.data()); |
1261 | fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.data()); | |
1262 | #endif | |
b75a7d8f | 1263 | |
f3c0d7a5 | 1264 | #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time |
b75a7d8f | 1265 | if(path == NULL) { |
73c04bcf | 1266 | path = COMMON_DATA_NAME; /* "icudt26e" */ |
b75a7d8f | 1267 | } |
f3c0d7a5 A |
1268 | #else |
1269 | // Windows UWP expects only a single data file. | |
1270 | path = COMMON_DATA_NAME; /* "icudt26e" */ | |
1271 | #endif | |
b75a7d8f A |
1272 | |
1273 | /************************ Begin loop looking for ind. files ***************/ | |
1274 | #ifdef UDATA_DEBUG | |
729e4ab9 | 1275 | fprintf(stderr, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path)); |
b75a7d8f A |
1276 | #endif |
1277 | ||
b75a7d8f | 1278 | /* End of dealing with a null basename */ |
b75a7d8f A |
1279 | dataPath = u_getDataDirectory(); |
1280 | ||
b331163b | 1281 | /**** Time zone individual files override */ |
f3c0d7a5 | 1282 | if (isICUData && isTimeZoneFile(name, type)) { |
b331163b A |
1283 | const char *tzFilesDir = u_getTimeZoneFilesDirectory(pErrorCode); |
1284 | if (tzFilesDir[0] != 0) { | |
1285 | #ifdef UDATA_DEBUG | |
1286 | fprintf(stderr, "Trying Time Zone Files directory = %s\n", tzFilesDir); | |
2ca993e8 A |
1287 | #endif |
1288 | #ifdef UDATA_TZFILES_DEBUG | |
1289 | fprintf(stderr, "# dOC U_TIMEZONE_FILES_DIR: %s\n", U_TIMEZONE_FILES_DIR); | |
1290 | #endif | |
1291 | ||
1292 | #if defined(U_TIMEZONE_PACKAGE) | |
1293 | // make tztocEntryName, like tocEntryName but with our package name | |
1294 | UErrorCode tzpkgErrorCode = U_ZERO_ERROR; | |
1295 | CharString tztocPkgPath; | |
1296 | tztocPkgPath.append(tzFilesDir, tzpkgErrorCode); | |
1297 | tztocPkgPath.append(U_FILE_SEP_CHAR, tzpkgErrorCode).append(U_TIMEZONE_PACKAGE, tzpkgErrorCode); | |
1298 | CharString tztocEntryName; | |
1299 | tztocEntryName.append(U_TIMEZONE_PACKAGE, tzpkgErrorCode); | |
1300 | if(!treeName.isEmpty()) { | |
1301 | tztocEntryName.append(U_TREE_ENTRY_SEP_CHAR, tzpkgErrorCode).append(treeName, tzpkgErrorCode); | |
1302 | } | |
1303 | tztocEntryName.append(U_TREE_ENTRY_SEP_CHAR, tzpkgErrorCode).append(name, tzpkgErrorCode); | |
1304 | if(type!=NULL && *type!=0) { | |
1305 | tztocEntryName.append(".", tzpkgErrorCode).append(type, tzpkgErrorCode); | |
1306 | } | |
1307 | #ifdef UDATA_TZFILES_DEBUG | |
1308 | fprintf(stderr, "# dOC tz pkg, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: %s, tztocPkgPath.data(): %s, tztocEntryName.data(): %s, name: %s\n", | |
1309 | U_TIMEZONE_PACKAGE, tztocPkgPath.data(), tztocEntryName.data(), name); | |
1310 | #endif | |
1311 | retVal = doLoadFromCommonData(FALSE, "" /*ignored*/, "" /*ignored*/, "" /*ignored*/, | |
1312 | tztocEntryName.data(), // tocEntryName, like icutz44/zoneinfo64.res | |
1313 | tztocPkgPath.data(), // path = path to pkg, like /usr/share/icu/icutz44l | |
1314 | type, name, isAcceptable, context, &subErrorCode, &tzpkgErrorCode); | |
1315 | #ifdef UDATA_TZFILES_DEBUG | |
1316 | fprintf(stderr, "# dOC tz pkg, doLoadFromCommonData end; status %d, retVal %p\n", tzpkgErrorCode, retVal); | |
1317 | #endif | |
1318 | if(U_SUCCESS(tzpkgErrorCode) && retVal != NULL) { | |
1319 | return retVal; | |
1320 | } | |
1321 | #endif /* defined(U_TIMEZONE_PACKAGE) */ | |
1322 | // The following assumes any timezone resources in tzFilesDir are in individual .res files | |
1323 | #ifdef UDATA_TZFILES_DEBUG | |
1324 | fprintf(stderr, "# dOC tz files, doLoadFromIndividualFiles start; tzFilesDir: %s, tocEntryPathSuffix: %s, name: %s\n", | |
1325 | tzFilesDir, tocEntryPathSuffix, name); | |
b331163b A |
1326 | #endif |
1327 | retVal = doLoadFromIndividualFiles(/* pkgName.data() */ "", tzFilesDir, tocEntryPathSuffix, | |
1328 | /* path */ "", type, name, isAcceptable, context, &subErrorCode, pErrorCode); | |
2ca993e8 A |
1329 | #ifdef UDATA_TZFILES_DEBUG |
1330 | fprintf(stderr, "# dOC tz files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode, retVal); | |
1331 | #endif | |
b331163b A |
1332 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { |
1333 | return retVal; | |
1334 | } | |
1335 | } | |
1336 | } | |
1337 | ||
73c04bcf A |
1338 | /**** COMMON PACKAGE - only if packages are first. */ |
1339 | if(gDataFileAccess == UDATA_PACKAGES_FIRST) { | |
374ca955 | 1340 | #ifdef UDATA_DEBUG |
73c04bcf | 1341 | fprintf(stderr, "Trying packages (UDATA_PACKAGES_FIRST)\n"); |
374ca955 | 1342 | #endif |
73c04bcf | 1343 | /* #2 */ |
2ca993e8 A |
1344 | #ifdef UDATA_TZFILES_DEBUG |
1345 | if (isTimeZoneFile(name, type)) { | |
1346 | fprintf(stderr, "# dOC std common 1, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: path: %s, tocEntryName.data(): %s, name: %s\n", | |
1347 | path, tocEntryName.data(), name); | |
1348 | } | |
1349 | #endif | |
73c04bcf | 1350 | retVal = doLoadFromCommonData(isICUData, |
729e4ab9 | 1351 | pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(), |
73c04bcf | 1352 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
2ca993e8 A |
1353 | #ifdef UDATA_TZFILES_DEBUG |
1354 | if (isTimeZoneFile(name, type)) { | |
1355 | fprintf(stderr, "# dOC std common 1, doLoadFromCommonData end; status %d, retVal %p\n", *pErrorCode, retVal); | |
1356 | } | |
1357 | #endif | |
73c04bcf | 1358 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { |
729e4ab9 | 1359 | return retVal; |
b75a7d8f | 1360 | } |
73c04bcf | 1361 | } |
729e4ab9 | 1362 | |
73c04bcf A |
1363 | /**** INDIVIDUAL FILES */ |
1364 | if((gDataFileAccess==UDATA_PACKAGES_FIRST) || | |
1365 | (gDataFileAccess==UDATA_FILES_FIRST)) { | |
374ca955 | 1366 | #ifdef UDATA_DEBUG |
73c04bcf | 1367 | fprintf(stderr, "Trying individual files\n"); |
374ca955 | 1368 | #endif |
73c04bcf A |
1369 | /* Check to make sure that there is a dataPath to iterate over */ |
1370 | if ((dataPath && *dataPath) || !isICUData) { | |
2ca993e8 A |
1371 | #ifdef UDATA_TZFILES_DEBUG |
1372 | if (isTimeZoneFile(name, type)) { | |
1373 | fprintf(stderr, "# dOC std indiv files, doLoadFromIndividualFiles start; dataPath: %s, tocEntryPathSuffix: %s, name: %s\n", | |
1374 | dataPath, tocEntryPathSuffix, name); | |
1375 | } | |
1376 | #endif | |
729e4ab9 | 1377 | retVal = doLoadFromIndividualFiles(pkgName.data(), dataPath, tocEntryPathSuffix, |
73c04bcf | 1378 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
2ca993e8 A |
1379 | #ifdef UDATA_TZFILES_DEBUG |
1380 | if (isTimeZoneFile(name, type)) { | |
1381 | fprintf(stderr, "# dOC std indiv files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode, retVal); | |
1382 | } | |
1383 | #endif | |
73c04bcf | 1384 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { |
729e4ab9 | 1385 | return retVal; |
374ca955 | 1386 | } |
374ca955 | 1387 | } |
b75a7d8f A |
1388 | } |
1389 | ||
73c04bcf A |
1390 | /**** COMMON PACKAGE */ |
1391 | if((gDataFileAccess==UDATA_ONLY_PACKAGES) || | |
1392 | (gDataFileAccess==UDATA_FILES_FIRST)) { | |
b75a7d8f | 1393 | #ifdef UDATA_DEBUG |
73c04bcf | 1394 | fprintf(stderr, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n"); |
2ca993e8 A |
1395 | #endif |
1396 | #ifdef UDATA_TZFILES_DEBUG | |
1397 | if (isTimeZoneFile(name, type)) { | |
1398 | fprintf(stderr, "# dOC std common 2, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: path: %s, tocEntryName.data(): %s, name: %s\n", | |
1399 | path, tocEntryName.data(), name); | |
1400 | } | |
b75a7d8f | 1401 | #endif |
729e4ab9 A |
1402 | retVal = doLoadFromCommonData(isICUData, |
1403 | pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(), | |
73c04bcf | 1404 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
2ca993e8 A |
1405 | #ifdef UDATA_TZFILES_DEBUG |
1406 | if (isTimeZoneFile(name, type)) { | |
1407 | fprintf(stderr, "# dOC std common 2, doLoadFromCommonData end; status %d, retVal %p\n", *pErrorCode, retVal); | |
1408 | } | |
1409 | #endif | |
73c04bcf | 1410 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { |
729e4ab9 | 1411 | return retVal; |
73c04bcf A |
1412 | } |
1413 | } | |
1414 | ||
1415 | /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to | |
1416 | try a DLL (static or setCommonData/etc) load. | |
1417 | If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */ | |
1418 | if(gDataFileAccess==UDATA_NO_FILES) { | |
b75a7d8f | 1419 | #ifdef UDATA_DEBUG |
73c04bcf | 1420 | fprintf(stderr, "Trying common data (UDATA_NO_FILES)\n"); |
b75a7d8f | 1421 | #endif |
729e4ab9 A |
1422 | retVal = doLoadFromCommonData(isICUData, |
1423 | pkgName.data(), "", tocEntryPathSuffix, tocEntryName.data(), | |
73c04bcf A |
1424 | path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); |
1425 | if((retVal != NULL) || U_FAILURE(*pErrorCode)) { | |
729e4ab9 | 1426 | return retVal; |
b75a7d8f A |
1427 | } |
1428 | } | |
1429 | ||
1430 | /* data not found */ | |
1431 | if(U_SUCCESS(*pErrorCode)) { | |
73c04bcf | 1432 | if(U_SUCCESS(subErrorCode)) { |
b75a7d8f A |
1433 | /* file not found */ |
1434 | *pErrorCode=U_FILE_ACCESS_ERROR; | |
1435 | } else { | |
1436 | /* entry point not found or rejected */ | |
73c04bcf | 1437 | *pErrorCode=subErrorCode; |
b75a7d8f A |
1438 | } |
1439 | } | |
374ca955 | 1440 | return retVal; |
b75a7d8f A |
1441 | } |
1442 | ||
1443 | ||
1444 | ||
1445 | /* API ---------------------------------------------------------------------- */ | |
1446 | ||
1447 | U_CAPI UDataMemory * U_EXPORT2 | |
1448 | udata_open(const char *path, const char *type, const char *name, | |
1449 | UErrorCode *pErrorCode) { | |
1450 | #ifdef UDATA_DEBUG | |
374ca955 | 1451 | fprintf(stderr, "udata_open(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type); |
b75a7d8f A |
1452 | fflush(stderr); |
1453 | #endif | |
1454 | ||
1455 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
1456 | return NULL; | |
1457 | } else if(name==NULL || *name==0) { | |
1458 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1459 | return NULL; | |
1460 | } else { | |
1461 | return doOpenChoice(path, type, name, NULL, NULL, pErrorCode); | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | ||
1466 | ||
1467 | U_CAPI UDataMemory * U_EXPORT2 | |
1468 | udata_openChoice(const char *path, const char *type, const char *name, | |
1469 | UDataMemoryIsAcceptable *isAcceptable, void *context, | |
1470 | UErrorCode *pErrorCode) { | |
1471 | #ifdef UDATA_DEBUG | |
374ca955 | 1472 | fprintf(stderr, "udata_openChoice(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type); |
b75a7d8f A |
1473 | #endif |
1474 | ||
1475 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
1476 | return NULL; | |
1477 | } else if(name==NULL || *name==0 || isAcceptable==NULL) { | |
1478 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1479 | return NULL; | |
1480 | } else { | |
1481 | return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode); | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | ||
1486 | ||
1487 | U_CAPI void U_EXPORT2 | |
1488 | udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) { | |
1489 | if(pInfo!=NULL) { | |
1490 | if(pData!=NULL && pData->pHeader!=NULL) { | |
1491 | const UDataInfo *info=&pData->pHeader->info; | |
374ca955 A |
1492 | uint16_t dataInfoSize=udata_getInfoSize(info); |
1493 | if(pInfo->size>dataInfoSize) { | |
1494 | pInfo->size=dataInfoSize; | |
1495 | } | |
1496 | uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2); | |
1497 | if(info->isBigEndian!=U_IS_BIG_ENDIAN) { | |
1498 | /* opposite endianness */ | |
1499 | uint16_t x=info->reservedWord; | |
1500 | pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8)); | |
b75a7d8f | 1501 | } |
b75a7d8f A |
1502 | } else { |
1503 | pInfo->size=0; | |
1504 | } | |
1505 | } | |
1506 | } | |
73c04bcf A |
1507 | |
1508 | ||
729e4ab9 | 1509 | U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/) |
73c04bcf | 1510 | { |
2ca993e8 | 1511 | // Note: this function is documented as not thread safe. |
73c04bcf A |
1512 | gDataFileAccess = access; |
1513 | } |