michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 1999-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * file name: udata.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 1999oct25 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" /* U_PLATFORM etc. */ michael@0: michael@0: #ifdef __GNUC__ michael@0: /* if gcc michael@0: #define ATTRIBUTE_WEAK __attribute__ ((weak)) michael@0: might have to #include some other header michael@0: */ michael@0: #endif michael@0: michael@0: #include "unicode/putil.h" michael@0: #include "unicode/udata.h" michael@0: #include "unicode/uversion.h" michael@0: #include "charstr.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "putilimp.h" michael@0: #include "uassert.h" michael@0: #include "ucln_cmn.h" michael@0: #include "ucmndata.h" michael@0: #include "udatamem.h" michael@0: #include "uhash.h" michael@0: #include "umapfile.h" michael@0: #include "umutex.h" michael@0: michael@0: /*********************************************************************** michael@0: * michael@0: * Notes on the organization of the ICU data implementation michael@0: * michael@0: * All of the public API is defined in udata.h michael@0: * michael@0: * The implementation is split into several files... michael@0: * michael@0: * - udata.c (this file) contains higher level code that knows about michael@0: * the search paths for locating data, caching opened data, etc. michael@0: * michael@0: * - umapfile.c contains the low level platform-specific code for actually loading michael@0: * (memory mapping, file reading, whatever) data into memory. michael@0: * michael@0: * - ucmndata.c deals with the tables of contents of ICU data items within michael@0: * an ICU common format data file. The implementation includes michael@0: * an abstract interface and support for multiple TOC formats. michael@0: * All knowledge of any specific TOC format is encapsulated here. michael@0: * michael@0: * - udatamem.c has code for managing UDataMemory structs. These are little michael@0: * descriptor objects for blocks of memory holding ICU data of michael@0: * various types. michael@0: */ michael@0: michael@0: /* configuration ---------------------------------------------------------- */ michael@0: michael@0: /* If you are excruciatingly bored turn this on .. */ michael@0: /* #define UDATA_DEBUG 1 */ michael@0: michael@0: #if defined(UDATA_DEBUG) michael@0: # include michael@0: #endif michael@0: michael@0: #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: /* michael@0: * Forward declarations michael@0: */ michael@0: static UDataMemory *udata_findCachedData(const char *path); michael@0: michael@0: /*********************************************************************** michael@0: * michael@0: * static (Global) data michael@0: * michael@0: ************************************************************************/ michael@0: michael@0: /* michael@0: * Pointers to the common ICU data. michael@0: * michael@0: * We store multiple pointers to ICU data packages and iterate through them michael@0: * when looking for a data item. michael@0: * michael@0: * It is possible to combine this with dependency inversion: michael@0: * One or more data package libraries may export michael@0: * functions that each return a pointer to their piece of the ICU data, michael@0: * and this file would import them as weak functions, without a michael@0: * strong linker dependency from the common library on the data library. michael@0: * michael@0: * Then we can have applications depend on only that part of ICU's data michael@0: * that they really need, reducing the size of binaries that take advantage michael@0: * of this. michael@0: */ michael@0: static UDataMemory *gCommonICUDataArray[10] = { NULL }; michael@0: michael@0: static UBool gHaveTriedToLoadCommonData = FALSE; /* See extendICUData(). */ michael@0: michael@0: static UHashtable *gCommonDataCache = NULL; /* Global hash table of opened ICU data files. */ michael@0: static icu::UInitOnce gCommonDataCacheInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: static UDataFileAccess gDataFileAccess = UDATA_DEFAULT_ACCESS; michael@0: michael@0: static UBool U_CALLCONV michael@0: udata_cleanup(void) michael@0: { michael@0: int32_t i; michael@0: michael@0: if (gCommonDataCache) { /* Delete the cache of user data mappings. */ michael@0: uhash_close(gCommonDataCache); /* Table owns the contents, and will delete them. */ michael@0: gCommonDataCache = NULL; /* Cleanup is not thread safe. */ michael@0: } michael@0: gCommonDataCacheInitOnce.reset(); michael@0: michael@0: for (i = 0; i < LENGTHOF(gCommonICUDataArray) && gCommonICUDataArray[i] != NULL; ++i) { michael@0: udata_close(gCommonICUDataArray[i]); michael@0: gCommonICUDataArray[i] = NULL; michael@0: } michael@0: gHaveTriedToLoadCommonData = FALSE; michael@0: michael@0: return TRUE; /* Everything was cleaned up */ michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: findCommonICUDataByName(const char *inBasename) michael@0: { michael@0: UBool found = FALSE; michael@0: int32_t i; michael@0: michael@0: UDataMemory *pData = udata_findCachedData(inBasename); michael@0: if (pData == NULL) michael@0: return FALSE; michael@0: michael@0: for (i = 0; i < LENGTHOF(gCommonICUDataArray); ++i) { michael@0: if ((gCommonICUDataArray[i] != NULL) && (gCommonICUDataArray[i]->pHeader == pData->pHeader)) { michael@0: /* The data pointer is already in the array. */ michael@0: found = TRUE; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return found; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * setCommonICUData. Set a UDataMemory to be the global ICU Data michael@0: */ michael@0: static UBool michael@0: setCommonICUData(UDataMemory *pData, /* The new common data. Belongs to caller, we copy it. */ michael@0: UBool warn, /* If true, set USING_DEFAULT warning if ICUData was */ michael@0: /* changed by another thread before we got to it. */ michael@0: UErrorCode *pErr) michael@0: { michael@0: UDataMemory *newCommonData = UDataMemory_createNewInstance(pErr); michael@0: int32_t i; michael@0: UBool didUpdate = FALSE; michael@0: if (U_FAILURE(*pErr)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: /* For the assignment, other threads must cleanly see either the old */ michael@0: /* or the new, not some partially initialized new. The old can not be */ michael@0: /* deleted - someone may still have a pointer to it lying around in */ michael@0: /* their locals. */ michael@0: UDatamemory_assign(newCommonData, pData); michael@0: umtx_lock(NULL); michael@0: for (i = 0; i < LENGTHOF(gCommonICUDataArray); ++i) { michael@0: if (gCommonICUDataArray[i] == NULL) { michael@0: gCommonICUDataArray[i] = newCommonData; michael@0: ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); michael@0: didUpdate = TRUE; michael@0: break; michael@0: } else if (gCommonICUDataArray[i]->pHeader == pData->pHeader) { michael@0: /* The same data pointer is already in the array. */ michael@0: break; michael@0: } michael@0: } michael@0: umtx_unlock(NULL); michael@0: michael@0: if (i == LENGTHOF(gCommonICUDataArray) && warn) { michael@0: *pErr = U_USING_DEFAULT_WARNING; michael@0: } michael@0: if (!didUpdate) { michael@0: uprv_free(newCommonData); michael@0: } michael@0: return didUpdate; michael@0: } michael@0: michael@0: static UBool michael@0: setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) { michael@0: UDataMemory tData; michael@0: UDataMemory_init(&tData); michael@0: UDataMemory_setData(&tData, pData); michael@0: udata_checkCommonData(&tData, pErrorCode); michael@0: return setCommonICUData(&tData, FALSE, pErrorCode); michael@0: } michael@0: michael@0: static const char * michael@0: findBasename(const char *path) { michael@0: const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR); michael@0: if(basename==NULL) { michael@0: return path; michael@0: } else { michael@0: return basename+1; michael@0: } michael@0: } michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: static const char * michael@0: packageNameFromPath(const char *path) michael@0: { michael@0: if((path == NULL) || (*path == 0)) { michael@0: return U_ICUDATA_NAME; michael@0: } michael@0: michael@0: path = findBasename(path); michael@0: michael@0: if((path == NULL) || (*path == 0)) { michael@0: return U_ICUDATA_NAME; michael@0: } michael@0: michael@0: return path; michael@0: } michael@0: #endif michael@0: michael@0: /*----------------------------------------------------------------------* michael@0: * * michael@0: * Cache for common data * michael@0: * Functions for looking up or adding entries to a cache of * michael@0: * data that has been previously opened. Avoids a potentially * michael@0: * expensive operation of re-opening the data for subsequent * michael@0: * uses. * michael@0: * * michael@0: * Data remains cached for the duration of the process. * michael@0: * * michael@0: *----------------------------------------------------------------------*/ michael@0: michael@0: typedef struct DataCacheElement { michael@0: char *name; michael@0: UDataMemory *item; michael@0: } DataCacheElement; michael@0: michael@0: michael@0: michael@0: /* michael@0: * Deleter function for DataCacheElements. michael@0: * udata cleanup function closes the hash table; hash table in turn calls back to michael@0: * here for each entry. michael@0: */ michael@0: static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) { michael@0: DataCacheElement *p = (DataCacheElement *)pDCEl; michael@0: udata_close(p->item); /* unmaps storage */ michael@0: uprv_free(p->name); /* delete the hash key string. */ michael@0: uprv_free(pDCEl); /* delete 'this' */ michael@0: } michael@0: michael@0: static void udata_initHashTable() { michael@0: UErrorCode err = U_ZERO_ERROR; michael@0: U_ASSERT(gCommonDataCache == NULL); michael@0: gCommonDataCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err); michael@0: if (U_FAILURE(err)) { michael@0: // TODO: handle errors better. michael@0: gCommonDataCache = NULL; michael@0: } michael@0: if (gCommonDataCache != NULL) { michael@0: uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter); michael@0: ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup); michael@0: } michael@0: } michael@0: michael@0: /* udata_getCacheHashTable() michael@0: * Get the hash table used to store the data cache entries. michael@0: * Lazy create it if it doesn't yet exist. michael@0: */ michael@0: static UHashtable *udata_getHashTable() { michael@0: umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable); michael@0: return gCommonDataCache; michael@0: } michael@0: michael@0: michael@0: michael@0: static UDataMemory *udata_findCachedData(const char *path) michael@0: { michael@0: UHashtable *htable; michael@0: UDataMemory *retVal = NULL; michael@0: DataCacheElement *el; michael@0: const char *baseName; michael@0: michael@0: baseName = findBasename(path); /* Cache remembers only the base name, not the full path. */ michael@0: htable = udata_getHashTable(); michael@0: umtx_lock(NULL); michael@0: el = (DataCacheElement *)uhash_get(htable, baseName); michael@0: umtx_unlock(NULL); michael@0: if (el != NULL) { michael@0: retVal = el->item; michael@0: } michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Cache: [%s] -> %p\n", baseName, retVal); michael@0: #endif michael@0: return retVal; michael@0: } michael@0: michael@0: michael@0: static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) { michael@0: DataCacheElement *newElement; michael@0: const char *baseName; michael@0: int32_t nameLen; michael@0: UHashtable *htable; michael@0: DataCacheElement *oldValue = NULL; michael@0: UErrorCode subErr = U_ZERO_ERROR; michael@0: michael@0: if (U_FAILURE(*pErr)) { michael@0: return NULL; michael@0: } michael@0: michael@0: /* Create a new DataCacheElement - the thingy we store in the hash table - michael@0: * and copy the supplied path and UDataMemoryItems into it. michael@0: */ michael@0: newElement = (DataCacheElement *)uprv_malloc(sizeof(DataCacheElement)); michael@0: if (newElement == NULL) { michael@0: *pErr = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: newElement->item = UDataMemory_createNewInstance(pErr); michael@0: if (U_FAILURE(*pErr)) { michael@0: uprv_free(newElement); michael@0: return NULL; michael@0: } michael@0: UDatamemory_assign(newElement->item, item); michael@0: michael@0: baseName = findBasename(path); michael@0: nameLen = (int32_t)uprv_strlen(baseName); michael@0: newElement->name = (char *)uprv_malloc(nameLen+1); michael@0: if (newElement->name == NULL) { michael@0: *pErr = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(newElement->item); michael@0: uprv_free(newElement); michael@0: return NULL; michael@0: } michael@0: uprv_strcpy(newElement->name, baseName); michael@0: michael@0: /* Stick the new DataCacheElement into the hash table. michael@0: */ michael@0: htable = udata_getHashTable(); michael@0: umtx_lock(NULL); michael@0: oldValue = (DataCacheElement *)uhash_get(htable, path); michael@0: if (oldValue != NULL) { michael@0: subErr = U_USING_DEFAULT_WARNING; michael@0: } michael@0: else { michael@0: uhash_put( michael@0: htable, michael@0: newElement->name, /* Key */ michael@0: newElement, /* Value */ michael@0: &subErr); michael@0: } michael@0: umtx_unlock(NULL); michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement->name, michael@0: newElement->item, u_errorName(subErr), newElement->item->vFuncs); michael@0: #endif michael@0: michael@0: if (subErr == U_USING_DEFAULT_WARNING || U_FAILURE(subErr)) { michael@0: *pErr = subErr; /* copy sub err unto fillin ONLY if something happens. */ michael@0: uprv_free(newElement->name); michael@0: uprv_free(newElement->item); michael@0: uprv_free(newElement); michael@0: return oldValue ? oldValue->item : NULL; michael@0: } michael@0: michael@0: return newElement->item; michael@0: } michael@0: michael@0: /*----------------------------------------------------------------------*============== michael@0: * * michael@0: * Path management. Could be shared with other tools/etc if need be * michael@0: * later on. * michael@0: * * michael@0: *----------------------------------------------------------------------*/ michael@0: michael@0: #define U_DATA_PATHITER_BUFSIZ 128 /* Size of local buffer for paths */ michael@0: /* Overflow causes malloc of larger buf */ michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class UDataPathIterator michael@0: { michael@0: public: michael@0: UDataPathIterator(const char *path, const char *pkg, michael@0: const char *item, const char *suffix, UBool doCheckLastFour, michael@0: UErrorCode *pErrorCode); michael@0: const char *next(UErrorCode *pErrorCode); michael@0: michael@0: private: michael@0: const char *path; /* working path (u_icudata_Dir) */ michael@0: const char *nextPath; /* path following this one */ michael@0: const char *basename; /* item's basename (icudt22e_mt.res)*/ michael@0: const char *suffix; /* item suffix (can be null) */ michael@0: michael@0: uint32_t basenameLen; /* length of basename */ michael@0: michael@0: CharString itemPath; /* path passed in with item name */ michael@0: CharString pathBuffer; /* output path for this it'ion */ michael@0: CharString packageStub; /* example: "/icudt28b". Will ignore that leaf in set paths. */ michael@0: michael@0: UBool checkLastFour; /* if TRUE then allow paths such as '/foo/myapp.dat' michael@0: * to match, checks last 4 chars of suffix with michael@0: * last 4 of path, then previous chars. */ michael@0: }; michael@0: michael@0: /** michael@0: * @param iter The iterator to be initialized. Its current state does not matter. michael@0: * @param path The full pathname to be iterated over. If NULL, defaults to U_ICUDATA_NAME michael@0: * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leave directories such as /icudt28l michael@0: * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat michael@0: * @param suffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly. michael@0: * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2. michael@0: * '/blarg/stuff.dat' would also be found. michael@0: */ michael@0: UDataPathIterator::UDataPathIterator(const char *inPath, const char *pkg, michael@0: const char *item, const char *inSuffix, UBool doCheckLastFour, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "SUFFIX1=%s PATH=%s\n", inSuffix, inPath); michael@0: #endif michael@0: /** Path **/ michael@0: if(inPath == NULL) { michael@0: path = u_getDataDirectory(); michael@0: } else { michael@0: path = inPath; michael@0: } michael@0: michael@0: /** Package **/ michael@0: if(pkg != NULL) { michael@0: packageStub.append(U_FILE_SEP_CHAR, *pErrorCode).append(pkg, *pErrorCode); michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "STUB=%s [%d]\n", packageStub.data(), packageStub.length()); michael@0: #endif michael@0: } michael@0: michael@0: /** Item **/ michael@0: basename = findBasename(item); michael@0: basenameLen = (int32_t)uprv_strlen(basename); michael@0: michael@0: /** Item path **/ michael@0: if(basename == item) { michael@0: nextPath = path; michael@0: } else { michael@0: itemPath.append(item, (int32_t)(basename-item), *pErrorCode); michael@0: nextPath = itemPath.data(); michael@0: } michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "SUFFIX=%s [%p]\n", inSuffix, inSuffix); michael@0: #endif michael@0: michael@0: /** Suffix **/ michael@0: if(inSuffix != NULL) { michael@0: suffix = inSuffix; michael@0: } else { michael@0: suffix = ""; michael@0: } michael@0: michael@0: checkLastFour = doCheckLastFour; michael@0: michael@0: /* pathBuffer will hold the output path strings returned by this iterator */ michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n", michael@0: iter, michael@0: item, michael@0: path, michael@0: basename, michael@0: suffix, michael@0: itemPath.data(), michael@0: nextPath, michael@0: checkLastFour?"TRUE":"false"); michael@0: #endif michael@0: } michael@0: michael@0: /** michael@0: * Get the next path on the list. michael@0: * michael@0: * @param iter The Iter to be used michael@0: * @param len If set, pointer to the length of the returned path, for convenience. michael@0: * @return Pointer to the next path segment, or NULL if there are no more. michael@0: */ michael@0: const char *UDataPathIterator::next(UErrorCode *pErrorCode) michael@0: { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: const char *currentPath = NULL; michael@0: int32_t pathLen = 0; michael@0: const char *pathBasename; michael@0: michael@0: do michael@0: { michael@0: if( nextPath == NULL ) { michael@0: break; michael@0: } michael@0: currentPath = nextPath; michael@0: michael@0: if(nextPath == itemPath.data()) { /* we were processing item's path. */ michael@0: nextPath = path; /* start with regular path next tm. */ michael@0: pathLen = (int32_t)uprv_strlen(currentPath); michael@0: } else { michael@0: /* fix up next for next time */ michael@0: nextPath = uprv_strchr(currentPath, U_PATH_SEP_CHAR); michael@0: if(nextPath == NULL) { michael@0: /* segment: entire path */ michael@0: pathLen = (int32_t)uprv_strlen(currentPath); michael@0: } else { michael@0: /* segment: until next segment */ michael@0: pathLen = (int32_t)(nextPath - currentPath); michael@0: /* skip divider */ michael@0: nextPath ++; michael@0: } michael@0: } michael@0: michael@0: if(pathLen == 0) { michael@0: continue; michael@0: } michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "rest of path (IDD) = %s\n", currentPath); michael@0: fprintf(stderr, " "); michael@0: { michael@0: uint32_t qqq; michael@0: for(qqq=0;qqq=4) && michael@0: uprv_strncmp(pathBuffer.data() +(pathLen-4), suffix, 4)==0 && /* suffix matches */ michael@0: uprv_strncmp(findBasename(pathBuffer.data()), basename, basenameLen)==0 && /* base matches */ michael@0: uprv_strlen(pathBasename)==(basenameLen+4)) { /* base+suffix = full len */ michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Have %s file on the path: %s\n", suffix, pathBuffer.data()); michael@0: #endif michael@0: /* do nothing */ michael@0: } michael@0: else michael@0: { /* regular dir path */ michael@0: if(pathBuffer[pathLen-1] != U_FILE_SEP_CHAR) { michael@0: if((pathLen>=4) && michael@0: uprv_strncmp(pathBuffer.data()+(pathLen-4), ".dat", 4) == 0) michael@0: { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "skipping non-directory .dat file %s\n", pathBuffer.data()); michael@0: #endif michael@0: continue; michael@0: } michael@0: michael@0: /* Check if it is a directory with the same name as our package */ michael@0: if(!packageStub.isEmpty() && michael@0: (pathLen > packageStub.length()) && michael@0: !uprv_strcmp(pathBuffer.data() + pathLen - packageStub.length(), packageStub.data())) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Found stub %s (will add package %s of len %d)\n", packageStub.data(), basename, basenameLen); michael@0: #endif michael@0: pathBuffer.truncate(pathLen - packageStub.length()); michael@0: } michael@0: pathBuffer.append(U_FILE_SEP_CHAR, *pErrorCode); michael@0: } michael@0: michael@0: /* + basename */ michael@0: pathBuffer.append(packageStub.data()+1, packageStub.length()-1, *pErrorCode); michael@0: michael@0: if(*suffix) /* tack on suffix */ michael@0: { michael@0: pathBuffer.append(suffix, *pErrorCode); michael@0: } michael@0: } michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, " --> %s\n", pathBuffer.data()); michael@0: #endif michael@0: michael@0: return pathBuffer.data(); michael@0: michael@0: } while(path); michael@0: michael@0: /* fell way off the end */ michael@0: return NULL; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* ==================================================================================*/ michael@0: michael@0: michael@0: /*----------------------------------------------------------------------* michael@0: * * michael@0: * Add a static reference to the common data library * michael@0: * Unless overridden by an explicit udata_setCommonData, this will be * michael@0: * our common data. * michael@0: * * michael@0: *----------------------------------------------------------------------*/ michael@0: extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT; michael@0: michael@0: /* michael@0: * This would be a good place for weak-linkage declarations of michael@0: * partial-data-library access functions where each returns a pointer michael@0: * to its data package, if it is linked in. michael@0: */ michael@0: /* michael@0: extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK; michael@0: extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK; michael@0: */ michael@0: michael@0: /*----------------------------------------------------------------------* michael@0: * * michael@0: * openCommonData Attempt to open a common format (.dat) file * michael@0: * Map it into memory (if it's not there already) * michael@0: * and return a UDataMemory object for it. * michael@0: * * michael@0: * If the requested data is already open and cached * michael@0: * just return the cached UDataMem object. * michael@0: * * michael@0: *----------------------------------------------------------------------*/ michael@0: static UDataMemory * michael@0: openCommonData(const char *path, /* Path from OpenChoice? */ michael@0: int32_t commonDataIndex, /* ICU Data (index >= 0) if path == NULL */ michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: UDataMemory tData; michael@0: const char *pathBuffer; michael@0: const char *inBasename; michael@0: michael@0: if (U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: UDataMemory_init(&tData); michael@0: michael@0: /* ??????? TODO revisit this */ michael@0: if (commonDataIndex >= 0) { michael@0: /* "mini-cache" for common ICU data */ michael@0: if(commonDataIndex >= LENGTHOF(gCommonICUDataArray)) { michael@0: return NULL; michael@0: } michael@0: if(gCommonICUDataArray[commonDataIndex] == NULL) { michael@0: int32_t i; michael@0: for(i = 0; i < commonDataIndex; ++i) { michael@0: if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) { michael@0: /* The linked-in data is already in the list. */ michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: /* Add the linked-in data to the list. */ michael@0: /* michael@0: * This is where we would check and call weakly linked partial-data-library michael@0: * access functions. michael@0: */ michael@0: /* michael@0: if (uprv_getICUData_collation) { michael@0: setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode); michael@0: } michael@0: if (uprv_getICUData_conversion) { michael@0: setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode); michael@0: } michael@0: */ michael@0: setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, FALSE, pErrorCode); michael@0: } michael@0: return gCommonICUDataArray[commonDataIndex]; michael@0: } michael@0: michael@0: michael@0: /* request is NOT for ICU Data. */ michael@0: michael@0: /* Find the base name portion of the supplied path. */ michael@0: /* inBasename will be left pointing somewhere within the original path string. */ michael@0: inBasename = findBasename(path); michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "inBasename = %s\n", inBasename); michael@0: #endif michael@0: michael@0: if(*inBasename==0) { michael@0: /* no basename. This will happen if the original path was a directory name, */ michael@0: /* like "a/b/c/". (Fallback to separate files will still work.) */ michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "ocd: no basename in %s, bailing.\n", path); michael@0: #endif michael@0: *pErrorCode=U_FILE_ACCESS_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: /* Is the requested common data file already open and cached? */ michael@0: /* Note that the cache is keyed by the base name only. The rest of the path, */ michael@0: /* if any, is not considered. */ michael@0: { michael@0: UDataMemory *dataToReturn = udata_findCachedData(inBasename); michael@0: if (dataToReturn != NULL) { michael@0: return dataToReturn; michael@0: } michael@0: } michael@0: michael@0: /* Requested item is not in the cache. michael@0: * Hunt it down, trying all the path locations michael@0: */ michael@0: michael@0: UDataPathIterator iter(u_getDataDirectory(), inBasename, path, ".dat", TRUE, pErrorCode); michael@0: michael@0: while((UDataMemory_isLoaded(&tData)==FALSE) && (pathBuffer = iter.next(pErrorCode)) != NULL) michael@0: { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "ocd: trying path %s - ", pathBuffer); michael@0: #endif michael@0: uprv_mapFile(&tData, pathBuffer); michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "%s\n", UDataMemory_isLoaded(&tData)?"LOADED":"not loaded"); michael@0: #endif michael@0: } michael@0: michael@0: #if defined(OS390_STUBDATA) && defined(OS390BATCH) michael@0: if (!UDataMemory_isLoaded(&tData)) { michael@0: char ourPathBuffer[1024]; michael@0: /* One more chance, for extendCommonData() */ michael@0: uprv_strncpy(ourPathBuffer, path, 1019); michael@0: ourPathBuffer[1019]=0; michael@0: uprv_strcat(ourPathBuffer, ".dat"); michael@0: uprv_mapFile(&tData, ourPathBuffer); michael@0: } michael@0: #endif michael@0: michael@0: if (!UDataMemory_isLoaded(&tData)) { michael@0: /* no common data */ michael@0: *pErrorCode=U_FILE_ACCESS_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: /* we have mapped a file, check its header */ michael@0: udata_checkCommonData(&tData, pErrorCode); michael@0: michael@0: michael@0: /* Cache the UDataMemory struct for this .dat file, michael@0: * so we won't need to hunt it down and map it again next time michael@0: * something is needed from it. */ michael@0: return udata_cacheDataItem(inBasename, &tData, pErrorCode); michael@0: } michael@0: michael@0: michael@0: /*----------------------------------------------------------------------* michael@0: * * michael@0: * extendICUData If the full set of ICU data was not loaded at * michael@0: * program startup, load it now. This function will * michael@0: * be called when the lookup of an ICU data item in * michael@0: * the common ICU data fails. * michael@0: * * michael@0: * return true if new data is loaded, false otherwise.* michael@0: * * michael@0: *----------------------------------------------------------------------*/ michael@0: static UBool extendICUData(UErrorCode *pErr) michael@0: { michael@0: UDataMemory *pData; michael@0: UDataMemory copyPData; michael@0: UBool didUpdate = FALSE; michael@0: michael@0: /* michael@0: * There is a chance for a race condition here. michael@0: * Normally, ICU data is loaded from a DLL or via mmap() and michael@0: * setCommonICUData() will detect if the same address is set twice. michael@0: * If ICU is built with data loading via fread() then the address will michael@0: * be different each time the common data is loaded and we may add michael@0: * multiple copies of the data. michael@0: * In this case, use a mutex to prevent the race. michael@0: * Use a specific mutex to avoid nested locks of the global mutex. michael@0: */ michael@0: #if MAP_IMPLEMENTATION==MAP_STDIO michael@0: static UMutex extendICUDataMutex = U_MUTEX_INITIALIZER; michael@0: umtx_lock(&extendICUDataMutex); michael@0: #endif michael@0: if(!gHaveTriedToLoadCommonData) { michael@0: /* See if we can explicitly open a .dat file for the ICUData. */ michael@0: pData = openCommonData( michael@0: U_ICUDATA_NAME, /* "icudt20l" , for example. */ michael@0: -1, /* Pretend we're not opening ICUData */ michael@0: pErr); michael@0: michael@0: /* How about if there is no pData, eh... */ michael@0: michael@0: UDataMemory_init(©PData); michael@0: if(pData != NULL) { michael@0: UDatamemory_assign(©PData, pData); michael@0: copyPData.map = 0; /* The mapping for this data is owned by the hash table */ michael@0: copyPData.mapAddr = 0; /* which will unmap it when ICU is shut down. */ michael@0: /* CommonICUData is also unmapped when ICU is shut down.*/ michael@0: /* To avoid unmapping the data twice, zero out the map */ michael@0: /* fields in the UDataMemory that we're assigning */ michael@0: /* to CommonICUData. */ michael@0: michael@0: didUpdate = /* no longer using this result */ michael@0: setCommonICUData(©PData,/* The new common data. */ michael@0: FALSE, /* No warnings if write didn't happen */ michael@0: pErr); /* setCommonICUData honors errors; NOP if error set */ michael@0: } michael@0: michael@0: gHaveTriedToLoadCommonData = TRUE; michael@0: } michael@0: michael@0: didUpdate = findCommonICUDataByName(U_ICUDATA_NAME); /* Return 'true' when a racing writes out the extended */ michael@0: /* data after another thread has failed to see it (in openCommonData), so */ michael@0: /* extended data can be examined. */ michael@0: /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */ michael@0: michael@0: #if MAP_IMPLEMENTATION==MAP_STDIO michael@0: umtx_unlock(&extendICUDataMutex); michael@0: #endif michael@0: return didUpdate; /* Return true if ICUData pointer was updated. */ michael@0: /* (Could potentialy have been done by another thread racing */ michael@0: /* us through here, but that's fine, we still return true */ michael@0: /* so that current thread will also examine extended data. */ michael@0: } michael@0: michael@0: /*----------------------------------------------------------------------* michael@0: * * michael@0: * udata_setCommonData * michael@0: * * michael@0: *----------------------------------------------------------------------*/ michael@0: U_CAPI void U_EXPORT2 michael@0: udata_setCommonData(const void *data, UErrorCode *pErrorCode) { michael@0: UDataMemory dataMemory; michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return; michael@0: } michael@0: michael@0: if(data==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: michael@0: /* set the data pointer and test for validity */ michael@0: UDataMemory_init(&dataMemory); michael@0: UDataMemory_setData(&dataMemory, data); michael@0: udata_checkCommonData(&dataMemory, pErrorCode); michael@0: if (U_FAILURE(*pErrorCode)) {return;} michael@0: michael@0: /* we have good data */ michael@0: /* Set it up as the ICU Common Data. */ michael@0: setCommonICUData(&dataMemory, TRUE, pErrorCode); michael@0: } michael@0: michael@0: /*--------------------------------------------------------------------------- michael@0: * michael@0: * udata_setAppData michael@0: * michael@0: *---------------------------------------------------------------------------- */ michael@0: U_CAPI void U_EXPORT2 michael@0: udata_setAppData(const char *path, const void *data, UErrorCode *err) michael@0: { michael@0: UDataMemory udm; michael@0: michael@0: if(err==NULL || U_FAILURE(*err)) { michael@0: return; michael@0: } michael@0: if(data==NULL) { michael@0: *err=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: michael@0: UDataMemory_init(&udm); michael@0: UDataMemory_setData(&udm, data); michael@0: udata_checkCommonData(&udm, err); michael@0: udata_cacheDataItem(path, &udm, err); michael@0: } michael@0: michael@0: /*----------------------------------------------------------------------------* michael@0: * * michael@0: * checkDataItem Given a freshly located/loaded data item, either * michael@0: * an entry in a common file or a separately loaded file, * michael@0: * sanity check its header, and see if the data is * michael@0: * acceptable to the app. * michael@0: * If the data is good, create and return a UDataMemory * michael@0: * object that can be returned to the application. * michael@0: * Return NULL on any sort of failure. * michael@0: * * michael@0: *----------------------------------------------------------------------------*/ michael@0: static UDataMemory * michael@0: checkDataItem michael@0: ( michael@0: const DataHeader *pHeader, /* The data item to be checked. */ michael@0: UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */ michael@0: void *context, /* pass-thru param for above. */ michael@0: const char *type, /* pass-thru param for above. */ michael@0: const char *name, /* pass-thru param for above. */ michael@0: UErrorCode *nonFatalErr, /* Error code if this data was not acceptable */ michael@0: /* but openChoice should continue with */ michael@0: /* trying to get data from fallback path. */ michael@0: UErrorCode *fatalErr /* Bad error, caller should return immediately */ michael@0: ) michael@0: { michael@0: UDataMemory *rDataMem = NULL; /* the new UDataMemory, to be returned. */ michael@0: michael@0: if (U_FAILURE(*fatalErr)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if(pHeader->dataHeader.magic1==0xda && michael@0: pHeader->dataHeader.magic2==0x27 && michael@0: (isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info)) michael@0: ) { michael@0: rDataMem=UDataMemory_createNewInstance(fatalErr); michael@0: if (U_FAILURE(*fatalErr)) { michael@0: return NULL; michael@0: } michael@0: rDataMem->pHeader = pHeader; michael@0: } else { michael@0: /* the data is not acceptable, look further */ michael@0: /* If we eventually find something good, this errorcode will be */ michael@0: /* cleared out. */ michael@0: *nonFatalErr=U_INVALID_FORMAT_ERROR; michael@0: } michael@0: return rDataMem; michael@0: } michael@0: michael@0: /** michael@0: * @return 0 if not loaded, 1 if loaded or err michael@0: */ michael@0: static UDataMemory *doLoadFromIndividualFiles(const char *pkgName, michael@0: const char *dataPath, const char *tocEntryPathSuffix, michael@0: /* following arguments are the same as doOpenChoice itself */ michael@0: const char *path, const char *type, const char *name, michael@0: UDataMemoryIsAcceptable *isAcceptable, void *context, michael@0: UErrorCode *subErrorCode, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: const char *pathBuffer; michael@0: UDataMemory dataMemory; michael@0: UDataMemory *pEntryData; michael@0: michael@0: /* look in ind. files: package\nam.typ ========================= */ michael@0: /* init path iterator for individual files */ michael@0: UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, FALSE, pErrorCode); michael@0: michael@0: while((pathBuffer = iter.next(pErrorCode))) michael@0: { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer); michael@0: #endif michael@0: if(uprv_mapFile(&dataMemory, pathBuffer)) michael@0: { michael@0: pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); michael@0: if (pEntryData != NULL) { michael@0: /* Data is good. michael@0: * Hand off ownership of the backing memory to the user's UDataMemory. michael@0: * and return it. */ michael@0: pEntryData->mapAddr = dataMemory.mapAddr; michael@0: pEntryData->map = dataMemory.map; michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "** Mapped file: %s\n", pathBuffer); michael@0: #endif michael@0: return pEntryData; michael@0: } michael@0: michael@0: /* the data is not acceptable, or some error occured. Either way, unmap the memory */ michael@0: udata_close(&dataMemory); michael@0: michael@0: /* If we had a nasty error, bail out completely. */ michael@0: if (U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: /* Otherwise remember that we found data but didn't like it for some reason */ michael@0: *subErrorCode=U_INVALID_FORMAT_ERROR; michael@0: } michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded"); michael@0: #endif michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: /** michael@0: * @return 0 if not loaded, 1 if loaded or err michael@0: */ michael@0: static UDataMemory *doLoadFromCommonData(UBool isICUData, const char * /*pkgName*/, michael@0: const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName, michael@0: /* following arguments are the same as doOpenChoice itself */ michael@0: const char *path, const char *type, const char *name, michael@0: UDataMemoryIsAcceptable *isAcceptable, void *context, michael@0: UErrorCode *subErrorCode, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: UDataMemory *pEntryData; michael@0: const DataHeader *pHeader; michael@0: UDataMemory *pCommonData; michael@0: int32_t commonDataIndex; michael@0: UBool checkedExtendedICUData = FALSE; michael@0: /* try to get common data. The loop is for platforms such as the 390 that do michael@0: * not initially load the full set of ICU data. If the lookup of an ICU data item michael@0: * fails, the full (but slower to load) set is loaded, the and the loop repeats, michael@0: * trying the lookup again. Once the full set of ICU data is loaded, the loop wont michael@0: * repeat because the full set will be checked the first time through. michael@0: * michael@0: * The loop also handles the fallback to a .dat file if the application linked michael@0: * to the stub data library rather than a real library. michael@0: */ michael@0: for (commonDataIndex = isICUData ? 0 : -1;;) { michael@0: pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/ michael@0: michael@0: if(U_SUCCESS(*subErrorCode) && pCommonData!=NULL) { michael@0: int32_t length; michael@0: michael@0: /* look up the data piece in the common data */ michael@0: pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &length, subErrorCode); michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "%s: pHeader=%p - %s\n", tocEntryName, pHeader, u_errorName(*subErrorCode)); michael@0: #endif michael@0: michael@0: if(pHeader!=NULL) { michael@0: pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode); michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "pEntryData=%p\n", pEntryData); michael@0: #endif michael@0: if (U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: if (pEntryData != NULL) { michael@0: pEntryData->length = length; michael@0: return pEntryData; michael@0: } michael@0: } michael@0: } michael@0: /* Data wasn't found. If we were looking for an ICUData item and there is michael@0: * more data available, load it and try again, michael@0: * otherwise break out of this loop. */ michael@0: if (!isICUData) { michael@0: return NULL; michael@0: } else if (pCommonData != NULL) { michael@0: ++commonDataIndex; /* try the next data package */ michael@0: } else if ((!checkedExtendedICUData) && extendICUData(subErrorCode)) { michael@0: checkedExtendedICUData = TRUE; michael@0: /* try this data package slot again: it changed from NULL to non-NULL */ michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * A note on the ownership of Mapped Memory michael@0: * michael@0: * For common format files, ownership resides with the UDataMemory object michael@0: * that lives in the cache of opened common data. These UDataMemorys are private michael@0: * to the udata implementation, and are never seen directly by users. michael@0: * michael@0: * The UDataMemory objects returned to users will have the address of some desired michael@0: * data within the mapped region, but they wont have the mapping info itself, and thus michael@0: * won't cause anything to be removed from memory when they are closed. michael@0: * michael@0: * For individual data files, the UDataMemory returned to the user holds the michael@0: * information necessary to unmap the data on close. If the user independently michael@0: * opens the same data file twice, two completely independent mappings will be made. michael@0: * (There is no cache of opened data items from individual files, only a cache of michael@0: * opened Common Data files, that is, files containing a collection of data items.) michael@0: * michael@0: * For common data passed in from the user via udata_setAppData() or michael@0: * udata_setCommonData(), ownership remains with the user. michael@0: * michael@0: * UDataMemory objects themselves, as opposed to the memory they describe, michael@0: * can be anywhere - heap, stack/local or global. michael@0: * They have a flag to indicate when they're heap allocated and thus michael@0: * must be deleted when closed. michael@0: */ michael@0: michael@0: michael@0: /*----------------------------------------------------------------------------* michael@0: * * michael@0: * main data loading functions * michael@0: * * michael@0: *----------------------------------------------------------------------------*/ michael@0: static UDataMemory * michael@0: doOpenChoice(const char *path, const char *type, const char *name, michael@0: UDataMemoryIsAcceptable *isAcceptable, void *context, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: UDataMemory *retVal = NULL; michael@0: michael@0: const char *dataPath; michael@0: michael@0: int32_t tocEntrySuffixIndex; michael@0: const char *tocEntryPathSuffix; michael@0: UErrorCode subErrorCode=U_ZERO_ERROR; michael@0: const char *treeChar; michael@0: michael@0: UBool isICUData = FALSE; michael@0: michael@0: michael@0: /* Is this path ICU data? */ michael@0: if(path == NULL || michael@0: !strcmp(path, U_ICUDATA_ALIAS) || /* "ICUDATA" */ michael@0: !uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING, /* "icudt26e-" */ michael@0: uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)) || michael@0: !uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING, /* "ICUDATA-" */ michael@0: uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING))) { michael@0: isICUData = TRUE; michael@0: } michael@0: michael@0: #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */ michael@0: /* remap from alternate path char to the main one */ michael@0: CharString altSepPath; michael@0: if(path) { michael@0: if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR) != NULL) { michael@0: altSepPath.append(path, *pErrorCode); michael@0: char *p; michael@0: while((p=uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR))) { michael@0: *p = U_FILE_SEP_CHAR; michael@0: } michael@0: #if defined (UDATA_DEBUG) michael@0: fprintf(stderr, "Changed path from [%s] to [%s]\n", path, altSepPath.s); michael@0: #endif michael@0: path = altSepPath.data(); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: CharString tocEntryName; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */ michael@0: CharString tocEntryPath; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */ michael@0: michael@0: CharString pkgName; michael@0: CharString treeName; michael@0: michael@0: /* ======= Set up strings */ michael@0: if(path==NULL) { michael@0: pkgName.append(U_ICUDATA_NAME, *pErrorCode); michael@0: } else { michael@0: const char *pkg; michael@0: const char *first; michael@0: pkg = uprv_strrchr(path, U_FILE_SEP_CHAR); michael@0: first = uprv_strchr(path, U_FILE_SEP_CHAR); michael@0: if(uprv_pathIsAbsolute(path) || (pkg != first)) { /* more than one slash in the path- not a tree name */ michael@0: /* see if this is an /absolute/path/to/package path */ michael@0: if(pkg) { michael@0: pkgName.append(pkg+1, *pErrorCode); michael@0: } else { michael@0: pkgName.append(path, *pErrorCode); michael@0: } michael@0: } else { michael@0: treeChar = uprv_strchr(path, U_TREE_SEPARATOR); michael@0: if(treeChar) { michael@0: treeName.append(treeChar+1, *pErrorCode); /* following '-' */ michael@0: if(isICUData) { michael@0: pkgName.append(U_ICUDATA_NAME, *pErrorCode); michael@0: } else { michael@0: pkgName.append(path, (int32_t)(treeChar-path), *pErrorCode); michael@0: if (first == NULL) { michael@0: /* michael@0: This user data has no path, but there is a tree name. michael@0: Look up the correct path from the data cache later. michael@0: */ michael@0: path = pkgName.data(); michael@0: } michael@0: } michael@0: } else { michael@0: if(isICUData) { michael@0: pkgName.append(U_ICUDATA_NAME, *pErrorCode); michael@0: } else { michael@0: pkgName.append(path, *pErrorCode); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, " P=%s T=%s\n", pkgName.data(), treeName.data()); michael@0: #endif michael@0: michael@0: /* setting up the entry name and file name michael@0: * Make up a full name by appending the type to the supplied michael@0: * name, assuming that a type was supplied. michael@0: */ michael@0: michael@0: /* prepend the package */ michael@0: tocEntryName.append(pkgName, *pErrorCode); michael@0: tocEntryPath.append(pkgName, *pErrorCode); michael@0: tocEntrySuffixIndex = tocEntryName.length(); michael@0: michael@0: if(!treeName.isEmpty()) { michael@0: tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode); michael@0: tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode); michael@0: } michael@0: michael@0: tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(name, *pErrorCode); michael@0: tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(name, *pErrorCode); michael@0: if(type!=NULL && *type!=0) { michael@0: tocEntryName.append(".", *pErrorCode).append(type, *pErrorCode); michael@0: tocEntryPath.append(".", *pErrorCode).append(type, *pErrorCode); michael@0: } michael@0: tocEntryPathSuffix = tocEntryPath.data()+tocEntrySuffixIndex; /* suffix starts here */ michael@0: michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, " tocEntryName = %s\n", tocEntryName.data()); michael@0: fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.data()); michael@0: #endif michael@0: michael@0: if(path == NULL) { michael@0: path = COMMON_DATA_NAME; /* "icudt26e" */ michael@0: } michael@0: michael@0: /************************ Begin loop looking for ind. files ***************/ michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path)); michael@0: #endif michael@0: michael@0: /* End of dealing with a null basename */ michael@0: dataPath = u_getDataDirectory(); michael@0: michael@0: /**** COMMON PACKAGE - only if packages are first. */ michael@0: if(gDataFileAccess == UDATA_PACKAGES_FIRST) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Trying packages (UDATA_PACKAGES_FIRST)\n"); michael@0: #endif michael@0: /* #2 */ michael@0: retVal = doLoadFromCommonData(isICUData, michael@0: pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(), michael@0: path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); michael@0: if((retVal != NULL) || U_FAILURE(*pErrorCode)) { michael@0: return retVal; michael@0: } michael@0: } michael@0: michael@0: /**** INDIVIDUAL FILES */ michael@0: if((gDataFileAccess==UDATA_PACKAGES_FIRST) || michael@0: (gDataFileAccess==UDATA_FILES_FIRST)) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Trying individual files\n"); michael@0: #endif michael@0: /* Check to make sure that there is a dataPath to iterate over */ michael@0: if ((dataPath && *dataPath) || !isICUData) { michael@0: retVal = doLoadFromIndividualFiles(pkgName.data(), dataPath, tocEntryPathSuffix, michael@0: path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); michael@0: if((retVal != NULL) || U_FAILURE(*pErrorCode)) { michael@0: return retVal; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /**** COMMON PACKAGE */ michael@0: if((gDataFileAccess==UDATA_ONLY_PACKAGES) || michael@0: (gDataFileAccess==UDATA_FILES_FIRST)) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n"); michael@0: #endif michael@0: retVal = doLoadFromCommonData(isICUData, michael@0: pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(), michael@0: path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); michael@0: if((retVal != NULL) || U_FAILURE(*pErrorCode)) { michael@0: return retVal; michael@0: } michael@0: } michael@0: michael@0: /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to michael@0: try a DLL (static or setCommonData/etc) load. michael@0: If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */ michael@0: if(gDataFileAccess==UDATA_NO_FILES) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "Trying common data (UDATA_NO_FILES)\n"); michael@0: #endif michael@0: retVal = doLoadFromCommonData(isICUData, michael@0: pkgName.data(), "", tocEntryPathSuffix, tocEntryName.data(), michael@0: path, type, name, isAcceptable, context, &subErrorCode, pErrorCode); michael@0: if((retVal != NULL) || U_FAILURE(*pErrorCode)) { michael@0: return retVal; michael@0: } michael@0: } michael@0: michael@0: /* data not found */ michael@0: if(U_SUCCESS(*pErrorCode)) { michael@0: if(U_SUCCESS(subErrorCode)) { michael@0: /* file not found */ michael@0: *pErrorCode=U_FILE_ACCESS_ERROR; michael@0: } else { michael@0: /* entry point not found or rejected */ michael@0: *pErrorCode=subErrorCode; michael@0: } michael@0: } michael@0: return retVal; michael@0: } michael@0: michael@0: michael@0: michael@0: /* API ---------------------------------------------------------------------- */ michael@0: michael@0: U_CAPI UDataMemory * U_EXPORT2 michael@0: udata_open(const char *path, const char *type, const char *name, michael@0: UErrorCode *pErrorCode) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "udata_open(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type); michael@0: fflush(stderr); michael@0: #endif michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } else if(name==NULL || *name==0) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } else { michael@0: return doOpenChoice(path, type, name, NULL, NULL, pErrorCode); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI UDataMemory * U_EXPORT2 michael@0: udata_openChoice(const char *path, const char *type, const char *name, michael@0: UDataMemoryIsAcceptable *isAcceptable, void *context, michael@0: UErrorCode *pErrorCode) { michael@0: #ifdef UDATA_DEBUG michael@0: fprintf(stderr, "udata_openChoice(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type); michael@0: #endif michael@0: michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } else if(name==NULL || *name==0 || isAcceptable==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } else { michael@0: return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) { michael@0: if(pInfo!=NULL) { michael@0: if(pData!=NULL && pData->pHeader!=NULL) { michael@0: const UDataInfo *info=&pData->pHeader->info; michael@0: uint16_t dataInfoSize=udata_getInfoSize(info); michael@0: if(pInfo->size>dataInfoSize) { michael@0: pInfo->size=dataInfoSize; michael@0: } michael@0: uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2); michael@0: if(info->isBigEndian!=U_IS_BIG_ENDIAN) { michael@0: /* opposite endianness */ michael@0: uint16_t x=info->reservedWord; michael@0: pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8)); michael@0: } michael@0: } else { michael@0: pInfo->size=0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/) michael@0: { michael@0: gDataFileAccess = access; michael@0: }