X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/2ca993e82fb37b597a3c73ecd1586a139a6579c5..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/common/udata.cpp diff --git a/icuSources/common/udata.cpp b/icuSources/common/udata.cpp index 2d92fbf2..8647865b 100644 --- a/icuSources/common/udata.cpp +++ b/icuSources/common/udata.cpp @@ -1,3 +1,5 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** * @@ -6,7 +8,7 @@ * ****************************************************************************** * file name: udata.cpp -* encoding: US-ASCII +* encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * @@ -79,7 +81,7 @@ U_NAMESPACE_USE /* * Forward declarations */ -static UDataMemory *udata_findCachedData(const char *path); +static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err); /*********************************************************************** * @@ -110,8 +112,12 @@ static u_atomic_int32_t gHaveTriedToLoadCommonData = ATOMIC_INT32_T_INITIALIZER( static UHashtable *gCommonDataCache = NULL; /* Global hash table of opened ICU data files. */ static icu::UInitOnce gCommonDataCacheInitOnce = U_INITONCE_INITIALIZER; +#if U_PLATFORM_HAS_WINUWP_API == 0 static UDataFileAccess gDataFileAccess = UDATA_DEFAULT_ACCESS; // Access not synchronized. // Modifying is documented as thread-unsafe. +#else +static UDataFileAccess gDataFileAccess = UDATA_NO_FILES; // Windows UWP looks in one spot explicitly +#endif static UBool U_CALLCONV udata_cleanup(void) @@ -134,13 +140,13 @@ udata_cleanup(void) } static UBool U_CALLCONV -findCommonICUDataByName(const char *inBasename) +findCommonICUDataByName(const char *inBasename, UErrorCode &err) { UBool found = FALSE; int32_t i; - UDataMemory *pData = udata_findCachedData(inBasename); - if (pData == NULL) + UDataMemory *pData = udata_findCachedData(inBasename, err); + if (U_FAILURE(err) || pData == NULL) return FALSE; { @@ -202,6 +208,8 @@ setCommonICUData(UDataMemory *pData, /* The new common data. Belongs to ca return didUpdate; } +#if U_PLATFORM_HAS_WINUWP_API == 0 + static UBool setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) { UDataMemory tData; @@ -211,6 +219,8 @@ setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCod return setCommonICUData(&tData, FALSE, pErrorCode); } +#endif + static const char * findBasename(const char *path) { const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR); @@ -270,40 +280,41 @@ static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) { uprv_free(pDCEl); /* delete 'this' */ } -static void udata_initHashTable() { - UErrorCode err = U_ZERO_ERROR; +static void U_CALLCONV udata_initHashTable(UErrorCode &err) { U_ASSERT(gCommonDataCache == NULL); gCommonDataCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err); if (U_FAILURE(err)) { - // TODO: handle errors better. - gCommonDataCache = NULL; - } - if (gCommonDataCache != NULL) { - uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter); - ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); + return; } + U_ASSERT(gCommonDataCache != NULL); + uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter); + ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); } /* udata_getCacheHashTable() * Get the hash table used to store the data cache entries. * Lazy create it if it doesn't yet exist. */ -static UHashtable *udata_getHashTable() { - umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable); +static UHashtable *udata_getHashTable(UErrorCode &err) { + umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable, err); return gCommonDataCache; } -static UDataMemory *udata_findCachedData(const char *path) +static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err) { UHashtable *htable; UDataMemory *retVal = NULL; DataCacheElement *el; const char *baseName; + htable = udata_getHashTable(err); + if (U_FAILURE(err)) { + return NULL; + } + baseName = findBasename(path); /* Cache remembers only the base name, not the full path. */ - htable = udata_getHashTable(); umtx_lock(NULL); el = (DataCacheElement *)uhash_get(htable, baseName); umtx_unlock(NULL); @@ -325,6 +336,7 @@ static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UEr DataCacheElement *oldValue = NULL; UErrorCode subErr = U_ZERO_ERROR; + htable = udata_getHashTable(*pErr); if (U_FAILURE(*pErr)) { return NULL; } @@ -357,7 +369,6 @@ static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UEr /* Stick the new DataCacheElement into the hash table. */ - htable = udata_getHashTable(); umtx_lock(NULL); oldValue = (DataCacheElement *)uhash_get(htable, path); if (oldValue != NULL) { @@ -395,9 +406,6 @@ static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UEr * * *----------------------------------------------------------------------*/ -#define U_DATA_PATHITER_BUFSIZ 128 /* Size of local buffer for paths */ - /* Overflow causes malloc of larger buf */ - U_NAMESPACE_BEGIN class UDataPathIterator @@ -621,12 +629,14 @@ U_NAMESPACE_END /*----------------------------------------------------------------------* * * - * Add a static reference to the common data library * + * Add a static reference to the common data library * * Unless overridden by an explicit udata_setCommonData, this will be * * our common data. * * * *----------------------------------------------------------------------*/ +#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT; +#endif /* * This would be a good place for weak-linkage declarations of @@ -674,6 +684,7 @@ openCommonData(const char *path, /* Path from OpenChoice? */ if(gCommonICUDataArray[commonDataIndex] != NULL) { return gCommonICUDataArray[commonDataIndex]; } +#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time int32_t i; for(i = 0; i < commonDataIndex; ++i) { if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) { @@ -681,6 +692,7 @@ openCommonData(const char *path, /* Path from OpenChoice? */ return NULL; } } +#endif } /* Add the linked-in data to the list. */ @@ -696,11 +708,13 @@ openCommonData(const char *path, /* Path from OpenChoice? */ setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode); } */ +#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, FALSE, pErrorCode); { Mutex lock; return gCommonICUDataArray[commonDataIndex]; } +#endif } @@ -719,18 +733,18 @@ openCommonData(const char *path, /* Path from OpenChoice? */ #ifdef UDATA_DEBUG fprintf(stderr, "ocd: no basename in %s, bailing.\n", path); #endif - *pErrorCode=U_FILE_ACCESS_ERROR; + if (U_SUCCESS(*pErrorCode)) { + *pErrorCode=U_FILE_ACCESS_ERROR; + } return NULL; } /* Is the requested common data file already open and cached? */ /* Note that the cache is keyed by the base name only. The rest of the path, */ /* if any, is not considered. */ - { - UDataMemory *dataToReturn = udata_findCachedData(inBasename); - if (dataToReturn != NULL) { - return dataToReturn; - } + UDataMemory *dataToReturn = udata_findCachedData(inBasename, *pErrorCode); + if (dataToReturn != NULL || U_FAILURE(*pErrorCode)) { + return dataToReturn; } /* Requested item is not in the cache. @@ -761,6 +775,9 @@ openCommonData(const char *path, /* Path from OpenChoice? */ } #endif + if (U_FAILURE(*pErrorCode)) { + return NULL; + } if (!UDataMemory_isLoaded(&tData)) { /* no common data */ *pErrorCode=U_FILE_ACCESS_ERROR; @@ -836,7 +853,7 @@ static UBool extendICUData(UErrorCode *pErr) umtx_storeRelease(gHaveTriedToLoadCommonData, 1); } - didUpdate = findCommonICUDataByName(U_ICUDATA_NAME); /* Return 'true' when a racing writes out the extended */ + didUpdate = findCommonICUDataByName(U_ICUDATA_NAME, *pErr); /* Return 'true' when a racing writes out the extended */ /* data after another thread has failed to see it (in openCommonData), so */ /* extended data can be examined. */ /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */ @@ -971,7 +988,7 @@ static UDataMemory *doLoadFromIndividualFiles(const char *pkgName, /* init path iterator for individual files */ UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, FALSE, pErrorCode); - while((pathBuffer = iter.next(pErrorCode))) + while((pathBuffer = iter.next(pErrorCode)) != NULL) { #ifdef UDATA_DEBUG fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer); @@ -1154,7 +1171,7 @@ doOpenChoice(const char *path, const char *type, const char *name, if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR) != NULL) { altSepPath.append(path, *pErrorCode); char *p; - while((p=uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR))) { + while ((p = uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR)) != NULL) { *p = U_FILE_SEP_CHAR; } #if defined (UDATA_DEBUG) @@ -1244,9 +1261,14 @@ doOpenChoice(const char *path, const char *type, const char *name, fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.data()); #endif +#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time if(path == NULL) { path = COMMON_DATA_NAME; /* "icudt26e" */ } +#else + // Windows UWP expects only a single data file. + path = COMMON_DATA_NAME; /* "icudt26e" */ +#endif /************************ Begin loop looking for ind. files ***************/ #ifdef UDATA_DEBUG @@ -1257,7 +1279,7 @@ doOpenChoice(const char *path, const char *type, const char *name, dataPath = u_getDataDirectory(); /**** Time zone individual files override */ - if (isTimeZoneFile(name, type) && isICUData) { + if (isICUData && isTimeZoneFile(name, type)) { const char *tzFilesDir = u_getTimeZoneFilesDirectory(pErrorCode); if (tzFilesDir[0] != 0) { #ifdef UDATA_DEBUG