michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: gencnval.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 1999nov05 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * This program reads convrtrs.txt and writes a memory-mappable michael@0: * converter name alias table to cnvalias.dat . michael@0: * michael@0: * This program currently writes version 2.1 of the data format. See michael@0: * ucnv_io.c for more details on the format. Note that version 2.1 michael@0: * is written in such a way that a 2.0 reader will be able to use it, michael@0: * and a 2.1 reader will be able to read 2.0. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/putil.h" michael@0: #include "unicode/ucnv.h" /* ucnv_compareNames() */ michael@0: #include "ucnv_io.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uinvchar.h" michael@0: #include "filestrm.h" michael@0: #include "unicode/uclean.h" michael@0: #include "unewdata.h" michael@0: #include "uoptions.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* TODO: Need to check alias name length is less than UCNV_MAX_CONVERTER_NAME_LENGTH */ michael@0: michael@0: /* STRING_STORE_SIZE + TAG_STORE_SIZE <= ((2^16 - 1) * 2) michael@0: That is the maximum size for the string stores combined michael@0: because the strings are index at 16-bit boundries by a michael@0: 16-bit index, and there is only one section for the michael@0: strings. michael@0: */ michael@0: #define STRING_STORE_SIZE 0x1FBFE /* 130046 */ michael@0: #define TAG_STORE_SIZE 0x400 /* 1024 */ michael@0: michael@0: /* The combined tag and converter count can affect the number of lists michael@0: created. The size of all lists must be less than (2^17 - 1) michael@0: because the lists are indexed as a 16-bit array with a 16-bit index. michael@0: */ michael@0: #define MAX_TAG_COUNT 0x3F /* 63 */ michael@0: #define MAX_CONV_COUNT UCNV_CONVERTER_INDEX_MASK michael@0: #define MAX_ALIAS_COUNT 0xFFFF /* 65535 */ michael@0: michael@0: /* The maximum number of aliases that a standard tag/converter combination can have. michael@0: At this moment 6/18/2002, IANA has 12 names for ASCII. Don't go below 15 for michael@0: this value. I don't recommend more than 31 for this value. michael@0: */ michael@0: #define MAX_TC_ALIAS_COUNT 0x1F /* 31 */ michael@0: michael@0: #define MAX_LINE_SIZE 0x7FFF /* 32767 */ michael@0: #define MAX_LIST_SIZE 0xFFFF /* 65535 */ michael@0: michael@0: #define DATA_NAME "cnvalias" michael@0: #define DATA_TYPE "icu" /* ICU alias table */ michael@0: michael@0: #define ALL_TAG_STR "ALL" michael@0: #define ALL_TAG_NUM 1 michael@0: #define EMPTY_TAG_NUM 0 michael@0: michael@0: /* UDataInfo cf. udata.h */ michael@0: static const UDataInfo dataInfo={ michael@0: sizeof(UDataInfo), michael@0: 0, michael@0: michael@0: U_IS_BIG_ENDIAN, michael@0: U_CHARSET_FAMILY, michael@0: sizeof(UChar), michael@0: 0, michael@0: michael@0: {0x43, 0x76, 0x41, 0x6c}, /* dataFormat="CvAl" */ michael@0: {3, 0, 1, 0}, /* formatVersion */ michael@0: {1, 4, 2, 0} /* dataVersion */ michael@0: }; michael@0: michael@0: typedef struct { michael@0: char *store; michael@0: uint32_t top; michael@0: uint32_t max; michael@0: } StringBlock; michael@0: michael@0: static char stringStore[STRING_STORE_SIZE]; michael@0: static StringBlock stringBlock = { stringStore, 0, STRING_STORE_SIZE }; michael@0: michael@0: typedef struct { michael@0: uint16_t aliasCount; michael@0: uint16_t *aliases; /* Index into stringStore */ michael@0: } AliasList; michael@0: michael@0: typedef struct { michael@0: uint16_t converter; /* Index into stringStore */ michael@0: uint16_t totalAliasCount; /* Total aliases in this column */ michael@0: } Converter; michael@0: michael@0: static Converter converters[MAX_CONV_COUNT]; michael@0: static uint16_t converterCount=0; michael@0: michael@0: static char tagStore[TAG_STORE_SIZE]; michael@0: static StringBlock tagBlock = { tagStore, 0, TAG_STORE_SIZE }; michael@0: michael@0: typedef struct { michael@0: uint16_t tag; /* Index into tagStore */ michael@0: uint16_t totalAliasCount; /* Total aliases in this row */ michael@0: AliasList aliasList[MAX_CONV_COUNT]; michael@0: } Tag; michael@0: michael@0: /* Think of this as a 3D array. It's tagCount by converterCount by aliasCount */ michael@0: static Tag tags[MAX_TAG_COUNT]; michael@0: static uint16_t tagCount = 0; michael@0: michael@0: /* Used for storing all aliases */ michael@0: static uint16_t knownAliases[MAX_ALIAS_COUNT]; michael@0: static uint16_t knownAliasesCount = 0; michael@0: /*static uint16_t duplicateKnownAliasesCount = 0;*/ michael@0: michael@0: /* Used for storing the lists section that point to aliases */ michael@0: static uint16_t aliasLists[MAX_LIST_SIZE]; michael@0: static uint16_t aliasListsSize = 0; michael@0: michael@0: /* Were the standard tags declared before the aliases. */ michael@0: static UBool standardTagsUsed = FALSE; michael@0: static UBool verbose = FALSE; michael@0: static int lineNum = 1; michael@0: michael@0: static UConverterAliasOptions tableOptions = { michael@0: UCNV_IO_STD_NORMALIZED, michael@0: 1 /* containsCnvOptionInfo */ michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * path to convrtrs.txt michael@0: */ michael@0: const char *path; michael@0: michael@0: /* prototypes --------------------------------------------------------------- */ michael@0: michael@0: static void michael@0: parseLine(const char *line); michael@0: michael@0: static void michael@0: parseFile(FileStream *in); michael@0: michael@0: static int32_t michael@0: chomp(char *line); michael@0: michael@0: static void michael@0: addOfficialTaggedStandards(char *line, int32_t lineLen); michael@0: michael@0: static uint16_t michael@0: addAlias(const char *alias, uint16_t standard, uint16_t converter, UBool defaultName); michael@0: michael@0: static uint16_t michael@0: addConverter(const char *converter); michael@0: michael@0: static char * michael@0: allocString(StringBlock *block, const char *s, int32_t length); michael@0: michael@0: static uint16_t michael@0: addToKnownAliases(const char *alias); michael@0: michael@0: static int michael@0: compareAliases(const void *alias1, const void *alias2); michael@0: michael@0: static uint16_t michael@0: getTagNumber(const char *tag, uint16_t tagLen); michael@0: michael@0: /*static void michael@0: addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter);*/ michael@0: michael@0: static void michael@0: writeAliasTable(UNewDataMemory *out); michael@0: michael@0: /* -------------------------------------------------------------------------- */ michael@0: michael@0: /* Presumes that you used allocString() */ michael@0: #define GET_ALIAS_STR(index) (stringStore + ((size_t)(index) << 1)) michael@0: #define GET_TAG_STR(index) (tagStore + ((size_t)(index) << 1)) michael@0: michael@0: /* Presumes that you used allocString() */ michael@0: #define GET_ALIAS_NUM(str) ((uint16_t)((str - stringStore) >> 1)) michael@0: #define GET_TAG_NUM(str) ((uint16_t)((str - tagStore) >> 1)) michael@0: michael@0: enum michael@0: { michael@0: HELP1, michael@0: HELP2, michael@0: VERBOSE, michael@0: COPYRIGHT, michael@0: DESTDIR, michael@0: SOURCEDIR michael@0: }; michael@0: michael@0: static UOption options[]={ michael@0: UOPTION_HELP_H, michael@0: UOPTION_HELP_QUESTION_MARK, michael@0: UOPTION_VERBOSE, michael@0: UOPTION_COPYRIGHT, michael@0: UOPTION_DESTDIR, michael@0: UOPTION_SOURCEDIR michael@0: }; michael@0: michael@0: extern int michael@0: main(int argc, char* argv[]) { michael@0: int i, n; michael@0: char pathBuf[512]; michael@0: FileStream *in; michael@0: UNewDataMemory *out; michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: michael@0: U_MAIN_INIT_ARGS(argc, argv); michael@0: michael@0: /* preset then read command line options */ michael@0: options[DESTDIR].value=options[SOURCEDIR].value=u_getDataDirectory(); michael@0: argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); michael@0: michael@0: /* error handling, printing usage message */ michael@0: if(argc<0) { michael@0: fprintf(stderr, michael@0: "error in command line argument \"%s\"\n", michael@0: argv[-argc]); michael@0: } michael@0: if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) { michael@0: fprintf(stderr, michael@0: "usage: %s [-options] [convrtrs.txt]\n" michael@0: "\tread convrtrs.txt and create " U_ICUDATA_NAME "_" DATA_NAME "." DATA_TYPE "\n" michael@0: "options:\n" michael@0: "\t-h or -? or --help this usage text\n" michael@0: "\t-v or --verbose prints out extra information about the alias table\n" michael@0: "\t-c or --copyright include a copyright notice\n" michael@0: "\t-d or --destdir destination directory, followed by the path\n" michael@0: "\t-s or --sourcedir source directory, followed by the path\n", michael@0: argv[0]); michael@0: return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; michael@0: } michael@0: michael@0: if(options[VERBOSE].doesOccur) { michael@0: verbose = TRUE; michael@0: } michael@0: michael@0: if(argc>=2) { michael@0: path=argv[1]; michael@0: } else { michael@0: path=options[SOURCEDIR].value; michael@0: if(path!=NULL && *path!=0) { michael@0: char *end; michael@0: michael@0: uprv_strcpy(pathBuf, path); michael@0: end = uprv_strchr(pathBuf, 0); michael@0: if(*(end-1)!=U_FILE_SEP_CHAR) { michael@0: *(end++)=U_FILE_SEP_CHAR; michael@0: } michael@0: uprv_strcpy(end, "convrtrs.txt"); michael@0: path=pathBuf; michael@0: } else { michael@0: path = "convrtrs.txt"; michael@0: } michael@0: } michael@0: michael@0: uprv_memset(stringStore, 0, sizeof(stringStore)); michael@0: uprv_memset(tagStore, 0, sizeof(tagStore)); michael@0: uprv_memset(converters, 0, sizeof(converters)); michael@0: uprv_memset(tags, 0, sizeof(tags)); michael@0: uprv_memset(aliasLists, 0, sizeof(aliasLists)); michael@0: uprv_memset(knownAliases, 0, sizeof(aliasLists)); michael@0: michael@0: michael@0: in=T_FileStream_open(path, "r"); michael@0: if(in==NULL) { michael@0: fprintf(stderr, "gencnval: unable to open input file %s\n", path); michael@0: exit(U_FILE_ACCESS_ERROR); michael@0: } michael@0: parseFile(in); michael@0: T_FileStream_close(in); michael@0: michael@0: /* create the output file */ michael@0: out=udata_create(options[DESTDIR].value, DATA_TYPE, DATA_NAME, &dataInfo, michael@0: options[COPYRIGHT].doesOccur ? U_COPYRIGHT_STRING : NULL, &errorCode); michael@0: if(U_FAILURE(errorCode)) { michael@0: fprintf(stderr, "gencnval: unable to open output file - error %s\n", u_errorName(errorCode)); michael@0: exit(errorCode); michael@0: } michael@0: michael@0: /* write the table of aliases based on a tag/converter name combination */ michael@0: writeAliasTable(out); michael@0: michael@0: /* finish */ michael@0: udata_finish(out, &errorCode); michael@0: if(U_FAILURE(errorCode)) { michael@0: fprintf(stderr, "gencnval: error finishing output file - %s\n", u_errorName(errorCode)); michael@0: exit(errorCode); michael@0: } michael@0: michael@0: /* clean up tags */ michael@0: for (i = 0; i < MAX_TAG_COUNT; i++) { michael@0: for (n = 0; n < MAX_CONV_COUNT; n++) { michael@0: if (tags[i].aliasList[n].aliases!=NULL) { michael@0: uprv_free(tags[i].aliasList[n].aliases); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: parseFile(FileStream *in) { michael@0: char line[MAX_LINE_SIZE]; michael@0: char lastLine[MAX_LINE_SIZE]; michael@0: int32_t lineSize = 0; michael@0: int32_t lastLineSize = 0; michael@0: UBool validParse = TRUE; michael@0: michael@0: lineNum = 0; michael@0: michael@0: /* Add the empty tag, which is for untagged aliases */ michael@0: getTagNumber("", 0); michael@0: getTagNumber(ALL_TAG_STR, 3); michael@0: allocString(&stringBlock, "", 0); michael@0: michael@0: /* read the list of aliases */ michael@0: while (validParse) { michael@0: validParse = FALSE; michael@0: michael@0: /* Read non-empty lines that don't start with a space character. */ michael@0: while (T_FileStream_readLine(in, lastLine, MAX_LINE_SIZE) != NULL) { michael@0: lastLineSize = chomp(lastLine); michael@0: if (lineSize == 0 || (lastLineSize > 0 && isspace((int)*lastLine))) { michael@0: uprv_strcpy(line + lineSize, lastLine); michael@0: lineSize += lastLineSize; michael@0: } else if (lineSize > 0) { michael@0: validParse = TRUE; michael@0: break; michael@0: } michael@0: lineNum++; michael@0: } michael@0: michael@0: if (validParse || lineSize > 0) { michael@0: if (isspace((int)*line)) { michael@0: fprintf(stderr, "%s:%d: error: cannot start an alias with a space\n", path, lineNum-1); michael@0: exit(U_PARSE_ERROR); michael@0: } else if (line[0] == '{') { michael@0: if (!standardTagsUsed && line[lineSize - 1] != '}') { michael@0: fprintf(stderr, "%s:%d: error: alias needs to start with a converter name\n", path, lineNum); michael@0: exit(U_PARSE_ERROR); michael@0: } michael@0: addOfficialTaggedStandards(line, lineSize); michael@0: standardTagsUsed = TRUE; michael@0: } else { michael@0: if (standardTagsUsed) { michael@0: parseLine(line); michael@0: } michael@0: else { michael@0: fprintf(stderr, "%s:%d: error: alias table needs to start a list of standard tags\n", path, lineNum); michael@0: exit(U_PARSE_ERROR); michael@0: } michael@0: } michael@0: /* Was the last line consumed */ michael@0: if (lastLineSize > 0) { michael@0: uprv_strcpy(line, lastLine); michael@0: lineSize = lastLineSize; michael@0: } michael@0: else { michael@0: lineSize = 0; michael@0: } michael@0: } michael@0: lineNum++; michael@0: } michael@0: } michael@0: michael@0: /* This works almost like the Perl chomp. michael@0: It removes the newlines, comments and trailing whitespace (not preceding whitespace). michael@0: */ michael@0: static int32_t michael@0: chomp(char *line) { michael@0: char *s = line; michael@0: char *lastNonSpace = line; michael@0: while(*s!=0) { michael@0: /* truncate at a newline or a comment */ michael@0: if(*s == '\r' || *s == '\n' || *s == '#') { michael@0: *s = 0; michael@0: break; michael@0: } michael@0: if (!isspace((int)*s)) { michael@0: lastNonSpace = s; michael@0: } michael@0: ++s; michael@0: } michael@0: if (lastNonSpace++ > line) { michael@0: *lastNonSpace = 0; michael@0: s = lastNonSpace; michael@0: } michael@0: return (int32_t)(s - line); michael@0: } michael@0: michael@0: static void michael@0: parseLine(const char *line) { michael@0: uint16_t pos=0, start, limit, length, cnv; michael@0: char *converter, *alias; michael@0: michael@0: /* skip leading white space */ michael@0: /* There is no whitespace at the beginning anymore */ michael@0: /* while(line[pos]!=0 && isspace(line[pos])) { michael@0: ++pos; michael@0: } michael@0: */ michael@0: michael@0: /* is there nothing on this line? */ michael@0: if(line[pos]==0) { michael@0: return; michael@0: } michael@0: michael@0: /* get the converter name */ michael@0: start=pos; michael@0: while(line[pos]!=0 && !isspace((int)line[pos])) { michael@0: ++pos; michael@0: } michael@0: limit=pos; michael@0: michael@0: /* store the converter name */ michael@0: length=(uint16_t)(limit-start); michael@0: converter=allocString(&stringBlock, line+start, length); michael@0: michael@0: /* add the converter to the converter table */ michael@0: cnv=addConverter(converter); michael@0: michael@0: /* The name itself may be tagged, so let's added it to the aliases list properly */ michael@0: pos = start; michael@0: michael@0: /* get all the real aliases */ michael@0: for(;;) { michael@0: michael@0: /* skip white space */ michael@0: while(line[pos]!=0 && isspace((int)line[pos])) { michael@0: ++pos; michael@0: } michael@0: michael@0: /* is there no more alias name on this line? */ michael@0: if(line[pos]==0) { michael@0: break; michael@0: } michael@0: michael@0: /* get an alias name */ michael@0: start=pos; michael@0: while(line[pos]!=0 && line[pos]!='{' && !isspace((int)line[pos])) { michael@0: ++pos; michael@0: } michael@0: limit=pos; michael@0: michael@0: /* store the alias name */ michael@0: length=(uint16_t)(limit-start); michael@0: if (start == 0) { michael@0: /* add the converter as its own alias to the alias table */ michael@0: alias = converter; michael@0: addAlias(alias, ALL_TAG_NUM, cnv, TRUE); michael@0: } michael@0: else { michael@0: alias=allocString(&stringBlock, line+start, length); michael@0: addAlias(alias, ALL_TAG_NUM, cnv, FALSE); michael@0: } michael@0: addToKnownAliases(alias); michael@0: michael@0: /* add the alias/converter pair to the alias table */ michael@0: /* addAlias(alias, 0, cnv, FALSE);*/ michael@0: michael@0: /* skip whitespace */ michael@0: while (line[pos] && isspace((int)line[pos])) { michael@0: ++pos; michael@0: } michael@0: michael@0: /* handle tags if they are present */ michael@0: if (line[pos] == '{') { michael@0: ++pos; michael@0: do { michael@0: start = pos; michael@0: while (line[pos] && line[pos] != '}' && !isspace((int)line[pos])) { michael@0: ++pos; michael@0: } michael@0: limit = pos; michael@0: michael@0: if (start != limit) { michael@0: /* add the tag to the tag table */ michael@0: uint16_t tag = getTagNumber(line + start, (uint16_t)(limit - start)); michael@0: addAlias(alias, tag, cnv, (UBool)(line[limit-1] == '*')); michael@0: } michael@0: michael@0: while (line[pos] && isspace((int)line[pos])) { michael@0: ++pos; michael@0: } michael@0: } while (line[pos] && line[pos] != '}'); michael@0: michael@0: if (line[pos] == '}') { michael@0: ++pos; michael@0: } else { michael@0: fprintf(stderr, "%s:%d: Unterminated tag list\n", path, lineNum); michael@0: exit(U_UNMATCHED_BRACES); michael@0: } michael@0: } else { michael@0: addAlias(alias, EMPTY_TAG_NUM, cnv, (UBool)(tags[0].aliasList[cnv].aliasCount == 0)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static uint16_t michael@0: getTagNumber(const char *tag, uint16_t tagLen) { michael@0: char *atag; michael@0: uint16_t t; michael@0: UBool preferredName = ((tagLen > 0) ? (tag[tagLen - 1] == '*') : (FALSE)); michael@0: michael@0: if (tagCount >= MAX_TAG_COUNT) { michael@0: fprintf(stderr, "%s:%d: too many tags\n", path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: michael@0: if (preferredName) { michael@0: /* puts(tag);*/ michael@0: tagLen--; michael@0: } michael@0: michael@0: for (t = 0; t < tagCount; ++t) { michael@0: const char *currTag = GET_TAG_STR(tags[t].tag); michael@0: if (uprv_strlen(currTag) == tagLen && !uprv_strnicmp(currTag, tag, tagLen)) { michael@0: return t; michael@0: } michael@0: } michael@0: michael@0: /* we need to add this tag */ michael@0: if (tagCount >= MAX_TAG_COUNT) { michael@0: fprintf(stderr, "%s:%d: error: too many tags\n", path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: michael@0: /* allocate a new entry in the tag table */ michael@0: atag = allocString(&tagBlock, tag, tagLen); michael@0: michael@0: if (standardTagsUsed) { michael@0: fprintf(stderr, "%s:%d: error: Tag \"%s\" is not declared at the beginning of the alias table.\n", michael@0: path, lineNum, atag); michael@0: exit(1); michael@0: } michael@0: else if (tagLen > 0 && strcmp(tag, ALL_TAG_STR) != 0) { michael@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", michael@0: path, lineNum, atag); michael@0: } michael@0: michael@0: /* add the tag to the tag table */ michael@0: tags[tagCount].tag = GET_TAG_NUM(atag); michael@0: /* The aliasList should be set to 0's already */ michael@0: michael@0: return tagCount++; michael@0: } michael@0: michael@0: /*static void michael@0: addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter) { michael@0: tags[tag].aliases[converter] = alias; michael@0: } michael@0: */ michael@0: michael@0: static void michael@0: addOfficialTaggedStandards(char *line, int32_t lineLen) { michael@0: char *atag; michael@0: char *endTagExp; michael@0: char *tag; michael@0: static const char WHITESPACE[] = " \t"; michael@0: michael@0: if (tagCount > UCNV_NUM_RESERVED_TAGS) { michael@0: fprintf(stderr, "%s:%d: error: official tags already added\n", path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: tag = strchr(line, '{'); michael@0: if (tag == NULL) { michael@0: /* Why were we called? */ michael@0: fprintf(stderr, "%s:%d: error: Missing start of tag group\n", path, lineNum); michael@0: exit(U_PARSE_ERROR); michael@0: } michael@0: tag++; michael@0: endTagExp = strchr(tag, '}'); michael@0: if (endTagExp == NULL) { michael@0: fprintf(stderr, "%s:%d: error: Missing end of tag group\n", path, lineNum); michael@0: exit(U_PARSE_ERROR); michael@0: } michael@0: endTagExp[0] = 0; michael@0: michael@0: tag = strtok(tag, WHITESPACE); michael@0: while (tag != NULL) { michael@0: /* printf("Adding original tag \"%s\"\n", tag);*/ michael@0: michael@0: /* allocate a new entry in the tag table */ michael@0: atag = allocString(&tagBlock, tag, -1); michael@0: michael@0: /* add the tag to the tag table */ michael@0: tags[tagCount++].tag = (uint16_t)((atag - tagStore) >> 1); michael@0: michael@0: /* The aliasList should already be set to 0's */ michael@0: michael@0: /* Get next tag */ michael@0: tag = strtok(NULL, WHITESPACE); michael@0: } michael@0: } michael@0: michael@0: static uint16_t michael@0: addToKnownAliases(const char *alias) { michael@0: /* uint32_t idx; */ michael@0: /* strict matching */ michael@0: /* for (idx = 0; idx < knownAliasesCount; idx++) { michael@0: uint16_t num = GET_ALIAS_NUM(alias); michael@0: if (knownAliases[idx] != num michael@0: && uprv_strcmp(alias, GET_ALIAS_STR(knownAliases[idx])) == 0) michael@0: { michael@0: fprintf(stderr, "%s:%d: warning: duplicate alias %s and %s found\n", path, michael@0: lineNum, alias, GET_ALIAS_STR(knownAliases[idx])); michael@0: duplicateKnownAliasesCount++; michael@0: break; michael@0: } michael@0: else if (knownAliases[idx] != num michael@0: && ucnv_compareNames(alias, GET_ALIAS_STR(knownAliases[idx])) == 0) michael@0: { michael@0: if (verbose) { michael@0: fprintf(stderr, "%s:%d: information: duplicate alias %s and %s found\n", path, michael@0: lineNum, alias, GET_ALIAS_STR(knownAliases[idx])); michael@0: } michael@0: duplicateKnownAliasesCount++; michael@0: break; michael@0: } michael@0: } michael@0: */ michael@0: if (knownAliasesCount >= MAX_ALIAS_COUNT) { michael@0: fprintf(stderr, "%s:%d: warning: Too many aliases defined for all converters\n", michael@0: path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: /* TODO: We could try to unlist exact duplicates. */ michael@0: return knownAliases[knownAliasesCount++] = GET_ALIAS_NUM(alias); michael@0: } michael@0: michael@0: /* michael@0: @param standard When standard is 0, then it's the "empty" tag. michael@0: */ michael@0: static uint16_t michael@0: addAlias(const char *alias, uint16_t standard, uint16_t converter, UBool defaultName) { michael@0: uint32_t idx, idx2; michael@0: UBool startEmptyWithoutDefault = FALSE; michael@0: AliasList *aliasList; michael@0: michael@0: if(standard>=MAX_TAG_COUNT) { michael@0: fprintf(stderr, "%s:%d: error: too many standard tags\n", path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: if(converter>=MAX_CONV_COUNT) { michael@0: fprintf(stderr, "%s:%d: error: too many converter names\n", path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: aliasList = &tags[standard].aliasList[converter]; michael@0: michael@0: if (strchr(alias, '}')) { michael@0: fprintf(stderr, "%s:%d: error: unmatched } found\n", path, michael@0: lineNum); michael@0: } michael@0: michael@0: if(aliasList->aliasCount + 1 >= MAX_TC_ALIAS_COUNT) { michael@0: fprintf(stderr, "%s:%d: error: too many aliases for alias %s and converter %s\n", path, michael@0: lineNum, alias, GET_ALIAS_STR(converters[converter].converter)); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: michael@0: /* Show this warning only once. All aliases are added to the "ALL" tag. */ michael@0: if (standard == ALL_TAG_NUM && GET_ALIAS_STR(converters[converter].converter) != alias) { michael@0: /* Normally these option values are parsed at runtime, and they can michael@0: be discarded when the alias is a default converter. Options should michael@0: only be on a converter and not an alias. */ michael@0: if (uprv_strchr(alias, UCNV_OPTION_SEP_CHAR) != 0) michael@0: { michael@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", michael@0: lineNum, alias); michael@0: } michael@0: if (uprv_strchr(alias, UCNV_VALUE_SEP_CHAR) != 0) michael@0: { michael@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", michael@0: lineNum, alias); michael@0: } michael@0: } michael@0: michael@0: if (standard != ALL_TAG_NUM) { michael@0: /* Check for duplicate aliases for this tag on all converters */ michael@0: for (idx = 0; idx < converterCount; idx++) { michael@0: for (idx2 = 0; idx2 < tags[standard].aliasList[idx].aliasCount; idx2++) { michael@0: uint16_t aliasNum = tags[standard].aliasList[idx].aliases[idx2]; michael@0: if (aliasNum michael@0: && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0) michael@0: { michael@0: if (idx == converter) { michael@0: /* michael@0: * (alias, standard) duplicates are harmless if they map to the same converter. michael@0: * Only print a warning in verbose mode, or if the alias is a precise duplicate, michael@0: * not just a lenient-match duplicate. michael@0: */ michael@0: if (verbose || 0 == uprv_strcmp(alias, GET_ALIAS_STR(aliasNum))) { michael@0: fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard %s and converter %s\n", path, michael@0: lineNum, alias, GET_ALIAS_STR(aliasNum), michael@0: GET_TAG_STR(tags[standard].tag), michael@0: GET_ALIAS_STR(converters[converter].converter)); michael@0: } michael@0: } else { michael@0: fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard tag %s between converter %s and converter %s\n", path, michael@0: lineNum, alias, GET_ALIAS_STR(aliasNum), michael@0: GET_TAG_STR(tags[standard].tag), michael@0: GET_ALIAS_STR(converters[converter].converter), michael@0: GET_ALIAS_STR(converters[idx].converter)); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Check for duplicate default aliases for this converter on all tags */ michael@0: /* It's okay to have multiple standards prefer the same name */ michael@0: /* if (verbose && !dupFound) { michael@0: for (idx = 0; idx < tagCount; idx++) { michael@0: if (tags[idx].aliasList[converter].aliases) { michael@0: uint16_t aliasNum = tags[idx].aliasList[converter].aliases[0]; michael@0: if (aliasNum michael@0: && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0) michael@0: { michael@0: fprintf(stderr, "%s:%d: warning: duplicate alias %s found for converter %s and standard tag %s\n", path, michael@0: lineNum, alias, GET_ALIAS_STR(converters[converter].converter), GET_TAG_STR(tags[standard].tag)); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: }*/ michael@0: } michael@0: michael@0: if (aliasList->aliasCount <= 0) { michael@0: aliasList->aliasCount++; michael@0: startEmptyWithoutDefault = TRUE; michael@0: } michael@0: aliasList->aliases = (uint16_t *)uprv_realloc(aliasList->aliases, (aliasList->aliasCount + 1) * sizeof(aliasList->aliases[0])); michael@0: if (startEmptyWithoutDefault) { michael@0: aliasList->aliases[0] = 0; michael@0: } michael@0: if (defaultName) { michael@0: if (aliasList->aliases[0] != 0) { michael@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, michael@0: lineNum, michael@0: alias, michael@0: GET_ALIAS_STR(aliasList->aliases[0]), michael@0: GET_TAG_STR(tags[standard].tag), michael@0: GET_ALIAS_STR(converters[converter].converter)); michael@0: exit(U_PARSE_ERROR); michael@0: } michael@0: aliasList->aliases[0] = GET_ALIAS_NUM(alias); michael@0: } else { michael@0: aliasList->aliases[aliasList->aliasCount++] = GET_ALIAS_NUM(alias); michael@0: } michael@0: /* aliasList->converter = converter;*/ michael@0: michael@0: converters[converter].totalAliasCount++; /* One more to the column */ michael@0: tags[standard].totalAliasCount++; /* One more to the row */ michael@0: michael@0: return aliasList->aliasCount; michael@0: } michael@0: michael@0: static uint16_t michael@0: addConverter(const char *converter) { michael@0: uint32_t idx; michael@0: if(converterCount>=MAX_CONV_COUNT) { michael@0: fprintf(stderr, "%s:%d: error: too many converters\n", path, lineNum); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: michael@0: for (idx = 0; idx < converterCount; idx++) { michael@0: if (ucnv_compareNames(converter, GET_ALIAS_STR(converters[idx].converter)) == 0) { michael@0: fprintf(stderr, "%s:%d: error: duplicate converter %s found!\n", path, lineNum, converter); michael@0: exit(U_PARSE_ERROR); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: converters[converterCount].converter = GET_ALIAS_NUM(converter); michael@0: converters[converterCount].totalAliasCount = 0; michael@0: michael@0: return converterCount++; michael@0: } michael@0: michael@0: /* resolve this alias based on the prioritization of the standard tags. */ michael@0: static void michael@0: resolveAliasToConverter(uint16_t alias, uint16_t *tagNum, uint16_t *converterNum) { michael@0: uint16_t idx, idx2, idx3; michael@0: michael@0: for (idx = UCNV_NUM_RESERVED_TAGS; idx < tagCount; idx++) { michael@0: for (idx2 = 0; idx2 < converterCount; idx2++) { michael@0: for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) { michael@0: uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3]; michael@0: if (aliasNum == alias) { michael@0: *tagNum = idx; michael@0: *converterNum = idx2; michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: /* Do the leftovers last, just in case */ michael@0: /* There is no need to do the ALL tag */ michael@0: idx = 0; michael@0: for (idx2 = 0; idx2 < converterCount; idx2++) { michael@0: for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) { michael@0: uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3]; michael@0: if (aliasNum == alias) { michael@0: *tagNum = idx; michael@0: *converterNum = idx2; michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: *tagNum = UINT16_MAX; michael@0: *converterNum = UINT16_MAX; michael@0: fprintf(stderr, "%s: warning: alias %s not found\n", michael@0: path, michael@0: GET_ALIAS_STR(alias)); michael@0: return; michael@0: } michael@0: michael@0: /* The knownAliases should be sorted before calling this function */ michael@0: static uint32_t michael@0: resolveAliases(uint16_t *uniqueAliasArr, uint16_t *uniqueAliasToConverterArr, uint16_t aliasOffset) { michael@0: uint32_t uniqueAliasIdx = 0; michael@0: uint32_t idx; michael@0: uint16_t currTagNum, oldTagNum; michael@0: uint16_t currConvNum, oldConvNum; michael@0: const char *lastName; michael@0: michael@0: resolveAliasToConverter(knownAliases[0], &oldTagNum, &currConvNum); michael@0: uniqueAliasToConverterArr[uniqueAliasIdx] = currConvNum; michael@0: oldConvNum = currConvNum; michael@0: uniqueAliasArr[uniqueAliasIdx] = knownAliases[0] + aliasOffset; michael@0: uniqueAliasIdx++; michael@0: lastName = GET_ALIAS_STR(knownAliases[0]); michael@0: michael@0: for (idx = 1; idx < knownAliasesCount; idx++) { michael@0: resolveAliasToConverter(knownAliases[idx], &currTagNum, &currConvNum); michael@0: if (ucnv_compareNames(lastName, GET_ALIAS_STR(knownAliases[idx])) == 0) { michael@0: /* duplicate found */ michael@0: if ((currTagNum < oldTagNum && currTagNum >= UCNV_NUM_RESERVED_TAGS) michael@0: || oldTagNum == 0) { michael@0: oldTagNum = currTagNum; michael@0: uniqueAliasToConverterArr[uniqueAliasIdx - 1] = currConvNum; michael@0: uniqueAliasArr[uniqueAliasIdx - 1] = knownAliases[idx] + aliasOffset; michael@0: if (verbose) { michael@0: printf("using %s instead of %s -> %s", michael@0: GET_ALIAS_STR(knownAliases[idx]), michael@0: lastName, michael@0: GET_ALIAS_STR(converters[currConvNum].converter)); michael@0: if (oldConvNum != currConvNum) { michael@0: printf(" (alias conflict)"); michael@0: } michael@0: puts(""); michael@0: } michael@0: } michael@0: else { michael@0: /* else ignore it */ michael@0: if (verbose) { michael@0: printf("folding %s into %s -> %s", michael@0: GET_ALIAS_STR(knownAliases[idx]), michael@0: lastName, michael@0: GET_ALIAS_STR(converters[oldConvNum].converter)); michael@0: if (oldConvNum != currConvNum) { michael@0: printf(" (alias conflict)"); michael@0: } michael@0: puts(""); michael@0: } michael@0: } michael@0: if (oldConvNum != currConvNum) { michael@0: uniqueAliasToConverterArr[uniqueAliasIdx - 1] |= UCNV_AMBIGUOUS_ALIAS_MAP_BIT; michael@0: } michael@0: } michael@0: else { michael@0: uniqueAliasToConverterArr[uniqueAliasIdx] = currConvNum; michael@0: oldConvNum = currConvNum; michael@0: uniqueAliasArr[uniqueAliasIdx] = knownAliases[idx] + aliasOffset; michael@0: uniqueAliasIdx++; michael@0: lastName = GET_ALIAS_STR(knownAliases[idx]); michael@0: oldTagNum = currTagNum; michael@0: /*printf("%s -> %s\n", GET_ALIAS_STR(knownAliases[idx]), GET_ALIAS_STR(converters[currConvNum].converter));*/ michael@0: } michael@0: if (uprv_strchr(GET_ALIAS_STR(converters[currConvNum].converter), UCNV_OPTION_SEP_CHAR) != NULL) { michael@0: uniqueAliasToConverterArr[uniqueAliasIdx-1] |= UCNV_CONTAINS_OPTION_BIT; michael@0: } michael@0: } michael@0: return uniqueAliasIdx; michael@0: } michael@0: michael@0: static void michael@0: createOneAliasList(uint16_t *aliasArrLists, uint32_t tag, uint32_t converter, uint16_t offset) { michael@0: uint32_t aliasNum; michael@0: AliasList *aliasList = &tags[tag].aliasList[converter]; michael@0: michael@0: if (aliasList->aliasCount == 0) { michael@0: aliasArrLists[tag*converterCount + converter] = 0; michael@0: } michael@0: else { michael@0: aliasLists[aliasListsSize++] = aliasList->aliasCount; michael@0: michael@0: /* write into the array area a 1's based index. */ michael@0: aliasArrLists[tag*converterCount + converter] = aliasListsSize; michael@0: michael@0: /* printf("tag %s converter %s\n", michael@0: GET_TAG_STR(tags[tag].tag), michael@0: GET_ALIAS_STR(converters[converter].converter));*/ michael@0: for (aliasNum = 0; aliasNum < aliasList->aliasCount; aliasNum++) { michael@0: uint16_t value; michael@0: /* printf(" %s\n", michael@0: GET_ALIAS_STR(aliasList->aliases[aliasNum]));*/ michael@0: if (aliasList->aliases[aliasNum]) { michael@0: value = aliasList->aliases[aliasNum] + offset; michael@0: } else { michael@0: value = 0; michael@0: if (tag != 0) { /* Only show the warning when it's not the leftover tag. */ michael@0: fprintf(stderr, "%s: warning: tag %s does not have a default alias for %s\n", michael@0: path, michael@0: GET_TAG_STR(tags[tag].tag), michael@0: GET_ALIAS_STR(converters[converter].converter)); michael@0: } michael@0: } michael@0: aliasLists[aliasListsSize++] = value; michael@0: if (aliasListsSize >= MAX_LIST_SIZE) { michael@0: fprintf(stderr, "%s: error: Too many alias lists\n", path); michael@0: exit(U_BUFFER_OVERFLOW_ERROR); michael@0: } michael@0: michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: createNormalizedAliasStrings(char *normalizedStrings, const char *origStringBlock, int32_t stringBlockLength) { michael@0: int32_t currStrLen; michael@0: uprv_memcpy(normalizedStrings, origStringBlock, stringBlockLength); michael@0: while ((currStrLen = (int32_t)uprv_strlen(origStringBlock)) < stringBlockLength) { michael@0: int32_t currStrSize = currStrLen + 1; michael@0: if (currStrLen > 0) { michael@0: int32_t normStrLen; michael@0: ucnv_io_stripForCompare(normalizedStrings, origStringBlock); michael@0: normStrLen = uprv_strlen(normalizedStrings); michael@0: if (normStrLen > 0) { michael@0: uprv_memset(normalizedStrings + normStrLen, 0, currStrSize - normStrLen); michael@0: } michael@0: } michael@0: stringBlockLength -= currStrSize; michael@0: normalizedStrings += currStrSize; michael@0: origStringBlock += currStrSize; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: writeAliasTable(UNewDataMemory *out) { michael@0: uint32_t i, j; michael@0: uint32_t uniqueAliasesSize; michael@0: uint16_t aliasOffset = (uint16_t)(tagBlock.top/sizeof(uint16_t)); michael@0: uint16_t *aliasArrLists = (uint16_t *)uprv_malloc(tagCount * converterCount * sizeof(uint16_t)); michael@0: uint16_t *uniqueAliases = (uint16_t *)uprv_malloc(knownAliasesCount * sizeof(uint16_t)); michael@0: uint16_t *uniqueAliasesToConverter = (uint16_t *)uprv_malloc(knownAliasesCount * sizeof(uint16_t)); michael@0: michael@0: qsort(knownAliases, knownAliasesCount, sizeof(knownAliases[0]), compareAliases); michael@0: uniqueAliasesSize = resolveAliases(uniqueAliases, uniqueAliasesToConverter, aliasOffset); michael@0: michael@0: /* Array index starts at 1. aliasLists[0] is the size of the lists section. */ michael@0: aliasListsSize = 0; michael@0: michael@0: /* write the offsets of all the aliases lists in a 2D array, and create the lists. */ michael@0: for (i = 0; i < tagCount; ++i) { michael@0: for (j = 0; j < converterCount; ++j) { michael@0: createOneAliasList(aliasArrLists, i, j, aliasOffset); michael@0: } michael@0: } michael@0: michael@0: /* Write the size of the TOC */ michael@0: if (tableOptions.stringNormalizationType == UCNV_IO_UNNORMALIZED) { michael@0: udata_write32(out, 8); michael@0: } michael@0: else { michael@0: udata_write32(out, 9); michael@0: } michael@0: michael@0: /* Write the sizes of each section */ michael@0: /* All sizes are the number of uint16_t units, not bytes */ michael@0: udata_write32(out, converterCount); michael@0: udata_write32(out, tagCount); michael@0: udata_write32(out, uniqueAliasesSize); /* list of aliases */ michael@0: udata_write32(out, uniqueAliasesSize); /* The preresolved form of mapping an untagged the alias to a converter */ michael@0: udata_write32(out, tagCount * converterCount); michael@0: udata_write32(out, aliasListsSize + 1); michael@0: udata_write32(out, sizeof(tableOptions) / sizeof(uint16_t)); michael@0: udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t)); michael@0: if (tableOptions.stringNormalizationType != UCNV_IO_UNNORMALIZED) { michael@0: udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t)); michael@0: } michael@0: michael@0: /* write the table of converters */ michael@0: /* Think of this as the column headers */ michael@0: for(i=0; itop + (uint32_t)((length + 1 + 1) & ~1); michael@0: michael@0: if(top >= block->max) { michael@0: fprintf(stderr, "%s:%d: error: out of memory\n", path, lineNum); michael@0: exit(U_MEMORY_ALLOCATION_ERROR); michael@0: } michael@0: michael@0: /* get the pointer and copy the string */ michael@0: p = block->store + block->top; michael@0: uprv_memcpy(p, s, length); michael@0: p[length] = 0; /* NUL-terminate it */ michael@0: if((length & 1) == 0) { michael@0: p[length + 1] = 0; /* set the padding byte */ michael@0: } michael@0: michael@0: /* check for invariant characters now that we have a NUL-terminated string for easy output */ michael@0: if(!uprv_isInvariantString(p, length)) { michael@0: fprintf(stderr, "%s:%d: error: the name %s contains not just invariant characters\n", path, lineNum, p); michael@0: exit(U_INVALID_TABLE_FORMAT); michael@0: } michael@0: michael@0: block->top = top; michael@0: return p; michael@0: } michael@0: michael@0: static int michael@0: compareAliases(const void *alias1, const void *alias2) { michael@0: /* Names like IBM850 and ibm-850 need to be sorted together */ michael@0: int result = ucnv_compareNames(GET_ALIAS_STR(*(uint16_t*)alias1), GET_ALIAS_STR(*(uint16_t*)alias2)); michael@0: if (!result) { michael@0: /* Sort the shortest first */ michael@0: return (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias1)) - (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias2)); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /* michael@0: * Hey, Emacs, please set the following: michael@0: * michael@0: * Local Variables: michael@0: * indent-tabs-mode: nil michael@0: * End: michael@0: * michael@0: */ michael@0: