2 *******************************************************************************
4 * Copyright (C) 1999-2012, 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, 1, 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 static UConverterAliasOptions tableOptions
= {
141 UCNV_IO_STD_NORMALIZED
,
142 1 /* containsCnvOptionInfo */
147 * path to convrtrs.txt
151 /* prototypes --------------------------------------------------------------- */
154 parseLine(const char *line
);
157 parseFile(FileStream
*in
);
163 addOfficialTaggedStandards(char *line
, int32_t lineLen
);
166 addAlias(const char *alias
, uint16_t standard
, uint16_t converter
, UBool defaultName
);
169 addConverter(const char *converter
);
172 allocString(StringBlock
*block
, const char *s
, int32_t length
);
175 addToKnownAliases(const char *alias
);
178 compareAliases(const void *alias1
, const void *alias2
);
181 getTagNumber(const char *tag
, uint16_t tagLen
);
184 addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter);*/
187 writeAliasTable(UNewDataMemory
*out
);
189 /* -------------------------------------------------------------------------- */
191 /* Presumes that you used allocString() */
192 #define GET_ALIAS_STR(index) (stringStore + ((size_t)(index) << 1))
193 #define GET_TAG_STR(index) (tagStore + ((size_t)(index) << 1))
195 /* Presumes that you used allocString() */
196 #define GET_ALIAS_NUM(str) ((uint16_t)((str - stringStore) >> 1))
197 #define GET_TAG_NUM(str) ((uint16_t)((str - tagStore) >> 1))
209 static UOption options
[]={
211 UOPTION_HELP_QUESTION_MARK
,
219 main(int argc
, char* argv
[]) {
224 UErrorCode errorCode
=U_ZERO_ERROR
;
226 U_MAIN_INIT_ARGS(argc
, argv
);
228 /* preset then read command line options */
229 options
[DESTDIR
].value
=options
[SOURCEDIR
].value
=u_getDataDirectory();
230 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
232 /* error handling, printing usage message */
235 "error in command line argument \"%s\"\n",
238 if(argc
<0 || options
[HELP1
].doesOccur
|| options
[HELP2
].doesOccur
) {
240 "usage: %s [-options] [convrtrs.txt]\n"
241 "\tread convrtrs.txt and create " U_ICUDATA_NAME
"_" DATA_NAME
"." DATA_TYPE
"\n"
243 "\t-h or -? or --help this usage text\n"
244 "\t-v or --verbose prints out extra information about the alias table\n"
245 "\t-c or --copyright include a copyright notice\n"
246 "\t-d or --destdir destination directory, followed by the path\n"
247 "\t-s or --sourcedir source directory, followed by the path\n",
249 return argc
<0 ? U_ILLEGAL_ARGUMENT_ERROR
: U_ZERO_ERROR
;
252 if(options
[VERBOSE
].doesOccur
) {
259 path
=options
[SOURCEDIR
].value
;
260 if(path
!=NULL
&& *path
!=0) {
263 uprv_strcpy(pathBuf
, path
);
264 end
= uprv_strchr(pathBuf
, 0);
265 if(*(end
-1)!=U_FILE_SEP_CHAR
) {
266 *(end
++)=U_FILE_SEP_CHAR
;
268 uprv_strcpy(end
, "convrtrs.txt");
271 path
= "convrtrs.txt";
275 uprv_memset(stringStore
, 0, sizeof(stringStore
));
276 uprv_memset(tagStore
, 0, sizeof(tagStore
));
277 uprv_memset(converters
, 0, sizeof(converters
));
278 uprv_memset(tags
, 0, sizeof(tags
));
279 uprv_memset(aliasLists
, 0, sizeof(aliasLists
));
280 uprv_memset(knownAliases
, 0, sizeof(aliasLists
));
283 in
=T_FileStream_open(path
, "r");
285 fprintf(stderr
, "gencnval: unable to open input file %s\n", path
);
286 exit(U_FILE_ACCESS_ERROR
);
289 T_FileStream_close(in
);
291 /* create the output file */
292 out
=udata_create(options
[DESTDIR
].value
, DATA_TYPE
, DATA_NAME
, &dataInfo
,
293 options
[COPYRIGHT
].doesOccur
? U_COPYRIGHT_STRING
: NULL
, &errorCode
);
294 if(U_FAILURE(errorCode
)) {
295 fprintf(stderr
, "gencnval: unable to open output file - error %s\n", u_errorName(errorCode
));
299 /* write the table of aliases based on a tag/converter name combination */
300 writeAliasTable(out
);
303 udata_finish(out
, &errorCode
);
304 if(U_FAILURE(errorCode
)) {
305 fprintf(stderr
, "gencnval: error finishing output file - %s\n", u_errorName(errorCode
));
310 for (i
= 0; i
< MAX_TAG_COUNT
; i
++) {
311 for (n
= 0; n
< MAX_CONV_COUNT
; n
++) {
312 if (tags
[i
].aliasList
[n
].aliases
!=NULL
) {
313 uprv_free(tags
[i
].aliasList
[n
].aliases
);
322 parseFile(FileStream
*in
) {
323 char line
[MAX_LINE_SIZE
];
324 char lastLine
[MAX_LINE_SIZE
];
325 int32_t lineSize
= 0;
326 int32_t lastLineSize
= 0;
327 UBool validParse
= TRUE
;
331 /* Add the empty tag, which is for untagged aliases */
333 getTagNumber(ALL_TAG_STR
, 3);
334 allocString(&stringBlock
, "", 0);
336 /* read the list of aliases */
340 /* Read non-empty lines that don't start with a space character. */
341 while (T_FileStream_readLine(in
, lastLine
, MAX_LINE_SIZE
) != NULL
) {
342 lastLineSize
= chomp(lastLine
);
343 if (lineSize
== 0 || (lastLineSize
> 0 && isspace((int)*lastLine
))) {
344 uprv_strcpy(line
+ lineSize
, lastLine
);
345 lineSize
+= lastLineSize
;
346 } else if (lineSize
> 0) {
353 if (validParse
|| lineSize
> 0) {
354 if (isspace((int)*line
)) {
355 fprintf(stderr
, "%s:%d: error: cannot start an alias with a space\n", path
, lineNum
-1);
357 } else if (line
[0] == '{') {
358 if (!standardTagsUsed
&& line
[lineSize
- 1] != '}') {
359 fprintf(stderr
, "%s:%d: error: alias needs to start with a converter name\n", path
, lineNum
);
362 addOfficialTaggedStandards(line
, lineSize
);
363 standardTagsUsed
= TRUE
;
365 if (standardTagsUsed
) {
369 fprintf(stderr
, "%s:%d: error: alias table needs to start a list of standard tags\n", path
, lineNum
);
373 /* Was the last line consumed */
374 if (lastLineSize
> 0) {
375 uprv_strcpy(line
, lastLine
);
376 lineSize
= lastLineSize
;
386 /* This works almost like the Perl chomp.
387 It removes the newlines, comments and trailing whitespace (not preceding whitespace).
392 char *lastNonSpace
= line
;
394 /* truncate at a newline or a comment */
395 if(*s
== '\r' || *s
== '\n' || *s
== '#') {
399 if (!isspace((int)*s
)) {
404 if (lastNonSpace
++ > line
) {
408 return (int32_t)(s
- line
);
412 parseLine(const char *line
) {
413 uint16_t pos
=0, start
, limit
, length
, cnv
;
414 char *converter
, *alias
;
416 /* skip leading white space */
417 /* There is no whitespace at the beginning anymore */
418 /* while(line[pos]!=0 && isspace(line[pos])) {
423 /* is there nothing on this line? */
428 /* get the converter name */
430 while(line
[pos
]!=0 && !isspace((int)line
[pos
])) {
435 /* store the converter name */
436 length
=(uint16_t)(limit
-start
);
437 converter
=allocString(&stringBlock
, line
+start
, length
);
439 /* add the converter to the converter table */
440 cnv
=addConverter(converter
);
442 /* The name itself may be tagged, so let's added it to the aliases list properly */
445 /* get all the real aliases */
448 /* skip white space */
449 while(line
[pos
]!=0 && isspace((int)line
[pos
])) {
453 /* is there no more alias name on this line? */
458 /* get an alias name */
460 while(line
[pos
]!=0 && line
[pos
]!='{' && !isspace((int)line
[pos
])) {
465 /* store the alias name */
466 length
=(uint16_t)(limit
-start
);
468 /* add the converter as its own alias to the alias table */
470 addAlias(alias
, ALL_TAG_NUM
, cnv
, TRUE
);
473 alias
=allocString(&stringBlock
, line
+start
, length
);
474 addAlias(alias
, ALL_TAG_NUM
, cnv
, FALSE
);
476 addToKnownAliases(alias
);
478 /* add the alias/converter pair to the alias table */
479 /* addAlias(alias, 0, cnv, FALSE);*/
481 /* skip whitespace */
482 while (line
[pos
] && isspace((int)line
[pos
])) {
486 /* handle tags if they are present */
487 if (line
[pos
] == '{') {
491 while (line
[pos
] && line
[pos
] != '}' && !isspace((int)line
[pos
])) {
496 if (start
!= limit
) {
497 /* add the tag to the tag table */
498 uint16_t tag
= getTagNumber(line
+ start
, (uint16_t)(limit
- start
));
499 addAlias(alias
, tag
, cnv
, (UBool
)(line
[limit
-1] == '*'));
502 while (line
[pos
] && isspace((int)line
[pos
])) {
505 } while (line
[pos
] && line
[pos
] != '}');
507 if (line
[pos
] == '}') {
510 fprintf(stderr
, "%s:%d: Unterminated tag list\n", path
, lineNum
);
511 exit(U_UNMATCHED_BRACES
);
514 addAlias(alias
, EMPTY_TAG_NUM
, cnv
, (UBool
)(tags
[0].aliasList
[cnv
].aliasCount
== 0));
520 getTagNumber(const char *tag
, uint16_t tagLen
) {
523 UBool preferredName
= ((tagLen
> 0) ? (tag
[tagLen
- 1] == '*') : (FALSE
));
525 if (tagCount
>= MAX_TAG_COUNT
) {
526 fprintf(stderr
, "%s:%d: too many tags\n", path
, lineNum
);
527 exit(U_BUFFER_OVERFLOW_ERROR
);
535 for (t
= 0; t
< tagCount
; ++t
) {
536 const char *currTag
= GET_TAG_STR(tags
[t
].tag
);
537 if (uprv_strlen(currTag
) == tagLen
&& !uprv_strnicmp(currTag
, tag
, tagLen
)) {
542 /* we need to add this tag */
543 if (tagCount
>= MAX_TAG_COUNT
) {
544 fprintf(stderr
, "%s:%d: error: too many tags\n", path
, lineNum
);
545 exit(U_BUFFER_OVERFLOW_ERROR
);
548 /* allocate a new entry in the tag table */
549 atag
= allocString(&tagBlock
, tag
, tagLen
);
551 if (standardTagsUsed
) {
552 fprintf(stderr
, "%s:%d: error: Tag \"%s\" is not declared at the beginning of the alias table.\n",
553 path
, lineNum
, atag
);
556 else if (tagLen
> 0 && strcmp(tag
, ALL_TAG_STR
) != 0) {
557 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",
558 path
, lineNum
, atag
);
561 /* add the tag to the tag table */
562 tags
[tagCount
].tag
= GET_TAG_NUM(atag
);
563 /* The aliasList should be set to 0's already */
569 addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter) {
570 tags[tag].aliases[converter] = alias;
575 addOfficialTaggedStandards(char *line
, int32_t lineLen
) {
579 static const char WHITESPACE
[] = " \t";
581 if (tagCount
> UCNV_NUM_RESERVED_TAGS
) {
582 fprintf(stderr
, "%s:%d: error: official tags already added\n", path
, lineNum
);
583 exit(U_BUFFER_OVERFLOW_ERROR
);
585 tag
= strchr(line
, '{');
587 /* Why were we called? */
588 fprintf(stderr
, "%s:%d: error: Missing start of tag group\n", path
, lineNum
);
592 endTagExp
= strchr(tag
, '}');
593 if (endTagExp
== NULL
) {
594 fprintf(stderr
, "%s:%d: error: Missing end of tag group\n", path
, lineNum
);
599 tag
= strtok(tag
, WHITESPACE
);
600 while (tag
!= NULL
) {
601 /* printf("Adding original tag \"%s\"\n", tag);*/
603 /* allocate a new entry in the tag table */
604 atag
= allocString(&tagBlock
, tag
, -1);
606 /* add the tag to the tag table */
607 tags
[tagCount
++].tag
= (uint16_t)((atag
- tagStore
) >> 1);
609 /* The aliasList should already be set to 0's */
612 tag
= strtok(NULL
, WHITESPACE
);
617 addToKnownAliases(const char *alias
) {
619 /* strict matching */
620 /* for (idx = 0; idx < knownAliasesCount; idx++) {
621 uint16_t num = GET_ALIAS_NUM(alias);
622 if (knownAliases[idx] != num
623 && uprv_strcmp(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
625 fprintf(stderr, "%s:%d: warning: duplicate alias %s and %s found\n", path,
626 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
627 duplicateKnownAliasesCount++;
630 else if (knownAliases[idx] != num
631 && ucnv_compareNames(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
634 fprintf(stderr, "%s:%d: information: duplicate alias %s and %s found\n", path,
635 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
637 duplicateKnownAliasesCount++;
642 if (knownAliasesCount
>= MAX_ALIAS_COUNT
) {
643 fprintf(stderr
, "%s:%d: warning: Too many aliases defined for all converters\n",
645 exit(U_BUFFER_OVERFLOW_ERROR
);
647 /* TODO: We could try to unlist exact duplicates. */
648 return knownAliases
[knownAliasesCount
++] = GET_ALIAS_NUM(alias
);
652 @param standard When standard is 0, then it's the "empty" tag.
655 addAlias(const char *alias
, uint16_t standard
, uint16_t converter
, UBool defaultName
) {
657 UBool startEmptyWithoutDefault
= FALSE
;
658 AliasList
*aliasList
;
660 if(standard
>=MAX_TAG_COUNT
) {
661 fprintf(stderr
, "%s:%d: error: too many standard tags\n", path
, lineNum
);
662 exit(U_BUFFER_OVERFLOW_ERROR
);
664 if(converter
>=MAX_CONV_COUNT
) {
665 fprintf(stderr
, "%s:%d: error: too many converter names\n", path
, lineNum
);
666 exit(U_BUFFER_OVERFLOW_ERROR
);
668 aliasList
= &tags
[standard
].aliasList
[converter
];
670 if (strchr(alias
, '}')) {
671 fprintf(stderr
, "%s:%d: error: unmatched } found\n", path
,
675 if(aliasList
->aliasCount
+ 1 >= MAX_TC_ALIAS_COUNT
) {
676 fprintf(stderr
, "%s:%d: error: too many aliases for alias %s and converter %s\n", path
,
677 lineNum
, alias
, GET_ALIAS_STR(converters
[converter
].converter
));
678 exit(U_BUFFER_OVERFLOW_ERROR
);
681 /* Show this warning only once. All aliases are added to the "ALL" tag. */
682 if (standard
== ALL_TAG_NUM
&& GET_ALIAS_STR(converters
[converter
].converter
) != alias
) {
683 /* Normally these option values are parsed at runtime, and they can
684 be discarded when the alias is a default converter. Options should
685 only be on a converter and not an alias. */
686 if (uprv_strchr(alias
, UCNV_OPTION_SEP_CHAR
) != 0)
688 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",
691 if (uprv_strchr(alias
, UCNV_VALUE_SEP_CHAR
) != 0)
693 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",
698 if (standard
!= ALL_TAG_NUM
) {
699 /* Check for duplicate aliases for this tag on all converters */
700 for (idx
= 0; idx
< converterCount
; idx
++) {
701 for (idx2
= 0; idx2
< tags
[standard
].aliasList
[idx
].aliasCount
; idx2
++) {
702 uint16_t aliasNum
= tags
[standard
].aliasList
[idx
].aliases
[idx2
];
704 && ucnv_compareNames(alias
, GET_ALIAS_STR(aliasNum
)) == 0)
706 if (idx
== converter
) {
708 * (alias, standard) duplicates are harmless if they map to the same converter.
709 * Only print a warning in verbose mode, or if the alias is a precise duplicate,
710 * not just a lenient-match duplicate.
712 if (verbose
|| 0 == uprv_strcmp(alias
, GET_ALIAS_STR(aliasNum
))) {
713 fprintf(stderr
, "%s:%d: warning: duplicate aliases %s and %s found for standard %s and converter %s\n", path
,
714 lineNum
, alias
, GET_ALIAS_STR(aliasNum
),
715 GET_TAG_STR(tags
[standard
].tag
),
716 GET_ALIAS_STR(converters
[converter
].converter
));
719 fprintf(stderr
, "%s:%d: warning: duplicate aliases %s and %s found for standard tag %s between converter %s and converter %s\n", path
,
720 lineNum
, alias
, GET_ALIAS_STR(aliasNum
),
721 GET_TAG_STR(tags
[standard
].tag
),
722 GET_ALIAS_STR(converters
[converter
].converter
),
723 GET_ALIAS_STR(converters
[idx
].converter
));
730 /* Check for duplicate default aliases for this converter on all tags */
731 /* It's okay to have multiple standards prefer the same name */
732 /* if (verbose && !dupFound) {
733 for (idx = 0; idx < tagCount; idx++) {
734 if (tags[idx].aliasList[converter].aliases) {
735 uint16_t aliasNum = tags[idx].aliasList[converter].aliases[0];
737 && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
739 fprintf(stderr, "%s:%d: warning: duplicate alias %s found for converter %s and standard tag %s\n", path,
740 lineNum, alias, GET_ALIAS_STR(converters[converter].converter), GET_TAG_STR(tags[standard].tag));
748 if (aliasList
->aliasCount
<= 0) {
749 aliasList
->aliasCount
++;
750 startEmptyWithoutDefault
= TRUE
;
752 aliasList
->aliases
= (uint16_t *)uprv_realloc(aliasList
->aliases
, (aliasList
->aliasCount
+ 1) * sizeof(aliasList
->aliases
[0]));
753 if (startEmptyWithoutDefault
) {
754 aliasList
->aliases
[0] = 0;
757 if (aliasList
->aliases
[0] != 0) {
758 fprintf(stderr
, "%s:%d: error: Alias %s and %s cannot both be the default alias for standard tag %s and converter %s\n", path
,
761 GET_ALIAS_STR(aliasList
->aliases
[0]),
762 GET_TAG_STR(tags
[standard
].tag
),
763 GET_ALIAS_STR(converters
[converter
].converter
));
766 aliasList
->aliases
[0] = GET_ALIAS_NUM(alias
);
768 aliasList
->aliases
[aliasList
->aliasCount
++] = GET_ALIAS_NUM(alias
);
770 /* aliasList->converter = converter;*/
772 converters
[converter
].totalAliasCount
++; /* One more to the column */
773 tags
[standard
].totalAliasCount
++; /* One more to the row */
775 return aliasList
->aliasCount
;
779 addConverter(const char *converter
) {
781 if(converterCount
>=MAX_CONV_COUNT
) {
782 fprintf(stderr
, "%s:%d: error: too many converters\n", path
, lineNum
);
783 exit(U_BUFFER_OVERFLOW_ERROR
);
786 for (idx
= 0; idx
< converterCount
; idx
++) {
787 if (ucnv_compareNames(converter
, GET_ALIAS_STR(converters
[idx
].converter
)) == 0) {
788 fprintf(stderr
, "%s:%d: error: duplicate converter %s found!\n", path
, lineNum
, converter
);
794 converters
[converterCount
].converter
= GET_ALIAS_NUM(converter
);
795 converters
[converterCount
].totalAliasCount
= 0;
797 return converterCount
++;
800 /* resolve this alias based on the prioritization of the standard tags. */
802 resolveAliasToConverter(uint16_t alias
, uint16_t *tagNum
, uint16_t *converterNum
) {
803 uint16_t idx
, idx2
, idx3
;
805 for (idx
= UCNV_NUM_RESERVED_TAGS
; idx
< tagCount
; idx
++) {
806 for (idx2
= 0; idx2
< converterCount
; idx2
++) {
807 for (idx3
= 0; idx3
< tags
[idx
].aliasList
[idx2
].aliasCount
; idx3
++) {
808 uint16_t aliasNum
= tags
[idx
].aliasList
[idx2
].aliases
[idx3
];
809 if (aliasNum
== alias
) {
811 *converterNum
= idx2
;
817 /* Do the leftovers last, just in case */
818 /* There is no need to do the ALL tag */
820 for (idx2
= 0; idx2
< converterCount
; idx2
++) {
821 for (idx3
= 0; idx3
< tags
[idx
].aliasList
[idx2
].aliasCount
; idx3
++) {
822 uint16_t aliasNum
= tags
[idx
].aliasList
[idx2
].aliases
[idx3
];
823 if (aliasNum
== alias
) {
825 *converterNum
= idx2
;
830 *tagNum
= UINT16_MAX
;
831 *converterNum
= UINT16_MAX
;
832 fprintf(stderr
, "%s: warning: alias %s not found\n",
834 GET_ALIAS_STR(alias
));
838 /* The knownAliases should be sorted before calling this function */
840 resolveAliases(uint16_t *uniqueAliasArr
, uint16_t *uniqueAliasToConverterArr
, uint16_t aliasOffset
) {
841 uint32_t uniqueAliasIdx
= 0;
843 uint16_t currTagNum
, oldTagNum
;
844 uint16_t currConvNum
, oldConvNum
;
845 const char *lastName
;
847 resolveAliasToConverter(knownAliases
[0], &oldTagNum
, &currConvNum
);
848 uniqueAliasToConverterArr
[uniqueAliasIdx
] = currConvNum
;
849 oldConvNum
= currConvNum
;
850 uniqueAliasArr
[uniqueAliasIdx
] = knownAliases
[0] + aliasOffset
;
852 lastName
= GET_ALIAS_STR(knownAliases
[0]);
854 for (idx
= 1; idx
< knownAliasesCount
; idx
++) {
855 resolveAliasToConverter(knownAliases
[idx
], &currTagNum
, &currConvNum
);
856 if (ucnv_compareNames(lastName
, GET_ALIAS_STR(knownAliases
[idx
])) == 0) {
857 /* duplicate found */
858 if ((currTagNum
< oldTagNum
&& currTagNum
>= UCNV_NUM_RESERVED_TAGS
)
860 oldTagNum
= currTagNum
;
861 uniqueAliasToConverterArr
[uniqueAliasIdx
- 1] = currConvNum
;
862 uniqueAliasArr
[uniqueAliasIdx
- 1] = knownAliases
[idx
] + aliasOffset
;
864 printf("using %s instead of %s -> %s",
865 GET_ALIAS_STR(knownAliases
[idx
]),
867 GET_ALIAS_STR(converters
[currConvNum
].converter
));
868 if (oldConvNum
!= currConvNum
) {
869 printf(" (alias conflict)");
877 printf("folding %s into %s -> %s",
878 GET_ALIAS_STR(knownAliases
[idx
]),
880 GET_ALIAS_STR(converters
[oldConvNum
].converter
));
881 if (oldConvNum
!= currConvNum
) {
882 printf(" (alias conflict)");
887 if (oldConvNum
!= currConvNum
) {
888 uniqueAliasToConverterArr
[uniqueAliasIdx
- 1] |= UCNV_AMBIGUOUS_ALIAS_MAP_BIT
;
892 uniqueAliasToConverterArr
[uniqueAliasIdx
] = currConvNum
;
893 oldConvNum
= currConvNum
;
894 uniqueAliasArr
[uniqueAliasIdx
] = knownAliases
[idx
] + aliasOffset
;
896 lastName
= GET_ALIAS_STR(knownAliases
[idx
]);
897 oldTagNum
= currTagNum
;
898 /*printf("%s -> %s\n", GET_ALIAS_STR(knownAliases[idx]), GET_ALIAS_STR(converters[currConvNum].converter));*/
900 if (uprv_strchr(GET_ALIAS_STR(converters
[currConvNum
].converter
), UCNV_OPTION_SEP_CHAR
) != NULL
) {
901 uniqueAliasToConverterArr
[uniqueAliasIdx
-1] |= UCNV_CONTAINS_OPTION_BIT
;
904 return uniqueAliasIdx
;
908 createOneAliasList(uint16_t *aliasArrLists
, uint32_t tag
, uint32_t converter
, uint16_t offset
) {
910 AliasList
*aliasList
= &tags
[tag
].aliasList
[converter
];
912 if (aliasList
->aliasCount
== 0) {
913 aliasArrLists
[tag
*converterCount
+ converter
] = 0;
916 aliasLists
[aliasListsSize
++] = aliasList
->aliasCount
;
918 /* write into the array area a 1's based index. */
919 aliasArrLists
[tag
*converterCount
+ converter
] = aliasListsSize
;
921 /* printf("tag %s converter %s\n",
922 GET_TAG_STR(tags[tag].tag),
923 GET_ALIAS_STR(converters[converter].converter));*/
924 for (aliasNum
= 0; aliasNum
< aliasList
->aliasCount
; aliasNum
++) {
927 GET_ALIAS_STR(aliasList->aliases[aliasNum]));*/
928 if (aliasList
->aliases
[aliasNum
]) {
929 value
= aliasList
->aliases
[aliasNum
] + offset
;
932 if (tag
!= 0) { /* Only show the warning when it's not the leftover tag. */
933 fprintf(stderr
, "%s: warning: tag %s does not have a default alias for %s\n",
935 GET_TAG_STR(tags
[tag
].tag
),
936 GET_ALIAS_STR(converters
[converter
].converter
));
939 aliasLists
[aliasListsSize
++] = value
;
940 if (aliasListsSize
>= MAX_LIST_SIZE
) {
941 fprintf(stderr
, "%s: error: Too many alias lists\n", path
);
942 exit(U_BUFFER_OVERFLOW_ERROR
);
950 createNormalizedAliasStrings(char *normalizedStrings
, const char *origStringBlock
, int32_t stringBlockLength
) {
952 uprv_memcpy(normalizedStrings
, origStringBlock
, stringBlockLength
);
953 while ((currStrLen
= (int32_t)uprv_strlen(origStringBlock
)) < stringBlockLength
) {
954 int32_t currStrSize
= currStrLen
+ 1;
955 if (currStrLen
> 0) {
957 ucnv_io_stripForCompare(normalizedStrings
, origStringBlock
);
958 normStrLen
= uprv_strlen(normalizedStrings
);
959 if (normStrLen
> 0) {
960 uprv_memset(normalizedStrings
+ normStrLen
, 0, currStrSize
- normStrLen
);
963 stringBlockLength
-= currStrSize
;
964 normalizedStrings
+= currStrSize
;
965 origStringBlock
+= currStrSize
;
970 writeAliasTable(UNewDataMemory
*out
) {
972 uint32_t uniqueAliasesSize
;
973 uint16_t aliasOffset
= (uint16_t)(tagBlock
.top
/sizeof(uint16_t));
974 uint16_t *aliasArrLists
= (uint16_t *)uprv_malloc(tagCount
* converterCount
* sizeof(uint16_t));
975 uint16_t *uniqueAliases
= (uint16_t *)uprv_malloc(knownAliasesCount
* sizeof(uint16_t));
976 uint16_t *uniqueAliasesToConverter
= (uint16_t *)uprv_malloc(knownAliasesCount
* sizeof(uint16_t));
978 qsort(knownAliases
, knownAliasesCount
, sizeof(knownAliases
[0]), compareAliases
);
979 uniqueAliasesSize
= resolveAliases(uniqueAliases
, uniqueAliasesToConverter
, aliasOffset
);
981 /* Array index starts at 1. aliasLists[0] is the size of the lists section. */
984 /* write the offsets of all the aliases lists in a 2D array, and create the lists. */
985 for (i
= 0; i
< tagCount
; ++i
) {
986 for (j
= 0; j
< converterCount
; ++j
) {
987 createOneAliasList(aliasArrLists
, i
, j
, aliasOffset
);
991 /* Write the size of the TOC */
992 if (tableOptions
.stringNormalizationType
== UCNV_IO_UNNORMALIZED
) {
993 udata_write32(out
, 8);
996 udata_write32(out
, 9);
999 /* Write the sizes of each section */
1000 /* All sizes are the number of uint16_t units, not bytes */
1001 udata_write32(out
, converterCount
);
1002 udata_write32(out
, tagCount
);
1003 udata_write32(out
, uniqueAliasesSize
); /* list of aliases */
1004 udata_write32(out
, uniqueAliasesSize
); /* The preresolved form of mapping an untagged the alias to a converter */
1005 udata_write32(out
, tagCount
* converterCount
);
1006 udata_write32(out
, aliasListsSize
+ 1);
1007 udata_write32(out
, sizeof(tableOptions
) / sizeof(uint16_t));
1008 udata_write32(out
, (tagBlock
.top
+ stringBlock
.top
) / sizeof(uint16_t));
1009 if (tableOptions
.stringNormalizationType
!= UCNV_IO_UNNORMALIZED
) {
1010 udata_write32(out
, (tagBlock
.top
+ stringBlock
.top
) / sizeof(uint16_t));
1013 /* write the table of converters */
1014 /* Think of this as the column headers */
1015 for(i
=0; i
<converterCount
; ++i
) {
1016 udata_write16(out
, (uint16_t)(converters
[i
].converter
+ aliasOffset
));
1019 /* write the table of tags */
1020 /* Think of this as the row headers */
1021 for(i
=UCNV_NUM_RESERVED_TAGS
; i
<tagCount
; ++i
) {
1022 udata_write16(out
, tags
[i
].tag
);
1024 /* The empty tag is considered the leftover list, and put that at the end of the priority list. */
1025 udata_write16(out
, tags
[EMPTY_TAG_NUM
].tag
);
1026 udata_write16(out
, tags
[ALL_TAG_NUM
].tag
);
1028 /* Write the unique list of aliases */
1029 udata_writeBlock(out
, uniqueAliases
, uniqueAliasesSize
* sizeof(uint16_t));
1031 /* Write the unique list of aliases */
1032 udata_writeBlock(out
, uniqueAliasesToConverter
, uniqueAliasesSize
* sizeof(uint16_t));
1034 /* Write the array to the lists */
1035 udata_writeBlock(out
, (const void *)(aliasArrLists
+ (2*converterCount
)), (((tagCount
- 2) * converterCount
) * sizeof(uint16_t)));
1036 /* Now write the leftover part of the array for the EMPTY and ALL lists */
1037 udata_writeBlock(out
, (const void *)aliasArrLists
, (2 * converterCount
* sizeof(uint16_t)));
1039 /* Offset the next array to make the index start at 1. */
1040 udata_write16(out
, 0xDEAD);
1042 /* Write the lists */
1043 udata_writeBlock(out
, (const void *)aliasLists
, aliasListsSize
* sizeof(uint16_t));
1045 /* Write any options for the alias table. */
1046 udata_writeBlock(out
, (const void *)&tableOptions
, sizeof(tableOptions
));
1048 /* write the tags strings */
1049 udata_writeString(out
, tagBlock
.store
, tagBlock
.top
);
1051 /* write the aliases strings */
1052 udata_writeString(out
, stringBlock
.store
, stringBlock
.top
);
1054 /* write the normalized aliases strings */
1055 if (tableOptions
.stringNormalizationType
!= UCNV_IO_UNNORMALIZED
) {
1056 char *normalizedStrings
= (char *)uprv_malloc(tagBlock
.top
+ stringBlock
.top
);
1057 createNormalizedAliasStrings(normalizedStrings
, tagBlock
.store
, tagBlock
.top
);
1058 createNormalizedAliasStrings(normalizedStrings
+ tagBlock
.top
, stringBlock
.store
, stringBlock
.top
);
1060 /* Write out the complete normalized array. */
1061 udata_writeString(out
, normalizedStrings
, tagBlock
.top
+ stringBlock
.top
);
1062 uprv_free(normalizedStrings
);
1065 uprv_free(uniqueAliasesToConverter
);
1066 uprv_free(uniqueAliases
);
1067 uprv_free(aliasArrLists
);
1071 allocString(StringBlock
*block
, const char *s
, int32_t length
) {
1076 length
=(int32_t)uprv_strlen(s
);
1080 * add 1 for the terminating NUL
1081 * and round up (+1 &~1)
1082 * to keep the addresses on a 16-bit boundary
1084 top
=block
->top
+ (uint32_t)((length
+ 1 + 1) & ~1);
1086 if(top
>= block
->max
) {
1087 fprintf(stderr
, "%s:%d: error: out of memory\n", path
, lineNum
);
1088 exit(U_MEMORY_ALLOCATION_ERROR
);
1091 /* get the pointer and copy the string */
1092 p
= block
->store
+ block
->top
;
1093 uprv_memcpy(p
, s
, length
);
1094 p
[length
] = 0; /* NUL-terminate it */
1095 if((length
& 1) == 0) {
1096 p
[length
+ 1] = 0; /* set the padding byte */
1099 /* check for invariant characters now that we have a NUL-terminated string for easy output */
1100 if(!uprv_isInvariantString(p
, length
)) {
1101 fprintf(stderr
, "%s:%d: error: the name %s contains not just invariant characters\n", path
, lineNum
, p
);
1102 exit(U_INVALID_TABLE_FORMAT
);
1110 compareAliases(const void *alias1
, const void *alias2
) {
1111 /* Names like IBM850 and ibm-850 need to be sorted together */
1112 int result
= ucnv_compareNames(GET_ALIAS_STR(*(uint16_t*)alias1
), GET_ALIAS_STR(*(uint16_t*)alias2
));
1114 /* Sort the shortest first */
1115 return (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias1
)) - (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias2
));
1121 * Hey, Emacs, please set the following:
1124 * indent-tabs-mode: nil