/* TODO: Need to check alias name length is less than UCNV_MAX_CONVERTER_NAME_LENGTH */
/* STRING_STORE_SIZE + TAG_STORE_SIZE <= ((2^16 - 1) * 2) That is the maximum size for the string stores combined because the strings are indexed at 16-bit boundaries by a 16-bit index, and there is only one section for the strings.
*/ #define STRING_STORE_SIZE 0x1FBFE /* 130046 */ #define TAG_STORE_SIZE 0x400 /* 1024 */
/* The combined tag and converter count can affect the number of lists created. The size of all lists must be less than (2^17 - 1) because the lists are indexed as a 16-bit array with a 16-bit index.
*/ #define MAX_TAG_COUNT 0x3F /* 63 */ #define MAX_CONV_COUNT UCNV_CONVERTER_INDEX_MASK #define MAX_ALIAS_COUNT 0xFFFF /* 65535 */
/* The maximum number of aliases that a standard tag/converter combination can have. At this moment 6/18/2002, IANA has 12 names for ASCII. Don't go below 15 for this value. I don't recommend more than 31 for this value.
*/ #define MAX_TC_ALIAS_COUNT 0x1F /* 31 */
typedefstruct {
uint16_t tag; /* Index into tagStore */
uint16_t totalAliasCount; /* Total aliases in this row */
AliasList aliasList[MAX_CONV_COUNT];
} Tag;
/* Think of this as a 3D array. It's tagCount by converterCount by aliasCount */ static Tag tags[MAX_TAG_COUNT]; static uint16_t tagCount = 0;
/* Used for storing all aliases */ static uint16_t knownAliases[MAX_ALIAS_COUNT]; static uint16_t knownAliasesCount = 0; /*static uint16_t duplicateKnownAliasesCount = 0;*/
/* Used for storing the lists section that point to aliases */ static uint16_t aliasLists[MAX_LIST_SIZE]; static uint16_t aliasListsSize = 0;
/* Were the standard tags declared before the aliases. */ static UBool standardTagsUsed = false; static UBool verbose = false; static UBool quiet = false; staticint lineNum = 1;
externint
main(int argc, char* argv[]) { int i, n; char pathBuf[512];
FileStream *in;
UNewDataMemory *out;
UErrorCode errorCode=U_ZERO_ERROR;
U_MAIN_INIT_ARGS(argc, argv);
/* preset then read command line options */
options[DESTDIR].value=options[SOURCEDIR].value=u_getDataDirectory();
argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
/* error handling, printing usage message */ if(argc<0) {
fprintf(stderr, "error in command line argument \"%s\"\n",
argv[-argc]);
} if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) {
fprintf(stderr, "usage: %s [-options] [convrtrs.txt]\n" "\tread convrtrs.txt and create " U_ICUDATA_NAME "_" DATA_NAME "." DATA_TYPE "\n" "options:\n" "\t-h or -? or --help this usage text\n" "\t-v or --verbose prints out extra information about the alias table\n" "\t-q or --quiet do not display warnings and progress\n" "\t-c or --copyright include a copyright notice\n" "\t-d or --destdir destination directory, followed by the path\n" "\t-s or --sourcedir source directory, followed by the path\n",
argv[0]); return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
}
constchar* sourcedir = options[SOURCEDIR].value; if (sourcedir != NULL && *sourcedir != 0) { if (strlen(sourcedir) + strlen(path) + 2 > sizeof(pathBuf)) {
fprintf(stderr, "The source file name is too long, it must be less than %d in bytes.\n",
(int) sizeof(pathBuf) - 1); exit(U_ILLEGAL_ARGUMENT_ERROR);
} char *end;
uprv_strcpy(pathBuf, sourcedir);
end = uprv_strchr(pathBuf, 0); if(*(end-1)!=U_FILE_SEP_CHAR) {
*(end++)=U_FILE_SEP_CHAR;
}
uprv_strcpy(end, path);
path = pathBuf;
}
/* clean up tags */ for (i = 0; i < MAX_TAG_COUNT; i++) { for (n = 0; n < MAX_CONV_COUNT; n++) { if (tags[i].aliasList[n].aliases!=NULL) {
uprv_free(tags[i].aliasList[n].aliases);
}
}
}
/* Add the empty tag, which is for untagged aliases */
getTagNumber("", 0);
getTagNumber(ALL_TAG_STR, 3);
allocString(&stringBlock, "", 0);
/* read the list of aliases */ while (validParse) {
validParse = false;
/* Read non-empty lines that don't start with a space character. */ while (T_FileStream_readLine(in, lastLine, MAX_LINE_SIZE) != NULL) {
lastLineSize = chomp(lastLine); if (lineSize == 0 || (lastLineSize > 0 && isspace((int)*lastLine))) {
uprv_strcpy(line + lineSize, lastLine);
lineSize += lastLineSize;
} elseif (lineSize > 0) {
validParse = true; break;
}
lineNum++;
}
if (validParse || lineSize > 0) { if (isspace((int)*line)) {
fprintf(stderr, "%s:%d: error: cannot start an alias with a space\n", path, lineNum-1); exit(U_PARSE_ERROR);
} elseif (line[0] == '{') { if (!standardTagsUsed && line[lineSize - 1] != '}') {
fprintf(stderr, "%s:%d: error: alias needs to start with a converter name\n", path, lineNum); exit(U_PARSE_ERROR);
}
addOfficialTaggedStandards(line, lineSize);
standardTagsUsed = true;
} else { if (standardTagsUsed) {
parseLine(line);
} else {
fprintf(stderr, "%s:%d: error: alias table needs to start a list of standard tags\n", path, lineNum); exit(U_PARSE_ERROR);
}
} /* Was the last line consumed */ if (lastLineSize > 0) {
uprv_strcpy(line, lastLine);
lineSize = lastLineSize;
} else {
lineSize = 0;
}
}
lineNum++;
}
}
/* This works almost like the Perl chomp. It removes the newlines, comments and trailing whitespace (not preceding whitespace).
*/ static int32_t
chomp(char *line) { char *s = line; char *lastNonSpace = line; while(*s!=0) { /* truncate at a newline or a comment */ if(*s == '\r' || *s == '\n' || *s == '#') {
*s = 0; break;
} if (!isspace((int)*s)) {
lastNonSpace = s;
}
++s;
} if (lastNonSpace++ > line) {
*lastNonSpace = 0;
s = lastNonSpace;
} return (int32_t)(s - line);
}
/* skip leading white space */ /* There is no whitespace at the beginning anymore */ /* while(line[pos]!=0 && isspace(line[pos])) { ++pos; }
*/
/* is there nothing on this line? */ if(line[pos]==0) { return;
}
/* get the converter name */
start=pos; while(line[pos]!=0 && !isspace((int)line[pos])) {
++pos;
}
limit=pos;
/* store the converter name */
length=(uint16_t)(limit-start);
converter=allocString(&stringBlock, line+start, length);
/* add the converter to the converter table */
cnv=addConverter(converter);
/* The name itself may be tagged, so let's added it to the aliases list properly */
pos = start;
/* get all the real aliases */ for(;;) {
/* skip white space */ while(line[pos]!=0 && isspace((int)line[pos])) {
++pos;
}
/* is there no more alias name on this line? */ if(line[pos]==0) { break;
}
/* get an alias name */
start=pos; while(line[pos]!=0 && line[pos]!='{' && !isspace((int)line[pos])) {
++pos;
}
limit=pos;
/* store the alias name */
length=(uint16_t)(limit-start); if (start == 0) { /* add the converter as its own alias to the alias table */
alias = converter;
addAlias(alias, ALL_TAG_NUM, cnv, true);
} else {
alias=allocString(&stringBlock, line+start, length);
addAlias(alias, ALL_TAG_NUM, cnv, false);
}
addToKnownAliases(alias);
/* add the alias/converter pair to the alias table */ /* addAlias(alias, 0, cnv, false);*/
/* handle tags if they are present */ if (line[pos] == '{') {
++pos; do {
start = pos; while (line[pos] && line[pos] != '}' && !isspace((int)line[pos])) {
++pos;
}
limit = pos;
if (start != limit) { /* add the tag to the tag table */
uint16_t tag = getTagNumber(line + start, (uint16_t)(limit - start));
addAlias(alias, tag, cnv, (UBool)(line[limit-1] == '*'));
}
while (line[pos] && isspace((int)line[pos])) {
++pos;
}
} while (line[pos] && line[pos] != '}');
if (tagCount >= MAX_TAG_COUNT) {
fprintf(stderr, "%s:%d: too many tags\n", path, lineNum); exit(U_BUFFER_OVERFLOW_ERROR);
}
if (preferredName) { /* puts(tag);*/
tagLen--;
}
for (t = 0; t < tagCount; ++t) { constchar *currTag = GET_TAG_STR(tags[t].tag); if (uprv_strlen(currTag) == tagLen && !uprv_strnicmp(currTag, tag, tagLen)) { return t;
}
}
/* we need to add this tag */ if (tagCount >= MAX_TAG_COUNT) {
fprintf(stderr, "%s:%d: error: too many tags\n", path, lineNum); exit(U_BUFFER_OVERFLOW_ERROR);
}
/* allocate a new entry in the tag table */
atag = allocString(&tagBlock, tag, tagLen);
if (standardTagsUsed) {
fprintf(stderr, "%s:%d: error: Tag \"%s\" is not declared at the beginning of the alias table.\n",
path, lineNum, atag); exit(1);
} elseif (tagLen > 0 && strcmp(tag, ALL_TAG_STR) != 0) {
fprintf(stderr, "%s:%d: warning: Tag \"%s\" was added to the list of standards because it was not declared at beginning of the alias table.\n",
path, lineNum, atag);
}
/* add the tag to the tag table */
tags[tagCount].tag = GET_TAG_NUM(atag); /* The aliasList should be set to 0's already */
if(aliasList->aliasCount + 1 >= MAX_TC_ALIAS_COUNT) {
fprintf(stderr, "%s:%d: error: too many aliases for alias %s and converter %s\n", path,
lineNum, alias, GET_ALIAS_STR(converters[converter].converter)); exit(U_BUFFER_OVERFLOW_ERROR);
}
/* Show this warning only once. All aliases are added to the "ALL" tag. */ if (standard == ALL_TAG_NUM && GET_ALIAS_STR(converters[converter].converter) != alias) { /* Normally these option values are parsed at runtime, and they can be discarded when the alias is a default converter. Options should
only be on a converter and not an alias. */ if (uprv_strchr(alias, UCNV_OPTION_SEP_CHAR) != 0)
{
fprintf(stderr, "warning(line %d): alias %s contains a \""UCNV_OPTION_SEP_STRING"\". Options are parsed at run-time and do not need to be in the alias table.\n",
lineNum, alias);
} if (uprv_strchr(alias, UCNV_VALUE_SEP_CHAR) != 0)
{
fprintf(stderr, "warning(line %d): alias %s contains an \""UCNV_VALUE_SEP_STRING"\". Options are parsed at run-time and do not need to be in the alias table.\n",
lineNum, alias);
}
}
if (standard != ALL_TAG_NUM) { /* Check for duplicate aliases for this tag on all converters */ for (idx = 0; idx < converterCount; idx++) { for (idx2 = 0; idx2 < tags[standard].aliasList[idx].aliasCount; idx2++) {
uint16_t aliasNum = tags[standard].aliasList[idx].aliases[idx2]; if (aliasNum
&& ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
{ if (idx == converter) { /* * (alias, standard) duplicates are harmless if they map to the same converter. * Only print a warning in verbose mode, or if the alias is a precise duplicate, * not just a lenient-match duplicate.
*/ if (verbose || 0 == uprv_strcmp(alias, GET_ALIAS_STR(aliasNum))) {
fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard %s and converter %s\n", path,
lineNum, alias, GET_ALIAS_STR(aliasNum),
GET_TAG_STR(tags[standard].tag),
GET_ALIAS_STR(converters[converter].converter));
}
} else {
fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard tag %s between converter %s and converter %s\n", path,
lineNum, alias, GET_ALIAS_STR(aliasNum),
GET_TAG_STR(tags[standard].tag),
GET_ALIAS_STR(converters[converter].converter),
GET_ALIAS_STR(converters[idx].converter));
} break;
}
}
}
/* Check for duplicate default aliases for this converter on all tags */ /* It's okay to have multiple standards prefer the same name */ /* if (verbose && !dupFound) { for (idx = 0; idx < tagCount; idx++) { if (tags[idx].aliasList[converter].aliases) { uint16_t aliasNum = tags[idx].aliasList[converter].aliases[0]; if (aliasNum && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0) { fprintf(stderr, "%s:%d: warning: duplicate alias %s found for converter %s and standard tag %s\n", path, lineNum, alias, GET_ALIAS_STR(converters[converter].converter), GET_TAG_STR(tags[standard].tag)); break; } } }
}*/
}
if (aliasList->aliasCount <= 0) {
aliasList->aliasCount++;
startEmptyWithoutDefault = true;
}
aliasList->aliases = (uint16_t *)uprv_realloc(aliasList->aliases, (aliasList->aliasCount + 1) * sizeof(aliasList->aliases[0])); if (startEmptyWithoutDefault) {
aliasList->aliases[0] = 0;
} if (defaultName) { if (aliasList->aliases[0] != 0) {
fprintf(stderr, "%s:%d: error: Alias %s and %s cannot both be the default alias for standard tag %s and converter %s\n", path,
lineNum,
alias,
GET_ALIAS_STR(aliasList->aliases[0]),
GET_TAG_STR(tags[standard].tag),
GET_ALIAS_STR(converters[converter].converter)); exit(U_PARSE_ERROR);
}
aliasList->aliases[0] = GET_ALIAS_NUM(alias);
} else {
aliasList->aliases[aliasList->aliasCount++] = GET_ALIAS_NUM(alias);
} /* aliasList->converter = converter;*/
converters[converter].totalAliasCount++; /* One more to the column */
tags[standard].totalAliasCount++; /* One more to the row */
return aliasList->aliasCount;
}
static uint16_t
addConverter(constchar *converter) {
uint32_t idx; if(converterCount>=MAX_CONV_COUNT) {
fprintf(stderr, "%s:%d: error: too many converters\n", path, lineNum); exit(U_BUFFER_OVERFLOW_ERROR);
}
/* resolve this alias based on the prioritization of the standard tags. */ staticvoid
resolveAliasToConverter(uint16_t alias, uint16_t *tagNum, uint16_t *converterNum) {
uint16_t idx, idx2, idx3;
for (idx = UCNV_NUM_RESERVED_TAGS; idx < tagCount; idx++) { for (idx2 = 0; idx2 < converterCount; idx2++) { for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) {
uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3]; if (aliasNum == alias) {
*tagNum = idx;
*converterNum = idx2; return;
}
}
}
} /* Do the leftovers last, just in case */ /* There is no need to do the ALL tag */
idx = 0; for (idx2 = 0; idx2 < converterCount; idx2++) { for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) {
uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3]; if (aliasNum == alias) {
*tagNum = idx;
*converterNum = idx2; return;
}
}
}
*tagNum = UINT16_MAX;
*converterNum = UINT16_MAX;
fprintf(stderr, "%s: warning: alias %s not found\n",
path,
GET_ALIAS_STR(alias));
}
/* The knownAliases should be sorted before calling this function */ static uint32_t
resolveAliases(uint16_t *uniqueAliasArr, uint16_t *uniqueAliasToConverterArr, uint16_t aliasOffset) {
uint32_t uniqueAliasIdx = 0;
uint32_t idx;
uint16_t currTagNum, oldTagNum;
uint16_t currConvNum, oldConvNum; constchar *lastName;
/* write into the array area a 1's based index. */
aliasArrLists[tag*converterCount + converter] = aliasListsSize;
/* printf("tag %s converter %s\n", GET_TAG_STR(tags[tag].tag),
GET_ALIAS_STR(converters[converter].converter));*/ for (aliasNum = 0; aliasNum < aliasList->aliasCount; aliasNum++) {
uint16_t value; /* printf(" %s\n",
GET_ALIAS_STR(aliasList->aliases[aliasNum]));*/ if (aliasList->aliases[aliasNum]) {
value = aliasList->aliases[aliasNum] + offset;
} else {
value = 0; if (tag != 0 && !quiet) { /* Only show the warning when it's not the leftover tag. */
fprintf(stderr, "%s: warning: tag %s does not have a default alias for %s\n",
path,
GET_TAG_STR(tags[tag].tag),
GET_ALIAS_STR(converters[converter].converter));
}
}
aliasLists[aliasListsSize++] = value; if (aliasListsSize >= MAX_LIST_SIZE) {
fprintf(stderr, "%s: error: Too many alias lists\n", path); exit(U_BUFFER_OVERFLOW_ERROR);
}
/* Array index starts at 1. aliasLists[0] is the size of the lists section. */
aliasListsSize = 0;
/* write the offsets of all the aliases lists in a 2D array, and create the lists. */ for (i = 0; i < tagCount; ++i) { for (j = 0; j < converterCount; ++j) {
createOneAliasList(aliasArrLists, i, j, aliasOffset);
}
}
/* Write the size of the TOC */ if (tableOptions.stringNormalizationType == UCNV_IO_UNNORMALIZED) {
udata_write32(out, 8);
} else {
udata_write32(out, 9);
}
/* Write the sizes of each section */ /* All sizes are the number of uint16_t units, not bytes */
udata_write32(out, converterCount);
udata_write32(out, tagCount);
udata_write32(out, uniqueAliasesSize); /* list of aliases */
udata_write32(out, uniqueAliasesSize); /* The preresolved form of mapping an untagged the alias to a converter */
udata_write32(out, tagCount * converterCount);
udata_write32(out, aliasListsSize + 1);
udata_write32(out, sizeof(tableOptions) / sizeof(uint16_t));
udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t)); if (tableOptions.stringNormalizationType != UCNV_IO_UNNORMALIZED) {
udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t));
}
/* write the table of converters */ /* Think of this as the column headers */ for(i=0; i<converterCount; ++i) {
udata_write16(out, (uint16_t)(converters[i].converter + aliasOffset));
}
/* write the table of tags */ /* Think of this as the row headers */ for(i=UCNV_NUM_RESERVED_TAGS; i<tagCount; ++i) {
udata_write16(out, tags[i].tag);
} /* The empty tag is considered the leftover list, and put that at the end of the priority list. */
udata_write16(out, tags[EMPTY_TAG_NUM].tag);
udata_write16(out, tags[ALL_TAG_NUM].tag);
/* Write the unique list of aliases */
udata_writeBlock(out, uniqueAliases, uniqueAliasesSize * sizeof(uint16_t));
/* Write the unique list of aliases */
udata_writeBlock(out, uniqueAliasesToConverter, uniqueAliasesSize * sizeof(uint16_t));
/* Write the array to the lists */
udata_writeBlock(out, (constvoid *)(aliasArrLists + (2*converterCount)), (((tagCount - 2) * converterCount) * sizeof(uint16_t))); /* Now write the leftover part of the array for the EMPTY and ALL lists */
udata_writeBlock(out, (constvoid *)aliasArrLists, (2 * converterCount * sizeof(uint16_t)));
/* Offset the next array to make the index start at 1. */
udata_write16(out, 0xDEAD);
/* * add 1 for the terminating NUL * and round up (+1 &~1) * to keep the addresses on a 16-bit boundary
*/
top=block->top + (uint32_t)((length + 1 + 1) & ~1);
if(top >= block->max) {
fprintf(stderr, "%s:%d: error: out of memory\n", path, lineNum); exit(U_MEMORY_ALLOCATION_ERROR);
}
/* get the pointer and copy the string */
p = block->store + block->top;
uprv_memcpy(p, s, length);
p[length] = 0; /* NUL-terminate it */ if((length & 1) == 0) {
p[length + 1] = 0; /* set the padding byte */
}
/* check for invariant characters now that we have a NUL-terminated string for easy output */ if(!uprv_isInvariantString(p, length)) {
fprintf(stderr, "%s:%d: error: the name %s contains not just invariant characters\n", path, lineNum, p); exit(U_INVALID_TABLE_FORMAT);
}
block->top = top; return p;
}
staticint
compareAliases(constvoid *alias1, constvoid *alias2) { /* Names like IBM850 and ibm-850 need to be sorted together */ int result = ucnv_compareNames(GET_ALIAS_STR(*(uint16_t*)alias1), GET_ALIAS_STR(*(uint16_t*)alias2)); if (!result) { /* Sort the shortest first */ return (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias1)) - (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias2));
} return result;
}
/* * Hey, Emacs, please set the following: * * Local Variables: * indent-tabs-mode: nil * End: *
*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.