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:
|
|
|
|
* initializes global variables and defines functions pertaining to file access,
|
|
|
|
* and name resolution aspect of the library.
|
|
|
|
*
|
|
|
|
* new implementation:
|
|
|
|
*
|
|
|
|
* created on: 1999nov22
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
|
|
|
* Use the binary cnvalias.dat (created from convrtrs.txt) to work
|
|
|
|
* with aliases for converter names.
|
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 */
|
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"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/udata.h"
|
2001-08-29 22:07:27 +00:00
|
|
|
#include "ucln_cmn.h"
|
1999-11-23 23:02:55 +00:00
|
|
|
|
|
|
|
/* Format of cnvalias.dat ------------------------------------------------------
|
|
|
|
*
|
|
|
|
* cnvalias.dat is a binary, memory-mappable form of convrtrs.txt .
|
|
|
|
* It contains two sorted tables and a block of zero-terminated strings.
|
|
|
|
* Each table is preceded by the number of table entries.
|
|
|
|
*
|
|
|
|
* The first table maps from aliases to converter indexes.
|
|
|
|
* The converter names themselves are listed as aliases in this table.
|
|
|
|
* Each entry in this table has an offset to the alias and
|
|
|
|
* an index of the converter in the converter table.
|
|
|
|
*
|
|
|
|
* The second table lists only the converters themselves.
|
|
|
|
* Each entry in this table has an offset to the converter name and
|
|
|
|
* the number of aliases, including the converter itself.
|
|
|
|
* A count of 1 means that there is no alias, only the converter name.
|
|
|
|
*
|
|
|
|
* In the block of strings after the tables, each converter name is directly
|
|
|
|
* followed by its aliases. All offsets to strings are offsets from the
|
|
|
|
* beginning of the data.
|
|
|
|
*
|
2000-08-10 01:32:29 +00:00
|
|
|
* More formal file data structure (data format 2.1):
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
|
|
|
* uint16_t aliasCount;
|
1999-12-07 18:29:32 +00:00
|
|
|
* uint16_t aliasOffsets[aliasCount];
|
|
|
|
* uint16_t converterIndexes[aliasCount];
|
1999-11-23 23:02:55 +00:00
|
|
|
*
|
|
|
|
* uint16_t converterCount;
|
|
|
|
* struct {
|
|
|
|
* uint16_t converterOffset;
|
|
|
|
* uint16_t aliasCount;
|
|
|
|
* } converters[converterCount];
|
|
|
|
*
|
2000-08-10 01:32:29 +00:00
|
|
|
* uint16_t tagCount;
|
|
|
|
* uint16_t taggedAliasesOffsets[tagCount][converterCount];
|
|
|
|
* char tags[] = { "Tag0\Tag1\0..." };
|
|
|
|
*
|
1999-11-23 23:02:55 +00:00
|
|
|
* char strings[]={
|
|
|
|
* "Converter0\0Alias1\0Alias2\0...Converter1\0Converter2\0Alias0\Alias1\0..."
|
|
|
|
* };
|
2000-08-10 01:32:29 +00:00
|
|
|
*
|
|
|
|
* The code included here can read versions 2 and 2.1 of the data format.
|
|
|
|
* Version 2 does not have tag information, but since the code never refers
|
|
|
|
* to strings[] by its base offset, it's okay.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2002-01-21 21:48:25 +00:00
|
|
|
static const char DATA_NAME[] = "cnvalias";
|
|
|
|
static const char DATA_TYPE[] = "dat";
|
1999-11-23 23:02:55 +00:00
|
|
|
|
|
|
|
static UDataMemory *aliasData=NULL;
|
|
|
|
static const uint16_t *aliasTable=NULL;
|
|
|
|
|
2002-05-13 21:21:19 +00:00
|
|
|
static const char **availableConverters = NULL;
|
|
|
|
static uint16_t availableConverterCount = 0;
|
|
|
|
|
2000-08-10 01:32:29 +00:00
|
|
|
static const uint16_t *converterTable = NULL;
|
|
|
|
static const uint16_t *tagTable = NULL;
|
|
|
|
|
2001-10-23 00:25:17 +00:00
|
|
|
static char defaultConverterNameBuffer[100];
|
|
|
|
static const char *defaultConverterName = NULL;
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
static UBool
|
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 &&
|
2001-11-28 22:24:58 +00:00
|
|
|
pInfo->formatVersion[0]==2);
|
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 */
|
|
|
|
if(aliasData==NULL) {
|
|
|
|
UDataMemory *data;
|
2000-08-10 01:32:29 +00:00
|
|
|
UDataInfo info;
|
1999-11-23 23:02:55 +00:00
|
|
|
const uint16_t *table=NULL;
|
|
|
|
|
|
|
|
/* open the data outside the mutex block */
|
|
|
|
data=udata_openChoice(NULL, DATA_TYPE, DATA_NAME, isAcceptable, NULL, pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
table=(const uint16_t *)udata_getMemory(data);
|
2000-08-10 16:34:07 +00:00
|
|
|
info.size=sizeof(UDataInfo);
|
2000-08-10 01:32:29 +00:00
|
|
|
udata_getInfo(data, &info);
|
1999-11-23 23:02:55 +00:00
|
|
|
|
|
|
|
/* in the mutex block, set the data for this process */
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if(aliasData==NULL) {
|
|
|
|
aliasData=data;
|
|
|
|
data=NULL;
|
|
|
|
aliasTable=table;
|
|
|
|
table=NULL;
|
2000-08-10 02:08:49 +00:00
|
|
|
converterTable = aliasTable + 1 + 2 * *aliasTable;
|
2000-08-10 01:32:29 +00:00
|
|
|
|
2001-11-28 22:24:58 +00:00
|
|
|
if (info.formatVersion[0] == 2 && info.formatVersion[1] > 0) {
|
2000-08-10 01:32:29 +00:00
|
|
|
tagTable = converterTable + 1 + 2 * *converterTable;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (aliasData) {
|
|
|
|
udata_close(aliasData);
|
2002-05-13 21:21:19 +00:00
|
|
|
aliasData = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (availableConverters) {
|
|
|
|
uprv_free((char **)availableConverters);
|
|
|
|
availableConverters = NULL;
|
2001-08-25 01:13:21 +00:00
|
|
|
}
|
2002-05-13 21:21:19 +00:00
|
|
|
availableConverterCount = 0;
|
2001-08-25 01:13:21 +00:00
|
|
|
|
|
|
|
aliasData = NULL;
|
|
|
|
aliasTable = NULL;
|
|
|
|
|
|
|
|
converterTable = NULL;
|
|
|
|
tagTable = NULL;
|
|
|
|
|
2001-10-23 00:25:17 +00:00
|
|
|
defaultConverterName = NULL;
|
|
|
|
|
2001-08-25 01:13:21 +00:00
|
|
|
return TRUE; /* Everything was cleaned up */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-10 01:32:29 +00:00
|
|
|
static int16_t getTagNumber(const char *tagname) {
|
|
|
|
if (tagTable) {
|
2000-08-10 02:08:49 +00:00
|
|
|
int16_t tag, count = (int16_t) *tagTable;
|
|
|
|
const char *tags = (const char *) (tagTable + 1 + count * *converterTable);
|
|
|
|
|
2000-08-10 21:44:42 +00:00
|
|
|
#if 0
|
|
|
|
|
2000-08-10 02:08:49 +00:00
|
|
|
char name[100];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* convert the tag name to lowercase to do case-insensitive comparisons */
|
|
|
|
for(i = 0; i < sizeof(name) - 1 && *tagname; ++i) {
|
|
|
|
name[i] = (char)uprv_tolower(*tagname++);
|
|
|
|
}
|
|
|
|
name[i] = 0;
|
|
|
|
|
2000-08-10 21:44:42 +00:00
|
|
|
#else
|
|
|
|
|
2000-11-17 02:51:31 +00:00
|
|
|
const char *name = tagname;
|
2000-08-10 21:44:42 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2000-08-10 02:08:49 +00:00
|
|
|
for (tag = 0; count--; ++tag) {
|
2000-08-10 21:44:42 +00:00
|
|
|
if (!uprv_stricmp(name, tags)) {
|
2000-08-10 02:08:49 +00:00
|
|
|
return tag;
|
|
|
|
}
|
2000-08-10 03:53:44 +00:00
|
|
|
tags += strlen(tags) + 1;
|
2000-08-10 02:08:49 +00:00
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
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;
|
|
|
|
unsigned char c1, c2;
|
|
|
|
|
2000-06-30 21:35:03 +00:00
|
|
|
for (;;) {
|
2000-07-19 22:18:43 +00:00
|
|
|
/* Ignore delimiters '-', '_', and ' ' */
|
|
|
|
while ((c1 = (unsigned char)*name1) == '-'
|
|
|
|
|| c1 == '_' || c1 == ' ') ++name1;
|
|
|
|
while ((c2 = (unsigned char)*name2) == '-'
|
|
|
|
|| c2 == '_' || c2 == ' ') ++name2;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (rc!=0) {
|
|
|
|
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
|
|
|
|
* return NULL or a pointer to the converter table entry
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
1999-11-23 23:02:55 +00:00
|
|
|
static const uint16_t *
|
|
|
|
findAlias(const char *alias) {
|
|
|
|
char name[100];
|
|
|
|
const uint16_t *p=aliasTable;
|
|
|
|
uint16_t i, start, limit;
|
|
|
|
|
|
|
|
limit=*p++;
|
|
|
|
if(limit==0) {
|
|
|
|
/* there are no aliases */
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
/* convert the alias name to lowercase to do case-insensitive comparisons */
|
|
|
|
for(i=0; i<sizeof(name)-1 && *alias!=0; ++i) {
|
2000-08-10 02:08:49 +00:00
|
|
|
name[i]=(char)uprv_tolower(*alias++);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-11-23 23:02:55 +00:00
|
|
|
name[i]=0;
|
|
|
|
|
|
|
|
/* do a binary search for the alias */
|
|
|
|
start=0;
|
|
|
|
while(start<limit-1) {
|
2000-08-10 02:08:49 +00:00
|
|
|
i=(uint16_t)((start+limit)/2);
|
2000-11-17 06:07:31 +00:00
|
|
|
if(ucnv_compareNames(name, (const char *)aliasTable+p[i])<0) {
|
1999-11-23 23:02:55 +00:00
|
|
|
limit=i;
|
|
|
|
} else {
|
|
|
|
start=i;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
/* did we really find it? */
|
2000-11-17 06:07:31 +00:00
|
|
|
if(ucnv_compareNames(name, (const char *)aliasTable+p[start])==0) {
|
1999-12-07 18:29:32 +00:00
|
|
|
limit=*(p-1); /* aliasCount */
|
|
|
|
p+=limit; /* advance to the second column of the alias table */
|
|
|
|
i=p[start]; /* converter index */
|
|
|
|
return
|
|
|
|
p+limit+ /* beginning of converter table */
|
|
|
|
1+ /* skip its count */
|
|
|
|
2*i; /* go to this converter's entry and return a pointer to it */
|
1999-11-23 23:02:55 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
const uint16_t *p=findAlias(alias);
|
|
|
|
if(p!=NULL) {
|
|
|
|
return (const char *)aliasTable+*p;
|
|
|
|
}
|
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
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC uint16_t
|
|
|
|
ucnv_io_getAliases(const char *alias, const char **aliases, UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
|
|
|
const uint16_t *p=findAlias(alias);
|
|
|
|
if(p!=NULL) {
|
|
|
|
*aliases=(const char *)aliasTable+*p;
|
|
|
|
return *(p+1);
|
|
|
|
}
|
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)) {
|
|
|
|
const uint16_t *p=findAlias(alias);
|
|
|
|
if(p!=NULL) {
|
|
|
|
uint16_t count=*(p+1);
|
2000-04-14 05:22:29 +00:00
|
|
|
if(n<count) {
|
1999-11-23 23:02:55 +00:00
|
|
|
const char *aliases=(const char *)aliasTable+*p;
|
2000-04-14 05:22:29 +00:00
|
|
|
while(n>0) {
|
1999-11-23 23:02:55 +00:00
|
|
|
/* skip a name, first the canonical converter name */
|
1999-12-28 23:39:02 +00:00
|
|
|
aliases+=uprv_strlen(aliases)+1;
|
2000-04-14 05:22:29 +00:00
|
|
|
--n;
|
1999-11-23 23:02:55 +00:00
|
|
|
}
|
|
|
|
return aliases;
|
|
|
|
}
|
|
|
|
}
|
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)) {
|
2000-08-10 02:08:49 +00:00
|
|
|
if (!tagTable) {
|
2000-11-17 05:49:58 +00:00
|
|
|
*pErrorCode = U_INVALID_FORMAT_ERROR;
|
2000-08-10 02:08:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
|
2000-08-10 02:08:49 +00:00
|
|
|
return *tagTable;
|
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) {
|
2000-08-10 01:32:29 +00:00
|
|
|
if (haveAliasData(pErrorCode) && tagTable) {
|
2000-08-10 02:08:49 +00:00
|
|
|
int16_t count = (int16_t) *tagTable;
|
2000-08-11 04:15:06 +00:00
|
|
|
const char *tags = (const char *) (tagTable + 1 + count * *converterTable);
|
2000-08-10 01:32:29 +00:00
|
|
|
|
2000-08-10 02:08:49 +00:00
|
|
|
while (n-- && count--) {
|
2000-08-11 04:15:06 +00:00
|
|
|
tags += strlen(tags) + 1;
|
2000-08-10 02:08:49 +00:00
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
|
2000-08-11 04:15:06 +00:00
|
|
|
return count ? tags : NULL;
|
2000-08-10 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-11-17 02:02:11 +00:00
|
|
|
U_CFUNC const char * U_EXPORT2
|
|
|
|
ucnv_getStandardName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
|
2000-08-10 02:08:49 +00:00
|
|
|
if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
|
2000-08-10 01:32:29 +00:00
|
|
|
const uint16_t *p = findAlias(alias);
|
|
|
|
if(p != NULL) {
|
2000-08-10 02:08:49 +00:00
|
|
|
int16_t tag = getTagNumber(standard);
|
|
|
|
|
|
|
|
if (tag > -1) {
|
|
|
|
uint16_t offset = tagTable[1 + tag * *converterTable + (p - converterTable) / 2];
|
|
|
|
return offset ? (const char *) aliasTable + offset : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-08-10 01:32:29 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-13 21:21:19 +00:00
|
|
|
static void ucnv_io_loadAvailableConverterList(void) {
|
|
|
|
uint16_t idx = 0;
|
|
|
|
uint16_t localConverterCount = 0;
|
|
|
|
UErrorCode status;
|
|
|
|
char *converterName;
|
|
|
|
|
|
|
|
/* We can't have more than "*converterTable" converters to open */
|
|
|
|
char **localConverterList = (char **) uprv_malloc(*converterTable * sizeof(char*));
|
|
|
|
|
|
|
|
for (; idx < *converterTable; idx++) {
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
converterName = (char *)aliasTable+converterTable[1+2*idx];
|
|
|
|
ucnv_close(ucnv_open(converterName, &status));
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
localConverterList[localConverterCount++] = converterName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if (availableConverters == NULL) {
|
2002-05-24 00:50:41 +00:00
|
|
|
availableConverters = (const char **)localConverterList;
|
2002-05-13 21:21:19 +00:00
|
|
|
availableConverterCount = localConverterCount;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uprv_free(localConverterList);
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if(haveAliasData(pErrorCode)) {
|
2002-05-13 21:21:19 +00:00
|
|
|
if (availableConverters == NULL) {
|
|
|
|
ucnv_io_loadAvailableConverterList();
|
|
|
|
}
|
|
|
|
return availableConverterCount;
|
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) {
|
1999-12-14 00:37:27 +00:00
|
|
|
if(haveAliasData(pErrorCode)) {
|
2002-05-13 21:21:19 +00:00
|
|
|
if (availableConverters == NULL) {
|
|
|
|
ucnv_io_loadAvailableConverterList();
|
|
|
|
}
|
|
|
|
if(n < availableConverterCount) {
|
|
|
|
return availableConverters[n];
|
1999-12-14 00:37:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC void
|
|
|
|
ucnv_io_fillAvailableConverters(const char **aliases, UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode)) {
|
2000-08-10 01:32:29 +00:00
|
|
|
const uint16_t *p=converterTable;
|
1999-12-14 00:37:27 +00:00
|
|
|
uint16_t count=*p++;
|
|
|
|
while(count>0) {
|
|
|
|
*aliases++=(const char *)aliasTable+*p;
|
|
|
|
p+=2;
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC uint16_t
|
|
|
|
ucnv_io_countAvailableAliases(UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode)) {
|
|
|
|
return *aliasTable;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-07-21 22:48:15 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* We are not currently using these functions, so I am commenting them out
|
|
|
|
* to reduce the binary file size and improve the code coverage;
|
|
|
|
* I do not currently want to remove this entirely because it may be useful
|
|
|
|
* in the future and also serves to some degree as another piece of
|
|
|
|
* documentation of the data structure.
|
|
|
|
*/
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC const char *
|
2000-04-14 05:22:29 +00:00
|
|
|
ucnv_io_getAvailableAlias(uint16_t n, UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode) && n<*aliasTable) {
|
|
|
|
return (const char *)aliasTable+*(aliasTable+1+n);
|
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
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
U_CFUNC void
|
|
|
|
ucnv_io_fillAvailableAliases(const char **aliases, UErrorCode *pErrorCode) {
|
|
|
|
if(haveAliasData(pErrorCode)) {
|
|
|
|
const uint16_t *p=aliasTable;
|
|
|
|
uint16_t count=*p++;
|
|
|
|
while(count>0) {
|
|
|
|
*aliases++=(const char *)aliasTable+*p;
|
1999-12-14 00:37:27 +00:00
|
|
|
++p;
|
1999-11-23 23:02:55 +00:00
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-07-21 22:48:15 +00:00
|
|
|
#endif
|
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 */
|
|
|
|
const char *name=defaultConverterName;
|
|
|
|
if(name==NULL) {
|
2000-08-07 23:04:41 +00:00
|
|
|
const char *codepage=0;
|
|
|
|
umtx_lock(NULL);
|
|
|
|
codepage = uprv_getDefaultCodepage();
|
|
|
|
umtx_unlock(NULL);
|
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) {
|
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv;
|
|
|
|
cnv = ucnv_open(name, &errorCode);
|
|
|
|
if(U_FAILURE(errorCode) || (cnv == NULL)) {
|
|
|
|
/* Panic time, let's use a fallback. */
|
|
|
|
#if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
|
|
|
|
name = "US-ASCII";
|
2001-11-15 20:18:36 +00:00
|
|
|
/* there is no 'algorithmic' converter for EBCDIC */
|
|
|
|
#elif defined(OS390)
|
|
|
|
name = "ibm-1047-s390";
|
2001-11-12 20:58:22 +00:00
|
|
|
#else
|
2001-11-15 20:18:36 +00:00
|
|
|
name = "ibm-37";
|
2001-11-12 20:58:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
ucnv_close(cnv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name != NULL) {
|
|
|
|
/* Did find a name. And it works.*/
|
|
|
|
defaultConverterName=name;
|
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 */
|
|
|
|
defaultConverterName=NULL;
|
|
|
|
} else {
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
const char *name=ucnv_io_getConverterName(converterName, &errorCode);
|
|
|
|
if(U_SUCCESS(errorCode) && name!=NULL) {
|
|
|
|
defaultConverterName=name;
|
|
|
|
} 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));
|
1999-12-04 00:55:54 +00:00
|
|
|
if(length<sizeof(defaultConverterNameBuffer)) {
|
|
|
|
/* it was not found as an alias, so copy it - accept an empty name */
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool didLock;
|
1999-12-04 00:55:54 +00:00
|
|
|
if(defaultConverterName==defaultConverterNameBuffer) {
|
|
|
|
umtx_lock(NULL);
|
|
|
|
didLock=TRUE;
|
|
|
|
} else {
|
|
|
|
didLock=FALSE;
|
|
|
|
}
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_memcpy(defaultConverterNameBuffer, converterName, length);
|
1999-12-04 00:55:54 +00:00
|
|
|
defaultConverterNameBuffer[length]=0;
|
|
|
|
defaultConverterName=defaultConverterNameBuffer;
|
|
|
|
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:
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|