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
);
212 setCommonICUDataPointer(const void *pData
, UBool
/*warn*/, UErrorCode
*pErrorCode
) {
214 UDataMemory_init(&tData
);
215 UDataMemory_setData(&tData
, pData
);
216 udata_checkCommonData(&tData
, pErrorCode
);
217 return setCommonICUData(&tData
, FALSE
, pErrorCode
);
221 findBasename(const char *path
) {
222 const char *basename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
232 packageNameFromPath(const char *path
)
234 if((path
== NULL
) || (*path
== 0)) {
235 return U_ICUDATA_NAME
;
238 path
= findBasename(path
);
240 if((path
== NULL
) || (*path
== 0)) {
241 return U_ICUDATA_NAME
;
248 /*----------------------------------------------------------------------*
250 * Cache for common data *
251 * Functions for looking up or adding entries to a cache of *
252 * data that has been previously opened. Avoids a potentially *
253 * expensive operation of re-opening the data for subsequent *
256 * Data remains cached for the duration of the process. *
258 *----------------------------------------------------------------------*/
260 typedef struct DataCacheElement
{
268 * Deleter function for DataCacheElements.
269 * udata cleanup function closes the hash table; hash table in turn calls back to
270 * here for each entry.
272 static void U_CALLCONV
DataCacheElement_deleter(void *pDCEl
) {
273 DataCacheElement
*p
= (DataCacheElement
*)pDCEl
;
274 udata_close(p
->item
); /* unmaps storage */
275 uprv_free(p
->name
); /* delete the hash key string. */
276 uprv_free(pDCEl
); /* delete 'this' */
279 static void U_CALLCONV
udata_initHashTable(UErrorCode
&err
) {
280 U_ASSERT(gCommonDataCache
== NULL
);
281 gCommonDataCache
= uhash_open(uhash_hashChars
, uhash_compareChars
, NULL
, &err
);
282 if (U_FAILURE(err
)) {
285 U_ASSERT(gCommonDataCache
!= NULL
);
286 uhash_setValueDeleter(gCommonDataCache
, DataCacheElement_deleter
);
287 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
290 /* udata_getCacheHashTable()
291 * Get the hash table used to store the data cache entries.
292 * Lazy create it if it doesn't yet exist.
294 static UHashtable
*udata_getHashTable(UErrorCode
&err
) {
295 umtx_initOnce(gCommonDataCacheInitOnce
, &udata_initHashTable
, err
);
296 return gCommonDataCache
;
301 static UDataMemory
*udata_findCachedData(const char *path
, UErrorCode
&err
)
304 UDataMemory
*retVal
= NULL
;
305 DataCacheElement
*el
;
306 const char *baseName
;
308 htable
= udata_getHashTable(err
);
309 if (U_FAILURE(err
)) {
313 baseName
= findBasename(path
); /* Cache remembers only the base name, not the full path. */
315 el
= (DataCacheElement
*)uhash_get(htable
, baseName
);
321 fprintf(stderr
, "Cache: [%s] -> %p\n", baseName
, retVal
);
327 static UDataMemory
*udata_cacheDataItem(const char *path
, UDataMemory
*item
, UErrorCode
*pErr
) {
328 DataCacheElement
*newElement
;
329 const char *baseName
;
332 DataCacheElement
*oldValue
= NULL
;
333 UErrorCode subErr
= U_ZERO_ERROR
;
335 htable
= udata_getHashTable(*pErr
);
336 if (U_FAILURE(*pErr
)) {
340 /* Create a new DataCacheElement - the thingy we store in the hash table -
341 * and copy the supplied path and UDataMemoryItems into it.
343 newElement
= (DataCacheElement
*)uprv_malloc(sizeof(DataCacheElement
));
344 if (newElement
== NULL
) {
345 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
348 newElement
->item
= UDataMemory_createNewInstance(pErr
);
349 if (U_FAILURE(*pErr
)) {
350 uprv_free(newElement
);
353 UDatamemory_assign(newElement
->item
, item
);
355 baseName
= findBasename(path
);
356 nameLen
= (int32_t)uprv_strlen(baseName
);
357 newElement
->name
= (char *)uprv_malloc(nameLen
+1);
358 if (newElement
->name
== NULL
) {
359 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
360 uprv_free(newElement
->item
);
361 uprv_free(newElement
);
364 uprv_strcpy(newElement
->name
, baseName
);
366 /* Stick the new DataCacheElement into the hash table.
369 oldValue
= (DataCacheElement
*)uhash_get(htable
, path
);
370 if (oldValue
!= NULL
) {
371 subErr
= U_USING_DEFAULT_WARNING
;
376 newElement
->name
, /* Key */
377 newElement
, /* Value */
383 fprintf(stderr
, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement
->name
,
384 newElement
->item
, u_errorName(subErr
), newElement
->item
->vFuncs
);
387 if (subErr
== U_USING_DEFAULT_WARNING
|| U_FAILURE(subErr
)) {
388 *pErr
= subErr
; /* copy sub err unto fillin ONLY if something happens. */
389 uprv_free(newElement
->name
);
390 uprv_free(newElement
->item
);
391 uprv_free(newElement
);
392 return oldValue
? oldValue
->item
: NULL
;
395 return newElement
->item
;
398 /*----------------------------------------------------------------------*==============
400 * Path management. Could be shared with other tools/etc if need be *
403 *----------------------------------------------------------------------*/
407 class UDataPathIterator
410 UDataPathIterator(const char *path
, const char *pkg
,
411 const char *item
, const char *suffix
, UBool doCheckLastFour
,
412 UErrorCode
*pErrorCode
);
413 const char *next(UErrorCode
*pErrorCode
);
416 const char *path
; /* working path (u_icudata_Dir) */
417 const char *nextPath
; /* path following this one */
418 const char *basename
; /* item's basename (icudt22e_mt.res)*/
419 const char *suffix
; /* item suffix (can be null) */
421 uint32_t basenameLen
; /* length of basename */
423 CharString itemPath
; /* path passed in with item name */
424 CharString pathBuffer
; /* output path for this it'ion */
425 CharString packageStub
; /* example: "/icudt28b". Will ignore that leaf in set paths. */
427 UBool checkLastFour
; /* if TRUE then allow paths such as '/foo/myapp.dat'
428 * to match, checks last 4 chars of suffix with
429 * last 4 of path, then previous chars. */
433 * @param iter The iterator to be initialized. Its current state does not matter.
434 * @param path The full pathname to be iterated over. If NULL, defaults to U_ICUDATA_NAME
435 * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leave directories such as /icudt28l
436 * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat
437 * @param suffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly.
438 * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2.
439 * '/blarg/stuff.dat' would also be found.
441 UDataPathIterator::UDataPathIterator(const char *inPath
, const char *pkg
,
442 const char *item
, const char *inSuffix
, UBool doCheckLastFour
,
443 UErrorCode
*pErrorCode
)
446 fprintf(stderr
, "SUFFIX1=%s PATH=%s\n", inSuffix
, inPath
);
450 path
= u_getDataDirectory();
457 packageStub
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(pkg
, *pErrorCode
);
459 fprintf(stderr
, "STUB=%s [%d]\n", packageStub
.data(), packageStub
.length());
464 basename
= findBasename(item
);
465 basenameLen
= (int32_t)uprv_strlen(basename
);
468 if(basename
== item
) {
471 itemPath
.append(item
, (int32_t)(basename
-item
), *pErrorCode
);
472 nextPath
= itemPath
.data();
475 fprintf(stderr
, "SUFFIX=%s [%p]\n", inSuffix
, inSuffix
);
479 if(inSuffix
!= NULL
) {
485 checkLastFour
= doCheckLastFour
;
487 /* pathBuffer will hold the output path strings returned by this iterator */
490 fprintf(stderr
, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
498 checkLastFour
?"TRUE":"false");
503 * Get the next path on the list.
505 * @param iter The Iter to be used
506 * @param len If set, pointer to the length of the returned path, for convenience.
507 * @return Pointer to the next path segment, or NULL if there are no more.
509 const char *UDataPathIterator::next(UErrorCode
*pErrorCode
)
511 if(U_FAILURE(*pErrorCode
)) {
515 const char *currentPath
= NULL
;
517 const char *pathBasename
;
521 if( nextPath
== NULL
) {
524 currentPath
= nextPath
;
526 if(nextPath
== itemPath
.data()) { /* we were processing item's path. */
527 nextPath
= path
; /* start with regular path next tm. */
528 pathLen
= (int32_t)uprv_strlen(currentPath
);
530 /* fix up next for next time */
531 nextPath
= uprv_strchr(currentPath
, U_PATH_SEP_CHAR
);
532 if(nextPath
== NULL
) {
533 /* segment: entire path */
534 pathLen
= (int32_t)uprv_strlen(currentPath
);
536 /* segment: until next segment */
537 pathLen
= (int32_t)(nextPath
- currentPath
);
548 fprintf(stderr
, "rest of path (IDD) = %s\n", currentPath
);
549 fprintf(stderr
, " ");
552 for(qqq
=0;qqq
<pathLen
;qqq
++)
554 fprintf(stderr
, " ");
557 fprintf(stderr
, "^\n");
560 pathBuffer
.clear().append(currentPath
, pathLen
, *pErrorCode
);
562 /* check for .dat files */
563 pathBasename
= findBasename(pathBuffer
.data());
565 if(checkLastFour
== TRUE
&&
567 uprv_strncmp(pathBuffer
.data() +(pathLen
-4), suffix
, 4)==0 && /* suffix matches */
568 uprv_strncmp(findBasename(pathBuffer
.data()), basename
, basenameLen
)==0 && /* base matches */
569 uprv_strlen(pathBasename
)==(basenameLen
+4)) { /* base+suffix = full len */
572 fprintf(stderr
, "Have %s file on the path: %s\n", suffix
, pathBuffer
.data());
577 { /* regular dir path */
578 if(pathBuffer
[pathLen
-1] != U_FILE_SEP_CHAR
) {
580 uprv_strncmp(pathBuffer
.data()+(pathLen
-4), ".dat", 4) == 0)
583 fprintf(stderr
, "skipping non-directory .dat file %s\n", pathBuffer
.data());
588 /* Check if it is a directory with the same name as our package */
589 if(!packageStub
.isEmpty() &&
590 (pathLen
> packageStub
.length()) &&
591 !uprv_strcmp(pathBuffer
.data() + pathLen
- packageStub
.length(), packageStub
.data())) {
593 fprintf(stderr
, "Found stub %s (will add package %s of len %d)\n", packageStub
.data(), basename
, basenameLen
);
595 pathBuffer
.truncate(pathLen
- packageStub
.length());
597 pathBuffer
.append(U_FILE_SEP_CHAR
, *pErrorCode
);
601 pathBuffer
.append(packageStub
.data()+1, packageStub
.length()-1, *pErrorCode
);
603 if(*suffix
) /* tack on suffix */
605 pathBuffer
.append(suffix
, *pErrorCode
);
610 fprintf(stderr
, " --> %s\n", pathBuffer
.data());
613 return pathBuffer
.data();
617 /* fell way off the end */
623 /* ==================================================================================*/
626 /*----------------------------------------------------------------------*
628 * Add a static reference to the common data library *
629 * Unless overridden by an explicit udata_setCommonData, this will be *
632 *----------------------------------------------------------------------*/
633 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
634 extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT
;
638 * This would be a good place for weak-linkage declarations of
639 * partial-data-library access functions where each returns a pointer
640 * to its data package, if it is linked in.
643 extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK;
644 extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK;
647 /*----------------------------------------------------------------------*
649 * openCommonData Attempt to open a common format (.dat) file *
650 * Map it into memory (if it's not there already) *
651 * and return a UDataMemory object for it. *
653 * If the requested data is already open and cached *
654 * just return the cached UDataMem object. *
656 *----------------------------------------------------------------------*/
658 openCommonData(const char *path
, /* Path from OpenChoice? */
659 int32_t commonDataIndex
, /* ICU Data (index >= 0) if path == NULL */
660 UErrorCode
*pErrorCode
)
663 const char *pathBuffer
;
664 const char *inBasename
;
666 if (U_FAILURE(*pErrorCode
)) {
670 UDataMemory_init(&tData
);
672 /* ??????? TODO revisit this */
673 if (commonDataIndex
>= 0) {
674 /* "mini-cache" for common ICU data */
675 if(commonDataIndex
>= UPRV_LENGTHOF(gCommonICUDataArray
)) {
680 if(gCommonICUDataArray
[commonDataIndex
] != NULL
) {
681 return gCommonICUDataArray
[commonDataIndex
];
683 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
685 for(i
= 0; i
< commonDataIndex
; ++i
) {
686 if(gCommonICUDataArray
[i
]->pHeader
== &U_ICUDATA_ENTRY_POINT
) {
687 /* The linked-in data is already in the list. */
694 /* Add the linked-in data to the list. */
696 * This is where we would check and call weakly linked partial-data-library
700 if (uprv_getICUData_collation) {
701 setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
703 if (uprv_getICUData_conversion) {
704 setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
707 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
708 setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT
, FALSE
, pErrorCode
);
711 return gCommonICUDataArray
[commonDataIndex
];
717 /* request is NOT for ICU Data. */
719 /* Find the base name portion of the supplied path. */
720 /* inBasename will be left pointing somewhere within the original path string. */
721 inBasename
= findBasename(path
);
723 fprintf(stderr
, "inBasename = %s\n", inBasename
);
727 /* no basename. This will happen if the original path was a directory name, */
728 /* like "a/b/c/". (Fallback to separate files will still work.) */
730 fprintf(stderr
, "ocd: no basename in %s, bailing.\n", path
);
732 if (U_SUCCESS(*pErrorCode
)) {
733 *pErrorCode
=U_FILE_ACCESS_ERROR
;
738 /* Is the requested common data file already open and cached? */
739 /* Note that the cache is keyed by the base name only. The rest of the path, */
740 /* if any, is not considered. */
741 UDataMemory
*dataToReturn
= udata_findCachedData(inBasename
, *pErrorCode
);
742 if (dataToReturn
!= NULL
|| U_FAILURE(*pErrorCode
)) {
746 /* Requested item is not in the cache.
747 * Hunt it down, trying all the path locations
750 UDataPathIterator
iter(u_getDataDirectory(), inBasename
, path
, ".dat", TRUE
, pErrorCode
);
752 while((UDataMemory_isLoaded(&tData
)==FALSE
) && (pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
755 fprintf(stderr
, "ocd: trying path %s - ", pathBuffer
);
757 uprv_mapFile(&tData
, pathBuffer
);
759 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&tData
)?"LOADED":"not loaded");
763 #if defined(OS390_STUBDATA) && defined(OS390BATCH)
764 if (!UDataMemory_isLoaded(&tData
)) {
765 char ourPathBuffer
[1024];
766 /* One more chance, for extendCommonData() */
767 uprv_strncpy(ourPathBuffer
, path
, 1019);
768 ourPathBuffer
[1019]=0;
769 uprv_strcat(ourPathBuffer
, ".dat");
770 uprv_mapFile(&tData
, ourPathBuffer
);
774 if (U_FAILURE(*pErrorCode
)) {
777 if (!UDataMemory_isLoaded(&tData
)) {
779 *pErrorCode
=U_FILE_ACCESS_ERROR
;
783 /* we have mapped a file, check its header */
784 udata_checkCommonData(&tData
, pErrorCode
);
787 /* Cache the UDataMemory struct for this .dat file,
788 * so we won't need to hunt it down and map it again next time
789 * something is needed from it. */
790 return udata_cacheDataItem(inBasename
, &tData
, pErrorCode
);
794 /*----------------------------------------------------------------------*
796 * extendICUData If the full set of ICU data was not loaded at *
797 * program startup, load it now. This function will *
798 * be called when the lookup of an ICU data item in *
799 * the common ICU data fails. *
801 * return true if new data is loaded, false otherwise.*
803 *----------------------------------------------------------------------*/
804 static UBool
extendICUData(UErrorCode
*pErr
)
807 UDataMemory copyPData
;
808 UBool didUpdate
= FALSE
;
811 * There is a chance for a race condition here.
812 * Normally, ICU data is loaded from a DLL or via mmap() and
813 * setCommonICUData() will detect if the same address is set twice.
814 * If ICU is built with data loading via fread() then the address will
815 * be different each time the common data is loaded and we may add
816 * multiple copies of the data.
817 * In this case, use a mutex to prevent the race.
818 * Use a specific mutex to avoid nested locks of the global mutex.
820 #if MAP_IMPLEMENTATION==MAP_STDIO
821 static UMutex extendICUDataMutex
= U_MUTEX_INITIALIZER
;
822 umtx_lock(&extendICUDataMutex
);
824 if(!umtx_loadAcquire(gHaveTriedToLoadCommonData
)) {
825 /* See if we can explicitly open a .dat file for the ICUData. */
826 pData
= openCommonData(
827 U_ICUDATA_NAME
, /* "icudt20l" , for example. */
828 -1, /* Pretend we're not opening ICUData */
831 /* How about if there is no pData, eh... */
833 UDataMemory_init(©PData
);
835 UDatamemory_assign(©PData
, pData
);
836 copyPData
.map
= 0; /* The mapping for this data is owned by the hash table */
837 copyPData
.mapAddr
= 0; /* which will unmap it when ICU is shut down. */
838 /* CommonICUData is also unmapped when ICU is shut down.*/
839 /* To avoid unmapping the data twice, zero out the map */
840 /* fields in the UDataMemory that we're assigning */
841 /* to CommonICUData. */
843 didUpdate
= /* no longer using this result */
844 setCommonICUData(©PData
,/* The new common data. */
845 FALSE
, /* No warnings if write didn't happen */
846 pErr
); /* setCommonICUData honors errors; NOP if error set */
849 umtx_storeRelease(gHaveTriedToLoadCommonData
, 1);
852 didUpdate
= findCommonICUDataByName(U_ICUDATA_NAME
, *pErr
); /* Return 'true' when a racing writes out the extended */
853 /* data after another thread has failed to see it (in openCommonData), so */
854 /* extended data can be examined. */
855 /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
857 #if MAP_IMPLEMENTATION==MAP_STDIO
858 umtx_unlock(&extendICUDataMutex
);
860 return didUpdate
; /* Return true if ICUData pointer was updated. */
861 /* (Could potentialy have been done by another thread racing */
862 /* us through here, but that's fine, we still return true */
863 /* so that current thread will also examine extended data. */
866 /*----------------------------------------------------------------------*
868 * udata_setCommonData *
870 *----------------------------------------------------------------------*/
871 U_CAPI
void U_EXPORT2
872 udata_setCommonData(const void *data
, UErrorCode
*pErrorCode
) {
873 UDataMemory dataMemory
;
875 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
880 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
884 /* set the data pointer and test for validity */
885 UDataMemory_init(&dataMemory
);
886 UDataMemory_setData(&dataMemory
, data
);
887 udata_checkCommonData(&dataMemory
, pErrorCode
);
888 if (U_FAILURE(*pErrorCode
)) {return;}
890 /* we have good data */
891 /* Set it up as the ICU Common Data. */
892 setCommonICUData(&dataMemory
, TRUE
, pErrorCode
);
895 /*---------------------------------------------------------------------------
899 *---------------------------------------------------------------------------- */
900 U_CAPI
void U_EXPORT2
901 udata_setAppData(const char *path
, const void *data
, UErrorCode
*err
)
905 if(err
==NULL
|| U_FAILURE(*err
)) {
909 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
913 UDataMemory_init(&udm
);
914 UDataMemory_setData(&udm
, data
);
915 udata_checkCommonData(&udm
, err
);
916 udata_cacheDataItem(path
, &udm
, err
);
919 /*----------------------------------------------------------------------------*
921 * checkDataItem Given a freshly located/loaded data item, either *
922 * an entry in a common file or a separately loaded file, *
923 * sanity check its header, and see if the data is *
924 * acceptable to the app. *
925 * If the data is good, create and return a UDataMemory *
926 * object that can be returned to the application. *
927 * Return NULL on any sort of failure. *
929 *----------------------------------------------------------------------------*/
933 const DataHeader
*pHeader
, /* The data item to be checked. */
934 UDataMemoryIsAcceptable
*isAcceptable
, /* App's call-back function */
935 void *context
, /* pass-thru param for above. */
936 const char *type
, /* pass-thru param for above. */
937 const char *name
, /* pass-thru param for above. */
938 UErrorCode
*nonFatalErr
, /* Error code if this data was not acceptable */
939 /* but openChoice should continue with */
940 /* trying to get data from fallback path. */
941 UErrorCode
*fatalErr
/* Bad error, caller should return immediately */
944 UDataMemory
*rDataMem
= NULL
; /* the new UDataMemory, to be returned. */
946 if (U_FAILURE(*fatalErr
)) {
950 if(pHeader
->dataHeader
.magic1
==0xda &&
951 pHeader
->dataHeader
.magic2
==0x27 &&
952 (isAcceptable
==NULL
|| isAcceptable(context
, type
, name
, &pHeader
->info
))
954 rDataMem
=UDataMemory_createNewInstance(fatalErr
);
955 if (U_FAILURE(*fatalErr
)) {
958 rDataMem
->pHeader
= pHeader
;
960 /* the data is not acceptable, look further */
961 /* If we eventually find something good, this errorcode will be */
963 *nonFatalErr
=U_INVALID_FORMAT_ERROR
;
969 * @return 0 if not loaded, 1 if loaded or err
971 static UDataMemory
*doLoadFromIndividualFiles(const char *pkgName
,
972 const char *dataPath
, const char *tocEntryPathSuffix
,
973 /* following arguments are the same as doOpenChoice itself */
974 const char *path
, const char *type
, const char *name
,
975 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
976 UErrorCode
*subErrorCode
,
977 UErrorCode
*pErrorCode
)
979 const char *pathBuffer
;
980 UDataMemory dataMemory
;
981 UDataMemory
*pEntryData
;
983 /* look in ind. files: package\nam.typ ========================= */
984 /* init path iterator for individual files */
985 UDataPathIterator
iter(dataPath
, pkgName
, path
, tocEntryPathSuffix
, FALSE
, pErrorCode
);
987 while((pathBuffer
= iter
.next(pErrorCode
)))
990 fprintf(stderr
, "UDATA: trying individual file %s\n", pathBuffer
);
992 if(uprv_mapFile(&dataMemory
, pathBuffer
))
994 pEntryData
= checkDataItem(dataMemory
.pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
995 if (pEntryData
!= NULL
) {
997 * Hand off ownership of the backing memory to the user's UDataMemory.
999 pEntryData
->mapAddr
= dataMemory
.mapAddr
;
1000 pEntryData
->map
= dataMemory
.map
;
1003 fprintf(stderr
, "** Mapped file: %s\n", pathBuffer
);
1008 /* the data is not acceptable, or some error occured. Either way, unmap the memory */
1009 udata_close(&dataMemory
);
1011 /* If we had a nasty error, bail out completely. */
1012 if (U_FAILURE(*pErrorCode
)) {
1016 /* Otherwise remember that we found data but didn't like it for some reason */
1017 *subErrorCode
=U_INVALID_FORMAT_ERROR
;
1020 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&dataMemory
)?"LOADED":"not loaded");
1027 * @return 0 if not loaded, 1 if loaded or err
1029 static UDataMemory
*doLoadFromCommonData(UBool isICUData
, const char * /*pkgName*/,
1030 const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName
,
1031 /* following arguments are the same as doOpenChoice itself */
1032 const char *path
, const char *type
, const char *name
,
1033 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1034 UErrorCode
*subErrorCode
,
1035 UErrorCode
*pErrorCode
)
1037 UDataMemory
*pEntryData
;
1038 const DataHeader
*pHeader
;
1039 UDataMemory
*pCommonData
;
1040 int32_t commonDataIndex
;
1041 UBool checkedExtendedICUData
= FALSE
;
1042 /* try to get common data. The loop is for platforms such as the 390 that do
1043 * not initially load the full set of ICU data. If the lookup of an ICU data item
1044 * fails, the full (but slower to load) set is loaded, the and the loop repeats,
1045 * trying the lookup again. Once the full set of ICU data is loaded, the loop wont
1046 * repeat because the full set will be checked the first time through.
1048 * The loop also handles the fallback to a .dat file if the application linked
1049 * to the stub data library rather than a real library.
1051 for (commonDataIndex
= isICUData
? 0 : -1;;) {
1052 pCommonData
=openCommonData(path
, commonDataIndex
, subErrorCode
); /** search for pkg **/
1054 if(U_SUCCESS(*subErrorCode
) && pCommonData
!=NULL
) {
1057 /* look up the data piece in the common data */
1058 pHeader
=pCommonData
->vFuncs
->Lookup(pCommonData
, tocEntryName
, &length
, subErrorCode
);
1060 fprintf(stderr
, "%s: pHeader=%p - %s\n", tocEntryName
, pHeader
, u_errorName(*subErrorCode
));
1064 pEntryData
= checkDataItem(pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
1066 fprintf(stderr
, "pEntryData=%p\n", pEntryData
);
1068 if (U_FAILURE(*pErrorCode
)) {
1071 if (pEntryData
!= NULL
) {
1072 pEntryData
->length
= length
;
1077 /* Data wasn't found. If we were looking for an ICUData item and there is
1078 * more data available, load it and try again,
1079 * otherwise break out of this loop. */
1082 } else if (pCommonData
!= NULL
) {
1083 ++commonDataIndex
; /* try the next data package */
1084 } else if ((!checkedExtendedICUData
) && extendICUData(subErrorCode
)) {
1085 checkedExtendedICUData
= TRUE
;
1086 /* try this data package slot again: it changed from NULL to non-NULL */
1094 * Identify the Time Zone resources that are subject to special override data loading.
1096 static UBool
isTimeZoneFile(const char *name
, const char *type
) {
1097 return ((uprv_strcmp(type
, "res") == 0) &&
1098 (uprv_strcmp(name
, "zoneinfo64") == 0 ||
1099 uprv_strcmp(name
, "timezoneTypes") == 0 ||
1100 uprv_strcmp(name
, "windowsZones") == 0 ||
1101 uprv_strcmp(name
, "metaZones") == 0));
1105 * A note on the ownership of Mapped Memory
1107 * For common format files, ownership resides with the UDataMemory object
1108 * that lives in the cache of opened common data. These UDataMemorys are private
1109 * to the udata implementation, and are never seen directly by users.
1111 * The UDataMemory objects returned to users will have the address of some desired
1112 * data within the mapped region, but they wont have the mapping info itself, and thus
1113 * won't cause anything to be removed from memory when they are closed.
1115 * For individual data files, the UDataMemory returned to the user holds the
1116 * information necessary to unmap the data on close. If the user independently
1117 * opens the same data file twice, two completely independent mappings will be made.
1118 * (There is no cache of opened data items from individual files, only a cache of
1119 * opened Common Data files, that is, files containing a collection of data items.)
1121 * For common data passed in from the user via udata_setAppData() or
1122 * udata_setCommonData(), ownership remains with the user.
1124 * UDataMemory objects themselves, as opposed to the memory they describe,
1125 * can be anywhere - heap, stack/local or global.
1126 * They have a flag to indicate when they're heap allocated and thus
1127 * must be deleted when closed.
1131 /*----------------------------------------------------------------------------*
1133 * main data loading functions *
1135 *----------------------------------------------------------------------------*/
1136 static UDataMemory
*
1137 doOpenChoice(const char *path
, const char *type
, const char *name
,
1138 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1139 UErrorCode
*pErrorCode
)
1141 UDataMemory
*retVal
= NULL
;
1143 const char *dataPath
;
1145 int32_t tocEntrySuffixIndex
;
1146 const char *tocEntryPathSuffix
;
1147 UErrorCode subErrorCode
=U_ZERO_ERROR
;
1148 const char *treeChar
;
1150 UBool isICUData
= FALSE
;
1153 /* Is this path ICU data? */
1155 !strcmp(path
, U_ICUDATA_ALIAS
) || /* "ICUDATA" */
1156 !uprv_strncmp(path
, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
, /* "icudt26e-" */
1157 uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
)) ||
1158 !uprv_strncmp(path
, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
, /* "ICUDATA-" */
1159 uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
))) {
1163 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */
1164 /* remap from alternate path char to the main one */
1165 CharString altSepPath
;
1167 if(uprv_strchr(path
,U_FILE_ALT_SEP_CHAR
) != NULL
) {
1168 altSepPath
.append(path
, *pErrorCode
);
1170 while((p
=uprv_strchr(altSepPath
.data(), U_FILE_ALT_SEP_CHAR
))) {
1171 *p
= U_FILE_SEP_CHAR
;
1173 #if defined (UDATA_DEBUG)
1174 fprintf(stderr
, "Changed path from [%s] to [%s]\n", path
, altSepPath
.s
);
1176 path
= altSepPath
.data();
1181 CharString tocEntryName
; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */
1182 CharString tocEntryPath
; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */
1185 CharString treeName
;
1187 /* ======= Set up strings */
1189 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1193 pkg
= uprv_strrchr(path
, U_FILE_SEP_CHAR
);
1194 first
= uprv_strchr(path
, U_FILE_SEP_CHAR
);
1195 if(uprv_pathIsAbsolute(path
) || (pkg
!= first
)) { /* more than one slash in the path- not a tree name */
1196 /* see if this is an /absolute/path/to/package path */
1198 pkgName
.append(pkg
+1, *pErrorCode
);
1200 pkgName
.append(path
, *pErrorCode
);
1203 treeChar
= uprv_strchr(path
, U_TREE_SEPARATOR
);
1205 treeName
.append(treeChar
+1, *pErrorCode
); /* following '-' */
1207 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1209 pkgName
.append(path
, (int32_t)(treeChar
-path
), *pErrorCode
);
1210 if (first
== NULL
) {
1212 This user data has no path, but there is a tree name.
1213 Look up the correct path from the data cache later.
1215 path
= pkgName
.data();
1220 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1222 pkgName
.append(path
, *pErrorCode
);
1229 fprintf(stderr
, " P=%s T=%s\n", pkgName
.data(), treeName
.data());
1232 /* setting up the entry name and file name
1233 * Make up a full name by appending the type to the supplied
1234 * name, assuming that a type was supplied.
1237 /* prepend the package */
1238 tocEntryName
.append(pkgName
, *pErrorCode
);
1239 tocEntryPath
.append(pkgName
, *pErrorCode
);
1240 tocEntrySuffixIndex
= tocEntryName
.length();
1242 if(!treeName
.isEmpty()) {
1243 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1244 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1247 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1248 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1249 if(type
!=NULL
&& *type
!=0) {
1250 tocEntryName
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1251 tocEntryPath
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1253 tocEntryPathSuffix
= tocEntryPath
.data()+tocEntrySuffixIndex
; /* suffix starts here */
1256 fprintf(stderr
, " tocEntryName = %s\n", tocEntryName
.data());
1257 fprintf(stderr
, " tocEntryPath = %s\n", tocEntryName
.data());
1260 #if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
1262 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1265 // Windows UWP expects only a single data file.
1266 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1269 /************************ Begin loop looking for ind. files ***************/
1271 fprintf(stderr
, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path
));
1274 /* End of dealing with a null basename */
1275 dataPath
= u_getDataDirectory();
1277 /**** Time zone individual files override */
1278 if (isICUData
&& isTimeZoneFile(name
, type
)) {
1279 const char *tzFilesDir
= u_getTimeZoneFilesDirectory(pErrorCode
);
1280 if (tzFilesDir
[0] != 0) {
1282 fprintf(stderr
, "Trying Time Zone Files directory = %s\n", tzFilesDir
);
1284 #ifdef UDATA_TZFILES_DEBUG
1285 fprintf(stderr
, "# dOC U_TIMEZONE_FILES_DIR: %s\n", U_TIMEZONE_FILES_DIR
);
1288 #if defined(U_TIMEZONE_PACKAGE)
1289 // make tztocEntryName, like tocEntryName but with our package name
1290 UErrorCode tzpkgErrorCode
= U_ZERO_ERROR
;
1291 CharString tztocPkgPath
;
1292 tztocPkgPath
.append(tzFilesDir
, tzpkgErrorCode
);
1293 tztocPkgPath
.append(U_FILE_SEP_CHAR
, tzpkgErrorCode
).append(U_TIMEZONE_PACKAGE
, tzpkgErrorCode
);
1294 CharString tztocEntryName
;
1295 tztocEntryName
.append(U_TIMEZONE_PACKAGE
, tzpkgErrorCode
);
1296 if(!treeName
.isEmpty()) {
1297 tztocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, tzpkgErrorCode
).append(treeName
, tzpkgErrorCode
);
1299 tztocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, tzpkgErrorCode
).append(name
, tzpkgErrorCode
);
1300 if(type
!=NULL
&& *type
!=0) {
1301 tztocEntryName
.append(".", tzpkgErrorCode
).append(type
, tzpkgErrorCode
);
1303 #ifdef UDATA_TZFILES_DEBUG
1304 fprintf(stderr
, "# dOC tz pkg, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: %s, tztocPkgPath.data(): %s, tztocEntryName.data(): %s, name: %s\n",
1305 U_TIMEZONE_PACKAGE
, tztocPkgPath
.data(), tztocEntryName
.data(), name
);
1307 retVal
= doLoadFromCommonData(FALSE
, "" /*ignored*/, "" /*ignored*/, "" /*ignored*/,
1308 tztocEntryName
.data(), // tocEntryName, like icutz44/zoneinfo64.res
1309 tztocPkgPath
.data(), // path = path to pkg, like /usr/share/icu/icutz44l
1310 type
, name
, isAcceptable
, context
, &subErrorCode
, &tzpkgErrorCode
);
1311 #ifdef UDATA_TZFILES_DEBUG
1312 fprintf(stderr
, "# dOC tz pkg, doLoadFromCommonData end; status %d, retVal %p\n", tzpkgErrorCode
, retVal
);
1314 if(U_SUCCESS(tzpkgErrorCode
) && retVal
!= NULL
) {
1317 #endif /* defined(U_TIMEZONE_PACKAGE) */
1318 // The following assumes any timezone resources in tzFilesDir are in individual .res files
1319 #ifdef UDATA_TZFILES_DEBUG
1320 fprintf(stderr
, "# dOC tz files, doLoadFromIndividualFiles start; tzFilesDir: %s, tocEntryPathSuffix: %s, name: %s\n",
1321 tzFilesDir
, tocEntryPathSuffix
, name
);
1323 retVal
= doLoadFromIndividualFiles(/* pkgName.data() */ "", tzFilesDir
, tocEntryPathSuffix
,
1324 /* path */ "", type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1325 #ifdef UDATA_TZFILES_DEBUG
1326 fprintf(stderr
, "# dOC tz files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1328 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1334 /**** COMMON PACKAGE - only if packages are first. */
1335 if(gDataFileAccess
== UDATA_PACKAGES_FIRST
) {
1337 fprintf(stderr
, "Trying packages (UDATA_PACKAGES_FIRST)\n");
1340 #ifdef UDATA_TZFILES_DEBUG
1341 if (isTimeZoneFile(name
, type
)) {
1342 fprintf(stderr
, "# dOC std common 1, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: path: %s, tocEntryName.data(): %s, name: %s\n",
1343 path
, tocEntryName
.data(), name
);
1346 retVal
= doLoadFromCommonData(isICUData
,
1347 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1348 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1349 #ifdef UDATA_TZFILES_DEBUG
1350 if (isTimeZoneFile(name
, type
)) {
1351 fprintf(stderr
, "# dOC std common 1, doLoadFromCommonData end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1354 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1359 /**** INDIVIDUAL FILES */
1360 if((gDataFileAccess
==UDATA_PACKAGES_FIRST
) ||
1361 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1363 fprintf(stderr
, "Trying individual files\n");
1365 /* Check to make sure that there is a dataPath to iterate over */
1366 if ((dataPath
&& *dataPath
) || !isICUData
) {
1367 #ifdef UDATA_TZFILES_DEBUG
1368 if (isTimeZoneFile(name
, type
)) {
1369 fprintf(stderr
, "# dOC std indiv files, doLoadFromIndividualFiles start; dataPath: %s, tocEntryPathSuffix: %s, name: %s\n",
1370 dataPath
, tocEntryPathSuffix
, name
);
1373 retVal
= doLoadFromIndividualFiles(pkgName
.data(), dataPath
, tocEntryPathSuffix
,
1374 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1375 #ifdef UDATA_TZFILES_DEBUG
1376 if (isTimeZoneFile(name
, type
)) {
1377 fprintf(stderr
, "# dOC std indiv files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1380 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1386 /**** COMMON PACKAGE */
1387 if((gDataFileAccess
==UDATA_ONLY_PACKAGES
) ||
1388 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1390 fprintf(stderr
, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
1392 #ifdef UDATA_TZFILES_DEBUG
1393 if (isTimeZoneFile(name
, type
)) {
1394 fprintf(stderr
, "# dOC std common 2, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: path: %s, tocEntryName.data(): %s, name: %s\n",
1395 path
, tocEntryName
.data(), name
);
1398 retVal
= doLoadFromCommonData(isICUData
,
1399 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1400 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1401 #ifdef UDATA_TZFILES_DEBUG
1402 if (isTimeZoneFile(name
, type
)) {
1403 fprintf(stderr
, "# dOC std common 2, doLoadFromCommonData end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1406 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1411 /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to
1412 try a DLL (static or setCommonData/etc) load.
1413 If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */
1414 if(gDataFileAccess
==UDATA_NO_FILES
) {
1416 fprintf(stderr
, "Trying common data (UDATA_NO_FILES)\n");
1418 retVal
= doLoadFromCommonData(isICUData
,
1419 pkgName
.data(), "", tocEntryPathSuffix
, tocEntryName
.data(),
1420 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1421 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1426 /* data not found */
1427 if(U_SUCCESS(*pErrorCode
)) {
1428 if(U_SUCCESS(subErrorCode
)) {
1429 /* file not found */
1430 *pErrorCode
=U_FILE_ACCESS_ERROR
;
1432 /* entry point not found or rejected */
1433 *pErrorCode
=subErrorCode
;
1441 /* API ---------------------------------------------------------------------- */
1443 U_CAPI UDataMemory
* U_EXPORT2
1444 udata_open(const char *path
, const char *type
, const char *name
,
1445 UErrorCode
*pErrorCode
) {
1447 fprintf(stderr
, "udata_open(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1451 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1453 } else if(name
==NULL
|| *name
==0) {
1454 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1457 return doOpenChoice(path
, type
, name
, NULL
, NULL
, pErrorCode
);
1463 U_CAPI UDataMemory
* U_EXPORT2
1464 udata_openChoice(const char *path
, const char *type
, const char *name
,
1465 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1466 UErrorCode
*pErrorCode
) {
1468 fprintf(stderr
, "udata_openChoice(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1471 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1473 } else if(name
==NULL
|| *name
==0 || isAcceptable
==NULL
) {
1474 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1477 return doOpenChoice(path
, type
, name
, isAcceptable
, context
, pErrorCode
);
1483 U_CAPI
void U_EXPORT2
1484 udata_getInfo(UDataMemory
*pData
, UDataInfo
*pInfo
) {
1486 if(pData
!=NULL
&& pData
->pHeader
!=NULL
) {
1487 const UDataInfo
*info
=&pData
->pHeader
->info
;
1488 uint16_t dataInfoSize
=udata_getInfoSize(info
);
1489 if(pInfo
->size
>dataInfoSize
) {
1490 pInfo
->size
=dataInfoSize
;
1492 uprv_memcpy((uint16_t *)pInfo
+1, (const uint16_t *)info
+1, pInfo
->size
-2);
1493 if(info
->isBigEndian
!=U_IS_BIG_ENDIAN
) {
1494 /* opposite endianness */
1495 uint16_t x
=info
->reservedWord
;
1496 pInfo
->reservedWord
=(uint16_t)((x
<<8)|(x
>>8));
1505 U_CAPI
void U_EXPORT2
udata_setFileAccess(UDataFileAccess access
, UErrorCode
* /*status*/)
1507 // Note: this function is documented as not thread safe.
1508 gDataFileAccess
= access
;