1999-08-16 21:50:52 +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-23 23:02:55 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* ucnv_io.c:
|
2002-07-03 16:46:21 +00:00
|
|
|
* initializes global variables and defines functions pertaining to file
|
|
|
|
* access, and name resolution aspect of the library.
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
|
|
|
* new implementation:
|
|
|
|
*
|
|
|
|
* created on: 1999nov22
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
2002-07-03 16:46:21 +00:00
|
|
|
* Use the binary cnvalias.icu (created from convrtrs.txt) to work
|
1999-11-23 23:02:55 +00:00
|
|
|
* with aliases for converter names.
|
2002-07-03 16:46:21 +00:00
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 11/22/1999 markus Created
|
|
|
|
* 06/28/2002 grhoten Major overhaul of the converter alias design.
|
|
|
|
* Now an alias can map to different converters
|
|
|
|
* depending on the specified standard.
|
2001-03-21 20:44:20 +00:00
|
|
|
*******************************************************************************
|
1999-11-23 23:02:55 +00:00
|
|
|
*/
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
2001-01-03 00:18:57 +00:00
|
|
|
#include "unicode/putil.h"
|
2000-11-17 02:02:11 +00:00
|
|
|
#include "unicode/ucnv.h" /* This file implements ucnv_xXXX() APIs */
|
2002-07-03 16:46:21 +00:00
|
|
|
#include "unicode/udata.h"
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "umutex.h"
|
|
|
|
#include "cstring.h"
|
1999-12-04 00:55:54 +00:00
|
|
|
#include "cmemory.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ucnv_io.h"
|
2002-07-13 16:32:16 +00:00
|
|
|
#include "uenumimp.h"
|
2001-08-29 22:07:27 +00:00
|
|
|
#include "ucln_cmn.h"
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
/* Format of cnvalias.icu -----------------------------------------------------
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
2002-07-03 16:46:21 +00:00
|
|
|
* cnvalias.icu is a binary, memory-mappable form of convrtrs.txt.
|
2002-06-28 23:13:30 +00:00
|
|
|
* This binary form contains several tables. All indexes are to uint16_t
|
|
|
|
* units, and not to the bytes (uint8_t units). Addressing everything on
|
|
|
|
* 16-bit boundaries allows us to store more information with small index
|
|
|
|
* numbers, which are also 16-bit in size. The majority of the table (except
|
|
|
|
* the string table) are 16-bit numbers.
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
2002-06-28 23:13:30 +00:00
|
|
|
* First there is the size of the Table of Contents (TOC). The TOC
|
|
|
|
* entries contain the size of each section. In order to find the offset
|
|
|
|
* you just need to sum up the previous offsets.
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
2002-06-28 23:13:30 +00:00
|
|
|
* 1) This section contains a list of converters. This list contains indexes
|
|
|
|
* into the string table for the converter name. The index of this list is
|
|
|
|
* also used by other sections, which are mentioned later on.
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
2002-06-28 23:13:30 +00:00
|
|
|
* 2) This section contains a list of tags. This list contains indexes
|
|
|
|
* into the string table for the tag name. The index of this list is
|
|
|
|
* also used by other sections, which are mentioned later on.
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
2002-08-02 21:59:23 +00:00
|
|
|
* 3) This section contains a list of sorted unique aliases. This
|
2002-06-28 23:13:30 +00:00
|
|
|
* list contains indexes into the string table for the alias name. The
|
2002-08-02 21:59:23 +00:00
|
|
|
* index of this list is also used by other sections, like the 4th section.
|
2002-08-08 22:46:05 +00:00
|
|
|
* The index for the 3rd and 4th section is used to get the
|
|
|
|
* alias -> converter name mapping. Section 3 and 4 form a two column table.
|
2002-06-28 23:13:30 +00:00
|
|
|
*
|
|
|
|
* 4) This section contains a list of mapped converter names. Consider this
|
|
|
|
* as a table that maps the 3rd section to the 1st section. This list contains
|
|
|
|
* indexes into the 1st section. The index of this list is the same index in
|
|
|
|
* the 3rd section. There is also some extra information in the high bits of
|
|
|
|
* each converter index in this table. Currently it's only used to say that
|
|
|
|
* an alias mapped to this converter is ambiguous. See UCNV_CONVERTER_INDEX_MASK
|
|
|
|
* and UCNV_AMBIGUOUS_ALIAS_MAP_BIT for more information. This section is
|
|
|
|
* the predigested form of the 5th section so that an alias lookup can be fast.
|
|
|
|
*
|
|
|
|
* 5) This section contains a 2D array with indexes to the 6th section. This
|
|
|
|
* section is the full form of all alias mappings. The column index is the
|
|
|
|
* index into the converter list (column header). The row index is the index
|
|
|
|
* to tag list (row header). This 2D array is the top part a 3D array. The
|
|
|
|
* third dimension is in the 6th section.
|
|
|
|
*
|
|
|
|
* 6) This is blob of variable length arrays. Each array starts with a size,
|
|
|
|
* and is followed by indexes to alias names in the string table. This is
|
|
|
|
* the third dimension to the section 5. No other section should be referencing
|
|
|
|
* this section.
|
|
|
|
*
|
|
|
|
* 7) Reserved at this time (There is no information). This _usually_ has a
|
|
|
|
* size of 0. Future versions may add more information here.
|
|
|
|
*
|
|
|
|
* 8) This is the string table. All strings are indexed on an even address.
|
|
|
|
* There are two reasons for this. First many chip architectures locate strings
|
|
|
|
* faster on even address boundaries. Second, since all indexes are 16-bit
|
|
|
|
* numbers, this string table can be 128KB in size instead of 64KB when we
|
|
|
|
* only have strings starting on an even address.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Here is the concept of section 5 and 6. It's a 3D cube. Each tag
|
|
|
|
* has a unique alias among all converters. That same alias can
|
|
|
|
* be mentioned in other standards on different converters,
|
|
|
|
* but only one alias per tag can be unique.
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
|
|
|
*
|
2002-06-28 23:13:30 +00:00
|
|
|
* Converter Names (Usually in TR22 form)
|
|
|
|
* -------------------------------------------.
|
|
|
|
* T / /|
|
|
|
|
* a / / |
|
|
|
|
* g / / |
|
|
|
|
* s / / |
|
|
|
|
* / / |
|
|
|
|
* ------------------------------------------/ |
|
|
|
|
* A | | |
|
|
|
|
* l | | |
|
|
|
|
* i | | /
|
|
|
|
* a | | /
|
|
|
|
* s | | /
|
|
|
|
* e | | /
|
|
|
|
* s | |/
|
|
|
|
* -------------------------------------------
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
2000-08-10 01:32:29 +00:00
|
|
|
*
|
|
|
|
*
|
2002-06-28 23:13:30 +00:00
|
|
|
* Here is what it really looks like. It's like swiss cheese.
|
|
|
|
* There are holes. Some converters aren't recognized by
|
|
|
|
* a standard, or they are really old converters that the
|
|
|
|
* standard doesn't recognize anymore.
|
2000-08-10 01:32:29 +00:00
|
|
|
*
|
2002-06-28 23:13:30 +00:00
|
|
|
* Converter Names (Usually in TR22 form)
|
|
|
|
* -------------------------------------------.
|
|
|
|
* T /##########################################/|
|
|
|
|
* a / # # /#
|
|
|
|
* g / # ## ## ### # ### ### ### #/
|
|
|
|
* s / # ##### #### ## ## #/#
|
|
|
|
* / ### # # ## # # # ### # # #/##
|
|
|
|
* ------------------------------------------/# #
|
|
|
|
* A |### # # ## # # # ### # # #|# #
|
|
|
|
* l |# # # # # ## # #|# #
|
|
|
|
* i |# # # # # # #|#
|
|
|
|
* a |# #|#
|
|
|
|
* s | #|#
|
|
|
|
* e
|
|
|
|
* s
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2002-08-02 21:59:23 +00:00
|
|
|
/**
|
|
|
|
* Used by the UEnumeration API
|
|
|
|
*/
|
2002-07-13 16:32:16 +00:00
|
|
|
typedef struct UAliasContext {
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listOffset;
|
2002-07-13 16:32:16 +00:00
|
|
|
uint32_t listIdx;
|
|
|
|
} UAliasContext;
|
|
|
|
|
2002-01-21 21:48:25 +00:00
|
|
|
static const char DATA_NAME[] = "cnvalias";
|
2002-06-28 23:13:30 +00:00
|
|
|
static const char DATA_TYPE[] = "icu";
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
static UDataMemory *gAliasData=NULL;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
static const uint16_t *gConverterList = NULL;
|
|
|
|
static const uint16_t *gTagList = NULL;
|
|
|
|
static const uint16_t *gAliasList = NULL;
|
|
|
|
static const uint16_t *gUntaggedConvArray = NULL;
|
|
|
|
static const uint16_t *gTaggedAliasArray = NULL;
|
|
|
|
static const uint16_t *gTaggedAliasLists = NULL;
|
|
|
|
static const uint16_t *gStringTable = NULL;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
static uint32_t gConverterListSize;
|
|
|
|
static uint32_t gTagListSize;
|
|
|
|
static uint32_t gAliasListSize;
|
|
|
|
static uint32_t gUntaggedConvArraySize;
|
|
|
|
static uint32_t gTaggedAliasArraySize;
|
|
|
|
static uint32_t gTaggedAliasListsSize;
|
|
|
|
static uint32_t gStringTableSize;
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
static const char **gAvailableConverters = NULL;
|
|
|
|
static uint16_t gAvailableConverterCount = 0;
|
2002-05-13 21:21:19 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
static char gDefaultConverterNameBuffer[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; /* +1 for NULL */
|
|
|
|
static const char *gDefaultConverterName = NULL;
|
2001-10-23 00:25:17 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
#define GET_STRING(idx) (const char *)(gStringTable + (idx))
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-07-23 23:01:08 +00:00
|
|
|
static UBool U_CALLCONV
|
1999-11-23 23:02:55 +00:00
|
|
|
isAcceptable(void *context,
|
|
|
|
const char *type, const char *name,
|
2000-05-03 23:15:02 +00:00
|
|
|
const UDataInfo *pInfo) {
|
2000-08-10 02:08:49 +00:00
|
|
|
return (UBool)(
|
1999-11-23 23:02:55 +00:00
|
|
|
pInfo->size>=20 &&
|
|
|
|
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
|
|
|
|
pInfo->charsetFamily==U_CHARSET_FAMILY &&
|
|
|
|
pInfo->dataFormat[0]==0x43 && /* dataFormat="CvAl" */
|
|
|
|
pInfo->dataFormat[1]==0x76 &&
|
|
|
|
pInfo->dataFormat[2]==0x41 &&
|
|
|
|
pInfo->dataFormat[3]==0x6c &&
|
2002-06-28 23:13:30 +00:00
|
|
|
pInfo->formatVersion[0]==3);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
static UBool
|
1999-11-23 23:02:55 +00:00
|
|
|
haveAliasData(UErrorCode *pErrorCode) {
|
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
/* load converter alias data from file if necessary */
|
2002-08-08 22:46:05 +00:00
|
|
|
if(gAliasData==NULL) {
|
2002-06-28 23:13:30 +00:00
|
|
|
UDataMemory *data = NULL;
|
|
|
|
const uint16_t *table = NULL;
|
|
|
|
uint32_t tableStart;
|
|
|
|
uint32_t currOffset;
|
|
|
|
uint32_t reservedSize1;
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
data = udata_openChoice(NULL, DATA_TYPE, DATA_NAME, isAcceptable, NULL, pErrorCode);
|
1999-11-23 23:02:55 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
table = (const uint16_t *)udata_getMemory(data);
|
|
|
|
|
|
|
|
tableStart = ((const uint32_t *)(table))[0];
|
|
|
|
if (tableStart < 8) {
|
|
|
|
*pErrorCode = U_INVALID_FORMAT_ERROR;
|
2002-08-02 21:59:23 +00:00
|
|
|
udata_close(data);
|
2002-06-28 23:13:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
1999-11-23 23:02:55 +00:00
|
|
|
|
|
|
|
umtx_lock(NULL);
|
2002-08-08 22:46:05 +00:00
|
|
|
if(gAliasData==NULL) {
|
|
|
|
gAliasData = data;
|
1999-11-23 23:02:55 +00:00
|
|
|
data=NULL;
|
2000-08-10 01:32:29 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
gConverterListSize = ((const uint32_t *)(table))[1];
|
|
|
|
gTagListSize = ((const uint32_t *)(table))[2];
|
|
|
|
gAliasListSize = ((const uint32_t *)(table))[3];
|
|
|
|
gUntaggedConvArraySize = ((const uint32_t *)(table))[4];
|
|
|
|
gTaggedAliasArraySize = ((const uint32_t *)(table))[5];
|
|
|
|
gTaggedAliasListsSize = ((const uint32_t *)(table))[6];
|
2002-06-28 23:13:30 +00:00
|
|
|
reservedSize1 = ((const uint32_t *)(table))[7]; /* reserved */
|
2002-08-08 22:46:05 +00:00
|
|
|
gStringTableSize = ((const uint32_t *)(table))[8];
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
currOffset = tableStart * (sizeof(uint32_t)/sizeof(uint16_t)) + (sizeof(uint32_t)/sizeof(uint16_t));
|
2002-08-08 22:46:05 +00:00
|
|
|
gConverterList = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
currOffset += gConverterListSize;
|
|
|
|
gTagList = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
currOffset += gTagListSize;
|
|
|
|
gAliasList = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
currOffset += gAliasListSize;
|
|
|
|
gUntaggedConvArray = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
currOffset += gUntaggedConvArraySize;
|
|
|
|
gTaggedAliasArray = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
/* aliasLists is a 1's based array, but it has a padding character */
|
2002-08-08 22:46:05 +00:00
|
|
|
currOffset += gTaggedAliasArraySize;
|
|
|
|
gTaggedAliasLists = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
currOffset += gTaggedAliasListsSize;
|
2002-06-28 23:13:30 +00:00
|
|
|
/* reserved */
|
|
|
|
|
|
|
|
currOffset += reservedSize1;
|
2002-08-08 22:46:05 +00:00
|
|
|
gStringTable = table + currOffset;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
|
|
|
|
/* if a different thread set it first, then close the extra data */
|
|
|
|
if(data!=NULL) {
|
|
|
|
udata_close(data); /* NULL if it was set correctly */
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
U_INLINE static UBool
|
1999-11-23 23:02:55 +00:00
|
|
|
isAlias(const char *alias, UErrorCode *pErrorCode) {
|
|
|
|
if(alias==NULL) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
} else if(*alias==0) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-30 02:59:19 +00:00
|
|
|
UBool
|
2001-08-25 01:13:21 +00:00
|
|
|
ucnv_io_cleanup()
|
|
|
|
{
|
2002-08-08 22:46:05 +00:00
|
|
|
if (gAliasData) {
|
|
|
|
udata_close(gAliasData);
|
|
|
|
gAliasData = NULL;
|
2002-05-13 21:21:19 +00:00
|
|
|
}
|
|
|
|
|
2002-06-13 16:43:41 +00:00
|
|
|
ucnv_io_flushAvailableConverterCache();
|
2001-08-25 01:13:21 +00:00
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
gConverterListSize = 0;
|
|
|
|
gTagListSize = 0;
|
|
|
|
gAliasListSize = 0;
|
|
|
|
gUntaggedConvArraySize = 0;
|
|
|
|
gTaggedAliasArraySize = 0;
|
|
|
|
gTaggedAliasListsSize = 0;
|
|
|
|
gStringTableSize = 0;
|
|
|
|
|
|
|
|
gConverterList = NULL;
|
|
|
|
gTagList = NULL;
|
|
|
|
gAliasList = NULL;
|
|
|
|
gUntaggedConvArray = NULL;
|
|
|
|
gTaggedAliasArray = NULL;
|
|
|
|
gTaggedAliasLists = NULL;
|
|
|
|
gStringTable = NULL;
|
|
|
|
|
|
|
|
gDefaultConverterName = NULL;
|
|
|
|
gDefaultConverterNameBuffer[0] = 0;
|
2001-10-23 00:25:17 +00:00
|
|
|
|
2001-08-25 01:13:21 +00:00
|
|
|
return TRUE; /* Everything was cleaned up */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
static uint32_t getTagNumber(const char *tagname) {
|
2002-08-08 22:46:05 +00:00
|
|
|
if (gTagList) {
|
2002-06-28 23:13:30 +00:00
|
|
|
uint32_t tagNum;
|
2002-08-08 22:46:05 +00:00
|
|
|
for (tagNum = 0; tagNum < gTagListSize; tagNum++) {
|
|
|
|
if (!uprv_stricmp(GET_STRING(gTagList[tagNum]), tagname)) {
|
2002-06-28 23:13:30 +00:00
|
|
|
return tagNum;
|
2000-08-10 02:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
return UINT32_MAX;
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 00:48:35 +00:00
|
|
|
/* @see ucnv_compareNames */
|
|
|
|
U_CFUNC char * U_EXPORT2
|
|
|
|
ucnv_io_stripForCompare(char *dst, const char *name) {
|
|
|
|
char c1 = *name;
|
|
|
|
char *dstItr = dst;
|
|
|
|
|
|
|
|
while (c1) {
|
|
|
|
/* Ignore delimiters '-', '_', and ' ' */
|
|
|
|
while ((c1 = *name) == '-' || c1 == '_' || c1 == ' ') {
|
|
|
|
++name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lowercase for case-insensitive comparison */
|
|
|
|
*(dstItr++) = uprv_tolower(c1);
|
|
|
|
++name;
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2000-07-19 22:18:43 +00:00
|
|
|
/**
|
|
|
|
* Do a fuzzy compare of a two converter/alias names. The comparison
|
|
|
|
* is case-insensitive. It also ignores the characters '-', '_', and
|
|
|
|
* ' ' (dash, underscore, and space). Thus the strings "UTF-8",
|
|
|
|
* "utf_8", and "Utf 8" are exactly equivalent.
|
|
|
|
*
|
|
|
|
* This is a symmetrical (commutative) operation; order of arguments
|
|
|
|
* is insignificant. This is an important property for sorting the
|
|
|
|
* list (when the list is preprocessed into binary form) and for
|
|
|
|
* performing binary searches on it at run time.
|
|
|
|
*
|
|
|
|
* @param name1 a converter name or alias, zero-terminated
|
|
|
|
* @param name2 a converter name or alias, zero-terminated
|
|
|
|
* @return 0 if the names match, or a negative value if the name1
|
|
|
|
* lexically precedes name2, or a positive value if the name1
|
|
|
|
* lexically follows name2.
|
2002-09-06 00:48:35 +00:00
|
|
|
*
|
|
|
|
* @see ucnv_io_stripForCompare
|
2000-07-19 22:18:43 +00:00
|
|
|
*/
|
2000-11-17 05:02:31 +00:00
|
|
|
U_CAPI int U_EXPORT2
|
2000-11-17 06:07:31 +00:00
|
|
|
ucnv_compareNames(const char *name1, const char *name2) {
|
1999-11-23 23:02:55 +00:00
|
|
|
int rc;
|
2002-06-28 23:13:30 +00:00
|
|
|
char c1, c2;
|
1999-11-23 23:02:55 +00:00
|
|
|
|
2000-06-30 21:35:03 +00:00
|
|
|
for (;;) {
|
2000-07-19 22:18:43 +00:00
|
|
|
/* Ignore delimiters '-', '_', and ' ' */
|
2002-06-28 23:13:30 +00:00
|
|
|
while ((c1 = *name1) == '-' || c1 == '_' || c1 == ' ') {
|
|
|
|
++name1;
|
|
|
|
}
|
|
|
|
while ((c2 = *name2) == '-' || c2 == '_' || c2 == ' ') {
|
|
|
|
++name2;
|
|
|
|
}
|
2000-07-19 22:18:43 +00:00
|
|
|
|
|
|
|
/* If we reach the ends of both strings then they match */
|
|
|
|
if ((c1|c2)==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Case-insensitive comparison */
|
|
|
|
rc = (int)(unsigned char)uprv_tolower(c1) -
|
|
|
|
(int)(unsigned char)uprv_tolower(c2);
|
2002-06-28 23:13:30 +00:00
|
|
|
if (rc != 0) {
|
2000-07-19 22:18:43 +00:00
|
|
|
return rc;
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
2000-07-19 22:18:43 +00:00
|
|
|
++name1;
|
|
|
|
++name2;
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
/*
|
|
|
|
* search for an alias
|
2002-08-08 22:46:05 +00:00
|
|
|
* return the converter number index for gConverterList
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2002-10-30 22:52:08 +00:00
|
|
|
static U_INLINE uint32_t
|
2002-06-28 23:13:30 +00:00
|
|
|
findConverter(const char *alias, UErrorCode *pErrorCode) {
|
|
|
|
uint32_t mid, start, limit;
|
2002-09-04 00:36:40 +00:00
|
|
|
uint32_t lastMid;
|
2002-06-28 23:13:30 +00:00
|
|
|
int result;
|
1999-11-23 23:02:55 +00:00
|
|
|
|
|
|
|
/* do a binary search for the alias */
|
2002-06-28 23:13:30 +00:00
|
|
|
start = 0;
|
2002-09-04 00:36:40 +00:00
|
|
|
limit = gUntaggedConvArraySize;
|
2002-06-28 23:13:30 +00:00
|
|
|
mid = limit;
|
2002-09-04 00:36:40 +00:00
|
|
|
lastMid = UINT32_MAX;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
2002-09-04 00:36:40 +00:00
|
|
|
for (;;) {
|
|
|
|
mid = (uint32_t)((start + limit) / 2);
|
|
|
|
if (lastMid == mid) { /* Have we moved? */
|
|
|
|
break; /* We haven't moved, and it wasn't found. */
|
|
|
|
}
|
|
|
|
lastMid = mid;
|
2002-08-08 22:46:05 +00:00
|
|
|
result = ucnv_compareNames(alias, GET_STRING(gAliasList[mid]));
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
if (result < 0) {
|
2002-09-04 00:36:40 +00:00
|
|
|
limit = mid;
|
2002-06-28 23:13:30 +00:00
|
|
|
} else if (result > 0) {
|
2002-09-04 00:36:40 +00:00
|
|
|
start = mid;
|
1999-11-23 23:02:55 +00:00
|
|
|
} else {
|
2002-06-28 23:13:30 +00:00
|
|
|
/* Since the gencnval tool folds duplicates into one entry,
|
2002-08-08 22:46:05 +00:00
|
|
|
* this alias in gAliasList is unique, but different standards
|
2002-06-28 23:13:30 +00:00
|
|
|
* may map an alias to different converters.
|
|
|
|
*/
|
2002-08-08 22:46:05 +00:00
|
|
|
if (gUntaggedConvArray[mid] & UCNV_AMBIGUOUS_ALIAS_MAP_BIT) {
|
2002-06-28 23:13:30 +00:00
|
|
|
*pErrorCode = U_AMBIGUOUS_ALIAS_WARNING;
|
|
|
|
}
|
2002-08-08 22:46:05 +00:00
|
|
|
return gUntaggedConvArray[mid] & UCNV_CONVERTER_INDEX_MASK;
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
return UINT32_MAX;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-10-30 22:52:08 +00:00
|
|
|
/*
|
|
|
|
* Is this alias in this list?
|
|
|
|
* alias and listOffset should be non-NULL.
|
|
|
|
*/
|
|
|
|
static U_INLINE UBool
|
|
|
|
isAliasInList(const char *alias, uint32_t listOffset) {
|
|
|
|
if (listOffset) {
|
|
|
|
uint32_t currAlias;
|
|
|
|
uint32_t listCount = gTaggedAliasLists[listOffset];
|
|
|
|
/* +1 to skip listCount */
|
|
|
|
const uint16_t *currList = gTaggedAliasLists + listOffset + 1;
|
|
|
|
for (currAlias = 0; currAlias < listCount; currAlias++) {
|
|
|
|
if (currList[currAlias]
|
|
|
|
&& ucnv_compareNames(alias, GET_STRING(currList[currAlias]))==0)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
/*
|
|
|
|
* Search for an standard name of an alias (what is the default name
|
|
|
|
* that this standard uses?)
|
|
|
|
* return the listOffset for gTaggedAliasLists. If it's 0,
|
|
|
|
* the it couldn't be found, but the parameters are valid.
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
findTaggedAliasListsOffset(const char *alias, const char *standard, UErrorCode *pErrorCode) {
|
|
|
|
uint32_t idx;
|
|
|
|
uint32_t listOffset;
|
|
|
|
uint32_t convNum;
|
|
|
|
UErrorCode myErr = U_ZERO_ERROR;
|
|
|
|
uint32_t tagNum = getTagNumber(standard);
|
|
|
|
|
|
|
|
/* Make a quick guess. Hopefully they used a TR22 canonical alias. */
|
|
|
|
convNum = findConverter(alias, &myErr);
|
|
|
|
if (myErr != U_ZERO_ERROR) {
|
|
|
|
*pErrorCode = myErr;
|
|
|
|
}
|
|
|
|
|
2002-10-30 22:52:08 +00:00
|
|
|
if (tagNum < (gTagListSize - UCNV_NUM_HIDDEN_TAGS) && convNum < gConverterListSize) {
|
2002-08-08 22:46:05 +00:00
|
|
|
listOffset = gTaggedAliasArray[tagNum*gConverterListSize + convNum];
|
|
|
|
if (listOffset && gTaggedAliasLists[listOffset + 1]) {
|
|
|
|
return listOffset;
|
|
|
|
}
|
|
|
|
if (myErr == U_AMBIGUOUS_ALIAS_WARNING) {
|
|
|
|
/* Uh Oh! They used an ambiguous alias.
|
|
|
|
We have to search the whole swiss cheese starting
|
|
|
|
at the highest standard affinity.
|
|
|
|
This may take a while.
|
|
|
|
*/
|
|
|
|
for (idx = 0; idx < gTaggedAliasArraySize; idx++) {
|
|
|
|
listOffset = gTaggedAliasArray[idx];
|
2002-10-30 22:52:08 +00:00
|
|
|
if (listOffset && isAliasInList(alias, listOffset)) {
|
|
|
|
uint32_t currTagNum = idx/gConverterListSize;
|
|
|
|
uint32_t currConvNum = (idx - currTagNum*gConverterListSize);
|
|
|
|
uint32_t tempListOffset = gTaggedAliasArray[tagNum*gConverterListSize + currConvNum];
|
|
|
|
if (tempListOffset && gTaggedAliasLists[tempListOffset + 1]) {
|
|
|
|
return tempListOffset;
|
2002-08-08 22:46:05 +00:00
|
|
|
}
|
2002-10-30 22:52:08 +00:00
|
|
|
/* else keep on looking */
|
|
|
|
/* We could speed this up by starting on the next row
|
|
|
|
because an alias is unique per row, right now.
|
|
|
|
This would change if alias versioning appears. */
|
2002-08-08 22:46:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The standard doesn't know about the alias */
|
|
|
|
}
|
|
|
|
/* else no default name */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* else converter or tag not found */
|
|
|
|
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
2002-10-30 22:52:08 +00:00
|
|
|
/* Return the canonical name */
|
|
|
|
static uint32_t
|
|
|
|
findTaggedConverterNum(const char *alias, const char *standard, UErrorCode *pErrorCode) {
|
|
|
|
uint32_t idx;
|
|
|
|
uint32_t listOffset;
|
|
|
|
uint32_t convNum;
|
|
|
|
UErrorCode myErr = U_ZERO_ERROR;
|
|
|
|
uint32_t tagNum = getTagNumber(standard);
|
|
|
|
|
|
|
|
/* Make a quick guess. Hopefully they used a TR22 canonical alias. */
|
|
|
|
convNum = findConverter(alias, &myErr);
|
|
|
|
if (myErr != U_ZERO_ERROR) {
|
|
|
|
*pErrorCode = myErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tagNum < (gTagListSize - UCNV_NUM_HIDDEN_TAGS) && convNum < gConverterListSize) {
|
|
|
|
listOffset = gTaggedAliasArray[tagNum*gConverterListSize + convNum];
|
|
|
|
if (listOffset && isAliasInList(alias, listOffset)) {
|
|
|
|
return convNum;
|
|
|
|
}
|
|
|
|
if (myErr == U_AMBIGUOUS_ALIAS_WARNING) {
|
|
|
|
/* Uh Oh! They used an ambiguous alias.
|
|
|
|
We have to search one slice of the swiss cheese.
|
|
|
|
We search only in the requested tag, not the whole thing.
|
|
|
|
This may take a while.
|
|
|
|
*/
|
|
|
|
uint32_t convStart = (tagNum)*gConverterListSize;
|
|
|
|
uint32_t convLimit = (tagNum+1)*gConverterListSize;
|
|
|
|
for (idx = convStart; idx < convLimit; idx++) {
|
|
|
|
listOffset = gTaggedAliasArray[idx];
|
|
|
|
if (listOffset && isAliasInList(alias, listOffset)) {
|
|
|
|
return convNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The standard doesn't know about the alias */
|
|
|
|
}
|
|
|
|
/* else no canonical name */
|
|
|
|
}
|
|
|
|
/* else converter or tag not found */
|
|
|
|
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC const char *
|
|
|
|
ucnv_io_getConverterName(const char *alias, UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
2002-06-28 23:13:30 +00:00
|
|
|
uint32_t convNum = findConverter(alias, pErrorCode);
|
2002-08-08 22:46:05 +00:00
|
|
|
if (convNum < gConverterListSize) {
|
|
|
|
return GET_STRING(gConverterList[convNum]);
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else converter not found */
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t U_CALLCONV
|
|
|
|
ucnv_io_countStandardAliases(UEnumeration *enumerator, UErrorCode *pErrorCode) {
|
2002-07-22 21:06:41 +00:00
|
|
|
int32_t value = 0;
|
|
|
|
UAliasContext *myContext = (UAliasContext *)(enumerator->context);
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listOffset = myContext->listOffset;
|
2002-07-13 16:32:16 +00:00
|
|
|
|
2002-07-22 21:06:41 +00:00
|
|
|
if (listOffset) {
|
2002-08-08 22:46:05 +00:00
|
|
|
value = gTaggedAliasLists[listOffset];
|
2002-07-13 16:32:16 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* U_CALLCONV
|
|
|
|
ucnv_io_nextStandardAliases(UEnumeration *enumerator,
|
|
|
|
int32_t* resultLength,
|
|
|
|
UErrorCode *pErrorCode)
|
|
|
|
{
|
2002-07-22 21:06:41 +00:00
|
|
|
UAliasContext *myContext = (UAliasContext *)(enumerator->context);
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listOffset = myContext->listOffset;
|
2002-07-13 16:32:16 +00:00
|
|
|
|
2002-07-22 21:06:41 +00:00
|
|
|
if (listOffset) {
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listCount = gTaggedAliasLists[listOffset];
|
|
|
|
const uint16_t *currList = gTaggedAliasLists + listOffset + 1;
|
2002-07-13 16:32:16 +00:00
|
|
|
|
2002-07-22 21:06:41 +00:00
|
|
|
if (myContext->listIdx < listCount) {
|
2002-08-02 23:01:10 +00:00
|
|
|
const char *myStr = GET_STRING(currList[myContext->listIdx++]);
|
|
|
|
if (resultLength) {
|
|
|
|
*resultLength = uprv_strlen(myStr);
|
|
|
|
}
|
|
|
|
return myStr;
|
2002-07-13 16:32:16 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-07-22 21:06:41 +00:00
|
|
|
/* Either we accessed a zero length list, or we enumerated too far. */
|
|
|
|
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-11-23 23:02:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-07-13 16:32:16 +00:00
|
|
|
static void U_CALLCONV
|
|
|
|
ucnv_io_resetStandardAliases(UEnumeration *enumerator, UErrorCode *pErrorCode) {
|
2002-07-22 21:06:41 +00:00
|
|
|
((UAliasContext *)(enumerator->context))->listIdx = 0;
|
2002-07-13 16:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
ucnv_io_closeUEnumeration(UEnumeration *enumerator) {
|
|
|
|
uprv_free(enumerator->context);
|
|
|
|
uprv_free(enumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enumerate the aliases for the specified converter and standard tag */
|
|
|
|
static const UEnumeration gEnumAliases = {
|
2002-08-02 23:01:10 +00:00
|
|
|
NULL,
|
2002-07-13 16:32:16 +00:00
|
|
|
NULL,
|
|
|
|
ucnv_io_closeUEnumeration,
|
|
|
|
ucnv_io_countStandardAliases,
|
2002-08-02 23:01:10 +00:00
|
|
|
uenum_unextDefault,
|
2002-07-13 16:32:16 +00:00
|
|
|
ucnv_io_nextStandardAliases,
|
|
|
|
ucnv_io_resetStandardAliases
|
|
|
|
};
|
|
|
|
|
|
|
|
U_CAPI UEnumeration *
|
|
|
|
ucnv_openStandardNames(const char *convName,
|
|
|
|
const char *standard,
|
|
|
|
UErrorCode *pErrorCode)
|
|
|
|
{
|
|
|
|
UEnumeration *myEnum = NULL;
|
|
|
|
if (haveAliasData(pErrorCode) && isAlias(convName, pErrorCode)) {
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listOffset = findTaggedAliasListsOffset(convName, standard, pErrorCode);
|
|
|
|
|
2002-08-09 01:48:58 +00:00
|
|
|
/* When listOffset == 0, we want to acknowledge that the
|
|
|
|
converter name and standard are okay, but there
|
|
|
|
is nothing to enumerate. */
|
2002-08-08 22:46:05 +00:00
|
|
|
if (listOffset < gTaggedAliasListsSize) {
|
2002-07-13 16:32:16 +00:00
|
|
|
UAliasContext *myContext;
|
|
|
|
|
|
|
|
myEnum = uprv_malloc(sizeof(UEnumeration));
|
|
|
|
if (myEnum == NULL) {
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uprv_memcpy(myEnum, &gEnumAliases, sizeof(UEnumeration));
|
|
|
|
myContext = uprv_malloc(sizeof(UAliasContext));
|
|
|
|
if (myContext == NULL) {
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
uprv_free(myEnum);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-08-08 22:46:05 +00:00
|
|
|
myContext->listOffset = listOffset;
|
2002-07-13 16:32:16 +00:00
|
|
|
myContext->listIdx = 0;
|
2002-10-30 22:52:08 +00:00
|
|
|
myEnum->context = myContext;
|
2002-07-13 16:32:16 +00:00
|
|
|
}
|
|
|
|
/* else converter or tag not found */
|
|
|
|
}
|
|
|
|
return myEnum;
|
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC uint16_t
|
2002-06-28 23:13:30 +00:00
|
|
|
ucnv_io_countAliases(const char *alias, UErrorCode *pErrorCode) {
|
1999-11-23 23:02:55 +00:00
|
|
|
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
2002-06-28 23:13:30 +00:00
|
|
|
uint32_t convNum = findConverter(alias, pErrorCode);
|
2002-08-08 22:46:05 +00:00
|
|
|
if (convNum < gConverterListSize) {
|
2002-06-28 23:13:30 +00:00
|
|
|
/* tagListNum - 1 is the ALL tag */
|
2002-08-08 22:46:05 +00:00
|
|
|
int32_t listOffset = gTaggedAliasArray[(gTagListSize - 1)*gConverterListSize + convNum];
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
if (listOffset) {
|
2002-08-08 22:46:05 +00:00
|
|
|
return gTaggedAliasLists[listOffset];
|
2002-06-28 23:13:30 +00:00
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else this shouldn't happen. internal program error */
|
2002-06-28 23:13:30 +00:00
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else converter not found */
|
2002-06-28 23:13:30 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC uint16_t
|
|
|
|
ucnv_io_getAliases(const char *alias, uint16_t start, const char **aliases, UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
|
|
|
uint32_t currAlias;
|
|
|
|
uint32_t convNum = findConverter(alias, pErrorCode);
|
2002-08-08 22:46:05 +00:00
|
|
|
if (convNum < gConverterListSize) {
|
2002-06-28 23:13:30 +00:00
|
|
|
/* tagListNum - 1 is the ALL tag */
|
2002-08-08 22:46:05 +00:00
|
|
|
int32_t listOffset = gTaggedAliasArray[(gTagListSize - 1)*gConverterListSize + convNum];
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
if (listOffset) {
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listCount = gTaggedAliasLists[listOffset];
|
2002-06-28 23:13:30 +00:00
|
|
|
/* +1 to skip listCount */
|
2002-08-08 22:46:05 +00:00
|
|
|
const uint16_t *currList = gTaggedAliasLists + listOffset + 1;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
for (currAlias = start; currAlias < listCount; currAlias++) {
|
|
|
|
aliases[currAlias] = GET_STRING(currList[currAlias]);
|
|
|
|
}
|
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else this shouldn't happen. internal program error */
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else converter not found */
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-11-23 23:02:55 +00:00
|
|
|
return 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC const char *
|
2000-04-14 05:22:29 +00:00
|
|
|
ucnv_io_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode) {
|
1999-11-23 23:02:55 +00:00
|
|
|
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
2002-06-28 23:13:30 +00:00
|
|
|
uint32_t convNum = findConverter(alias, pErrorCode);
|
2002-08-08 22:46:05 +00:00
|
|
|
if (convNum < gConverterListSize) {
|
2002-06-28 23:13:30 +00:00
|
|
|
/* tagListNum - 1 is the ALL tag */
|
2002-08-08 22:46:05 +00:00
|
|
|
int32_t listOffset = gTaggedAliasArray[(gTagListSize - 1)*gConverterListSize + convNum];
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
if (listOffset) {
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listCount = gTaggedAliasLists[listOffset];
|
2002-06-28 23:13:30 +00:00
|
|
|
/* +1 to skip listCount */
|
2002-08-08 22:46:05 +00:00
|
|
|
const uint16_t *currList = gTaggedAliasLists + listOffset + 1;
|
2002-06-28 23:13:30 +00:00
|
|
|
|
|
|
|
if (n < listCount) {
|
|
|
|
return GET_STRING(currList[n]);
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
2002-06-28 23:13:30 +00:00
|
|
|
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else this shouldn't happen. internal program error */
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
2002-07-13 16:32:16 +00:00
|
|
|
/* else converter not found */
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-11-23 23:02:55 +00:00
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-11-17 19:28:31 +00:00
|
|
|
U_CFUNC uint16_t
|
|
|
|
ucnv_io_countStandards(UErrorCode *pErrorCode) {
|
2000-11-17 05:49:58 +00:00
|
|
|
if (haveAliasData(pErrorCode)) {
|
2002-06-28 23:13:30 +00:00
|
|
|
/* Don't include the empty list */
|
2002-10-30 22:52:08 +00:00
|
|
|
return (uint16_t)(gTagListSize - UCNV_NUM_HIDDEN_TAGS);
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-11-17 02:02:11 +00:00
|
|
|
U_CAPI const char * U_EXPORT2
|
|
|
|
ucnv_getStandard(uint16_t n, UErrorCode *pErrorCode) {
|
2002-06-28 23:13:30 +00:00
|
|
|
if (haveAliasData(pErrorCode)) {
|
2002-10-30 22:52:08 +00:00
|
|
|
if (n < gTagListSize - UCNV_NUM_HIDDEN_TAGS) {
|
2002-08-08 22:46:05 +00:00
|
|
|
return GET_STRING(gTagList[n]);
|
2000-08-10 02:08:49 +00:00
|
|
|
}
|
2002-06-28 23:13:30 +00:00
|
|
|
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-07-13 16:32:16 +00:00
|
|
|
U_CAPI const char * U_EXPORT2
|
2000-11-17 02:02:11 +00:00
|
|
|
ucnv_getStandardName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
|
2000-08-10 02:08:49 +00:00
|
|
|
if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
2002-08-08 22:46:05 +00:00
|
|
|
uint32_t listOffset = findTaggedAliasListsOffset(alias, standard, pErrorCode);
|
|
|
|
|
2002-08-09 01:48:58 +00:00
|
|
|
if (0 < listOffset && listOffset < gTaggedAliasListsSize) {
|
2002-08-08 22:46:05 +00:00
|
|
|
const uint16_t *currList = gTaggedAliasLists + listOffset + 1;
|
|
|
|
|
|
|
|
/* Get the preferred name from this list */
|
|
|
|
if (currList[0]) {
|
|
|
|
return GET_STRING(currList[0]);
|
2002-06-28 23:13:30 +00:00
|
|
|
}
|
2002-08-08 22:46:05 +00:00
|
|
|
/* else someone screwed up the alias table. */
|
|
|
|
/* *pErrorCode = U_INVALID_FORMAT_ERROR */
|
2000-08-10 02:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
return NULL;
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
2002-10-30 22:52:08 +00:00
|
|
|
U_CAPI const char * U_EXPORT2
|
|
|
|
ucnv_getCanonicalName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
|
|
|
|
if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
|
|
|
uint32_t convNum = findTaggedConverterNum(alias, standard, pErrorCode);
|
|
|
|
|
|
|
|
if (convNum < gConverterListSize) {
|
|
|
|
return GET_STRING(gConverterList[convNum]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-13 16:43:41 +00:00
|
|
|
void
|
|
|
|
ucnv_io_flushAvailableConverterCache() {
|
2002-08-08 22:46:05 +00:00
|
|
|
if (gAvailableConverters) {
|
2002-06-13 16:43:41 +00:00
|
|
|
umtx_lock(NULL);
|
2002-08-08 22:46:05 +00:00
|
|
|
gAvailableConverterCount = 0;
|
|
|
|
uprv_free((char **)gAvailableConverters);
|
|
|
|
gAvailableConverters = NULL;
|
2002-06-13 16:43:41 +00:00
|
|
|
umtx_unlock(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
static UBool haveAvailableConverterList(UErrorCode *pErrorCode) {
|
2002-08-08 22:46:05 +00:00
|
|
|
if (gAvailableConverters == NULL) {
|
2002-06-28 23:13:30 +00:00
|
|
|
uint16_t idx;
|
|
|
|
uint16_t localConverterCount;
|
|
|
|
UErrorCode status;
|
|
|
|
const char *converterName;
|
|
|
|
const char **localConverterList;
|
|
|
|
|
|
|
|
if (!haveAliasData(pErrorCode)) {
|
|
|
|
return FALSE;
|
2002-05-13 21:21:19 +00:00
|
|
|
}
|
|
|
|
|
2002-06-28 23:13:30 +00:00
|
|
|
/* We can't have more than "*converterTable" converters to open */
|
2002-08-08 22:46:05 +00:00
|
|
|
localConverterList = (const char **) uprv_malloc(gConverterListSize * sizeof(char*));
|
2002-06-28 23:13:30 +00:00
|
|
|
if (!localConverterList) {
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
localConverterCount = 0;
|
|
|
|
|
2002-08-08 22:46:05 +00:00
|
|
|
for (idx = 0; idx < gConverterListSize; idx++) {
|
2002-06-28 23:13:30 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2002-08-08 22:46:05 +00:00
|
|
|
converterName = GET_STRING(gConverterList[idx]);
|
2002-06-28 23:13:30 +00:00
|
|
|
ucnv_close(ucnv_open(converterName, &status));
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
localConverterList[localConverterCount++] = converterName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
umtx_lock(NULL);
|
2002-08-08 22:46:05 +00:00
|
|
|
if (gAvailableConverters == NULL) {
|
|
|
|
gAvailableConverters = localConverterList;
|
|
|
|
gAvailableConverterCount = localConverterCount;
|
2002-06-28 23:13:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
uprv_free((char **)localConverterList);
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
2002-05-13 21:21:19 +00:00
|
|
|
}
|
2002-06-28 23:13:30 +00:00
|
|
|
return TRUE;
|
2002-05-13 21:21:19 +00:00
|
|
|
}
|
|
|
|
|
2000-11-17 02:51:31 +00:00
|
|
|
U_CFUNC uint16_t
|
1999-12-14 00:37:27 +00:00
|
|
|
ucnv_io_countAvailableConverters(UErrorCode *pErrorCode) {
|
2002-06-28 23:13:30 +00:00
|
|
|
if (haveAvailableConverterList(pErrorCode)) {
|
2002-08-08 22:46:05 +00:00
|
|
|
return gAvailableConverterCount;
|
1999-12-14 00:37:27 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC const char *
|
2000-04-14 05:22:29 +00:00
|
|
|
ucnv_io_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode) {
|
2002-06-28 23:13:30 +00:00
|
|
|
if (haveAvailableConverterList(pErrorCode)) {
|
2002-08-08 22:46:05 +00:00
|
|
|
if (n < gAvailableConverterCount) {
|
|
|
|
return gAvailableConverters[n];
|
1999-12-14 00:37:27 +00:00
|
|
|
}
|
2002-06-28 23:13:30 +00:00
|
|
|
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-12-14 00:37:27 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-30 22:52:08 +00:00
|
|
|
static int32_t U_CALLCONV
|
|
|
|
ucnv_io_countAllConverters(UEnumeration *enumerator, UErrorCode *pErrorCode) {
|
|
|
|
return gConverterListSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* U_CALLCONV
|
|
|
|
ucnv_io_nextAllConverters(UEnumeration *enumerator,
|
|
|
|
int32_t* resultLength,
|
|
|
|
UErrorCode *pErrorCode)
|
|
|
|
{
|
|
|
|
uint16_t *myContext = (uint16_t *)(enumerator->context);
|
|
|
|
|
|
|
|
if (*myContext < gConverterListSize) {
|
|
|
|
const char *myStr = GET_STRING(gConverterList[(*myContext)++]);
|
|
|
|
if (resultLength) {
|
|
|
|
*resultLength = uprv_strlen(myStr);
|
|
|
|
}
|
|
|
|
return myStr;
|
|
|
|
}
|
|
|
|
/* Either we accessed a zero length list, or we enumerated too far. */
|
|
|
|
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
ucnv_io_resetAllConverters(UEnumeration *enumerator, UErrorCode *pErrorCode) {
|
|
|
|
*((uint16_t *)(enumerator->context)) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const UEnumeration gEnumAllConverters = {
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
ucnv_io_closeUEnumeration,
|
|
|
|
ucnv_io_countAllConverters,
|
|
|
|
uenum_unextDefault,
|
|
|
|
ucnv_io_nextAllConverters,
|
|
|
|
ucnv_io_resetAllConverters
|
|
|
|
};
|
|
|
|
|
|
|
|
U_CAPI UEnumeration * U_EXPORT2
|
|
|
|
ucnv_openAllNames(UErrorCode *pErrorCode) {
|
|
|
|
UEnumeration *myEnum = NULL;
|
|
|
|
if (haveAliasData(pErrorCode)) {
|
|
|
|
uint16_t *myContext;
|
|
|
|
|
|
|
|
myEnum = uprv_malloc(sizeof(UEnumeration));
|
|
|
|
if (myEnum == NULL) {
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uprv_memcpy(myEnum, &gEnumAllConverters, sizeof(UEnumeration));
|
|
|
|
myContext = uprv_malloc(sizeof(uint16_t));
|
|
|
|
if (myContext == NULL) {
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
uprv_free(myEnum);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*myContext = 0;
|
|
|
|
myEnum->context = myContext;
|
|
|
|
}
|
|
|
|
return myEnum;
|
|
|
|
}
|
|
|
|
|
1999-12-14 00:37:27 +00:00
|
|
|
U_CFUNC void
|
|
|
|
ucnv_io_fillAvailableConverters(const char **aliases, UErrorCode *pErrorCode) {
|
2002-06-28 23:13:30 +00:00
|
|
|
if (haveAvailableConverterList(pErrorCode)) {
|
2002-06-14 00:09:01 +00:00
|
|
|
uint16_t count = 0;
|
2002-08-08 22:46:05 +00:00
|
|
|
while (count < gAvailableConverterCount) {
|
|
|
|
*aliases++=gAvailableConverters[count++];
|
1999-12-14 00:37:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC uint16_t
|
|
|
|
ucnv_io_countAvailableAliases(UErrorCode *pErrorCode) {
|
2002-06-28 23:13:30 +00:00
|
|
|
if (haveAliasData(pErrorCode)) {
|
2002-08-08 22:46:05 +00:00
|
|
|
return (uint16_t)gAliasListSize;
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-04 00:55:54 +00:00
|
|
|
/* default converter name --------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In order to be really thread-safe, the get function would have to take
|
|
|
|
* a buffer parameter and copy the current string inside a mutex block.
|
|
|
|
* This implementation only tries to be really thread-safe while
|
|
|
|
* setting the name.
|
|
|
|
* It assumes that setting a pointer is atomic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
U_CFUNC const char *
|
|
|
|
ucnv_io_getDefaultConverterName() {
|
|
|
|
/* local variable to be thread-safe */
|
2002-08-08 22:46:05 +00:00
|
|
|
const char *name=gDefaultConverterName;
|
1999-12-04 00:55:54 +00:00
|
|
|
if(name==NULL) {
|
2002-06-28 23:13:30 +00:00
|
|
|
const char *codepage = uprv_getDefaultCodepage();
|
1999-12-04 00:55:54 +00:00
|
|
|
if(codepage!=NULL) {
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
name=ucnv_io_getConverterName(codepage, &errorCode);
|
|
|
|
if(U_FAILURE(errorCode) || name==NULL) {
|
|
|
|
name=codepage;
|
|
|
|
}
|
2001-11-12 20:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the name is there, test it out */
|
|
|
|
if(name != NULL) {
|
2002-06-28 23:13:30 +00:00
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = ucnv_open(name, &errorCode);
|
|
|
|
if(U_FAILURE(errorCode) || (cnv == NULL)) {
|
|
|
|
/* Panic time, let's use a fallback. */
|
2001-11-12 20:58:22 +00:00
|
|
|
#if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
|
2002-06-28 23:13:30 +00:00
|
|
|
name = "US-ASCII";
|
|
|
|
/* there is no 'algorithmic' converter for EBCDIC */
|
2001-11-15 20:18:36 +00:00
|
|
|
#elif defined(OS390)
|
2002-09-18 01:38:17 +00:00
|
|
|
name = "ibm-1047" UCNV_SWAP_LFNL_OPTION_STRING;
|
2001-11-12 20:58:22 +00:00
|
|
|
#else
|
2002-06-28 23:13:30 +00:00
|
|
|
name = "ibm-37";
|
2001-11-12 20:58:22 +00:00
|
|
|
#endif
|
2002-06-28 23:13:30 +00:00
|
|
|
}
|
|
|
|
ucnv_close(cnv);
|
2001-11-12 20:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(name != NULL) {
|
2002-06-28 23:13:30 +00:00
|
|
|
umtx_lock(NULL);
|
|
|
|
/* Did find a name. And it works.*/
|
2002-08-08 22:46:05 +00:00
|
|
|
gDefaultConverterName=name;
|
2002-06-28 23:13:30 +00:00
|
|
|
umtx_unlock(NULL);
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-12 20:58:22 +00:00
|
|
|
|
1999-12-04 00:55:54 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC void
|
|
|
|
ucnv_io_setDefaultConverterName(const char *converterName) {
|
|
|
|
if(converterName==NULL) {
|
|
|
|
/* reset to the default codepage */
|
2002-08-08 22:46:05 +00:00
|
|
|
gDefaultConverterName=NULL;
|
1999-12-04 00:55:54 +00:00
|
|
|
} else {
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
const char *name=ucnv_io_getConverterName(converterName, &errorCode);
|
|
|
|
if(U_SUCCESS(errorCode) && name!=NULL) {
|
2002-08-08 22:46:05 +00:00
|
|
|
gDefaultConverterName=name;
|
1999-12-04 00:55:54 +00:00
|
|
|
} else {
|
|
|
|
/* do not set the name if the alias lookup failed and it is too long */
|
2002-05-22 22:56:27 +00:00
|
|
|
int32_t length=(int32_t)(uprv_strlen(converterName));
|
2002-08-08 22:46:05 +00:00
|
|
|
if(length<sizeof(gDefaultConverterNameBuffer)) {
|
1999-12-04 00:55:54 +00:00
|
|
|
/* it was not found as an alias, so copy it - accept an empty name */
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool didLock;
|
2002-08-08 22:46:05 +00:00
|
|
|
if(gDefaultConverterName==gDefaultConverterNameBuffer) {
|
1999-12-04 00:55:54 +00:00
|
|
|
umtx_lock(NULL);
|
|
|
|
didLock=TRUE;
|
|
|
|
} else {
|
|
|
|
didLock=FALSE;
|
|
|
|
}
|
2002-08-08 22:46:05 +00:00
|
|
|
uprv_memcpy(gDefaultConverterNameBuffer, converterName, length);
|
|
|
|
gDefaultConverterNameBuffer[length]=0;
|
|
|
|
gDefaultConverterName=gDefaultConverterNameBuffer;
|
1999-12-04 00:55:54 +00:00
|
|
|
if(didLock) {
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hey, Emacs, please set the following:
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|