1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-12-04 02:31:40 +00:00
|
|
|
********************************************************************
|
|
|
|
* COPYRIGHT:
|
2001-03-21 20:44:20 +00:00
|
|
|
* Copyright (c) 1996-2001, International Business Machines Corporation and
|
1999-12-04 02:31:40 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* uconv_bld.c:
|
|
|
|
*
|
|
|
|
* Defines functions that are used in the creation/initialization/deletion
|
|
|
|
* of converters and related structures.
|
|
|
|
* uses uconv_io.h routines to access disk information
|
|
|
|
* is used by ucnv.h to implement public API create/delete/flushCache routines
|
2000-06-20 23:42:20 +00:00
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
*
|
|
|
|
* 06/20/2000 helena OS/400 port changes; mostly typecast.
|
2000-06-27 20:47:56 +00:00
|
|
|
* 06/29/2000 helena Major rewrite of the callback interface.
|
2000-06-20 23:42:20 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "ucnv_io.h"
|
|
|
|
#include "uhash.h"
|
2000-06-27 20:47:56 +00:00
|
|
|
#include "ucnv_bld.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
2000-02-05 00:19:15 +00:00
|
|
|
#include "ucnv_cnv.h"
|
1999-10-18 22:48:32 +00:00
|
|
|
#include "ucnv_imp.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/udata.h"
|
|
|
|
#include "unicode/ucnv.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "umutex.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "filestrm.h"
|
2001-08-11 00:29:58 +00:00
|
|
|
#include "ucln_cmn.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2000-08-14 23:11:00 +00:00
|
|
|
#if 0
|
2001-02-06 22:26:52 +00:00
|
|
|
#include <stdio.h>
|
2000-08-14 23:11:00 +00:00
|
|
|
extern void UCNV_DEBUG_LOG(char *what, char *who, void *p, int l);
|
|
|
|
#define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
|
|
|
|
#else
|
|
|
|
# define UCNV_DEBUG_LOG(x,y,z)
|
|
|
|
#endif
|
|
|
|
|
2001-08-24 00:22:07 +00:00
|
|
|
static const UConverterSharedData * const
|
2000-02-05 00:19:15 +00:00
|
|
|
converterData[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES]={
|
2001-01-02 23:09:45 +00:00
|
|
|
NULL, NULL, &_MBCSData, &_Latin1Data,
|
2000-11-16 17:20:03 +00:00
|
|
|
&_UTF8Data, &_UTF16BEData, &_UTF16LEData, &_UTF32BEData, &_UTF32LEData,
|
2001-01-02 23:09:45 +00:00
|
|
|
NULL, &_ISO2022Data,
|
2000-03-31 16:53:09 +00:00
|
|
|
&_LMBCSData1,&_LMBCSData2, &_LMBCSData3, &_LMBCSData4, &_LMBCSData5, &_LMBCSData6,
|
2000-10-17 08:03:36 +00:00
|
|
|
&_LMBCSData8,&_LMBCSData11,&_LMBCSData16,&_LMBCSData17,&_LMBCSData18,&_LMBCSData19,
|
2001-07-14 02:29:44 +00:00
|
|
|
&_HZData, &_SCSUData, &_ISCIIData, &_ASCIIData, &_UTF7Data
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
2001-08-24 00:22:07 +00:00
|
|
|
const UConverterType type;
|
|
|
|
} const cnvNameType[] = {
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "ISO-8859-1", UCNV_LATIN_1 },
|
|
|
|
{ "UTF-8", UCNV_UTF8 },
|
|
|
|
{ "UTF-16BE", UCNV_UTF16_BigEndian },
|
|
|
|
{ "UTF-16LE", UCNV_UTF16_LittleEndian },
|
2000-02-05 00:19:15 +00:00
|
|
|
#if U_IS_BIG_ENDIAN
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "UTF-16", UCNV_UTF16_BigEndian },
|
2000-02-05 00:19:15 +00:00
|
|
|
{ "UTF16_OppositeEndian", UCNV_UTF16_LittleEndian },
|
|
|
|
#else
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "UTF-16", UCNV_UTF16_LittleEndian },
|
2000-02-05 00:19:15 +00:00
|
|
|
{ "UTF16_OppositeEndian", UCNV_UTF16_BigEndian},
|
2000-11-16 17:20:03 +00:00
|
|
|
#endif
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "UTF-32BE", UCNV_UTF32_BigEndian },
|
|
|
|
{ "UTF-32LE", UCNV_UTF32_LittleEndian },
|
2000-11-16 17:20:03 +00:00
|
|
|
#if U_IS_BIG_ENDIAN
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "UTF-32", UCNV_UTF32_BigEndian },
|
2000-11-16 17:20:03 +00:00
|
|
|
{ "UTF32_OppositeEndian", UCNV_UTF32_LittleEndian },
|
|
|
|
#else
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "UTF-32", UCNV_UTF32_LittleEndian },
|
2000-11-16 17:20:03 +00:00
|
|
|
{ "UTF32_OppositeEndian", UCNV_UTF32_BigEndian},
|
2000-02-05 00:19:15 +00:00
|
|
|
#endif
|
2000-08-14 23:57:20 +00:00
|
|
|
{ "ISO_2022", UCNV_ISO_2022 },
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "LMBCS-1", UCNV_LMBCS_1 },
|
|
|
|
{ "LMBCS-2", UCNV_LMBCS_2 },
|
|
|
|
{ "LMBCS-3", UCNV_LMBCS_3 },
|
|
|
|
{ "LMBCS-4", UCNV_LMBCS_4 },
|
|
|
|
{ "LMBCS-5", UCNV_LMBCS_5 },
|
|
|
|
{ "LMBCS-6", UCNV_LMBCS_6 },
|
|
|
|
{ "LMBCS-8", UCNV_LMBCS_8 },
|
|
|
|
{ "LMBCS-11",UCNV_LMBCS_11 },
|
|
|
|
{ "LMBCS-16",UCNV_LMBCS_16 },
|
|
|
|
{ "LMBCS-17",UCNV_LMBCS_17 },
|
|
|
|
{ "LMBCS-18",UCNV_LMBCS_18 },
|
|
|
|
{ "LMBCS-19",UCNV_LMBCS_19 },
|
2000-12-19 01:24:45 +00:00
|
|
|
{ "HZ",UCNV_HZ },
|
2000-12-20 02:08:39 +00:00
|
|
|
{ "SCSU", UCNV_SCSU },
|
2001-07-14 02:29:44 +00:00
|
|
|
{ "ISCII", UCNV_ISCII },
|
2001-09-28 18:10:49 +00:00
|
|
|
{ "US-ASCII", UCNV_US_ASCII },
|
|
|
|
{ "UTF-7", UCNV_UTF7 }
|
1999-10-18 22:48:32 +00:00
|
|
|
};
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-31 16:53:09 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
/*Takes an alias name gets an actual converter file name
|
1999-08-16 21:50:52 +00:00
|
|
|
*goes to disk and opens it.
|
|
|
|
*allocates the memory and returns a new UConverter object
|
|
|
|
*/
|
2000-02-09 19:15:17 +00:00
|
|
|
static UConverterSharedData *createConverterFromFile (const char *converterName, UErrorCode * err);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
static const UConverterSharedData *getAlgorithmicTypeFromName (const char *realName);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-04 02:31:40 +00:00
|
|
|
/**
|
|
|
|
* Un flatten shared data from a UDATA..
|
|
|
|
*/
|
2000-02-08 23:41:16 +00:00
|
|
|
U_CAPI UConverterSharedData* U_EXPORT2 ucnv_data_unFlattenClone(UDataMemory *pData, UErrorCode *status);
|
1999-12-04 02:31:40 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*initializes some global variables */
|
2001-10-04 01:04:28 +00:00
|
|
|
static UHashtable *SHARED_DATA_HASHTABLE = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
/* The calling function uses the global mutex, so there is no need to use it here too */
|
2001-08-25 01:09:11 +00:00
|
|
|
UBool ucnv_cleanup(void) {
|
2001-08-11 00:29:58 +00:00
|
|
|
if (SHARED_DATA_HASHTABLE != NULL) {
|
|
|
|
ucnv_flushCache();
|
|
|
|
if (SHARED_DATA_HASHTABLE != NULL && uhash_count(SHARED_DATA_HASHTABLE) == 0) {
|
|
|
|
uhash_close(SHARED_DATA_HASHTABLE);
|
|
|
|
SHARED_DATA_HASHTABLE = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (SHARED_DATA_HASHTABLE == NULL);
|
2000-08-14 23:11:00 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
static UBool
|
1999-12-04 02:31:40 +00:00
|
|
|
isCnvAcceptable(void *context,
|
|
|
|
const char *type, const char *name,
|
2000-05-03 23:15:02 +00:00
|
|
|
const UDataInfo *pInfo) {
|
2000-08-10 01:59:18 +00:00
|
|
|
return (UBool)(
|
1999-12-04 02:31:40 +00:00
|
|
|
pInfo->size>=20 &&
|
|
|
|
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
|
|
|
|
pInfo->charsetFamily==U_CHARSET_FAMILY &&
|
|
|
|
pInfo->sizeofUChar==U_SIZEOF_UCHAR &&
|
|
|
|
pInfo->dataFormat[0]==0x63 && /* dataFormat="cnvt" */
|
|
|
|
pInfo->dataFormat[1]==0x6e &&
|
|
|
|
pInfo->dataFormat[2]==0x76 &&
|
|
|
|
pInfo->dataFormat[3]==0x74 &&
|
2000-10-03 20:18:22 +00:00
|
|
|
pInfo->formatVersion[0]==6);
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DATA_TYPE "cnv"
|
|
|
|
|
2000-08-11 01:27:17 +00:00
|
|
|
static UConverterSharedData *createConverterFromFile (const char *fileName, UErrorCode * err)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UDataMemory *data;
|
|
|
|
UConverterSharedData *sharedData;
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
if (err == NULL || U_FAILURE (*err)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
data = udata_openChoice(NULL, DATA_TYPE, fileName, isCnvAcceptable, NULL, err);
|
|
|
|
if(U_FAILURE(*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
sharedData = ucnv_data_unFlattenClone(data, err);
|
|
|
|
if(U_FAILURE(*err))
|
1999-12-04 02:31:40 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
udata_close(data);
|
|
|
|
return NULL;
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
return sharedData;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-10-04 01:04:28 +00:00
|
|
|
ucnv_copyPlatformString(char *platformString, UConverterPlatform pltfrm)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
switch (pltfrm)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
case UCNV_IBM:
|
2001-08-11 00:29:58 +00:00
|
|
|
{
|
|
|
|
uprv_strcpy (platformString, "ibm");
|
|
|
|
break;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
default:
|
2001-08-11 00:29:58 +00:00
|
|
|
{
|
|
|
|
uprv_strcpy (platformString, "");
|
|
|
|
break;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
};
|
2001-08-11 00:29:58 +00:00
|
|
|
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*returns a converter type from a string
|
|
|
|
*/
|
2000-08-11 01:27:17 +00:00
|
|
|
static const UConverterSharedData *
|
2001-08-11 00:29:58 +00:00
|
|
|
getAlgorithmicTypeFromName(const char *realName)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<sizeof(cnvNameType)/sizeof(cnvNameType[0]); ++i) {
|
|
|
|
if(uprv_strcmp(realName, cnvNameType[i].name)==0) {
|
|
|
|
return converterData[cnvNameType[i].type];
|
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2001-08-11 00:29:58 +00:00
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
|
2001-10-04 01:14:52 +00:00
|
|
|
void
|
|
|
|
ucnv_shareConverterData(UConverterSharedData * data)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
/*Lazy evaluates the Hashtable itself */
|
|
|
|
/*void *sanity = NULL;*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
if (SHARED_DATA_HASHTABLE == NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UHashtable* myHT = uhash_openSize (uhash_hashIChars, uhash_compareIChars,
|
|
|
|
ucnv_io_countAvailableAliases(&err),
|
|
|
|
&err);
|
|
|
|
if (U_FAILURE(err))
|
|
|
|
return;
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if (SHARED_DATA_HASHTABLE == NULL)
|
|
|
|
SHARED_DATA_HASHTABLE = myHT;
|
|
|
|
else
|
|
|
|
uhash_close(myHT);
|
|
|
|
umtx_unlock(NULL);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-08-11 00:29:58 +00:00
|
|
|
umtx_lock (NULL);
|
|
|
|
/* ### check to see if the element is not already there! */
|
2000-08-14 23:11:00 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
/*
|
2001-10-04 01:14:52 +00:00
|
|
|
sanity = ucnv_getSharedConverterData (data->staticData->name);
|
2000-08-14 23:11:00 +00:00
|
|
|
if(sanity != NULL)
|
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
|
2000-08-14 23:11:00 +00:00
|
|
|
}
|
|
|
|
UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
|
2001-08-11 00:29:58 +00:00
|
|
|
*/
|
2000-08-14 23:11:00 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
uhash_put(SHARED_DATA_HASHTABLE,
|
|
|
|
(void*) data->staticData->name, /* Okay to cast away const as long as
|
|
|
|
keyDeleter == NULL */
|
1999-12-04 00:55:54 +00:00
|
|
|
data,
|
|
|
|
&err);
|
2001-08-11 00:29:58 +00:00
|
|
|
UCNV_DEBUG_LOG("put", data->staticData->name,data);
|
|
|
|
umtx_unlock (NULL);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-10-04 01:04:28 +00:00
|
|
|
UConverterSharedData *
|
2001-10-04 01:14:52 +00:00
|
|
|
ucnv_getSharedConverterData(const char *name)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
/*special case when no Table has yet been created we return NULL */
|
|
|
|
if (SHARED_DATA_HASHTABLE == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UConverterSharedData *rc;
|
2000-08-14 23:11:00 +00:00
|
|
|
|
|
|
|
umtx_lock(NULL);
|
2001-08-11 00:29:58 +00:00
|
|
|
rc = (UConverterSharedData*)uhash_get(SHARED_DATA_HASHTABLE, name);
|
2000-08-14 23:11:00 +00:00
|
|
|
umtx_unlock(NULL);
|
2001-08-11 00:29:58 +00:00
|
|
|
UCNV_DEBUG_LOG("get",name,rc);
|
|
|
|
return rc;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*frees the string of memory blocks associates with a sharedConverter
|
|
|
|
*if and only if the referenceCounter == 0
|
|
|
|
*/
|
2001-10-04 01:04:28 +00:00
|
|
|
UBool
|
2001-10-04 01:14:52 +00:00
|
|
|
ucnv_deleteSharedConverterData(UConverterSharedData * deadSharedData)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-12-04 02:31:40 +00:00
|
|
|
if (deadSharedData->referenceCounter > 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
if (deadSharedData->impl->unload != NULL) {
|
|
|
|
deadSharedData->impl->unload(deadSharedData);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-04 02:31:40 +00:00
|
|
|
if(deadSharedData->dataMemory != NULL)
|
|
|
|
{
|
|
|
|
UDataMemory *data = (UDataMemory*)deadSharedData->dataMemory;
|
|
|
|
udata_close(data);
|
|
|
|
}
|
|
|
|
|
2000-08-14 23:11:00 +00:00
|
|
|
if(deadSharedData->table != NULL)
|
|
|
|
{
|
|
|
|
uprv_free(deadSharedData->table);
|
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
uprv_free(deadSharedData);
|
1999-12-04 02:31:40 +00:00
|
|
|
|
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-22 20:15:09 +00:00
|
|
|
static void
|
|
|
|
parseConverterOptions(const char *inName,
|
|
|
|
char *cnvName, char *locale, uint32_t *pFlags) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
/* copy the converter name itself to cnvName */
|
|
|
|
while((c=*inName)!=0 && c!=UCNV_OPTION_SEP_CHAR) {
|
|
|
|
*cnvName++=c;
|
|
|
|
++inName;
|
|
|
|
}
|
|
|
|
*cnvName=0;
|
|
|
|
|
|
|
|
/* parse options */
|
|
|
|
if(c==UCNV_OPTION_SEP_CHAR) {
|
|
|
|
++inName;
|
|
|
|
for(;;) {
|
|
|
|
/* inName is behind an option separator */
|
|
|
|
if(uprv_strncmp(inName, "locale=", 7)==0) {
|
|
|
|
/* do not modify locale itself in case we have multiple locale options */
|
|
|
|
char *dest=locale;
|
|
|
|
|
|
|
|
/* copy the locale option value */
|
|
|
|
inName+=7;
|
|
|
|
for(;;) {
|
|
|
|
c=*inName;
|
|
|
|
if(c!=0) {
|
|
|
|
++inName;
|
|
|
|
if(c!=UCNV_OPTION_SEP_CHAR) {
|
|
|
|
*dest++=c;
|
|
|
|
} else {
|
|
|
|
*dest=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*dest=0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-01-09 03:33:07 +00:00
|
|
|
} else if(uprv_strncmp(inName, "version=", 8)==0) {
|
|
|
|
/* copy the version option value into bits 3..0 of *pFlags */
|
|
|
|
inName+=8;
|
|
|
|
c=*inName;
|
|
|
|
*pFlags=0;
|
|
|
|
if(c==0) {
|
|
|
|
return;
|
|
|
|
} else if((uint8_t)(c-'0')<10) {
|
|
|
|
*pFlags=c-'0';
|
|
|
|
++inName;
|
|
|
|
}
|
|
|
|
/* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
|
2000-05-22 20:15:09 +00:00
|
|
|
} else {
|
|
|
|
/* ignore any other options until we define some */
|
2001-01-09 03:33:07 +00:00
|
|
|
do {
|
|
|
|
c=*inName++;
|
|
|
|
if(c==0) {
|
2000-05-22 20:15:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-01-09 03:33:07 +00:00
|
|
|
} while(c!=UCNV_OPTION_SEP_CHAR);
|
2000-05-22 20:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*Logic determines if the converter is Algorithmic AND/OR cached
|
|
|
|
*depending on that:
|
|
|
|
* -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
|
|
|
|
* -Get it from a Hashtable (Data=X, Cached=TRUE)
|
|
|
|
* -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
|
|
|
|
* -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
|
|
|
|
*/
|
|
|
|
UConverter *
|
2001-10-04 01:04:28 +00:00
|
|
|
ucnv_createConverter (const char *converterName, UErrorCode * err)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
char cnvName[100], locale[20];
|
|
|
|
const char *realName;
|
|
|
|
UConverter *myUConverter = NULL;
|
|
|
|
UConverterSharedData *mySharedConverterData = NULL;
|
|
|
|
UErrorCode internalErrorCode = U_ZERO_ERROR;
|
|
|
|
uint32_t options=0;
|
|
|
|
if (U_FAILURE (*err))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
locale[0] = 0;
|
|
|
|
|
|
|
|
/* In case "name" is NULL we want to open the default converter. */
|
|
|
|
if (converterName == NULL) {
|
|
|
|
realName = ucnv_io_getDefaultConverterName();
|
|
|
|
if (realName == NULL) {
|
|
|
|
*err = U_MISSING_RESOURCE_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* the default converter name is already canonical */
|
|
|
|
} else {
|
|
|
|
/* separate the converter name from the options */
|
|
|
|
parseConverterOptions(converterName, cnvName, locale,&options);
|
|
|
|
|
|
|
|
/* get the canonical converter name */
|
|
|
|
realName = ucnv_io_getConverterName(cnvName, &internalErrorCode);
|
|
|
|
if (U_FAILURE(internalErrorCode) || realName == NULL) {
|
|
|
|
/*
|
|
|
|
* set the input name in case the converter was added
|
|
|
|
* without updating the alias table, or when there is no alias table
|
|
|
|
*/
|
|
|
|
realName = cnvName;
|
|
|
|
}
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
2001-08-11 00:29:58 +00:00
|
|
|
|
2000-05-22 20:15:09 +00:00
|
|
|
/* separate the converter name from the options */
|
2001-08-11 00:29:58 +00:00
|
|
|
if(realName != cnvName) {
|
|
|
|
parseConverterOptions(realName, cnvName, locale,&options);
|
|
|
|
realName = cnvName;
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
2001-08-11 00:29:58 +00:00
|
|
|
|
|
|
|
/* get the shared data for an algorithmic converter, if it is one */
|
|
|
|
mySharedConverterData = (UConverterSharedData *)getAlgorithmicTypeFromName (realName);
|
|
|
|
if (mySharedConverterData == NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
/* it is a data-based converter, get its shared data */
|
2001-10-04 01:14:52 +00:00
|
|
|
mySharedConverterData = ucnv_getSharedConverterData (realName);
|
2001-08-11 00:29:58 +00:00
|
|
|
if (mySharedConverterData == NULL)
|
1999-12-04 00:55:54 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
/*Not cached, we need to stream it in from file */
|
|
|
|
mySharedConverterData = createConverterFromFile (realName, err);
|
|
|
|
if (U_FAILURE (*err) || (mySharedConverterData == NULL))
|
1999-12-04 00:55:54 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
return NULL;
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
2001-08-11 00:29:58 +00:00
|
|
|
else
|
1999-12-04 00:55:54 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
/* share it with other library clients */
|
2001-10-04 01:14:52 +00:00
|
|
|
ucnv_shareConverterData(mySharedConverterData);
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-11 00:29:58 +00:00
|
|
|
else
|
1999-12-04 00:55:54 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
/* ### this is unsafe: the shared data could have been deleted since sharing or getting it - these operations should increase the counter! */
|
|
|
|
/* update the reference counter: one more client */
|
|
|
|
umtx_lock (NULL);
|
|
|
|
mySharedConverterData->referenceCounter++;
|
|
|
|
umtx_unlock (NULL);
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
/* allocate the converter */
|
|
|
|
myUConverter = (UConverter *) uprv_malloc (sizeof (UConverter));
|
|
|
|
if (myUConverter == NULL)
|
2000-02-09 19:15:17 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
if (mySharedConverterData->referenceCounter != ~0) {
|
|
|
|
umtx_lock (NULL);
|
|
|
|
--mySharedConverterData->referenceCounter;
|
|
|
|
umtx_unlock (NULL);
|
|
|
|
}
|
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
/* initialize the converter */
|
|
|
|
uprv_memset(myUConverter, 0, sizeof(UConverter));
|
|
|
|
myUConverter->sharedData = mySharedConverterData;
|
|
|
|
myUConverter->mode = UCNV_SI;
|
|
|
|
myUConverter->fromCharErrorBehaviour = (UConverterToUCallback) UCNV_TO_U_CALLBACK_SUBSTITUTE;
|
|
|
|
myUConverter->fromUCharErrorBehaviour = (UConverterFromUCallback) UCNV_FROM_U_CALLBACK_SUBSTITUTE;
|
|
|
|
myUConverter->toUnicodeStatus = myUConverter->sharedData->toUnicodeStatus;
|
|
|
|
myUConverter->subChar1 = myUConverter->sharedData->staticData->subChar1;
|
|
|
|
myUConverter->subCharLen = myUConverter->sharedData->staticData->subCharLen;
|
|
|
|
uprv_memcpy (myUConverter->subChar, myUConverter->sharedData->staticData->subChar, myUConverter->subCharLen);
|
|
|
|
|
|
|
|
if(myUConverter != NULL && myUConverter->sharedData->impl->open != NULL) {
|
|
|
|
myUConverter->sharedData->impl->open(myUConverter, realName, locale,options, err);
|
|
|
|
if(U_FAILURE(*err)) {
|
|
|
|
ucnv_close(myUConverter);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-09 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
return myUConverter;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-10-04 01:04:28 +00:00
|
|
|
UConverterSharedData*
|
|
|
|
ucnv_data_unFlattenClone(UDataMemory *pData, UErrorCode *status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-07-13 23:55:33 +00:00
|
|
|
/* UDataInfo info; -- necessary only if some converters have different formatVersion */
|
2000-02-08 23:41:16 +00:00
|
|
|
const uint8_t *raw = (const uint8_t *)udata_getMemory(pData);
|
2000-04-19 23:05:27 +00:00
|
|
|
const UConverterStaticData *source = (const UConverterStaticData *) raw;
|
2000-02-08 23:41:16 +00:00
|
|
|
UConverterSharedData *data;
|
2000-06-20 23:42:20 +00:00
|
|
|
UConverterType type = (UConverterType)source->conversionType;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
if( (uint16_t)type >= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES ||
|
2001-01-02 23:09:45 +00:00
|
|
|
converterData[type] == NULL ||
|
2000-02-05 00:19:15 +00:00
|
|
|
converterData[type]->referenceCounter != 1 ||
|
2000-04-19 23:05:27 +00:00
|
|
|
source->structSize != sizeof(UConverterStaticData))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-02-05 00:19:15 +00:00
|
|
|
*status = U_INVALID_TABLE_FORMAT;
|
2000-07-13 00:11:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-13 23:55:33 +00:00
|
|
|
#if 0
|
|
|
|
/* necessary only if some converters have different formatVersion; now everything is at version 5 */
|
2000-07-13 00:11:11 +00:00
|
|
|
/* test for the format version: MBCS is at version 5, the rest still at 4 */
|
|
|
|
info.size=sizeof(UDataInfo);
|
|
|
|
udata_getInfo(pData, &info);
|
|
|
|
if(type == UCNV_MBCS ? info.formatVersion[0] != 5 : info.formatVersion[0] != 4) {
|
|
|
|
*status = U_INVALID_TABLE_FORMAT;
|
2000-02-05 00:19:15 +00:00
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-07-13 23:55:33 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
data = (UConverterSharedData *)uprv_malloc(sizeof(UConverterSharedData));
|
|
|
|
if(data == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
/* copy initial values from the static structure for this type */
|
|
|
|
uprv_memcpy(data, converterData[type], sizeof(UConverterSharedData));
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
/* ### it would be much more efficient if the table were a direct member, not a pointer */
|
|
|
|
data->table = (UConverterTable *)uprv_malloc(sizeof(UConverterTable));
|
|
|
|
if(data->table == NULL) {
|
|
|
|
uprv_free(data);
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
1999-12-04 02:31:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-04-19 23:05:27 +00:00
|
|
|
|
|
|
|
data->staticData = source;
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
/* fill in fields from the loaded data */
|
2000-02-08 23:41:16 +00:00
|
|
|
data->dataMemory = (void*)pData; /* for future use */
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
if(data->impl->load != NULL) {
|
|
|
|
data->impl->load(data, raw + source->structSize, status);
|
|
|
|
if(U_FAILURE(*status)) {
|
2000-12-14 00:29:46 +00:00
|
|
|
uprv_free(data->table);
|
2000-02-08 23:41:16 +00:00
|
|
|
uprv_free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
2000-02-08 23:41:16 +00:00
|
|
|
return data;
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
2001-10-04 01:04:28 +00:00
|
|
|
|
|
|
|
/*Frees all shared immutable objects that aren't referred to (reference count = 0)
|
|
|
|
*/
|
|
|
|
int32_t
|
|
|
|
ucnv_flushCache ()
|
|
|
|
{
|
|
|
|
UConverterSharedData *mySharedData = NULL;
|
|
|
|
int32_t pos = -1;
|
|
|
|
int32_t tableDeletedNum = 0;
|
|
|
|
const UHashElement *e;
|
|
|
|
|
|
|
|
/*if shared data hasn't even been lazy evaluated yet
|
|
|
|
* return 0
|
|
|
|
*/
|
|
|
|
if (SHARED_DATA_HASHTABLE == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*creates an enumeration to iterate through every element in the
|
|
|
|
*table
|
|
|
|
*/
|
|
|
|
umtx_lock (NULL);
|
|
|
|
while ((e = uhash_nextElement (SHARED_DATA_HASHTABLE, &pos)) != NULL)
|
|
|
|
{
|
2001-10-16 18:31:13 +00:00
|
|
|
mySharedData = (UConverterSharedData *) e->value.pointer;
|
2001-10-04 01:04:28 +00:00
|
|
|
/*deletes only if reference counter == 0 */
|
|
|
|
if (mySharedData->referenceCounter == 0)
|
|
|
|
{
|
|
|
|
tableDeletedNum++;
|
|
|
|
|
|
|
|
UCNV_DEBUG_LOG("del",mySharedData->staticData->name,mySharedData);
|
|
|
|
|
|
|
|
uhash_removeElement(SHARED_DATA_HASHTABLE, e);
|
2001-10-04 01:14:52 +00:00
|
|
|
ucnv_deleteSharedConverterData (mySharedData);
|
2001-10-04 01:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
umtx_unlock (NULL);
|
|
|
|
|
|
|
|
return tableDeletedNum;
|
|
|
|
}
|
|
|
|
|