1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1999-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
10 * file name: udata.cpp
12 * tab size: 8 (not used)
15 * created on: 1999oct25
16 * created by: Markus W. Scherer
19 #include "unicode/utypes.h" /* U_PLATFORM etc. */
23 #define ATTRIBUTE_WEAK __attribute__ ((weak))
24 might have to #include some other header
28 #include "unicode/putil.h"
29 #include "unicode/udata.h"
30 #include "unicode/uversion.h"
44 /***********************************************************************
46 * Notes on the organization of the ICU data implementation
48 * All of the public API is defined in udata.h
50 * The implementation is split into several files...
52 * - udata.c (this file) contains higher level code that knows about
53 * the search paths for locating data, caching opened data, etc.
55 * - umapfile.c contains the low level platform-specific code for actually loading
56 * (memory mapping, file reading, whatever) data into memory.
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.
63 * - udatamem.c has code for managing UDataMemory structs. These are little
64 * descriptor objects for blocks of memory holding ICU data of
68 /* configuration ---------------------------------------------------------- */
70 /* If you are excruciatingly bored turn this on .. */
71 /* #define UDATA_DEBUG 1 */
72 /* For debugging use of timezone data in a separate file */
73 /* #define UDATA_TZFILES_DEBUG 1 */
75 #if defined(UDATA_DEBUG) || defined(UDATA_TZFILES_DEBUG)
82 * Forward declarations
84 static UDataMemory
*udata_findCachedData(const char *path
, UErrorCode
&err
);
86 /***********************************************************************
88 * static (Global) data
90 ************************************************************************/
93 * Pointers to the common ICU data.
95 * We store multiple pointers to ICU data packages and iterate through them
96 * when looking for a data item.
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.
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
108 static UDataMemory
*gCommonICUDataArray
[10] = { NULL
}; // Access protected by icu global mutex.
110 static u_atomic_int32_t gHaveTriedToLoadCommonData
= ATOMIC_INT32_T_INITIALIZER(0); // See extendICUData().
112 static UHashtable
*gCommonDataCache
= NULL
; /* Global hash table of opened ICU data files. */
113 static icu::UInitOnce gCommonDataCacheInitOnce
= U_INITONCE_INITIALIZER
;
115 #if U_PLATFORM_HAS_WINUWP_API == 0
116 static UDataFileAccess gDataFileAccess
= UDATA_DEFAULT_ACCESS
; // Access not synchronized.
117 // Modifying is documented as thread-unsafe.
119 static UDataFileAccess gDataFileAccess
= UDATA_NO_FILES
; // Windows UWP looks in one spot explicitly
122 static UBool U_CALLCONV
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. */
131 gCommonDataCacheInitOnce
.reset();
133 for (i
= 0; i
< UPRV_LENGTHOF(gCommonICUDataArray
) && gCommonICUDataArray
[i
] != NULL
; ++i
) {
134 udata_close(gCommonICUDataArray
[i
]);
135 gCommonICUDataArray
[i
] = NULL
;
137 gHaveTriedToLoadCommonData
= 0;
139 return TRUE
; /* Everything was cleaned up */
142 static UBool U_CALLCONV
143 findCommonICUDataByName(const char *inBasename
, UErrorCode
&err
)
148 UDataMemory
*pData
= udata_findCachedData(inBasename
, err
);
149 if (U_FAILURE(err
) || pData
== NULL
)
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. */
167 * setCommonICUData. Set a UDataMemory to be the global ICU Data
170 setCommonICUData(UDataMemory
*pData
, /* The new common data. Belongs to caller, we copy it. */
171 UBool warn
, /* If true, set USING_DEFAULT warning if ICUData was */
172 /* changed by another thread before we got to it. */
175 UDataMemory
*newCommonData
= UDataMemory_createNewInstance(pErr
);
177 UBool didUpdate
= FALSE
;
178 if (U_FAILURE(*pErr
)) {
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 */
186 UDatamemory_assign(newCommonData
, pData
);
188 for (i
= 0; i
< UPRV_LENGTHOF(gCommonICUDataArray
); ++i
) {
189 if (gCommonICUDataArray
[i
] == NULL
) {
190 gCommonICUDataArray
[i
] = newCommonData
;
193 } else if (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
) {
194 /* The same data pointer is already in the array. */
200 if (i
== UPRV_LENGTHOF(gCommonICUDataArray
) && warn
) {
201 *pErr
= U_USING_DEFAULT_WARNING
;
204 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
206 uprv_free(newCommonData
);
211 #if U_PLATFORM_HAS_WINUWP_API == 0
214 setCommonICUDataPointer(const void *pData
, UBool
/*warn*/, UErrorCode
*pErrorCode
) {
216 UDataMemory_init(&tData
);
217 UDataMemory_setData(&tData
, pData
);
218 udata_checkCommonData(&tData
, pErrorCode
);
219 return setCommonICUData(&tData
, FALSE
, pErrorCode
);
225 findBasename(const char *path
) {
226 const char *basename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
236 packageNameFromPath(const char *path
)
238 if((path
== NULL
) || (*path
== 0)) {
239 return U_ICUDATA_NAME
;
242 path
= findBasename(path
);
244 if((path
== NULL
) || (*path
== 0)) {
245 return U_ICUDATA_NAME
;
252 /*----------------------------------------------------------------------*
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 *
260 * Data remains cached for the duration of the process. *
262 *----------------------------------------------------------------------*/
264 typedef struct DataCacheElement
{
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.
276 static void U_CALLCONV
DataCacheElement_deleter(void *pDCEl
) {
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' */
283 static void U_CALLCONV
udata_initHashTable(UErrorCode
&err
) {
284 U_ASSERT(gCommonDataCache
== NULL
);
285 gCommonDataCache
= uhash_open(uhash_hashChars
, uhash_compareChars
, NULL
, &err
);
286 if (U_FAILURE(err
)) {
289 U_ASSERT(gCommonDataCache
!= NULL
);
290 uhash_setValueDeleter(gCommonDataCache
, DataCacheElement_deleter
);
291 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
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.
298 static UHashtable
*udata_getHashTable(UErrorCode
&err
) {
299 umtx_initOnce(gCommonDataCacheInitOnce
, &udata_initHashTable
, err
);
300 return gCommonDataCache
;
305 static UDataMemory
*udata_findCachedData(const char *path
, UErrorCode
&err
)
308 UDataMemory
*retVal
= NULL
;
309 DataCacheElement
*el
;
310 const char *baseName
;
312 htable
= udata_getHashTable(err
);
313 if (U_FAILURE(err
)) {
317 baseName
= findBasename(path
); /* Cache remembers only the base name, not the full path. */
319 el
= (DataCacheElement
*)uhash_get(htable
, baseName
);
325 fprintf(stderr
, "Cache: [%s] -> %p\n", baseName
, retVal
);
331 static UDataMemory
*udata_cacheDataItem(const char *path
, UDataMemory
*item
, UErrorCode
*pErr
) {
332 DataCacheElement
*newElement
;
333 const char *baseName
;
336 DataCacheElement
*oldValue
= NULL
;
337 UErrorCode subErr
= U_ZERO_ERROR
;
339 htable
= udata_getHashTable(*pErr
);
340 if (U_FAILURE(*pErr
)) {
344 /* Create a new DataCacheElement - the thingy we store in the hash table -
345 * and copy the supplied path and UDataMemoryItems into it.
347 newElement
= (DataCacheElement
*)uprv_malloc(sizeof(DataCacheElement
));
348 if (newElement
== NULL
) {
349 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
352 newElement
->item
= UDataMemory_createNewInstance(pErr
);
353 if (U_FAILURE(*pErr
)) {
354 uprv_free(newElement
);
357 UDatamemory_assign(newElement
->item
, item
);
359 baseName
= findBasename(path
);
360 nameLen
= (int32_t)uprv_strlen(baseName
);
361 newElement
->name
= (char *)uprv_malloc(nameLen
+1);
362 if (newElement
->name
== NULL
) {
363 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
364 uprv_free(newElement
->item
);
365 uprv_free(newElement
);
368 uprv_strcpy(newElement
->name
, baseName
);
370 /* Stick the new DataCacheElement into the hash table.
373 oldValue
= (DataCacheElement
*)uhash_get(htable
, path
);
374 if (oldValue
!= NULL
) {
375 subErr
= U_USING_DEFAULT_WARNING
;
380 newElement
->name
, /* Key */
381 newElement
, /* Value */
387 fprintf(stderr
, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement
->name
,
388 newElement
->item
, u_errorName(subErr
), newElement
->item
->vFuncs
);
391 if (subErr
== U_USING_DEFAULT_WARNING
|| U_FAILURE(subErr
)) {
392 *pErr
= subErr
; /* copy sub err unto fillin ONLY if something happens. */
393 uprv_free(newElement
->name
);
394 uprv_free(newElement
->item
);
395 uprv_free(newElement
);
396 return oldValue
? oldValue
->item
: NULL
;
399 return newElement
->item
;
402 /*----------------------------------------------------------------------*==============
404 * Path management. Could be shared with other tools/etc if need be *
407 *----------------------------------------------------------------------*/
411 class UDataPathIterator
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
);
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) */
425 uint32_t basenameLen
; /* length of basename */
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. */
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. */
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
439 * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leave directories such as /icudt28l
440 * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat
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.
445 UDataPathIterator::UDataPathIterator(const char *inPath
, const char *pkg
,
446 const char *item
, const char *inSuffix
, UBool doCheckLastFour
,
447 UErrorCode
*pErrorCode
)
450 fprintf(stderr
, "SUFFIX1=%s PATH=%s\n", inSuffix
, inPath
);
454 path
= u_getDataDirectory();
461 packageStub
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(pkg
, *pErrorCode
);
463 fprintf(stderr
, "STUB=%s [%d]\n", packageStub
.data(), packageStub
.length());
468 basename
= findBasename(item
);
469 basenameLen
= (int32_t)uprv_strlen(basename
);
472 if(basename
== item
) {
475 itemPath
.append(item
, (int32_t)(basename
-item
), *pErrorCode
);
476 nextPath
= itemPath
.data();
479 fprintf(stderr
, "SUFFIX=%s [%p]\n", inSuffix
, inSuffix
);
483 if(inSuffix
!= NULL
) {
489 checkLastFour
= doCheckLastFour
;
491 /* pathBuffer will hold the output path strings returned by this iterator */
494 fprintf(stderr
, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
502 checkLastFour
?"TRUE":"false");
507 * Get the next path on the list.
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.
513 const char *UDataPathIterator::next(UErrorCode
*pErrorCode
)
515 if(U_FAILURE(*pErrorCode
)) {
519 const char *currentPath
= NULL
;
521 const char *pathBasename
;
525 if( nextPath
== NULL
) {
528 currentPath
= nextPath
;
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
);
534 /* fix up next for next time */
535 nextPath
= uprv_strchr(currentPath
, U_PATH_SEP_CHAR
);
536 if(nextPath
== NULL
) {
537 /* segment: entire path */
538 pathLen
= (int32_t)uprv_strlen(currentPath
);
540 /* segment: until next segment */
541 pathLen
= (int32_t)(nextPath
- currentPath
);
552 fprintf(stderr
, "rest of path (IDD) = %s\n", currentPath
);
553 fprintf(stderr
, " ");
556 for(qqq
=0;qqq
<pathLen
;qqq
++)
558 fprintf(stderr
, " ");
561 fprintf(stderr
, "^\n");
564 pathBuffer
.clear().append(currentPath
, pathLen
, *pErrorCode
);
566 /* check for .dat files */
567 pathBasename
= findBasename(pathBuffer
.data());
569 if(checkLastFour
== TRUE
&&
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 */
576 fprintf(stderr
, "Have %s file on the path: %s\n", suffix
, pathBuffer
.data());
581 { /* regular dir path */
582 if(pathBuffer
[pathLen
-1] != U_FILE_SEP_CHAR
) {
584 uprv_strncmp(pathBuffer
.data()+(pathLen
-4), ".dat", 4) == 0)
587 fprintf(stderr
, "skipping non-directory .dat file %s\n", pathBuffer
.data());
592 /* Check if it is a directory with the same name as our package */
593 if(!packageStub
.isEmpty() &&
594 (pathLen
> packageStub
.length()) &&
595 !uprv_strcmp(pathBuffer
.data() + pathLen
- packageStub
.length(), packageStub
.data())) {
597 fprintf(stderr
, "Found stub %s (will add package %s of len %d)\n", packageStub
.data(), basename
, basenameLen
);
599 pathBuffer
.truncate(pathLen
- packageStub
.length());
601 pathBuffer
.append(U_FILE_SEP_CHAR
, *pErrorCode
);
605 pathBuffer
.append(packageStub
.data()+1, packageStub
.length()-1, *pErrorCode
);
607 if(*suffix
) /* tack on suffix */
609 pathBuffer
.append(suffix
, *pErrorCode
);
614 fprintf(stderr
, " --> %s\n", pathBuffer
.data());
617 return pathBuffer
.data();
621 /* fell way off the end */
627 /* ==================================================================================*/
630 /*----------------------------------------------------------------------*
632 * Add a static reference to the common data library *
633 * Unless overridden by an explicit udata_setCommonData, this will be *
636 *----------------------------------------------------------------------*/
637 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
638 extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT
;
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.
647 extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK;
648 extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK;
651 /*----------------------------------------------------------------------*
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. *
657 * If the requested data is already open and cached *
658 * just return the cached UDataMem object. *
660 *----------------------------------------------------------------------*/
662 openCommonData(const char *path
, /* Path from OpenChoice? */
663 int32_t commonDataIndex
, /* ICU Data (index >= 0) if path == NULL */
664 UErrorCode
*pErrorCode
)
667 const char *pathBuffer
;
668 const char *inBasename
;
670 if (U_FAILURE(*pErrorCode
)) {
674 UDataMemory_init(&tData
);
676 /* ??????? TODO revisit this */
677 if (commonDataIndex
>= 0) {
678 /* "mini-cache" for common ICU data */
679 if(commonDataIndex
>= UPRV_LENGTHOF(gCommonICUDataArray
)) {
684 if(gCommonICUDataArray
[commonDataIndex
] != NULL
) {
685 return gCommonICUDataArray
[commonDataIndex
];
687 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
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. */
698 /* Add the linked-in data to the list. */
700 * This is where we would check and call weakly linked partial-data-library
704 if (uprv_getICUData_collation) {
705 setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
707 if (uprv_getICUData_conversion) {
708 setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
711 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
712 setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT
, FALSE
, pErrorCode
);
715 return gCommonICUDataArray
[commonDataIndex
];
721 /* request is NOT for ICU Data. */
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
);
727 fprintf(stderr
, "inBasename = %s\n", inBasename
);
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.) */
734 fprintf(stderr
, "ocd: no basename in %s, bailing.\n", path
);
736 if (U_SUCCESS(*pErrorCode
)) {
737 *pErrorCode
=U_FILE_ACCESS_ERROR
;
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. */
745 UDataMemory
*dataToReturn
= udata_findCachedData(inBasename
, *pErrorCode
);
746 if (dataToReturn
!= NULL
|| U_FAILURE(*pErrorCode
)) {
750 /* Requested item is not in the cache.
751 * Hunt it down, trying all the path locations
754 UDataPathIterator
iter(u_getDataDirectory(), inBasename
, path
, ".dat", TRUE
, pErrorCode
);
756 while((UDataMemory_isLoaded(&tData
)==FALSE
) && (pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
759 fprintf(stderr
, "ocd: trying path %s - ", pathBuffer
);
761 uprv_mapFile(&tData
, pathBuffer
);
763 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&tData
)?"LOADED":"not loaded");
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
);
778 if (U_FAILURE(*pErrorCode
)) {
781 if (!UDataMemory_isLoaded(&tData
)) {
783 *pErrorCode
=U_FILE_ACCESS_ERROR
;
787 /* we have mapped a file, check its header */
788 udata_checkCommonData(&tData
, pErrorCode
);
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
);
798 /*----------------------------------------------------------------------*
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. *
805 * return true if new data is loaded, false otherwise.*
807 *----------------------------------------------------------------------*/
808 static UBool
extendICUData(UErrorCode
*pErr
)
811 UDataMemory copyPData
;
812 UBool didUpdate
= FALSE
;
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.
824 #if MAP_IMPLEMENTATION==MAP_STDIO
825 static UMutex extendICUDataMutex
= U_MUTEX_INITIALIZER
;
826 umtx_lock(&extendICUDataMutex
);
828 if(!umtx_loadAcquire(gHaveTriedToLoadCommonData
)) {
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 */
835 /* How about if there is no pData, eh... */
837 UDataMemory_init(©PData
);
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. */
847 didUpdate
= /* no longer using this result */
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 */
853 umtx_storeRelease(gHaveTriedToLoadCommonData
, 1);
856 didUpdate
= findCommonICUDataByName(U_ICUDATA_NAME
, *pErr
); /* Return 'true' when a racing writes out the extended */
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. */
861 #if MAP_IMPLEMENTATION==MAP_STDIO
862 umtx_unlock(&extendICUDataMutex
);
864 return didUpdate
; /* Return true if ICUData pointer was updated. */
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. */
870 /*----------------------------------------------------------------------*
872 * udata_setCommonData *
874 *----------------------------------------------------------------------*/
875 U_CAPI
void U_EXPORT2
876 udata_setCommonData(const void *data
, UErrorCode
*pErrorCode
) {
877 UDataMemory dataMemory
;
879 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
884 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
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;}
894 /* we have good data */
895 /* Set it up as the ICU Common Data. */
896 setCommonICUData(&dataMemory
, TRUE
, pErrorCode
);
899 /*---------------------------------------------------------------------------
903 *---------------------------------------------------------------------------- */
904 U_CAPI
void U_EXPORT2
905 udata_setAppData(const char *path
, const void *data
, UErrorCode
*err
)
909 if(err
==NULL
|| U_FAILURE(*err
)) {
913 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
917 UDataMemory_init(&udm
);
918 UDataMemory_setData(&udm
, data
);
919 udata_checkCommonData(&udm
, err
);
920 udata_cacheDataItem(path
, &udm
, err
);
923 /*----------------------------------------------------------------------------*
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. *
933 *----------------------------------------------------------------------------*/
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 */
948 UDataMemory
*rDataMem
= NULL
; /* the new UDataMemory, to be returned. */
950 if (U_FAILURE(*fatalErr
)) {
954 if(pHeader
->dataHeader
.magic1
==0xda &&
955 pHeader
->dataHeader
.magic2
==0x27 &&
956 (isAcceptable
==NULL
|| isAcceptable(context
, type
, name
, &pHeader
->info
))
958 rDataMem
=UDataMemory_createNewInstance(fatalErr
);
959 if (U_FAILURE(*fatalErr
)) {
962 rDataMem
->pHeader
= pHeader
;
964 /* the data is not acceptable, look further */
965 /* If we eventually find something good, this errorcode will be */
967 *nonFatalErr
=U_INVALID_FORMAT_ERROR
;
973 * @return 0 if not loaded, 1 if loaded or err
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
)
983 const char *pathBuffer
;
984 UDataMemory dataMemory
;
985 UDataMemory
*pEntryData
;
987 /* look in ind. files: package\nam.typ ========================= */
988 /* init path iterator for individual files */
989 UDataPathIterator
iter(dataPath
, pkgName
, path
, tocEntryPathSuffix
, FALSE
, pErrorCode
);
991 while((pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
994 fprintf(stderr
, "UDATA: trying individual file %s\n", pathBuffer
);
996 if(uprv_mapFile(&dataMemory
, pathBuffer
))
998 pEntryData
= checkDataItem(dataMemory
.pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
999 if (pEntryData
!= NULL
) {
1001 * Hand off ownership of the backing memory to the user's UDataMemory.
1003 pEntryData
->mapAddr
= dataMemory
.mapAddr
;
1004 pEntryData
->map
= dataMemory
.map
;
1007 fprintf(stderr
, "** Mapped file: %s\n", pathBuffer
);
1012 /* the data is not acceptable, or some error occured. Either way, unmap the memory */
1013 udata_close(&dataMemory
);
1015 /* If we had a nasty error, bail out completely. */
1016 if (U_FAILURE(*pErrorCode
)) {
1020 /* Otherwise remember that we found data but didn't like it for some reason */
1021 *subErrorCode
=U_INVALID_FORMAT_ERROR
;
1024 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&dataMemory
)?"LOADED":"not loaded");
1031 * @return 0 if not loaded, 1 if loaded or err
1033 static UDataMemory
*doLoadFromCommonData(UBool isICUData
, const char * /*pkgName*/,
1034 const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName
,
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
)
1041 UDataMemory
*pEntryData
;
1042 const DataHeader
*pHeader
;
1043 UDataMemory
*pCommonData
;
1044 int32_t commonDataIndex
;
1045 UBool checkedExtendedICUData
= FALSE
;
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.
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.
1055 for (commonDataIndex
= isICUData
? 0 : -1;;) {
1056 pCommonData
=openCommonData(path
, commonDataIndex
, subErrorCode
); /** search for pkg **/
1058 if(U_SUCCESS(*subErrorCode
) && pCommonData
!=NULL
) {
1061 /* look up the data piece in the common data */
1062 pHeader
=pCommonData
->vFuncs
->Lookup(pCommonData
, tocEntryName
, &length
, subErrorCode
);
1064 fprintf(stderr
, "%s: pHeader=%p - %s\n", tocEntryName
, pHeader
, u_errorName(*subErrorCode
));
1068 pEntryData
= checkDataItem(pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
1070 fprintf(stderr
, "pEntryData=%p\n", pEntryData
);
1072 if (U_FAILURE(*pErrorCode
)) {
1075 if (pEntryData
!= NULL
) {
1076 pEntryData
->length
= length
;
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. */
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 */
1098 * Identify the Time Zone resources that are subject to special override data loading.
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));
1109 * A note on the ownership of Mapped Memory
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.
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.
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.)
1125 * For common data passed in from the user via udata_setAppData() or
1126 * udata_setCommonData(), ownership remains with the user.
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.
1135 /*----------------------------------------------------------------------------*
1137 * main data loading functions *
1139 *----------------------------------------------------------------------------*/
1140 static UDataMemory
*
1141 doOpenChoice(const char *path
, const char *type
, const char *name
,
1142 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1143 UErrorCode
*pErrorCode
)
1145 UDataMemory
*retVal
= NULL
;
1147 const char *dataPath
;
1149 int32_t tocEntrySuffixIndex
;
1150 const char *tocEntryPathSuffix
;
1151 UErrorCode subErrorCode
=U_ZERO_ERROR
;
1152 const char *treeChar
;
1154 UBool isICUData
= FALSE
;
1157 /* Is this path ICU data? */
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-" */
1163 uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
))) {
1167 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */
1168 /* remap from alternate path char to the main one */
1169 CharString altSepPath
;
1171 if(uprv_strchr(path
,U_FILE_ALT_SEP_CHAR
) != NULL
) {
1172 altSepPath
.append(path
, *pErrorCode
);
1174 while ((p
= uprv_strchr(altSepPath
.data(), U_FILE_ALT_SEP_CHAR
)) != NULL
) {
1175 *p
= U_FILE_SEP_CHAR
;
1177 #if defined (UDATA_DEBUG)
1178 fprintf(stderr
, "Changed path from [%s] to [%s]\n", path
, altSepPath
.s
);
1180 path
= altSepPath
.data();
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' */
1189 CharString treeName
;
1191 /* ======= Set up strings */
1193 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
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 */
1202 pkgName
.append(pkg
+1, *pErrorCode
);
1204 pkgName
.append(path
, *pErrorCode
);
1207 treeChar
= uprv_strchr(path
, U_TREE_SEPARATOR
);
1209 treeName
.append(treeChar
+1, *pErrorCode
); /* following '-' */
1211 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1213 pkgName
.append(path
, (int32_t)(treeChar
-path
), *pErrorCode
);
1214 if (first
== NULL
) {
1216 This user data has no path, but there is a tree name.
1217 Look up the correct path from the data cache later.
1219 path
= pkgName
.data();
1224 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1226 pkgName
.append(path
, *pErrorCode
);
1233 fprintf(stderr
, " P=%s T=%s\n", pkgName
.data(), treeName
.data());
1236 /* setting up the entry name and file name
1237 * Make up a full name by appending the type to the supplied
1238 * name, assuming that a type was supplied.
1241 /* prepend the package */
1242 tocEntryName
.append(pkgName
, *pErrorCode
);
1243 tocEntryPath
.append(pkgName
, *pErrorCode
);
1244 tocEntrySuffixIndex
= tocEntryName
.length();
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
);
1251 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1252 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1253 if(type
!=NULL
&& *type
!=0) {
1254 tocEntryName
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1255 tocEntryPath
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1257 tocEntryPathSuffix
= tocEntryPath
.data()+tocEntrySuffixIndex
; /* suffix starts here */
1260 fprintf(stderr
, " tocEntryName = %s\n", tocEntryName
.data());
1261 fprintf(stderr
, " tocEntryPath = %s\n", tocEntryName
.data());
1264 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
1266 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1269 // Windows UWP expects only a single data file.
1270 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1273 /************************ Begin loop looking for ind. files ***************/
1275 fprintf(stderr
, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path
));
1278 /* End of dealing with a null basename */
1279 dataPath
= u_getDataDirectory();
1281 /**** Time zone individual files override */
1282 if (isICUData
&& isTimeZoneFile(name
, type
)) {
1283 const char *tzFilesDir
= u_getTimeZoneFilesDirectory(pErrorCode
);
1284 if (tzFilesDir
[0] != 0) {
1286 fprintf(stderr
, "Trying Time Zone Files directory = %s\n", tzFilesDir
);
1288 #ifdef UDATA_TZFILES_DEBUG
1289 fprintf(stderr
, "# dOC U_TIMEZONE_FILES_DIR: %s\n", U_TIMEZONE_FILES_DIR
);
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
);
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
);
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
);
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
);
1318 if(U_SUCCESS(tzpkgErrorCode
) && retVal
!= NULL
) {
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
);
1327 retVal
= doLoadFromIndividualFiles(/* pkgName.data() */ "", tzFilesDir
, tocEntryPathSuffix
,
1328 /* path */ "", type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1329 #ifdef UDATA_TZFILES_DEBUG
1330 fprintf(stderr
, "# dOC tz files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1332 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1338 /**** COMMON PACKAGE - only if packages are first. */
1339 if(gDataFileAccess
== UDATA_PACKAGES_FIRST
) {
1341 fprintf(stderr
, "Trying packages (UDATA_PACKAGES_FIRST)\n");
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
);
1350 retVal
= doLoadFromCommonData(isICUData
,
1351 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1352 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
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
);
1358 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1363 /**** INDIVIDUAL FILES */
1364 if((gDataFileAccess
==UDATA_PACKAGES_FIRST
) ||
1365 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1367 fprintf(stderr
, "Trying individual files\n");
1369 /* Check to make sure that there is a dataPath to iterate over */
1370 if ((dataPath
&& *dataPath
) || !isICUData
) {
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
);
1377 retVal
= doLoadFromIndividualFiles(pkgName
.data(), dataPath
, tocEntryPathSuffix
,
1378 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
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
);
1384 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1390 /**** COMMON PACKAGE */
1391 if((gDataFileAccess
==UDATA_ONLY_PACKAGES
) ||
1392 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1394 fprintf(stderr
, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
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
);
1402 retVal
= doLoadFromCommonData(isICUData
,
1403 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1404 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
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
);
1410 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
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
) {
1420 fprintf(stderr
, "Trying common data (UDATA_NO_FILES)\n");
1422 retVal
= doLoadFromCommonData(isICUData
,
1423 pkgName
.data(), "", tocEntryPathSuffix
, tocEntryName
.data(),
1424 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1425 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1430 /* data not found */
1431 if(U_SUCCESS(*pErrorCode
)) {
1432 if(U_SUCCESS(subErrorCode
)) {
1433 /* file not found */
1434 *pErrorCode
=U_FILE_ACCESS_ERROR
;
1436 /* entry point not found or rejected */
1437 *pErrorCode
=subErrorCode
;
1445 /* API ---------------------------------------------------------------------- */
1447 U_CAPI UDataMemory
* U_EXPORT2
1448 udata_open(const char *path
, const char *type
, const char *name
,
1449 UErrorCode
*pErrorCode
) {
1451 fprintf(stderr
, "udata_open(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1455 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1457 } else if(name
==NULL
|| *name
==0) {
1458 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1461 return doOpenChoice(path
, type
, name
, NULL
, NULL
, pErrorCode
);
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
) {
1472 fprintf(stderr
, "udata_openChoice(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1475 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1477 } else if(name
==NULL
|| *name
==0 || isAcceptable
==NULL
) {
1478 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1481 return doOpenChoice(path
, type
, name
, isAcceptable
, context
, pErrorCode
);
1487 U_CAPI
void U_EXPORT2
1488 udata_getInfo(UDataMemory
*pData
, UDataInfo
*pInfo
) {
1490 if(pData
!=NULL
&& pData
->pHeader
!=NULL
) {
1491 const UDataInfo
*info
=&pData
->pHeader
->info
;
1492 uint16_t dataInfoSize
=udata_getInfoSize(info
);
1493 if(pInfo
->size
>dataInfoSize
) {
1494 pInfo
->size
=dataInfoSize
;
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));
1509 U_CAPI
void U_EXPORT2
udata_setFileAccess(UDataFileAccess access
, UErrorCode
* /*status*/)
1511 // Note: this function is documented as not thread safe.
1512 gDataFileAccess
= access
;