2 *******************************************************************************
4 * Copyright (C) 1999-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: gencnval.c
10 * tab size: 8 (not used)
13 * created on: 1999nov05
14 * created by: Markus W. Scherer
16 * This program reads convrtrs.txt and writes a memory-mappable
17 * converter name alias table to cnvalias.dat .
19 * This program currently writes version 2.1 of the data format. See
20 * ucnv_io.c for more details on the format. Note that version 2.1
21 * is written in such a way that a 2.0 reader will be able to use it,
22 * and a 2.1 reader will be able to read 2.0.
25 #include "unicode/utypes.h"
26 #include "unicode/putil.h"
27 #include "unicode/ucnv.h" /* ucnv_compareNames() */
33 #include "unicode/uclean.h"
41 /* TODO: Need to check alias name length is less than UCNV_MAX_CONVERTER_NAME_LENGTH */
43 /* STRING_STORE_SIZE + TAG_STORE_SIZE <= ((2^16 - 1) * 2)
44 That is the maximum size for the string stores combined
45 because the strings are index at 16-bit boundries by a
46 16-bit index, and there is only one section for the
49 #define STRING_STORE_SIZE 0x1FBFE /* 130046 */
50 #define TAG_STORE_SIZE 0x400 /* 1024 */
52 /* The combined tag and converter count can affect the number of lists
53 created. The size of all lists must be less than (2^17 - 1)
54 because the lists are indexed as a 16-bit array with a 16-bit index.
56 #define MAX_TAG_COUNT 0x3F /* 63 */
57 #define MAX_CONV_COUNT UCNV_CONVERTER_INDEX_MASK
58 #define MAX_ALIAS_COUNT 0xFFFF /* 65535 */
60 /* The maximum number of aliases that a standard tag/converter combination can have.
61 At this moment 6/18/2002, IANA has 12 names for ASCII. Don't go below 15 for
62 this value. I don't recommend more than 31 for this value.
64 #define MAX_TC_ALIAS_COUNT 0x1F /* 31 */
66 #define MAX_LINE_SIZE 0x7FFF /* 32767 */
67 #define MAX_LIST_SIZE 0xFFFF /* 65535 */
69 #define DATA_NAME "cnvalias"
70 #define DATA_TYPE "icu" /* ICU alias table */
72 #define ALL_TAG_STR "ALL"
74 #define EMPTY_TAG_NUM 0
76 /* UDataInfo cf. udata.h */
77 static const UDataInfo dataInfo
={
86 {0x43, 0x76, 0x41, 0x6c}, /* dataFormat="CvAl" */
87 {3, 0, 0, 0}, /* formatVersion */
88 {1, 4, 2, 0} /* dataVersion */
97 static char stringStore
[STRING_STORE_SIZE
];
98 static StringBlock stringBlock
= { stringStore
, 0, STRING_STORE_SIZE
};
102 uint16_t *aliases
; /* Index into stringStore */
106 uint16_t converter
; /* Index into stringStore */
107 uint16_t totalAliasCount
; /* Total aliases in this column */
110 static Converter converters
[MAX_CONV_COUNT
];
111 static uint16_t converterCount
=0;
113 static char tagStore
[TAG_STORE_SIZE
];
114 static StringBlock tagBlock
= { tagStore
, 0, TAG_STORE_SIZE
};
117 uint16_t tag
; /* Index into tagStore */
118 uint16_t totalAliasCount
; /* Total aliases in this row */
119 AliasList aliasList
[MAX_CONV_COUNT
];
122 /* Think of this as a 3D array. It's tagCount by converterCount by aliasCount */
123 static Tag tags
[MAX_TAG_COUNT
];
124 static uint16_t tagCount
= 0;
126 /* Used for storing all aliases */
127 static uint16_t knownAliases
[MAX_ALIAS_COUNT
];
128 static uint16_t knownAliasesCount
= 0;
129 /*static uint16_t duplicateKnownAliasesCount = 0;*/
131 /* Used for storing the lists section that point to aliases */
132 static uint16_t aliasLists
[MAX_LIST_SIZE
];
133 static uint16_t aliasListsSize
= 0;
135 /* Were the standard tags declared before the aliases. */
136 static UBool standardTagsUsed
= FALSE
;
137 static UBool verbose
= FALSE
;
138 static int lineNum
= 1;
140 /* prototypes --------------------------------------------------------------- */
143 parseLine(const char *line
);
146 parseFile(FileStream
*in
);
152 addOfficialTaggedStandards(char *line
, int32_t lineLen
);
155 addAlias(const char *alias
, uint16_t standard
, uint16_t converter
, UBool defaultName
);
158 addConverter(const char *converter
);
161 allocString(StringBlock
*block
, const char *s
, int32_t length
);
164 addToKnownAliases(const char *alias
);
167 compareAliases(const void *alias1
, const void *alias2
);
170 getTagNumber(const char *tag
, uint16_t tagLen
);
173 addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter);*/
176 writeAliasTable(UNewDataMemory
*out
);
178 /* -------------------------------------------------------------------------- */
180 /* Presumes that you used allocString() */
181 #define GET_ALIAS_STR(index) (stringStore + ((size_t)(index) << 1))
182 #define GET_TAG_STR(index) (tagStore + ((size_t)(index) << 1))
184 /* Presumes that you used allocString() */
185 #define GET_ALIAS_NUM(str) ((uint16_t)((str - stringStore) >> 1))
186 #define GET_TAG_NUM(str) ((uint16_t)((str - tagStore) >> 1))
198 static UOption options
[]={
200 UOPTION_HELP_QUESTION_MARK
,
208 main(int argc
, char* argv
[]) {
213 UErrorCode errorCode
=U_ZERO_ERROR
;
215 U_MAIN_INIT_ARGS(argc
, argv
);
217 /* preset then read command line options */
218 options
[DESTDIR
].value
=options
[SOURCEDIR
].value
=u_getDataDirectory();
219 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
221 /* error handling, printing usage message */
224 "error in command line argument \"%s\"\n",
227 if(argc
<0 || options
[HELP1
].doesOccur
|| options
[HELP2
].doesOccur
) {
229 "usage: %s [-options] [convrtrs.txt]\n"
230 "\tread convrtrs.txt and create " U_ICUDATA_NAME
"_" DATA_NAME
"." DATA_TYPE
"\n"
232 "\t-h or -? or --help this usage text\n"
233 "\t-v or --verbose prints out extra information about the alias table\n"
234 "\t-c or --copyright include a copyright notice\n"
235 "\t-d or --destdir destination directory, followed by the path\n"
236 "\t-s or --sourcedir source directory, followed by the path\n",
238 return argc
<0 ? U_ILLEGAL_ARGUMENT_ERROR
: U_ZERO_ERROR
;
241 if(options
[VERBOSE
].doesOccur
) {
248 path
=options
[SOURCEDIR
].value
;
249 if(path
!=NULL
&& *path
!=0) {
252 uprv_strcpy(pathBuf
, path
);
253 end
= uprv_strchr(pathBuf
, 0);
254 if(*(end
-1)!=U_FILE_SEP_CHAR
) {
255 *(end
++)=U_FILE_SEP_CHAR
;
257 uprv_strcpy(end
, "convrtrs.txt");
260 path
= "convrtrs.txt";
264 uprv_memset(stringStore
, 0, sizeof(stringStore
));
265 uprv_memset(tagStore
, 0, sizeof(tagStore
));
266 uprv_memset(converters
, 0, sizeof(converters
));
267 uprv_memset(tags
, 0, sizeof(tags
));
268 uprv_memset(aliasLists
, 0, sizeof(aliasLists
));
269 uprv_memset(knownAliases
, 0, sizeof(aliasLists
));
272 in
=T_FileStream_open(path
, "r");
274 fprintf(stderr
, "gencnval: unable to open input file convrtrs.txt\n");
275 exit(U_FILE_ACCESS_ERROR
);
278 T_FileStream_close(in
);
280 /* create the output file */
281 out
=udata_create(options
[DESTDIR
].value
, DATA_TYPE
, DATA_NAME
, &dataInfo
,
282 options
[COPYRIGHT
].doesOccur
? U_COPYRIGHT_STRING
: NULL
, &errorCode
);
283 if(U_FAILURE(errorCode
)) {
284 fprintf(stderr
, "gencnval: unable to open output file - error %s\n", u_errorName(errorCode
));
288 /* write the table of aliases based on a tag/converter name combination */
289 writeAliasTable(out
);
292 udata_finish(out
, &errorCode
);
293 if(U_FAILURE(errorCode
)) {
294 fprintf(stderr
, "gencnval: error finishing output file - %s\n", u_errorName(errorCode
));
302 parseFile(FileStream
*in
) {
303 char line
[MAX_LINE_SIZE
];
304 char lastLine
[MAX_LINE_SIZE
];
305 int32_t lineSize
= 0;
306 int32_t lastLineSize
= 0;
307 UBool validParse
= TRUE
;
311 /* Add the empty tag, which is for untagged aliases */
313 getTagNumber(ALL_TAG_STR
, 3);
314 allocString(&stringBlock
, "", 0);
316 /* read the list of aliases */
320 /* Read non-empty lines that don't start with a space character. */
321 while (T_FileStream_readLine(in
, lastLine
, MAX_LINE_SIZE
) != NULL
) {
322 lastLineSize
= chomp(lastLine
);
323 if (lineSize
== 0 || (lastLineSize
> 0 && isspace(*lastLine
))) {
324 uprv_strcpy(line
+ lineSize
, lastLine
);
325 lineSize
+= lastLineSize
;
326 } else if (lineSize
> 0) {
333 if (validParse
|| lineSize
> 0) {
334 if (isspace(*line
)) {
335 fprintf(stderr
, "error(line %d): cannot start an alias with a space\n", lineNum
-1);
337 } else if (line
[0] == '{') {
338 if (!standardTagsUsed
&& line
[lineSize
- 1] != '}') {
339 fprintf(stderr
, "error(line %d): alias needs to start with a converter name\n", lineNum
);
342 addOfficialTaggedStandards(line
, lineSize
);
343 standardTagsUsed
= TRUE
;
345 if (standardTagsUsed
) {
349 fprintf(stderr
, "error(line %d): alias table needs to start a list of standard tags\n", lineNum
);
353 /* Was the last line consumed */
354 if (lastLineSize
> 0) {
355 uprv_strcpy(line
, lastLine
);
356 lineSize
= lastLineSize
;
366 /* This works almost like the Perl chomp.
367 It removes the newlines, comments and trailing whitespace (not preceding whitespace).
372 char *lastNonSpace
= line
;
374 /* truncate at a newline or a comment */
375 if(*s
== '\r' || *s
== '\n' || *s
== '#') {
384 if (lastNonSpace
++ > line
) {
388 return (int32_t)(s
- line
);
392 parseLine(const char *line
) {
393 uint16_t pos
=0, start
, limit
, length
, cnv
;
394 char *converter
, *alias
;
396 /* skip leading white space */
397 /* There is no whitespace at the beginning anymore */
398 /* while(line[pos]!=0 && isspace(line[pos])) {
403 /* is there nothing on this line? */
408 /* get the converter name */
410 while(line
[pos
]!=0 && !isspace(line
[pos
])) {
415 /* store the converter name */
416 length
=(uint16_t)(limit
-start
);
417 converter
=allocString(&stringBlock
, line
+start
, length
);
419 /* add the converter to the converter table */
420 cnv
=addConverter(converter
);
422 /* The name itself may be tagged, so let's added it to the aliases list properly */
425 /* get all the real aliases */
428 /* skip white space */
429 while(line
[pos
]!=0 && isspace(line
[pos
])) {
433 /* is there no more alias name on this line? */
438 /* get an alias name */
440 while(line
[pos
]!=0 && line
[pos
]!='{' && !isspace(line
[pos
])) {
445 /* store the alias name */
446 length
=(uint16_t)(limit
-start
);
448 /* add the converter as its own alias to the alias table */
450 addAlias(alias
, ALL_TAG_NUM
, cnv
, TRUE
);
453 alias
=allocString(&stringBlock
, line
+start
, length
);
454 addAlias(alias
, ALL_TAG_NUM
, cnv
, FALSE
);
456 addToKnownAliases(alias
);
458 /* add the alias/converter pair to the alias table */
459 /* addAlias(alias, 0, cnv, FALSE);*/
461 /* skip whitespace */
462 while (line
[pos
] && isspace(line
[pos
])) {
466 /* handle tags if they are present */
467 if (line
[pos
] == '{') {
471 while (line
[pos
] && line
[pos
] != '}' && !isspace( line
[pos
])) {
476 if (start
!= limit
) {
477 /* add the tag to the tag table */
478 uint16_t tag
= getTagNumber(line
+ start
, (uint16_t)(limit
- start
));
479 addAlias(alias
, tag
, cnv
, (UBool
)(line
[limit
-1] == '*'));
482 while (line
[pos
] && isspace(line
[pos
])) {
485 } while (line
[pos
] && line
[pos
] != '}');
487 if (line
[pos
] == '}') {
490 fprintf(stderr
, "error(line %d): Unterminated tag list\n", lineNum
);
491 exit(U_UNMATCHED_BRACES
);
494 addAlias(alias
, EMPTY_TAG_NUM
, cnv
, (UBool
)(tags
[0].aliasList
[cnv
].aliasCount
== 0));
500 getTagNumber(const char *tag
, uint16_t tagLen
) {
503 UBool preferredName
= ((tagLen
> 0) ? (tag
[tagLen
- 1] == '*') : (FALSE
));
505 if (tagCount
>= MAX_TAG_COUNT
) {
506 fprintf(stderr
, "error(line %d): too many tags\n", lineNum
);
507 exit(U_BUFFER_OVERFLOW_ERROR
);
515 for (t
= 0; t
< tagCount
; ++t
) {
516 const char *currTag
= GET_TAG_STR(tags
[t
].tag
);
517 if (uprv_strlen(currTag
) == tagLen
&& !uprv_strnicmp(currTag
, tag
, tagLen
)) {
522 /* we need to add this tag */
523 if (tagCount
>= MAX_TAG_COUNT
) {
524 fprintf(stderr
, "error(line %d): too many tags\n", lineNum
);
525 exit(U_BUFFER_OVERFLOW_ERROR
);
528 /* allocate a new entry in the tag table */
529 atag
= allocString(&tagBlock
, tag
, tagLen
);
531 if (standardTagsUsed
) {
532 fprintf(stderr
, "error(line %d): Tag \"%s\" is not declared at the beginning of the alias table.\n",
536 else if (tagLen
> 0 && strcmp(tag
, ALL_TAG_STR
) != 0) {
537 fprintf(stderr
, "warning(line %d): Tag \"%s\" was added to the list of standards because it was not declared at beginning of the alias table.\n",
541 /* add the tag to the tag table */
542 tags
[tagCount
].tag
= GET_TAG_NUM(atag
);
543 /* The aliasList should be set to 0's already */
549 addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter) {
550 tags[tag].aliases[converter] = alias;
555 addOfficialTaggedStandards(char *line
, int32_t lineLen
) {
557 char *tag
= strchr(line
, '{') + 1;
558 static const char WHITESPACE
[] = " \t";
560 if (tagCount
> UCNV_NUM_RESERVED_TAGS
) {
561 fprintf(stderr
, "error(line %d): official tags already added\n", lineNum
);
562 exit(U_BUFFER_OVERFLOW_ERROR
);
564 strchr(tag
, '}')[0] = 0;
566 tag
= strtok(tag
, WHITESPACE
);
567 while (tag
!= NULL
) {
568 /* printf("Adding original tag \"%s\"\n", tag);*/
570 /* allocate a new entry in the tag table */
571 atag
= allocString(&tagBlock
, tag
, -1);
573 /* add the tag to the tag table */
574 tags
[tagCount
++].tag
= (uint16_t)((atag
- tagStore
) >> 1);
576 /* The aliasList should already be set to 0's */
579 tag
= strtok(NULL
, WHITESPACE
);
584 addToKnownAliases(const char *alias
) {
586 /* strict matching */
587 /* for (idx = 0; idx < knownAliasesCount; idx++) {
588 uint16_t num = GET_ALIAS_NUM(alias);
589 if (knownAliases[idx] != num
590 && uprv_strcmp(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
592 fprintf(stderr, "warning(line %d): duplicate alias %s and %s found\n",
593 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
594 duplicateKnownAliasesCount++;
597 else if (knownAliases[idx] != num
598 && ucnv_compareNames(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
601 fprintf(stderr, "information(line %d): duplicate alias %s and %s found\n",
602 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
604 duplicateKnownAliasesCount++;
609 if (knownAliasesCount
>= MAX_ALIAS_COUNT
) {
610 fprintf(stderr
, "warning(line %d): Too many aliases defined for all converters\n",
612 exit(U_BUFFER_OVERFLOW_ERROR
);
614 /* TODO: We could try to unlist exact duplicates. */
615 return knownAliases
[knownAliasesCount
++] = GET_ALIAS_NUM(alias
);
619 @param standard When standard is 0, then it's the "empty" tag.
622 addAlias(const char *alias
, uint16_t standard
, uint16_t converter
, UBool defaultName
) {
624 UBool dupFound
= FALSE
;
625 UBool startEmptyWithoutDefault
= FALSE
;
626 AliasList
*aliasList
;
628 if(standard
>=MAX_TAG_COUNT
) {
629 fprintf(stderr
, "error(line %d): too many standard tags\n", lineNum
);
630 exit(U_BUFFER_OVERFLOW_ERROR
);
632 if(converter
>=MAX_CONV_COUNT
) {
633 fprintf(stderr
, "error(line %d): too many converter names\n", lineNum
);
634 exit(U_BUFFER_OVERFLOW_ERROR
);
636 aliasList
= &tags
[standard
].aliasList
[converter
];
638 if (strchr(alias
, '}')) {
639 fprintf(stderr
, "error(line %d): unmatched } found\n",
643 if(aliasList
->aliasCount
+ 1 >= MAX_TC_ALIAS_COUNT
) {
644 fprintf(stderr
, "error(line %d): too many aliases for alias %s and converter %s\n",
645 lineNum
, alias
, GET_ALIAS_STR(converters
[converter
].converter
));
646 exit(U_BUFFER_OVERFLOW_ERROR
);
649 /* Show this warning only once. All aliases are added to the "ALL" tag. */
650 if (standard
== ALL_TAG_NUM
&& GET_ALIAS_STR(converters
[converter
].converter
) != alias
) {
651 /* Normally these option values are parsed at runtime, and they can
652 be discarded when the alias is a default converter. Options should
653 only be on a converter and not an alias. */
654 if (uprv_strchr(alias
, UCNV_OPTION_SEP_CHAR
) != 0)
656 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",
659 if (uprv_strchr(alias
, UCNV_VALUE_SEP_CHAR
) != 0)
661 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",
666 /* Check for duplicates in a tag/converter combination */
667 for (idx
= 0; idx
< aliasList
->aliasCount
; idx
++) {
668 uint16_t aliasNum
= tags
[standard
].aliasList
[converter
].aliases
[idx
];
669 if (aliasNum
&& ucnv_compareNames(alias
, GET_ALIAS_STR(aliasNum
)) == 0 && standard
!= ALL_TAG_NUM
)
671 fprintf(stderr
, "warning(line %d): duplicate alias %s and %s found for standard %s\n",
672 lineNum
, alias
, GET_ALIAS_STR(aliasNum
), GET_TAG_STR(tags
[standard
].tag
));
678 if (!dupFound
&& standard
!= ALL_TAG_NUM
) {
679 /* Check for duplicate aliases for this tag on all converters */
680 for (idx
= 0; idx
< converterCount
; idx
++) {
681 for (idx2
= 0; idx2
< tags
[standard
].aliasList
[idx
].aliasCount
; idx2
++) {
682 uint16_t aliasNum
= tags
[standard
].aliasList
[idx
].aliases
[idx2
];
684 && ucnv_compareNames(alias
, GET_ALIAS_STR(aliasNum
)) == 0)
686 fprintf(stderr
, "warning(line %d): duplicate alias %s found for standard tag %s between converter %s and converter %s\n",
687 lineNum
, alias
, GET_TAG_STR(tags
[standard
].tag
), GET_ALIAS_STR(converters
[converter
].converter
), GET_ALIAS_STR(converters
[idx
].converter
));
694 /* Check for duplicate default aliases for this converter on all tags */
695 /* It's okay to have multiple standards prefer the same name */
696 /* if (verbose && !dupFound) {
697 for (idx = 0; idx < tagCount; idx++) {
698 if (tags[idx].aliasList[converter].aliases) {
699 uint16_t aliasNum = tags[idx].aliasList[converter].aliases[0];
701 && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
703 fprintf(stderr, "warning(line %d): duplicate alias %s found for converter %s and standard tag %s\n",
704 lineNum, alias, GET_ALIAS_STR(converters[converter].converter), GET_TAG_STR(tags[standard].tag));
712 if (aliasList
->aliasCount
<= 0) {
713 aliasList
->aliasCount
++;
714 startEmptyWithoutDefault
= TRUE
;
716 aliasList
->aliases
= (uint16_t *)uprv_realloc(aliasList
->aliases
, (aliasList
->aliasCount
+ 1) * sizeof(aliasList
->aliases
[0]));
717 if (startEmptyWithoutDefault
) {
718 aliasList
->aliases
[0] = 0;
721 if (aliasList
->aliases
[0] != 0) {
722 fprintf(stderr
, "error(line %d): Alias %s and %s cannot both be the default alias for standard tag %s and converter %s\n",
725 GET_ALIAS_STR(aliasList
->aliases
[0]),
726 GET_TAG_STR(tags
[standard
].tag
),
727 GET_ALIAS_STR(converters
[converter
].converter
));
730 aliasList
->aliases
[0] = GET_ALIAS_NUM(alias
);
732 aliasList
->aliases
[aliasList
->aliasCount
++] = GET_ALIAS_NUM(alias
);
734 /* aliasList->converter = converter;*/
736 converters
[converter
].totalAliasCount
++; /* One more to the column */
737 tags
[standard
].totalAliasCount
++; /* One more to the row */
739 return aliasList
->aliasCount
;
743 addConverter(const char *converter
) {
745 if(converterCount
>=MAX_CONV_COUNT
) {
746 fprintf(stderr
, "error(line %d): too many converters\n", lineNum
);
747 exit(U_BUFFER_OVERFLOW_ERROR
);
750 for (idx
= 0; idx
< converterCount
; idx
++) {
751 if (ucnv_compareNames(converter
, GET_ALIAS_STR(converters
[idx
].converter
)) == 0) {
752 fprintf(stderr
, "error(line %d): duplicate converter %s found!\n", lineNum
, converter
);
758 converters
[converterCount
].converter
= GET_ALIAS_NUM(converter
);
759 converters
[converterCount
].totalAliasCount
= 0;
761 return converterCount
++;
764 /* resolve this alias based on the prioritization of the standard tags. */
766 resolveAliasToConverter(uint16_t alias
, uint16_t *tagNum
, uint16_t *converterNum
) {
767 uint16_t idx
, idx2
, idx3
;
769 for (idx
= UCNV_NUM_RESERVED_TAGS
; idx
< tagCount
; idx
++) {
770 for (idx2
= 0; idx2
< converterCount
; idx2
++) {
771 for (idx3
= 0; idx3
< tags
[idx
].aliasList
[idx2
].aliasCount
; idx3
++) {
772 uint16_t aliasNum
= tags
[idx
].aliasList
[idx2
].aliases
[idx3
];
773 if (aliasNum
== alias
) {
775 *converterNum
= idx2
;
781 /* Do the leftovers last, just in case */
782 /* There is no need to do the ALL tag */
784 for (idx2
= 0; idx2
< converterCount
; idx2
++) {
785 for (idx3
= 0; idx3
< tags
[idx
].aliasList
[idx2
].aliasCount
; idx3
++) {
786 uint16_t aliasNum
= tags
[idx
].aliasList
[idx2
].aliases
[idx3
];
787 if (aliasNum
== alias
) {
789 *converterNum
= idx2
;
794 *tagNum
= UINT16_MAX
;
795 *converterNum
= UINT16_MAX
;
796 fprintf(stderr
, "warning: alias %s not found\n",
797 GET_ALIAS_STR(alias
));
801 /* The knownAliases should be sorted before calling this function */
803 resolveAliases(uint16_t *uniqueAliasArr
, uint16_t *uniqueAliasToConverterArr
, uint16_t aliasOffset
) {
804 uint32_t uniqueAliasIdx
= 0;
806 uint16_t currTagNum
, oldTagNum
;
807 uint16_t currConvNum
, oldConvNum
;
808 const char *lastName
;
810 resolveAliasToConverter(knownAliases
[0], &oldTagNum
, &currConvNum
);
811 uniqueAliasToConverterArr
[uniqueAliasIdx
] = currConvNum
;
812 oldConvNum
= currConvNum
;
813 uniqueAliasArr
[uniqueAliasIdx
] = knownAliases
[0] + aliasOffset
;
815 lastName
= GET_ALIAS_STR(knownAliases
[0]);
817 for (idx
= 1; idx
< knownAliasesCount
; idx
++) {
818 resolveAliasToConverter(knownAliases
[idx
], &currTagNum
, &currConvNum
);
819 if (ucnv_compareNames(lastName
, GET_ALIAS_STR(knownAliases
[idx
])) == 0) {
820 /* duplicate found */
821 if ((currTagNum
< oldTagNum
&& currTagNum
>= UCNV_NUM_RESERVED_TAGS
)
823 oldTagNum
= currTagNum
;
824 uniqueAliasToConverterArr
[uniqueAliasIdx
- 1] = currConvNum
;
825 uniqueAliasArr
[uniqueAliasIdx
- 1] = knownAliases
[idx
] + aliasOffset
;
827 printf("using %s instead of %s -> %s",
828 GET_ALIAS_STR(knownAliases
[idx
]),
830 GET_ALIAS_STR(converters
[currConvNum
].converter
));
831 if (oldConvNum
!= currConvNum
) {
832 printf(" (alias conflict)");
840 printf("folding %s into %s -> %s",
841 GET_ALIAS_STR(knownAliases
[idx
]),
843 GET_ALIAS_STR(converters
[oldConvNum
].converter
));
844 if (oldConvNum
!= currConvNum
) {
845 printf(" (alias conflict)");
850 if (oldConvNum
!= currConvNum
) {
851 uniqueAliasToConverterArr
[uniqueAliasIdx
- 1] |= UCNV_AMBIGUOUS_ALIAS_MAP_BIT
;
855 uniqueAliasToConverterArr
[uniqueAliasIdx
] = currConvNum
;
856 oldConvNum
= currConvNum
;
857 uniqueAliasArr
[uniqueAliasIdx
] = knownAliases
[idx
] + aliasOffset
;
859 lastName
= GET_ALIAS_STR(knownAliases
[idx
]);
860 oldTagNum
= currTagNum
;
861 /*printf("%s -> %s\n", GET_ALIAS_STR(knownAliases[idx]), GET_ALIAS_STR(converters[currConvNum].converter));*/
864 return uniqueAliasIdx
;
868 createOneAliasList(uint16_t *aliasArrLists
, uint32_t tag
, uint32_t converter
, uint16_t offset
) {
870 AliasList
*aliasList
= &tags
[tag
].aliasList
[converter
];
872 if (aliasList
->aliasCount
== 0) {
873 aliasArrLists
[tag
*converterCount
+ converter
] = 0;
876 aliasLists
[aliasListsSize
++] = aliasList
->aliasCount
;
878 /* write into the array area a 1's based index. */
879 aliasArrLists
[tag
*converterCount
+ converter
] = aliasListsSize
;
881 /* printf("tag %s converter %s\n",
882 GET_TAG_STR(tags[tag].tag),
883 GET_ALIAS_STR(converters[converter].converter));*/
884 for (aliasNum
= 0; aliasNum
< aliasList
->aliasCount
; aliasNum
++) {
887 GET_ALIAS_STR(aliasList->aliases[aliasNum]));*/
888 if (aliasList
->aliases
[aliasNum
]) {
889 value
= aliasList
->aliases
[aliasNum
] + offset
;
892 if (tag
!= 0) { /* Only show the warning when it's not the leftover tag. */
893 printf("warning: tag %s does not have a default alias for %s\n",
894 GET_TAG_STR(tags
[tag
].tag
),
895 GET_ALIAS_STR(converters
[converter
].converter
));
898 aliasLists
[aliasListsSize
++] = value
;
899 if (aliasListsSize
>= MAX_LIST_SIZE
) {
900 fprintf(stderr
, "error: Too many alias lists\n");
901 exit(U_BUFFER_OVERFLOW_ERROR
);
909 writeAliasTable(UNewDataMemory
*out
) {
911 uint32_t uniqueAliasesSize
;
912 uint16_t aliasOffset
= (uint16_t)(tagBlock
.top
/sizeof(uint16_t));
913 uint16_t *aliasArrLists
= (uint16_t *)uprv_malloc(tagCount
* converterCount
* sizeof(uint16_t));
914 uint16_t *uniqueAliases
= (uint16_t *)uprv_malloc(knownAliasesCount
* sizeof(uint16_t));
915 uint16_t *uniqueAliasesToConverter
= (uint16_t *)uprv_malloc(knownAliasesCount
* sizeof(uint16_t));
917 qsort(knownAliases
, knownAliasesCount
, sizeof(knownAliases
[0]), compareAliases
);
918 uniqueAliasesSize
= resolveAliases(uniqueAliases
, uniqueAliasesToConverter
, aliasOffset
);
920 /* Array index starts at 1. aliasLists[0] is the size of the lists section. */
923 /* write the offsets of all the aliases lists in a 2D array, and create the lists. */
924 for (i
= 0; i
< tagCount
; ++i
) {
925 for (j
= 0; j
< converterCount
; ++j
) {
926 createOneAliasList(aliasArrLists
, i
, j
, aliasOffset
);
930 /* Write the size of the TOC */
931 udata_write32(out
, 8);
933 /* Write the sizes of each section */
934 /* All sizes are the number of uint16_t units, not bytes */
935 udata_write32(out
, converterCount
);
936 udata_write32(out
, tagCount
);
937 udata_write32(out
, uniqueAliasesSize
); /* list of aliases */
938 udata_write32(out
, uniqueAliasesSize
); /* The preresolved form of mapping an untagged the alias to a converter */
939 udata_write32(out
, tagCount
* converterCount
);
940 udata_write32(out
, aliasListsSize
+ 1);
941 udata_write32(out
, 0); /* Reserved space. */
942 udata_write32(out
, (tagBlock
.top
+ stringBlock
.top
) / sizeof(uint16_t));
944 /* write the table of converters */
945 /* Think of this as the column headers */
946 for(i
=0; i
<converterCount
; ++i
) {
947 udata_write16(out
, (uint16_t)(converters
[i
].converter
+ aliasOffset
));
950 /* write the table of tags */
951 /* Think of this as the row headers */
952 for(i
=UCNV_NUM_RESERVED_TAGS
; i
<tagCount
; ++i
) {
953 udata_write16(out
, tags
[i
].tag
);
955 /* The empty tag is considered the leftover list, and put that at the end of the priority list. */
956 udata_write16(out
, tags
[EMPTY_TAG_NUM
].tag
);
957 udata_write16(out
, tags
[ALL_TAG_NUM
].tag
);
959 /* Write the unique list of aliases */
960 udata_writeBlock(out
, uniqueAliases
, uniqueAliasesSize
* sizeof(uint16_t));
962 /* Write the unique list of aliases */
963 udata_writeBlock(out
, uniqueAliasesToConverter
, uniqueAliasesSize
* sizeof(uint16_t));
965 /* Write the array to the lists */
966 udata_writeBlock(out
, (const void *)(aliasArrLists
+ (2*converterCount
)), (((tagCount
- 2) * converterCount
) * sizeof(uint16_t)));
967 /* Now write the leftover part of the array for the EMPTY and ALL lists */
968 udata_writeBlock(out
, (const void *)aliasArrLists
, (2 * converterCount
* sizeof(uint16_t)));
970 /* Offset the next array to make the index start at 1. */
971 udata_write16(out
, 0xDEAD);
973 /* Write the lists */
974 udata_writeBlock(out
, (const void *)aliasLists
, aliasListsSize
* sizeof(uint16_t));
976 /* write the tags strings */
977 udata_writeString(out
, tagBlock
.store
, tagBlock
.top
);
979 /* write the aliases strings */
980 udata_writeString(out
, stringBlock
.store
, stringBlock
.top
);
982 uprv_free(aliasArrLists
);
983 uprv_free(uniqueAliases
);
987 allocString(StringBlock
*block
, const char *s
, int32_t length
) {
992 length
=(int32_t)uprv_strlen(s
);
996 * add 1 for the terminating NUL
997 * and round up (+1 &~1)
998 * to keep the addresses on a 16-bit boundary
1000 top
=block
->top
+ (uint32_t)((length
+ 1 + 1) & ~1);
1002 if(top
>= block
->max
) {
1003 fprintf(stderr
, "error(line %d): out of memory\n", lineNum
);
1004 exit(U_MEMORY_ALLOCATION_ERROR
);
1007 /* get the pointer and copy the string */
1008 p
= block
->store
+ block
->top
;
1009 uprv_memcpy(p
, s
, length
);
1010 p
[length
] = 0; /* NUL-terminate it */
1011 if((length
& 1) == 0) {
1012 p
[length
+ 1] = 0; /* set the padding byte */
1015 /* check for invariant characters now that we have a NUL-terminated string for easy output */
1016 if(!uprv_isInvariantString(p
, length
)) {
1017 fprintf(stderr
, "error(line %d): the name %s contains not just invariant characters\n", lineNum
, p
);
1018 exit(U_INVALID_TABLE_FORMAT
);
1026 compareAliases(const void *alias1
, const void *alias2
) {
1027 /* Names like IBM850 and ibm-850 need to be sorted together */
1028 int result
= ucnv_compareNames(GET_ALIAS_STR(*(uint16_t*)alias1
), GET_ALIAS_STR(*(uint16_t*)alias2
));
1030 /* Sort the shortest first */
1031 return (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias1
)) - (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias2
));
1037 * Hey, Emacs, please set the following:
1040 * indent-tabs-mode: nil