1999-11-22 19:43:01 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
* Copyright (C) 1999-2001, International Business Machines
|
2000-01-13 23:54:23 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-11-22 19:43:01 +00:00
|
|
|
* file name: udata.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 1999oct25
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/putil.h"
|
1999-11-22 19:43:01 +00:00
|
|
|
#include "umutex.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/udata.h"
|
2001-07-26 21:55:49 +00:00
|
|
|
#include "unicode/uversion.h"
|
2001-08-10 20:50:07 +00:00
|
|
|
#include "uhash.h"
|
2001-08-17 00:18:49 +00:00
|
|
|
#include "ucln_cmn.h"
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
#include "udatamem.h"
|
|
|
|
#include "umapfile.h"
|
|
|
|
#include "ucmndata.h"
|
2000-11-22 01:24:40 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* Notes on the organization of the ICU data implementation
|
|
|
|
*
|
|
|
|
* All of the public API is defined in udata.h
|
|
|
|
*
|
|
|
|
* The implementation is split into several files...
|
|
|
|
*
|
|
|
|
* - udata.c (this file) contains higher level code that knows about
|
|
|
|
* the search paths for locating data, caching opened data, etc.
|
|
|
|
*
|
|
|
|
* - umapfile.c contains the low level platform-specific code for actually loading
|
|
|
|
* (memory mapping, file reading, whatever) data into memory.
|
|
|
|
*
|
|
|
|
* - ucmndata.c deals with the tables of contents of ICU data items within
|
|
|
|
* an ICU common format data file. The implementation includes
|
|
|
|
* an abstract interface and support for multiple TOC formats.
|
|
|
|
* All knowledge of any specific TOC format is encapsulated here.
|
|
|
|
*
|
|
|
|
* - udatamem.c has code for managing UDataMemory structs. These are little
|
|
|
|
* descriptor objects for blocks of memory holding ICU data of
|
|
|
|
* various types.
|
2001-09-25 16:43:20 +00:00
|
|
|
*/
|
2000-12-05 02:28:32 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/* configuration ---------------------------------------------------------- */
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2000-01-19 07:23:50 +00:00
|
|
|
/* If you are excruciatingly bored turn this on .. */
|
2000-05-03 23:11:04 +00:00
|
|
|
/* #define UDATA_DEBUG 1 */
|
1999-11-22 19:43:01 +00:00
|
|
|
|
|
|
|
#if defined(UDATA_DEBUG)
|
2000-05-03 23:11:04 +00:00
|
|
|
# include <stdio.h>
|
1999-11-22 19:43:01 +00:00
|
|
|
#endif
|
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
|
2001-08-30 18:41:30 +00:00
|
|
|
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* static (Global) data
|
|
|
|
*
|
|
|
|
************************************************************************/
|
2001-09-17 22:57:38 +00:00
|
|
|
static UDataMemory *gCommonICUData = NULL; /* Pointer to the common ICU data. */
|
|
|
|
/* May be updated once, if we started with */
|
|
|
|
/* a stub or subset library. */
|
2001-08-10 20:50:07 +00:00
|
|
|
|
2001-09-17 22:57:38 +00:00
|
|
|
static UDataMemory *gStubICUData = NULL; /* If gCommonICUData does get updated, remember */
|
|
|
|
/* the original one so that it can be cleaned */
|
|
|
|
/* up when ICU is shut down. */
|
|
|
|
|
|
|
|
static UHashtable *gCommonDataCache = NULL; /* Global hash table of opened ICU data files. */
|
2000-11-04 02:16:12 +00:00
|
|
|
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
UBool
|
|
|
|
udata_cleanup()
|
|
|
|
{
|
|
|
|
if (gCommonDataCache) { /* Delete the cache of user data mappings. */
|
|
|
|
uhash_close(gCommonDataCache); /* Table owns the contents, and will delete them. */
|
|
|
|
gCommonDataCache = 0; /* Cleanup is not thread safe. */
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
2001-09-17 22:57:38 +00:00
|
|
|
if (gCommonICUData != NULL) {
|
|
|
|
udata_close(gCommonICUData); /* Clean up common ICU Data */
|
2001-09-07 18:42:29 +00:00
|
|
|
gCommonICUData = NULL;
|
2001-09-17 22:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gStubICUData != NULL) {
|
|
|
|
udata_close(gStubICUData); /* Clean up the stub ICU Data */
|
|
|
|
gStubICUData = NULL;
|
|
|
|
}
|
|
|
|
|
2000-11-04 02:16:12 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
return TRUE; /* Everything was cleaned up */
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-30 18:41:30 +00:00
|
|
|
/*
|
|
|
|
* setCommonICUData. Set a UDataMemory to be the global ICU Data
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setCommonICUData(UDataMemory *pData, /* The new common data. Belongs to caller, we copy it. */
|
|
|
|
UDataMemory *oldData, /* Old ICUData ptr. Overwrite of this value is ok, */
|
|
|
|
/* of any others is not. */
|
|
|
|
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);
|
|
|
|
if (U_FAILURE(*pErr)) {
|
|
|
|
return;
|
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2001-08-30 18:41:30 +00:00
|
|
|
/* 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);
|
2000-05-03 23:11:04 +00:00
|
|
|
umtx_lock(NULL);
|
2001-09-07 18:42:29 +00:00
|
|
|
if (gCommonICUData==oldData) {
|
2001-09-17 22:57:38 +00:00
|
|
|
gStubICUData = gCommonICUData; /* remember the old Common Data, so it can be cleaned up. */
|
2001-09-07 18:42:29 +00:00
|
|
|
gCommonICUData = newCommonData;
|
2001-08-30 18:41:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (warn==TRUE) {
|
|
|
|
*pErr = U_USING_DEFAULT_WARNING;
|
|
|
|
}
|
|
|
|
uprv_free(newCommonData);
|
2000-05-03 23:11:04 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
2001-08-30 18:41:30 +00:00
|
|
|
return;
|
1999-12-01 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-30 18:41:30 +00:00
|
|
|
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
static char *
|
|
|
|
strcpy_returnEnd(char *dest, const char *src) {
|
|
|
|
while((*dest=*src)!=0) {
|
|
|
|
++dest;
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
1999-12-01 01:02:06 +00:00
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
/*------------------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* setPathGetBasename given a (possibly partial) path of an item *
|
|
|
|
* to be opened, compute a full directory path and leave *
|
|
|
|
* it in pathBuffer. Returns a pointer to the null at *
|
|
|
|
* the end of the computed path. *
|
2001-08-15 22:39:42 +00:00
|
|
|
* Overwrites any contents in the output pathBuffer *
|
2001-08-10 20:50:07 +00:00
|
|
|
* *
|
|
|
|
*------------------------------------------------------------------------------*/
|
2000-05-03 23:11:04 +00:00
|
|
|
static char *
|
|
|
|
setPathGetBasename(const char *path, char *pathBuffer) {
|
2001-08-15 22:39:42 +00:00
|
|
|
if(path==NULL) {
|
2000-05-03 23:11:04 +00:00
|
|
|
/* copy the ICU_DATA path to the path buffer */
|
|
|
|
path=u_getDataDirectory();
|
|
|
|
if(path!=NULL && *path!=0) {
|
|
|
|
return strcpy_returnEnd(pathBuffer, path);
|
|
|
|
} else {
|
|
|
|
/* there is no path */
|
|
|
|
return pathBuffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* find the last file sepator in the input path */
|
|
|
|
char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
|
|
|
|
if(basename==NULL) {
|
|
|
|
/* copy the ICU_DATA path to the path buffer */
|
|
|
|
path=u_getDataDirectory();
|
|
|
|
if(path!=NULL && *path!=0) {
|
|
|
|
return strcpy_returnEnd(pathBuffer, path);
|
|
|
|
} else {
|
|
|
|
/* there is no path */
|
|
|
|
return pathBuffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* copy the path to the path buffer */
|
|
|
|
++basename;
|
|
|
|
uprv_memcpy(pathBuffer, path, basename-path);
|
|
|
|
basename=pathBuffer+(basename-path);
|
|
|
|
*basename=0;
|
|
|
|
return basename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-12-01 01:02:06 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
static const char *
|
|
|
|
findBasename(const char *path) {
|
|
|
|
const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
|
|
|
|
if(basename==NULL) {
|
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
return basename+1;
|
1999-12-01 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-26 21:55:49 +00:00
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
/*----------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* Cache for common data *
|
|
|
|
* Functions for looking up or adding entries to a cache of *
|
|
|
|
* data that has been previously opened. Avoids a potentially *
|
|
|
|
* expensive operation of re-opening the data for subsequent *
|
|
|
|
* uses. *
|
|
|
|
* *
|
|
|
|
* Data remains cached for the duration of the process. *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
typedef struct DataCacheElement {
|
2001-08-29 22:57:37 +00:00
|
|
|
char *name;
|
2001-09-07 18:42:29 +00:00
|
|
|
UDataMemory *item;
|
2001-08-10 20:50:07 +00:00
|
|
|
} DataCacheElement;
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-08-15 23:27:38 +00:00
|
|
|
* Deleter function for DataCacheElements.
|
2001-08-15 22:39:42 +00:00
|
|
|
* udata cleanup function closes the hash table; hash table in turn calls back to
|
|
|
|
* here for each entry.
|
|
|
|
*/
|
|
|
|
static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) {
|
|
|
|
DataCacheElement *p = (DataCacheElement *)pDCEl;
|
2001-09-07 18:42:29 +00:00
|
|
|
udata_close(p->item); /* unmaps storage */
|
2001-08-29 22:57:37 +00:00
|
|
|
uprv_free(p->name); /* delete the hash key string. */
|
2001-08-15 22:39:42 +00:00
|
|
|
uprv_free(pDCEl); /* delete 'this' */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
/* udata_getCacheHashTable()
|
|
|
|
* Get the hash table used to store the data cache entries.
|
|
|
|
* Lazy create it if it doesn't yet exist.
|
2001-08-15 23:27:38 +00:00
|
|
|
*/
|
2001-08-10 20:50:07 +00:00
|
|
|
static UHashtable *udata_getHashTable() {
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
if (gCommonDataCache != NULL) {
|
|
|
|
return gCommonDataCache;
|
2001-08-10 20:50:07 +00:00
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
umtx_lock(NULL);
|
2001-09-07 18:42:29 +00:00
|
|
|
if (gCommonDataCache == NULL) {
|
|
|
|
gCommonDataCache = uhash_open(uhash_hashChars, uhash_compareChars, &err);
|
|
|
|
uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter);
|
2001-08-10 20:50:07 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
|
2001-08-30 02:59:19 +00:00
|
|
|
if (U_FAILURE(err)) {
|
2001-08-15 22:39:42 +00:00
|
|
|
return NULL; /* TODO: handle this error better. */
|
2001-08-10 20:50:07 +00:00
|
|
|
}
|
2001-09-07 18:42:29 +00:00
|
|
|
return gCommonDataCache;
|
2001-08-10 20:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static UDataMemory *udata_findCachedData(const char *path)
|
|
|
|
{
|
2001-08-15 22:39:42 +00:00
|
|
|
UHashtable *htable;
|
|
|
|
UDataMemory *retVal = NULL;
|
|
|
|
DataCacheElement *el;
|
2001-08-29 22:57:37 +00:00
|
|
|
const char *baseName;
|
2001-08-10 20:50:07 +00:00
|
|
|
|
2001-08-30 02:59:19 +00:00
|
|
|
baseName = findBasename(path); /* Cache remembers only the base name, not the full path. */
|
2001-08-10 20:50:07 +00:00
|
|
|
htable = udata_getHashTable();
|
2001-08-15 23:27:38 +00:00
|
|
|
umtx_lock(NULL);
|
2001-08-29 22:57:37 +00:00
|
|
|
el = (DataCacheElement *)uhash_get(htable, baseName);
|
2001-08-10 20:50:07 +00:00
|
|
|
umtx_unlock(NULL);
|
2001-08-15 22:39:42 +00:00
|
|
|
if (el != NULL) {
|
2001-09-07 18:42:29 +00:00
|
|
|
retVal = el->item;
|
2001-08-15 22:39:42 +00:00
|
|
|
}
|
2001-08-10 20:50:07 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) {
|
2001-08-10 20:50:07 +00:00
|
|
|
DataCacheElement *newElement;
|
2001-08-29 22:57:37 +00:00
|
|
|
const char *baseName;
|
|
|
|
int nameLen;
|
2001-08-15 22:39:42 +00:00
|
|
|
UHashtable *htable;
|
|
|
|
UDataMemory *oldValue = NULL;
|
|
|
|
|
|
|
|
if (U_FAILURE(*pErr)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-10 20:50:07 +00:00
|
|
|
|
|
|
|
/* Create a new DataCacheElement - the thingy we store in the hash table -
|
|
|
|
* and copy the supplied path and UDataMemoryItems into it.
|
|
|
|
*/
|
|
|
|
newElement = uprv_malloc(sizeof(DataCacheElement));
|
2001-08-15 22:39:42 +00:00
|
|
|
if (newElement == NULL) {
|
|
|
|
*pErr = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-07 18:42:29 +00:00
|
|
|
newElement->item = UDataMemory_createNewInstance(pErr);
|
|
|
|
if (U_FAILURE(*pErr)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
UDatamemory_assign(newElement->item, item);
|
2001-08-29 22:57:37 +00:00
|
|
|
|
|
|
|
baseName = findBasename(path);
|
|
|
|
nameLen = uprv_strlen(baseName);
|
|
|
|
newElement->name = uprv_malloc(nameLen+1);
|
|
|
|
if (newElement->name == NULL) {
|
2001-08-15 22:39:42 +00:00
|
|
|
*pErr = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-29 22:57:37 +00:00
|
|
|
uprv_strcpy(newElement->name, baseName);
|
2001-08-10 20:50:07 +00:00
|
|
|
|
|
|
|
/* Stick the new DataCacheElement into the hash table.
|
|
|
|
*/
|
2001-08-15 22:39:42 +00:00
|
|
|
htable = udata_getHashTable();
|
2001-08-15 23:27:38 +00:00
|
|
|
umtx_lock(NULL);
|
2001-08-15 22:39:42 +00:00
|
|
|
oldValue = uhash_get(htable, path);
|
|
|
|
if (oldValue != NULL) {
|
|
|
|
*pErr = U_USING_DEFAULT_WARNING; }
|
|
|
|
else {
|
2001-08-15 23:27:38 +00:00
|
|
|
uhash_put(
|
2001-08-15 22:39:42 +00:00
|
|
|
htable,
|
2001-08-29 22:57:37 +00:00
|
|
|
newElement->name, /* Key */
|
2001-08-15 22:39:42 +00:00
|
|
|
newElement, /* Value */
|
|
|
|
pErr);
|
|
|
|
}
|
2001-08-10 20:50:07 +00:00
|
|
|
umtx_unlock(NULL);
|
2001-08-15 22:39:42 +00:00
|
|
|
|
|
|
|
if (*pErr == U_USING_DEFAULT_WARNING || U_FAILURE(*pErr)) {
|
2001-09-18 20:24:36 +00:00
|
|
|
/* TODO - get rid of newElement->item too. */
|
2001-08-29 22:57:37 +00:00
|
|
|
uprv_free(newElement->name);
|
2001-08-15 22:39:42 +00:00
|
|
|
uprv_free(newElement);
|
|
|
|
return oldValue;
|
|
|
|
}
|
2001-08-10 20:50:07 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
return newElement->item;
|
2001-08-10 20:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
|
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/*----------------------------------------------------------------------*
|
|
|
|
* *
|
2001-09-07 18:42:29 +00:00
|
|
|
* Add a static reference to the common data library *
|
|
|
|
* Unless overridden by an explicit u_setCommonData, this will be *
|
|
|
|
* our common data. *
|
2001-08-15 22:39:42 +00:00
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------*/
|
2001-08-06 22:53:11 +00:00
|
|
|
extern const DataHeader U_IMPORT U_ICUDATA_ENTRY_POINT;
|
2001-07-26 21:55:49 +00:00
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* openCommonData Attempt to open a common format (.dat) file *
|
|
|
|
* Map it into memory (if it's not there already) *
|
|
|
|
* and return a UDataMemory object for it. *
|
2001-09-07 18:42:29 +00:00
|
|
|
* *
|
|
|
|
* If the requested data is already open and cached *
|
|
|
|
* just return the cached UDataMem object. *
|
2001-08-10 20:50:07 +00:00
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------*/
|
1999-11-22 19:43:01 +00:00
|
|
|
static UDataMemory *
|
2001-08-10 20:50:07 +00:00
|
|
|
openCommonData(
|
|
|
|
const char *path, /* Path from OpenCHoice? */
|
|
|
|
UBool isICUData, /* ICU Data true if path == NULL */
|
|
|
|
UErrorCode *pErrorCode)
|
2001-08-15 22:39:42 +00:00
|
|
|
{
|
2000-05-03 23:11:04 +00:00
|
|
|
const char *inBasename;
|
1999-11-22 19:43:01 +00:00
|
|
|
char *basename, *suffix;
|
2001-08-10 20:50:07 +00:00
|
|
|
char pathBuffer[1024];
|
|
|
|
UDataMemory tData;
|
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
if (U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
UDataMemory_init(&tData);
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
if (isICUData) {
|
|
|
|
/* "mini-cache" for common ICU data */
|
2001-09-07 18:42:29 +00:00
|
|
|
if(gCommonICUData != NULL) {
|
|
|
|
return gCommonICUData;
|
2001-08-15 22:39:42 +00:00
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
tData.pHeader = &U_ICUDATA_ENTRY_POINT;
|
2001-10-05 21:46:52 +00:00
|
|
|
udata_checkCommonData(&tData, pErrorCode);
|
2001-08-30 18:41:30 +00:00
|
|
|
setCommonICUData(&tData, NULL, FALSE, pErrorCode);
|
2001-09-07 18:42:29 +00:00
|
|
|
return gCommonICUData;
|
2000-05-03 23:11:04 +00:00
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* request is NOT for ICU Data.
|
2001-08-15 22:39:42 +00:00
|
|
|
* Is the requested data already cached? */
|
2001-09-07 18:42:29 +00:00
|
|
|
inBasename=findBasename(path);
|
2001-08-15 22:39:42 +00:00
|
|
|
{
|
2001-09-07 18:42:29 +00:00
|
|
|
UDataMemory *dataToReturn = udata_findCachedData(inBasename);
|
2001-08-10 20:50:07 +00:00
|
|
|
if (dataToReturn != NULL) {
|
|
|
|
return dataToReturn;
|
|
|
|
}
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* Requested item is not in the cache.
|
|
|
|
* Hunt it down, trying all the fall back locations.
|
|
|
|
*/
|
2000-05-03 23:11:04 +00:00
|
|
|
basename=setPathGetBasename(path, pathBuffer);
|
2001-08-15 22:39:42 +00:00
|
|
|
if(*inBasename==0) {
|
|
|
|
/* no basename, no common data */
|
|
|
|
*pErrorCode=U_FILE_ACCESS_ERROR;
|
|
|
|
return NULL;
|
2000-05-03 23:11:04 +00:00
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
/* set up the file name */
|
|
|
|
suffix=strcpy_returnEnd(basename, inBasename);
|
2001-09-07 18:42:29 +00:00
|
|
|
uprv_strcpy(suffix, ".dat");
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
/* try path/basename first, then basename only */
|
2001-08-15 22:39:42 +00:00
|
|
|
uprv_mapFile(&tData, pathBuffer);
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
if (!UDataMemory_isLoaded(&tData)) {
|
2001-08-15 22:39:42 +00:00
|
|
|
if (basename!=pathBuffer) {
|
|
|
|
uprv_mapFile(&tData, basename);
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
if (!UDataMemory_isLoaded(&tData)) {
|
2001-08-15 22:39:42 +00:00
|
|
|
/* no common data */
|
|
|
|
*pErrorCode=U_FILE_ACCESS_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* we have mapped a file, check its header */
|
2001-10-05 21:46:52 +00:00
|
|
|
udata_checkCommonData(&tData, pErrorCode);
|
2001-08-15 23:27:38 +00:00
|
|
|
|
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* Cache the UDataMemory struct for this .dat file,
|
|
|
|
* so we won't need to hunt it down and map it again next time
|
|
|
|
* something is needed from it. */
|
2001-09-07 18:42:29 +00:00
|
|
|
return udata_cacheDataItem(inBasename, &tData, pErrorCode);
|
2001-08-15 22:39:42 +00:00
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
|
1999-12-01 01:02:06 +00:00
|
|
|
|
|
|
|
|
2001-08-17 00:00:55 +00:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* extendICUData If the full set of ICU data was not loaded at *
|
|
|
|
* program startup, load it now. This function will *
|
|
|
|
* be called when the lookup of an ICU data item in *
|
|
|
|
* the common ICU data fails. *
|
|
|
|
* *
|
2001-08-30 18:41:30 +00:00
|
|
|
* The parameter is the UDataMemory in which the *
|
|
|
|
* search for a requested item failed. *
|
|
|
|
* *
|
2001-08-17 00:00:55 +00:00
|
|
|
* return true if new data is loaded, false otherwise.*
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------*/
|
2001-08-30 18:41:30 +00:00
|
|
|
static UBool extendICUData(UDataMemory *failedData, UErrorCode *pErr)
|
2001-08-17 00:00:55 +00:00
|
|
|
{
|
2001-09-07 18:42:29 +00:00
|
|
|
/* If the data library that we are running with turns out to be the
|
|
|
|
* stub library (or, on the 390, the subset library), we will try to
|
2001-08-30 18:41:30 +00:00
|
|
|
* load a .dat file instead. The stub library has no entries in its
|
|
|
|
* TOC, which is how we identify it here.
|
|
|
|
*/
|
|
|
|
UDataMemory *pData;
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
if (failedData->vFuncs->NumEntries(failedData) > 0) {
|
2001-08-30 18:41:30 +00:00
|
|
|
/* Not the stub. We can't extend. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if we can explicitly open a .dat file for the ICUData. */
|
|
|
|
pData = openCommonData(
|
|
|
|
U_ICUDATA_NAME, /* "icudt20l" , for example. */
|
|
|
|
FALSE, /* Pretend we're not opening ICUData */
|
|
|
|
pErr);
|
|
|
|
|
|
|
|
|
2001-10-05 21:46:52 +00:00
|
|
|
pData->map = 0; /* The mapping for this data is owned by the hash table */
|
|
|
|
pData->mapAddr = 0; /* which will unmap it when ICU is shut down. */
|
|
|
|
/* CommonICUData is also unmapped when ICU is shut down.*/
|
|
|
|
/* To avoid unmapping the data twice, zero out the map */
|
|
|
|
/* fields in the UDataMemory that we're assigning */
|
|
|
|
/* to CommonICUData. */
|
|
|
|
|
|
|
|
|
2001-08-30 18:41:30 +00:00
|
|
|
setCommonICUData(pData, /* The new common data. */
|
|
|
|
failedData, /* Old ICUData ptr. Overwrite of this value is ok, */
|
|
|
|
FALSE, /* No warnings if write didn't happen */
|
|
|
|
pErr); /* setCommonICUData honors errors; NOP if error set */
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
return gCommonICUData != failedData; /* Return true if ICUData pointer was updated. */
|
2001-08-30 18:41:30 +00:00
|
|
|
/* (Could potentialy 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. */
|
2001-08-17 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/*----------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* udata_setCommonData *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------*/
|
2000-05-03 23:11:04 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_setCommonData(const void *data, UErrorCode *pErrorCode) {
|
2001-08-15 22:39:42 +00:00
|
|
|
UDataMemory dataMemory;
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
1999-12-01 01:02:06 +00:00
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
if(data==NULL) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
/* do we already have common ICU data set? */
|
2001-09-07 18:42:29 +00:00
|
|
|
if(gCommonICUData != NULL) {
|
2000-05-03 23:11:04 +00:00
|
|
|
*pErrorCode=U_USING_DEFAULT_ERROR;
|
|
|
|
return;
|
2001-08-15 22:39:42 +00:00
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/* set the data pointer and test for validity */
|
2001-08-15 22:39:42 +00:00
|
|
|
UDataMemory_init(&dataMemory);
|
2001-09-07 18:42:29 +00:00
|
|
|
UDataMemory_setData(&dataMemory, data);
|
2001-10-05 21:46:52 +00:00
|
|
|
udata_checkCommonData(&dataMemory, pErrorCode);
|
2001-08-15 22:39:42 +00:00
|
|
|
if (U_FAILURE(*pErrorCode)) {return;}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-30 18:41:30 +00:00
|
|
|
/* we have good data */
|
|
|
|
/* Set it up as the ICU Common Data. */
|
|
|
|
setCommonICUData(&dataMemory, NULL, TRUE, pErrorCode);
|
2000-05-03 23:11:04 +00:00
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* udata_setAppData
|
|
|
|
*
|
|
|
|
*---------------------------------------------------------------------------- */
|
2001-08-15 23:27:38 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-10 20:50:07 +00:00
|
|
|
udata_setAppData(const char *path, const void *data, UErrorCode *err)
|
|
|
|
{
|
2001-08-15 22:39:42 +00:00
|
|
|
UDataMemory udm;
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
if(err==NULL || U_FAILURE(*err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(data==NULL) {
|
|
|
|
*err=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
UDataMemory_init(&udm);
|
|
|
|
udm.pHeader = data;
|
2001-10-05 21:46:52 +00:00
|
|
|
udata_checkCommonData(&udm, err);
|
2001-08-15 22:39:42 +00:00
|
|
|
udata_cacheDataItem(path, &udm, err);
|
2001-08-17 00:18:49 +00:00
|
|
|
}
|
2001-08-10 20:50:07 +00:00
|
|
|
|
2001-08-17 00:18:49 +00:00
|
|
|
/*----------------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* checkDataItem Given a freshly located/loaded data item, either *
|
|
|
|
* an entry in a common file or a separately loaded file, *
|
|
|
|
* sanity check its header, and see if the data is *
|
|
|
|
* acceptable to the app. *
|
|
|
|
* If the data is good, create and return a UDataMemory *
|
|
|
|
* object that can be returned to the application. *
|
|
|
|
* Return NULL on any sort of failure. *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------------*/
|
2001-08-15 22:39:42 +00:00
|
|
|
static UDataMemory *
|
2001-08-15 23:27:38 +00:00
|
|
|
checkDataItem
|
2001-08-15 22:39:42 +00:00
|
|
|
(
|
|
|
|
const DataHeader *pHeader, /* The data item to be checked. */
|
|
|
|
UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */
|
|
|
|
void *context, /* pass-thru param for above. */
|
|
|
|
const char *type, /* pass-thru param for above. */
|
|
|
|
const char *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 = NULL; /* the new UDataMemory, to be returned. */
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
if (U_FAILURE(*fatalErr)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pHeader->dataHeader.magic1==0xda &&
|
|
|
|
pHeader->dataHeader.magic2==0x27 &&
|
|
|
|
pHeader->info.isBigEndian==U_IS_BIG_ENDIAN &&
|
|
|
|
(isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info))
|
|
|
|
) {
|
|
|
|
rDataMem=UDataMemory_createNewInstance(fatalErr);
|
|
|
|
if (U_FAILURE(*fatalErr)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A note on the ownership of Mapped Memory
|
|
|
|
*
|
|
|
|
* For common format files, ownership resides with the UDataMemory object
|
|
|
|
* that lives in the cache of opened common data. These UDataMemorys are private
|
|
|
|
* to the udata implementation, and are never seen directly by users.
|
|
|
|
*
|
|
|
|
* The UDataMemory objects returned to users will have the address of some desired
|
|
|
|
* data within the mapped region, but they wont have the mapping info itself, and thus
|
|
|
|
* won't cause anything to be removed from memory when they are closed.
|
|
|
|
*
|
|
|
|
* For individual data files, the UDataMemory returned to the user holds the
|
|
|
|
* information necessary to unmap the data on close. If the user independently
|
|
|
|
* opens the same data file twice, two completely independent mappings will be made.
|
2001-09-07 18:42:29 +00:00
|
|
|
* (There is no cache of opened data items from individual files, only a cache of
|
|
|
|
* opened Common Data files, that is, files containing a collection of data items.)
|
2001-08-15 22:39:42 +00:00
|
|
|
*
|
|
|
|
* For common data passed in from the user via udata_setAppData() or
|
|
|
|
* udata_setCommonData(), ownership remains with the user.
|
|
|
|
*
|
|
|
|
* UDataMemory objects themselves, as opposed to the memory they describe,
|
2001-08-15 23:27:38 +00:00
|
|
|
* can be anywhere - heap, stack/local or global.
|
2001-09-07 18:42:29 +00:00
|
|
|
* They have a flag to indicate when they're heap allocated and thus
|
2001-08-15 22:39:42 +00:00
|
|
|
* must be deleted when closed.
|
|
|
|
*/
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/*----------------------------------------------------------------------------*
|
|
|
|
* *
|
|
|
|
* main data loading functions *
|
|
|
|
* *
|
|
|
|
*----------------------------------------------------------------------------*/
|
2000-05-03 23:11:04 +00:00
|
|
|
static UDataMemory *
|
|
|
|
doOpenChoice(const char *path, const char *type, const char *name,
|
|
|
|
UDataMemoryIsAcceptable *isAcceptable, void *context,
|
2001-08-15 23:27:38 +00:00
|
|
|
UErrorCode *pErrorCode)
|
2001-08-15 22:39:42 +00:00
|
|
|
{
|
|
|
|
char pathBuffer[1024];
|
|
|
|
char tocEntryName[100];
|
|
|
|
UDataMemory dataMemory;
|
2001-08-15 23:27:38 +00:00
|
|
|
UDataMemory *pCommonData;
|
2001-08-15 22:39:42 +00:00
|
|
|
UDataMemory *pEntryData;
|
|
|
|
const DataHeader *pHeader;
|
|
|
|
const char *inBasename;
|
2001-08-15 23:27:38 +00:00
|
|
|
char *basename;
|
2001-08-15 22:39:42 +00:00
|
|
|
char *suffix;
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
UBool isICUData= (UBool)(path==NULL);
|
2000-05-03 23:11:04 +00:00
|
|
|
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/* Make up a full mame by appending the type to the supplied
|
|
|
|
* name, assuming that a type was supplied.
|
|
|
|
*/
|
|
|
|
uprv_strcpy(tocEntryName, name);
|
|
|
|
if(type!=NULL && *type!=0) {
|
|
|
|
uprv_strcat(tocEntryName, ".");
|
|
|
|
uprv_strcat(tocEntryName, type);
|
|
|
|
}
|
2000-12-05 02:28:32 +00:00
|
|
|
|
2001-08-17 00:00:55 +00:00
|
|
|
/* try to get common data. The loop is for platforms such as the 390 that do
|
|
|
|
* not initially load the full set of ICU data. If the lookup of an ICU data item
|
|
|
|
* fails, the full (but slower to load) set is loaded, the and the loop repeats,
|
|
|
|
* trying the lookup again. Once the full set of ICU data is loaded, the loop wont
|
2001-09-07 18:42:29 +00:00
|
|
|
* repeat because the full set will be checked the first time through.
|
|
|
|
*
|
|
|
|
* The loop also handles the fallback to a .dat file if the application linked
|
|
|
|
* to the stub data library rather than a real library.
|
|
|
|
*/
|
2001-08-17 00:00:55 +00:00
|
|
|
for (;;) {
|
|
|
|
pCommonData=openCommonData(path, isICUData, &errorCode);
|
2001-08-30 18:41:30 +00:00
|
|
|
|
2001-08-17 00:00:55 +00:00
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
/* look up the data piece in the common data */
|
2001-09-07 18:42:29 +00:00
|
|
|
pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &errorCode);
|
2001-08-17 00:00:55 +00:00
|
|
|
if(pHeader!=NULL) {
|
|
|
|
pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, &errorCode, pErrorCode);
|
|
|
|
if (U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pEntryData != NULL) {
|
|
|
|
return pEntryData;
|
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-30 18:41:30 +00:00
|
|
|
/* Data wasn't found. If we were looking for an ICUData item and there is
|
2001-08-17 00:00:55 +00:00
|
|
|
* more data available, load it and try again,
|
|
|
|
* otherwise break out of this loop. */
|
2001-08-30 18:41:30 +00:00
|
|
|
if (!(isICUData && extendICUData(pCommonData, &errorCode))) {
|
2001-08-17 00:00:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
/* the data was not found in the common data, look further, */
|
2000-05-03 23:11:04 +00:00
|
|
|
/* try to get an individual data file */
|
|
|
|
basename=setPathGetBasename(path, pathBuffer);
|
|
|
|
if(isICUData) {
|
|
|
|
inBasename=COMMON_DATA_NAME;
|
|
|
|
} else {
|
|
|
|
inBasename=findBasename(path);
|
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2000-11-04 02:16:12 +00:00
|
|
|
#ifdef UDATA_DEBUG
|
|
|
|
fprintf(stderr, "looking for ind. file\n");
|
|
|
|
#endif
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
/* try path+basename+"_"+entryName first */
|
2000-06-05 17:24:34 +00:00
|
|
|
if(*inBasename!=0) {
|
2000-05-03 23:11:04 +00:00
|
|
|
suffix=strcpy_returnEnd(basename, inBasename);
|
|
|
|
*suffix++='_';
|
|
|
|
uprv_strcpy(suffix, tocEntryName);
|
|
|
|
|
2001-08-10 20:50:07 +00:00
|
|
|
if( uprv_mapFile(&dataMemory, pathBuffer) ||
|
2001-08-15 23:27:38 +00:00
|
|
|
(basename!=pathBuffer && uprv_mapFile(&dataMemory, basename)))
|
2001-08-15 22:39:42 +00:00
|
|
|
{
|
|
|
|
/* We mapped a file. Check out its contents. */
|
|
|
|
pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, &errorCode, pErrorCode);
|
2001-08-15 23:27:38 +00:00
|
|
|
if (pEntryData != NULL)
|
2001-08-15 22:39:42 +00:00
|
|
|
{
|
|
|
|
/* Got good data.
|
|
|
|
* Hand off ownership of the backing memory to the user's UDataMemory.
|
|
|
|
* and return it. */
|
2001-08-15 23:27:38 +00:00
|
|
|
pEntryData->mapAddr = dataMemory.mapAddr;
|
2001-08-15 22:39:42 +00:00
|
|
|
pEntryData->map = dataMemory.map;
|
2000-05-03 23:11:04 +00:00
|
|
|
return pEntryData;
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* the data is not acceptable, or some error occured. Either way, unmap the memory */
|
2001-09-07 18:42:29 +00:00
|
|
|
udata_close(&dataMemory);
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* If we had a nasty error, bail out completely. */
|
|
|
|
if (U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* Otherwise remember that we found data but didn't like it for some reason,
|
|
|
|
* and continue looking
|
|
|
|
*/
|
|
|
|
errorCode=U_INVALID_FORMAT_ERROR;
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
/* try path+entryName next */
|
|
|
|
uprv_strcpy(basename, tocEntryName);
|
2001-08-10 20:50:07 +00:00
|
|
|
if( uprv_mapFile(&dataMemory, pathBuffer) ||
|
2001-08-15 23:27:38 +00:00
|
|
|
(basename!=pathBuffer && uprv_mapFile(&dataMemory, basename)))
|
2001-08-15 22:39:42 +00:00
|
|
|
{
|
|
|
|
pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, &errorCode, pErrorCode);
|
|
|
|
if (pEntryData != NULL) {
|
|
|
|
/* Data is good.
|
|
|
|
* Hand off ownership of the backing memory to the user's UDataMemory.
|
|
|
|
* and return it. */
|
2001-08-15 23:27:38 +00:00
|
|
|
pEntryData->mapAddr = dataMemory.mapAddr;
|
2001-08-15 22:39:42 +00:00
|
|
|
pEntryData->map = dataMemory.map;
|
2000-05-03 23:11:04 +00:00
|
|
|
return pEntryData;
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* the data is not acceptable, or some error occured. Either way, unmap the memory */
|
2001-09-07 18:42:29 +00:00
|
|
|
udata_close(&dataMemory);
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* If we had a nasty error, bail out completely. */
|
|
|
|
if (U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-15 23:27:38 +00:00
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
/* Otherwise remember that we found data but didn't like it for some reason */
|
|
|
|
errorCode=U_INVALID_FORMAT_ERROR;
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* data not found */
|
|
|
|
if(U_SUCCESS(*pErrorCode)) {
|
1999-11-22 19:53:19 +00:00
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
/* file not found */
|
|
|
|
*pErrorCode=U_FILE_ACCESS_ERROR;
|
|
|
|
} else {
|
|
|
|
/* entry point not found or rejected */
|
|
|
|
*pErrorCode=errorCode;
|
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-15 22:39:42 +00:00
|
|
|
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
/* API ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
U_CAPI UDataMemory * U_EXPORT2
|
|
|
|
udata_open(const char *path, const char *type, const char *name,
|
|
|
|
UErrorCode *pErrorCode) {
|
2000-05-30 21:47:19 +00:00
|
|
|
#ifdef UDATA_DEBUG
|
2000-12-05 02:28:32 +00:00
|
|
|
fprintf(stderr, "udata_open(): Opening: %s . %s\n", name, type);
|
|
|
|
fflush(stderr);
|
2000-05-30 21:47:19 +00:00
|
|
|
#endif
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
} else if(name==NULL || *name==0) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return doOpenChoice(path, type, name, NULL, NULL, pErrorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-07 18:42:29 +00:00
|
|
|
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
U_CAPI UDataMemory * U_EXPORT2
|
|
|
|
udata_openChoice(const char *path, const char *type, const char *name,
|
|
|
|
UDataMemoryIsAcceptable *isAcceptable, void *context,
|
|
|
|
UErrorCode *pErrorCode) {
|
2000-11-04 02:16:12 +00:00
|
|
|
#ifdef UDATA_DEBUG
|
|
|
|
fprintf(stderr, "udata_openChoice(): Opening: %s . %s\n", name, type);fflush(stderr);
|
|
|
|
#endif
|
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
} else if(name==NULL || *name==0 || isAcceptable==NULL) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
1999-11-22 19:43:01 +00:00
|
|
|
return NULL;
|
2000-05-03 23:11:04 +00:00
|
|
|
} else {
|
|
|
|
return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode);
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|
2000-05-03 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 19:43:01 +00:00
|
|
|
|
2000-05-03 23:11:04 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) {
|
|
|
|
if(pInfo!=NULL) {
|
|
|
|
if(pData!=NULL && pData->pHeader!=NULL) {
|
|
|
|
const UDataInfo *info=&pData->pHeader->info;
|
2001-05-25 21:50:10 +00:00
|
|
|
if(pInfo->size>info->size) {
|
2000-05-03 23:11:04 +00:00
|
|
|
pInfo->size=info->size;
|
|
|
|
}
|
2001-05-25 21:50:10 +00:00
|
|
|
uprv_memcpy((uint16_t *)pInfo+1, (uint16_t *)info+1, pInfo->size-2);
|
2000-05-03 23:11:04 +00:00
|
|
|
} else {
|
|
|
|
pInfo->size=0;
|
|
|
|
}
|
|
|
|
}
|
1999-11-22 19:43:01 +00:00
|
|
|
}
|