static u_atomic_int32_t gHaveTriedToLoadCommonData {0}; // See extendICUData().
static UHashtable *gCommonDataCache = nullptr; /* Global hash table of opened ICU data files. */ static icu::UInitOnce gCommonDataCacheInitOnce {};
#if !defined(ICU_DATA_DIR_WINDOWS) static UDataFileAccess gDataFileAccess = UDATA_DEFAULT_ACCESS; // Access not synchronized. // Modifying is documented as thread-unsafe. #else // If we are using the Windows data directory, then look in one spot only. static UDataFileAccess gDataFileAccess = UDATA_NO_FILES; #endif
if (gCommonDataCache) { /* Delete the cache of user data mappings. */
uhash_close(gCommonDataCache); /* Table owns the contents, and will delete them. */
gCommonDataCache = nullptr; /* Cleanup is not thread safe. */
}
gCommonDataCacheInitOnce.reset();
for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray) && gCommonICUDataArray[i] != nullptr; ++i) {
udata_close(gCommonICUDataArray[i]);
gCommonICUDataArray[i] = nullptr;
}
gHaveTriedToLoadCommonData = 0;
{
Mutex lock; for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray); ++i) { if ((gCommonICUDataArray[i] != nullptr) && (gCommonICUDataArray[i]->pHeader == pData->pHeader)) { /* The data pointer is already in the array. */
found = true; break;
}
}
} return found;
}
/* *setCommonICUData.SetaUDataMemorytobetheglobalICUData
*/ static UBool
setCommonICUData(UDataMemory *pData, /* The new common data. Belongs to caller, we copy it. */
UBool warn, /* If true, set USING_DEFAULT warning if ICUData was */ /* changed by another thread before we got to it. */
UErrorCode *pErr)
{
UDataMemory *newCommonData = UDataMemory_createNewInstance(pErr);
int32_t i;
UBool didUpdate = false; if (U_FAILURE(*pErr)) { returnfalse;
}
/* For the assignment, other threads must cleanly see either the old */ /* or the new, not some partially initialized new. The old can not be */ /* deleted - someone may still have a pointer to it lying around in */ /* their locals. */
UDatamemory_assign(newCommonData, pData);
umtx_lock(nullptr); for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray); ++i) { if (gCommonICUDataArray[i] == nullptr) {
gCommonICUDataArray[i] = newCommonData;
didUpdate = true; break;
} elseif (gCommonICUDataArray[i]->pHeader == pData->pHeader) { /* The same data pointer is already in the array. */ break;
}
}
umtx_unlock(nullptr);
if (i == UPRV_LENGTHOF(gCommonICUDataArray) && warn) {
*pErr = U_USING_DEFAULT_WARNING;
} if (didUpdate) {
ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
} else {
uprv_free(newCommonData);
} return didUpdate;
}
htable = udata_getHashTable(*pErr); if (U_FAILURE(*pErr)) { return nullptr;
}
/* Create a new DataCacheElement - the thingy we store in the hash table - *andcopythesuppliedpathandUDataMemoryItemsintoit.
*/
newElement = static_cast<DataCacheElement*>(uprv_malloc(sizeof(DataCacheElement))); if (newElement == nullptr) {
*pErr = U_MEMORY_ALLOCATION_ERROR; return nullptr;
}
newElement->item = UDataMemory_createNewInstance(pErr); if (U_FAILURE(*pErr)) {
uprv_free(newElement); return nullptr;
}
UDatamemory_assign(newElement->item, item);
private: constchar *path; /* working path (u_icudata_Dir) */ constchar *nextPath; /* path following this one */ constchar *basename; /* item's basename (icudt22e_mt.res)*/
StringPiece suffix; /* item suffix (can be null) */
uint32_t basenameLen; /* length of basename */
CharString itemPath; /* path passed in with item name */
CharString pathBuffer; /* output path for this it'ion */
CharString packageStub; /* example: "/icudt28b". Will ignore that leaf in set paths. */
UBool checkLastFour; /* if true then allow paths such as '/foo/myapp.dat' *tomatch,checkslast4charsofsuffixwith
* last 4 of path, then previous chars. */
};
/* Check if it is a directory with the same name as our package */ if(!packageStub.isEmpty() &&
(pathLen > packageStub.length()) &&
!uprv_strcmp(pathBuffer.data() + pathLen - packageStub.length(), packageStub.data())) { #ifdef UDATA_DEBUG
fprintf(stderr, "Found stub %s (will add package %s of len %d)\n", packageStub.data(), basename, basenameLen); #endif
pathBuffer.truncate(pathLen - packageStub.length());
}
pathBuffer.append(U_FILE_SEP_CHAR, *pErrorCode);
}
if (!suffix.empty()) /* tack on suffix */
{ if (suffix.length() > 4) { // If the suffix is actually an item ("ibm-5348_P100-1997.cnv") and not an extension (".res") // then we need to ensure that the path ends with a separator.
pathBuffer.ensureEndsWithFileSeparator(*pErrorCode);
}
pathBuffer.append(suffix, *pErrorCode);
}
}
/*----------------------------------------------------------------------* ** *Addastaticreferencetothecommondatalibrary* *Unlessoverriddenbyanexplicitudata_setCommonData,thiswillbe* *ourcommondata.* **
*----------------------------------------------------------------------*/ #if !defined(ICU_DATA_DIR_WINDOWS) // When using the Windows system data, we expect only a single data file. extern"C"const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT; #endif
/* ??????? TODO revisit this */ if (commonDataIndex >= 0) { /* "mini-cache" for common ICU data */ if(commonDataIndex >= UPRV_LENGTHOF(gCommonICUDataArray)) { return nullptr;
}
{
Mutex lock; if(gCommonICUDataArray[commonDataIndex] != nullptr) { return gCommonICUDataArray[commonDataIndex];
} #if !defined(ICU_DATA_DIR_WINDOWS) // When using the Windows system data, we expect only a single data file.
int32_t i; for(i = 0; i < commonDataIndex; ++i) { if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) { /* The linked-in data is already in the list. */ return nullptr;
}
} #endif
}
/* Add the linked-in data to the list. */ /* *Thisiswherewewouldcheckandcallweaklylinkedpartial-data-library *accessfunctions.
*/ /* if(uprv_getICUData_collation){ setCommonICUDataPointer(uprv_getICUData_collation(),false,pErrorCode); } if(uprv_getICUData_conversion){ setCommonICUDataPointer(uprv_getICUData_conversion(),false,pErrorCode); }
*/ #if !defined(ICU_DATA_DIR_WINDOWS) // When using the Windows system data, we expect only a single data file.
setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, false, pErrorCode);
{
Mutex lock; return gCommonICUDataArray[commonDataIndex];
} #endif
}
/* request is NOT for ICU Data. */
/* Find the base name portion of the supplied path. */ /* inBasename will be left pointing somewhere within the original path string. */
inBasename = findBasename(path); #ifdef UDATA_DEBUG
fprintf(stderr, "inBasename = %s\n", inBasename); #endif
if(*inBasename==0) { /* no basename. This will happen if the original path was a directory name, */ /* like "a/b/c/". (Fallback to separate files will still work.) */ #ifdef UDATA_DEBUG
fprintf(stderr, "ocd: no basename in %s, bailing.\n", path); #endif if (U_SUCCESS(*pErrorCode)) {
*pErrorCode=U_FILE_ACCESS_ERROR;
} return nullptr;
}
/* 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, *pErrorCode); if (dataToReturn != nullptr || U_FAILURE(*pErrorCode)) { return dataToReturn;
}
/* Requested item is not in the cache. *Huntitdown,tryingallthepathlocations
*/
if (U_FAILURE(*pErrorCode)) { return nullptr;
} if (!UDataMemory_isLoaded(&tData)) { /* no common data */
*pErrorCode=U_FILE_ACCESS_ERROR; return nullptr;
}
/* we have mapped a file, check its header */
udata_checkCommonData(&tData, pErrorCode);
/* Cache the UDataMemory struct for this .dat file, *sowewon'tneedtohuntitdownandmapitagainnexttime
* something is needed from it. */ return udata_cacheDataItem(inBasename, &tData, pErrorCode);
}
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. */
#if MAP_IMPLEMENTATION==MAP_STDIO
umtx_unlock(&extendICUDataMutex); #endif return didUpdate; /* Return true if ICUData pointer was updated. */ /* (Could potentially have been done by another thread racing */ /* us through here, but that's fine, we still return true */ /* so that current thread will also examine extended data. */
}
/* set the data pointer and test for validity */
UDataMemory_init(&dataMemory);
UDataMemory_setData(&dataMemory, data);
udata_checkCommonData(&dataMemory, pErrorCode); if (U_FAILURE(*pErrorCode)) {return;}
/* we have good data */ /* Set it up as the ICU Common Data. */
setCommonICUData(&dataMemory, true, pErrorCode);
}
/*----------------------------------------------------------------------------* ** *checkDataItemGivenafreshlylocated/loadeddataitem,either* *anentryinacommonfileoraseparatelyloadedfile,* *sanitycheckitsheader,andseeifthedatais* *acceptabletotheapp.* *Ifthedataisgood,createandreturnaUDataMemory* *objectthatcanbereturnedtotheapplication.* *Returnnullptronanysortoffailure.* **
*----------------------------------------------------------------------------*/ static UDataMemory *
checkDataItem
( const DataHeader *pHeader, /* The data item to be checked. */
UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */ void *context, /* pass-thru param for above. */ constchar *type, /* pass-thru param for above. */ constchar *name, /* pass-thru param for above. */
UErrorCode *nonFatalErr, /* Error code if this data was not acceptable */ /* but openChoice should continue with */ /* trying to get data from fallback path. */
UErrorCode *fatalErr /* Bad error, caller should return immediately */
)
{
UDataMemory *rDataMem = nullptr; /* the new UDataMemory, to be returned. */
if (U_FAILURE(*fatalErr)) { return nullptr;
}
if(pHeader->dataHeader.magic1==0xda &&
pHeader->dataHeader.magic2==0x27 &&
(isAcceptable==nullptr || isAcceptable(context, type, name, &pHeader->info))
) {
rDataMem=UDataMemory_createNewInstance(fatalErr); if (U_FAILURE(*fatalErr)) { return nullptr;
}
rDataMem->pHeader = pHeader;
} else { /* the data is not acceptable, look further */ /* If we eventually find something good, this errorcode will be */ /* cleared out. */
*nonFatalErr=U_INVALID_FORMAT_ERROR;
} return rDataMem;
}
/** *@return0ifnotloaded,1ifloadedorerr
*/ static UDataMemory *doLoadFromIndividualFiles(constchar *pkgName, constchar *dataPath, constchar *tocEntryPathSuffix, /* following arguments are the same as doOpenChoice itself */ constchar *path, constchar *type, constchar *name,
UDataMemoryIsAcceptable *isAcceptable, void *context,
UErrorCode *subErrorCode,
UErrorCode *pErrorCode)
{ constchar *pathBuffer;
UDataMemory dataMemory;
UDataMemory *pEntryData;
/* the data is not acceptable, or some error occurred. Either way, unmap the memory */
udata_close(&dataMemory);
/* If we had a nasty error, bail out completely. */ if (U_FAILURE(*pErrorCode)) { return nullptr;
}
/* Otherwise remember that we found data but didn't like it for some reason */
*subErrorCode=U_INVALID_FORMAT_ERROR;
} #ifdef UDATA_DEBUG
fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded"); #endif
} return nullptr;
}
/** *@return0ifnotloaded,1ifloadedorerr
*/ static UDataMemory *doLoadFromCommonData(UBool isICUData, constchar * /*pkgName*/, constchar * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName, /* following arguments are the same as doOpenChoice itself */ constchar *path, constchar *type, constchar *name,
UDataMemoryIsAcceptable *isAcceptable, void *context,
UErrorCode *subErrorCode,
UErrorCode *pErrorCode)
{
UDataMemory *pEntryData; const DataHeader *pHeader;
UDataMemory *pCommonData;
int32_t commonDataIndex;
UBool checkedExtendedICUData = false; /* try to get common data. The loop is for platforms such as the 390 that do *notinitiallyloadthefullsetofICUdata.IfthelookupofanICUdataitem *fails,thefull(butslowertoload)setisloaded,theandthelooprepeats, *tryingthelookupagain.OncethefullsetofICUdataisloaded,theloopwont *repeatbecausethefullsetwillbecheckedthefirsttimethrough. * *Theloopalsohandlesthefallbacktoa.datfileiftheapplicationlinked *tothestubdatalibraryratherthanareallibrary.
*/ for (commonDataIndex = isICUData ? 0 : -1;;) {
pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/
/* look up the data piece in the common data */
pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &length, subErrorCode); #ifdef UDATA_DEBUG
fprintf(stderr, "%s: pHeader=%p - %s\n", tocEntryName, (void*) pHeader, u_errorName(*subErrorCode)); #endif
if(pHeader!=nullptr) {
pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); #ifdef UDATA_DEBUG
fprintf(stderr, "pEntryData=%p\n", (void*) pEntryData); #endif if (U_FAILURE(*pErrorCode)) { return nullptr;
} if (pEntryData != nullptr) {
pEntryData->length = length; return pEntryData;
}
}
} // If we failed due to being out-of-memory, then stop early and report the error. if (*subErrorCode == U_MEMORY_ALLOCATION_ERROR) {
*pErrorCode = *subErrorCode; return nullptr;
} /* Data wasn't found. If we were looking for an ICUData item and there is *moredataavailable,loaditandtryagain,
* otherwise break out of this loop. */ if (!isICUData) { return nullptr;
} elseif (pCommonData != nullptr) {
++commonDataIndex; /* try the next data package */
} elseif ((!checkedExtendedICUData) && extendICUData(subErrorCode)) {
checkedExtendedICUData = true; /* try this data package slot again: it changed from nullptr to non-nullptr */
} else { return nullptr;
}
}
}
#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */ /* remap from alternate path char to the main one */
CharString altSepPath; if(path) { if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR) != nullptr) {
altSepPath.append(path, *pErrorCode); char *p; while ((p = uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR)) != nullptr) {
*p = U_FILE_SEP_CHAR;
} #ifdefined (UDATA_DEBUG)
fprintf(stderr, "Changed path from [%s] to [%s]\n", path, altSepPath.data()); #endif
path = altSepPath.data();
}
} #endif
CharString tocEntryName; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */
CharString tocEntryPath; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */
CharString pkgName;
CharString treeName;
/* ======= Set up strings */ if(path==nullptr) {
pkgName.append(U_ICUDATA_NAME, *pErrorCode);
} else { constchar *pkg; constchar *first;
pkg = uprv_strrchr(path, U_FILE_SEP_CHAR);
first = uprv_strchr(path, U_FILE_SEP_CHAR); if(uprv_pathIsAbsolute(path) || (pkg != first)) { /* more than one slash in the path- not a tree name */ /* see if this is an /absolute/path/to/package path */ if(pkg) {
pkgName.append(pkg+1, *pErrorCode);
} else {
pkgName.append(path, *pErrorCode);
}
} else {
treeChar = uprv_strchr(path, U_TREE_SEPARATOR); if(treeChar) {
treeName.append(treeChar+1, *pErrorCode); /* following '-' */ if(isICUData) {
pkgName.append(U_ICUDATA_NAME, *pErrorCode);
} else {
pkgName.append(path, static_cast<int32_t>(treeChar - path), *pErrorCode); if (first == nullptr) { /* Thisuserdatahasnopath,butthereisatreename. Lookupthecorrectpathfromthedatacachelater.
*/
path = pkgName.data();
}
}
} else { if(isICUData) {
pkgName.append(U_ICUDATA_NAME, *pErrorCode);
} else {
pkgName.append(path, *pErrorCode);
}
}
}
}
#if !defined(ICU_DATA_DIR_WINDOWS) if(path == nullptr) {
path = COMMON_DATA_NAME; /* "icudt26e" */
} #else // When using the Windows system data, we expects only a single data file.
path = COMMON_DATA_NAME; /* "icudt26e" */ #endif
/************************ Begin loop looking for ind. files ***************/ #ifdef UDATA_DEBUG
fprintf(stderr, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path)); #endif
/* End of dealing with a null basename */
dataPath = u_getDataDirectory();
/* Load from DLL. If we haven't attempted package load, we also haven't had any chance to tryaDLL(staticorsetCommonData/etc)load.
If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */ if(gDataFileAccess==UDATA_NO_FILES) { #ifdef UDATA_DEBUG
fprintf(stderr, "Trying common data (UDATA_NO_FILES)\n"); #endif
retVal = doLoadFromCommonData(isICUData,
pkgName.data(), "", tocEntryPathSuffix, tocEntryName.data(),
path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); if((retVal != nullptr) || U_FAILURE(*pErrorCode)) { return retVal;
}
}
/* data not found */ if(U_SUCCESS(*pErrorCode)) { if(U_SUCCESS(subErrorCode)) { /* file not found */
*pErrorCode=U_FILE_ACCESS_ERROR;
} else { /* entry point not found or rejected */
*pErrorCode=subErrorCode;
}
} return retVal;
}
/* API ---------------------------------------------------------------------- */
U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/)
{ // Note: this function is documented as not thread safe.
gDataFileAccess = access;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.