]> git.saurik.com Git - apple/icu.git/blame - icuSources/tools/gencnval/gencnval.c
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / tools / gencnval / gencnval.c
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4*******************************************************************************
5*
2ca993e8 6* Copyright (C) 1999-2016 International Business Machines
b75a7d8f
A
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: gencnval.c
f3c0d7a5 11* encoding: UTF-8
b75a7d8f
A
12* tab size: 8 (not used)
13* indentation:4
14*
15* created on: 1999nov05
16* created by: Markus W. Scherer
17*
18* This program reads convrtrs.txt and writes a memory-mappable
19* converter name alias table to cnvalias.dat .
20*
21* This program currently writes version 2.1 of the data format. See
22* ucnv_io.c for more details on the format. Note that version 2.1
23* is written in such a way that a 2.0 reader will be able to use it,
24* and a 2.1 reader will be able to read 2.0.
25*/
26
27#include "unicode/utypes.h"
28#include "unicode/putil.h"
29#include "unicode/ucnv.h" /* ucnv_compareNames() */
30#include "ucnv_io.h"
31#include "cmemory.h"
32#include "cstring.h"
374ca955 33#include "uinvchar.h"
b75a7d8f 34#include "filestrm.h"
374ca955 35#include "unicode/uclean.h"
b75a7d8f
A
36#include "unewdata.h"
37#include "uoptions.h"
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <ctype.h>
42
43/* TODO: Need to check alias name length is less than UCNV_MAX_CONVERTER_NAME_LENGTH */
44
45/* STRING_STORE_SIZE + TAG_STORE_SIZE <= ((2^16 - 1) * 2)
46 That is the maximum size for the string stores combined
47 because the strings are index at 16-bit boundries by a
48 16-bit index, and there is only one section for the
49 strings.
50 */
51#define STRING_STORE_SIZE 0x1FBFE /* 130046 */
52#define TAG_STORE_SIZE 0x400 /* 1024 */
53
54/* The combined tag and converter count can affect the number of lists
55 created. The size of all lists must be less than (2^17 - 1)
56 because the lists are indexed as a 16-bit array with a 16-bit index.
57 */
58#define MAX_TAG_COUNT 0x3F /* 63 */
59#define MAX_CONV_COUNT UCNV_CONVERTER_INDEX_MASK
60#define MAX_ALIAS_COUNT 0xFFFF /* 65535 */
61
62/* The maximum number of aliases that a standard tag/converter combination can have.
63 At this moment 6/18/2002, IANA has 12 names for ASCII. Don't go below 15 for
64 this value. I don't recommend more than 31 for this value.
65 */
66#define MAX_TC_ALIAS_COUNT 0x1F /* 31 */
67
68#define MAX_LINE_SIZE 0x7FFF /* 32767 */
69#define MAX_LIST_SIZE 0xFFFF /* 65535 */
70
71#define DATA_NAME "cnvalias"
72#define DATA_TYPE "icu" /* ICU alias table */
73
74#define ALL_TAG_STR "ALL"
75#define ALL_TAG_NUM 1
76#define EMPTY_TAG_NUM 0
77
78/* UDataInfo cf. udata.h */
79static const UDataInfo dataInfo={
80 sizeof(UDataInfo),
81 0,
82
83 U_IS_BIG_ENDIAN,
84 U_CHARSET_FAMILY,
85 sizeof(UChar),
86 0,
87
88 {0x43, 0x76, 0x41, 0x6c}, /* dataFormat="CvAl" */
73c04bcf 89 {3, 0, 1, 0}, /* formatVersion */
b75a7d8f
A
90 {1, 4, 2, 0} /* dataVersion */
91};
92
93typedef struct {
94 char *store;
95 uint32_t top;
96 uint32_t max;
97} StringBlock;
98
99static char stringStore[STRING_STORE_SIZE];
100static StringBlock stringBlock = { stringStore, 0, STRING_STORE_SIZE };
101
102typedef struct {
103 uint16_t aliasCount;
104 uint16_t *aliases; /* Index into stringStore */
105} AliasList;
106
107typedef struct {
108 uint16_t converter; /* Index into stringStore */
109 uint16_t totalAliasCount; /* Total aliases in this column */
110} Converter;
111
112static Converter converters[MAX_CONV_COUNT];
113static uint16_t converterCount=0;
114
115static char tagStore[TAG_STORE_SIZE];
116static StringBlock tagBlock = { tagStore, 0, TAG_STORE_SIZE };
117
118typedef struct {
119 uint16_t tag; /* Index into tagStore */
120 uint16_t totalAliasCount; /* Total aliases in this row */
121 AliasList aliasList[MAX_CONV_COUNT];
122} Tag;
123
124/* Think of this as a 3D array. It's tagCount by converterCount by aliasCount */
125static Tag tags[MAX_TAG_COUNT];
126static uint16_t tagCount = 0;
127
128/* Used for storing all aliases */
129static uint16_t knownAliases[MAX_ALIAS_COUNT];
130static uint16_t knownAliasesCount = 0;
131/*static uint16_t duplicateKnownAliasesCount = 0;*/
132
133/* Used for storing the lists section that point to aliases */
134static uint16_t aliasLists[MAX_LIST_SIZE];
135static uint16_t aliasListsSize = 0;
136
137/* Were the standard tags declared before the aliases. */
138static UBool standardTagsUsed = FALSE;
139static UBool verbose = FALSE;
2ca993e8 140static UBool quiet = FALSE;
374ca955 141static int lineNum = 1;
b75a7d8f 142
73c04bcf
A
143static UConverterAliasOptions tableOptions = {
144 UCNV_IO_STD_NORMALIZED,
145 1 /* containsCnvOptionInfo */
146};
147
729e4ab9
A
148
149/**
150 * path to convrtrs.txt
151 */
152const char *path;
153
b75a7d8f
A
154/* prototypes --------------------------------------------------------------- */
155
156static void
157parseLine(const char *line);
158
159static void
160parseFile(FileStream *in);
161
162static int32_t
163chomp(char *line);
164
165static void
166addOfficialTaggedStandards(char *line, int32_t lineLen);
167
168static uint16_t
169addAlias(const char *alias, uint16_t standard, uint16_t converter, UBool defaultName);
170
171static uint16_t
172addConverter(const char *converter);
173
174static char *
374ca955 175allocString(StringBlock *block, const char *s, int32_t length);
b75a7d8f
A
176
177static uint16_t
178addToKnownAliases(const char *alias);
179
180static int
181compareAliases(const void *alias1, const void *alias2);
182
183static uint16_t
184getTagNumber(const char *tag, uint16_t tagLen);
185
186/*static void
187addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter);*/
188
189static void
190writeAliasTable(UNewDataMemory *out);
191
192/* -------------------------------------------------------------------------- */
193
194/* Presumes that you used allocString() */
195#define GET_ALIAS_STR(index) (stringStore + ((size_t)(index) << 1))
196#define GET_TAG_STR(index) (tagStore + ((size_t)(index) << 1))
197
198/* Presumes that you used allocString() */
199#define GET_ALIAS_NUM(str) ((uint16_t)((str - stringStore) >> 1))
200#define GET_TAG_NUM(str) ((uint16_t)((str - tagStore) >> 1))
201
202enum
203{
204 HELP1,
205 HELP2,
206 VERBOSE,
207 COPYRIGHT,
208 DESTDIR,
2ca993e8
A
209 SOURCEDIR,
210 QUIET
b75a7d8f
A
211};
212
213static UOption options[]={
214 UOPTION_HELP_H,
215 UOPTION_HELP_QUESTION_MARK,
216 UOPTION_VERBOSE,
217 UOPTION_COPYRIGHT,
218 UOPTION_DESTDIR,
2ca993e8
A
219 UOPTION_SOURCEDIR,
220 UOPTION_QUIET
b75a7d8f
A
221};
222
223extern int
224main(int argc, char* argv[]) {
51004dcb 225 int i, n;
b75a7d8f 226 char pathBuf[512];
b75a7d8f
A
227 FileStream *in;
228 UNewDataMemory *out;
229 UErrorCode errorCode=U_ZERO_ERROR;
230
231 U_MAIN_INIT_ARGS(argc, argv);
232
233 /* preset then read command line options */
374ca955 234 options[DESTDIR].value=options[SOURCEDIR].value=u_getDataDirectory();
2ca993e8 235 argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
b75a7d8f
A
236
237 /* error handling, printing usage message */
238 if(argc<0) {
239 fprintf(stderr,
240 "error in command line argument \"%s\"\n",
241 argv[-argc]);
242 }
243 if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) {
244 fprintf(stderr,
245 "usage: %s [-options] [convrtrs.txt]\n"
246 "\tread convrtrs.txt and create " U_ICUDATA_NAME "_" DATA_NAME "." DATA_TYPE "\n"
247 "options:\n"
248 "\t-h or -? or --help this usage text\n"
249 "\t-v or --verbose prints out extra information about the alias table\n"
2ca993e8 250 "\t-q or --quiet do not display warnings and progress\n"
b75a7d8f
A
251 "\t-c or --copyright include a copyright notice\n"
252 "\t-d or --destdir destination directory, followed by the path\n"
253 "\t-s or --sourcedir source directory, followed by the path\n",
254 argv[0]);
255 return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
256 }
257
258 if(options[VERBOSE].doesOccur) {
259 verbose = TRUE;
260 }
261
2ca993e8
A
262 if(options[QUIET].doesOccur) {
263 quiet = TRUE;
264 }
265
3d1f044b
A
266 if (argc >= 2) {
267 path = argv[1];
b75a7d8f 268 } else {
3d1f044b
A
269 path = "convrtrs.txt";
270 }
271
272 const char* sourcedir = options[SOURCEDIR].value;
273 if (sourcedir != NULL && *sourcedir != 0) {
274 char *end;
275 uprv_strcpy(pathBuf, sourcedir);
276 end = uprv_strchr(pathBuf, 0);
277 if(*(end-1)!=U_FILE_SEP_CHAR) {
278 *(end++)=U_FILE_SEP_CHAR;
b75a7d8f 279 }
3d1f044b
A
280 uprv_strcpy(end, path);
281 path = pathBuf;
b75a7d8f
A
282 }
283
284 uprv_memset(stringStore, 0, sizeof(stringStore));
285 uprv_memset(tagStore, 0, sizeof(tagStore));
286 uprv_memset(converters, 0, sizeof(converters));
287 uprv_memset(tags, 0, sizeof(tags));
288 uprv_memset(aliasLists, 0, sizeof(aliasLists));
289 uprv_memset(knownAliases, 0, sizeof(aliasLists));
290
291
292 in=T_FileStream_open(path, "r");
293 if(in==NULL) {
729e4ab9 294 fprintf(stderr, "gencnval: unable to open input file %s\n", path);
b75a7d8f
A
295 exit(U_FILE_ACCESS_ERROR);
296 }
297 parseFile(in);
298 T_FileStream_close(in);
299
300 /* create the output file */
374ca955
A
301 out=udata_create(options[DESTDIR].value, DATA_TYPE, DATA_NAME, &dataInfo,
302 options[COPYRIGHT].doesOccur ? U_COPYRIGHT_STRING : NULL, &errorCode);
b75a7d8f
A
303 if(U_FAILURE(errorCode)) {
304 fprintf(stderr, "gencnval: unable to open output file - error %s\n", u_errorName(errorCode));
305 exit(errorCode);
306 }
307
308 /* write the table of aliases based on a tag/converter name combination */
309 writeAliasTable(out);
310
311 /* finish */
312 udata_finish(out, &errorCode);
313 if(U_FAILURE(errorCode)) {
314 fprintf(stderr, "gencnval: error finishing output file - %s\n", u_errorName(errorCode));
315 exit(errorCode);
316 }
317
51004dcb
A
318 /* clean up tags */
319 for (i = 0; i < MAX_TAG_COUNT; i++) {
320 for (n = 0; n < MAX_CONV_COUNT; n++) {
321 if (tags[i].aliasList[n].aliases!=NULL) {
322 uprv_free(tags[i].aliasList[n].aliases);
323 }
324 }
325 }
326
b75a7d8f
A
327 return 0;
328}
329
330static void
331parseFile(FileStream *in) {
332 char line[MAX_LINE_SIZE];
333 char lastLine[MAX_LINE_SIZE];
334 int32_t lineSize = 0;
335 int32_t lastLineSize = 0;
336 UBool validParse = TRUE;
337
338 lineNum = 0;
339
340 /* Add the empty tag, which is for untagged aliases */
341 getTagNumber("", 0);
342 getTagNumber(ALL_TAG_STR, 3);
374ca955 343 allocString(&stringBlock, "", 0);
b75a7d8f
A
344
345 /* read the list of aliases */
346 while (validParse) {
347 validParse = FALSE;
348
349 /* Read non-empty lines that don't start with a space character. */
350 while (T_FileStream_readLine(in, lastLine, MAX_LINE_SIZE) != NULL) {
351 lastLineSize = chomp(lastLine);
4388f060 352 if (lineSize == 0 || (lastLineSize > 0 && isspace((int)*lastLine))) {
b75a7d8f
A
353 uprv_strcpy(line + lineSize, lastLine);
354 lineSize += lastLineSize;
355 } else if (lineSize > 0) {
356 validParse = TRUE;
357 break;
358 }
359 lineNum++;
360 }
361
362 if (validParse || lineSize > 0) {
4388f060 363 if (isspace((int)*line)) {
729e4ab9 364 fprintf(stderr, "%s:%d: error: cannot start an alias with a space\n", path, lineNum-1);
b75a7d8f
A
365 exit(U_PARSE_ERROR);
366 } else if (line[0] == '{') {
367 if (!standardTagsUsed && line[lineSize - 1] != '}') {
729e4ab9 368 fprintf(stderr, "%s:%d: error: alias needs to start with a converter name\n", path, lineNum);
b75a7d8f
A
369 exit(U_PARSE_ERROR);
370 }
371 addOfficialTaggedStandards(line, lineSize);
372 standardTagsUsed = TRUE;
373 } else {
374 if (standardTagsUsed) {
375 parseLine(line);
376 }
377 else {
729e4ab9 378 fprintf(stderr, "%s:%d: error: alias table needs to start a list of standard tags\n", path, lineNum);
b75a7d8f
A
379 exit(U_PARSE_ERROR);
380 }
381 }
382 /* Was the last line consumed */
383 if (lastLineSize > 0) {
384 uprv_strcpy(line, lastLine);
385 lineSize = lastLineSize;
386 }
387 else {
388 lineSize = 0;
389 }
390 }
391 lineNum++;
392 }
393}
394
395/* This works almost like the Perl chomp.
396 It removes the newlines, comments and trailing whitespace (not preceding whitespace).
397*/
398static int32_t
399chomp(char *line) {
400 char *s = line;
401 char *lastNonSpace = line;
402 while(*s!=0) {
403 /* truncate at a newline or a comment */
404 if(*s == '\r' || *s == '\n' || *s == '#') {
405 *s = 0;
406 break;
407 }
4388f060 408 if (!isspace((int)*s)) {
b75a7d8f
A
409 lastNonSpace = s;
410 }
411 ++s;
412 }
413 if (lastNonSpace++ > line) {
414 *lastNonSpace = 0;
415 s = lastNonSpace;
416 }
417 return (int32_t)(s - line);
418}
419
420static void
421parseLine(const char *line) {
422 uint16_t pos=0, start, limit, length, cnv;
423 char *converter, *alias;
424
425 /* skip leading white space */
426 /* There is no whitespace at the beginning anymore */
427/* while(line[pos]!=0 && isspace(line[pos])) {
428 ++pos;
429 }
430*/
431
432 /* is there nothing on this line? */
433 if(line[pos]==0) {
434 return;
435 }
436
437 /* get the converter name */
438 start=pos;
4388f060 439 while(line[pos]!=0 && !isspace((int)line[pos])) {
b75a7d8f
A
440 ++pos;
441 }
442 limit=pos;
443
444 /* store the converter name */
445 length=(uint16_t)(limit-start);
374ca955 446 converter=allocString(&stringBlock, line+start, length);
b75a7d8f
A
447
448 /* add the converter to the converter table */
449 cnv=addConverter(converter);
450
451 /* The name itself may be tagged, so let's added it to the aliases list properly */
452 pos = start;
453
454 /* get all the real aliases */
455 for(;;) {
456
457 /* skip white space */
4388f060 458 while(line[pos]!=0 && isspace((int)line[pos])) {
b75a7d8f
A
459 ++pos;
460 }
461
462 /* is there no more alias name on this line? */
463 if(line[pos]==0) {
464 break;
465 }
466
467 /* get an alias name */
468 start=pos;
4388f060 469 while(line[pos]!=0 && line[pos]!='{' && !isspace((int)line[pos])) {
b75a7d8f
A
470 ++pos;
471 }
472 limit=pos;
473
474 /* store the alias name */
475 length=(uint16_t)(limit-start);
476 if (start == 0) {
477 /* add the converter as its own alias to the alias table */
478 alias = converter;
479 addAlias(alias, ALL_TAG_NUM, cnv, TRUE);
480 }
481 else {
374ca955 482 alias=allocString(&stringBlock, line+start, length);
b75a7d8f
A
483 addAlias(alias, ALL_TAG_NUM, cnv, FALSE);
484 }
485 addToKnownAliases(alias);
486
487 /* add the alias/converter pair to the alias table */
488 /* addAlias(alias, 0, cnv, FALSE);*/
489
490 /* skip whitespace */
4388f060 491 while (line[pos] && isspace((int)line[pos])) {
b75a7d8f
A
492 ++pos;
493 }
494
495 /* handle tags if they are present */
496 if (line[pos] == '{') {
497 ++pos;
498 do {
499 start = pos;
4388f060 500 while (line[pos] && line[pos] != '}' && !isspace((int)line[pos])) {
b75a7d8f
A
501 ++pos;
502 }
503 limit = pos;
504
505 if (start != limit) {
506 /* add the tag to the tag table */
507 uint16_t tag = getTagNumber(line + start, (uint16_t)(limit - start));
508 addAlias(alias, tag, cnv, (UBool)(line[limit-1] == '*'));
509 }
510
4388f060 511 while (line[pos] && isspace((int)line[pos])) {
b75a7d8f
A
512 ++pos;
513 }
514 } while (line[pos] && line[pos] != '}');
515
516 if (line[pos] == '}') {
517 ++pos;
518 } else {
729e4ab9 519 fprintf(stderr, "%s:%d: Unterminated tag list\n", path, lineNum);
b75a7d8f
A
520 exit(U_UNMATCHED_BRACES);
521 }
522 } else {
523 addAlias(alias, EMPTY_TAG_NUM, cnv, (UBool)(tags[0].aliasList[cnv].aliasCount == 0));
524 }
525 }
526}
527
528static uint16_t
529getTagNumber(const char *tag, uint16_t tagLen) {
530 char *atag;
531 uint16_t t;
532 UBool preferredName = ((tagLen > 0) ? (tag[tagLen - 1] == '*') : (FALSE));
533
534 if (tagCount >= MAX_TAG_COUNT) {
729e4ab9 535 fprintf(stderr, "%s:%d: too many tags\n", path, lineNum);
b75a7d8f
A
536 exit(U_BUFFER_OVERFLOW_ERROR);
537 }
538
539 if (preferredName) {
540/* puts(tag);*/
541 tagLen--;
542 }
543
544 for (t = 0; t < tagCount; ++t) {
545 const char *currTag = GET_TAG_STR(tags[t].tag);
546 if (uprv_strlen(currTag) == tagLen && !uprv_strnicmp(currTag, tag, tagLen)) {
547 return t;
548 }
549 }
550
551 /* we need to add this tag */
552 if (tagCount >= MAX_TAG_COUNT) {
729e4ab9 553 fprintf(stderr, "%s:%d: error: too many tags\n", path, lineNum);
b75a7d8f
A
554 exit(U_BUFFER_OVERFLOW_ERROR);
555 }
556
557 /* allocate a new entry in the tag table */
374ca955 558 atag = allocString(&tagBlock, tag, tagLen);
b75a7d8f
A
559
560 if (standardTagsUsed) {
729e4ab9
A
561 fprintf(stderr, "%s:%d: error: Tag \"%s\" is not declared at the beginning of the alias table.\n",
562 path, lineNum, atag);
b75a7d8f
A
563 exit(1);
564 }
565 else if (tagLen > 0 && strcmp(tag, ALL_TAG_STR) != 0) {
729e4ab9
A
566 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",
567 path, lineNum, atag);
b75a7d8f
A
568 }
569
570 /* add the tag to the tag table */
571 tags[tagCount].tag = GET_TAG_NUM(atag);
572 /* The aliasList should be set to 0's already */
573
574 return tagCount++;
575}
576
577/*static void
578addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter) {
579 tags[tag].aliases[converter] = alias;
580}
581*/
582
583static void
584addOfficialTaggedStandards(char *line, int32_t lineLen) {
585 char *atag;
46f4442e
A
586 char *endTagExp;
587 char *tag;
b75a7d8f
A
588 static const char WHITESPACE[] = " \t";
589
590 if (tagCount > UCNV_NUM_RESERVED_TAGS) {
729e4ab9 591 fprintf(stderr, "%s:%d: error: official tags already added\n", path, lineNum);
b75a7d8f
A
592 exit(U_BUFFER_OVERFLOW_ERROR);
593 }
46f4442e
A
594 tag = strchr(line, '{');
595 if (tag == NULL) {
596 /* Why were we called? */
729e4ab9 597 fprintf(stderr, "%s:%d: error: Missing start of tag group\n", path, lineNum);
46f4442e
A
598 exit(U_PARSE_ERROR);
599 }
600 tag++;
601 endTagExp = strchr(tag, '}');
602 if (endTagExp == NULL) {
729e4ab9 603 fprintf(stderr, "%s:%d: error: Missing end of tag group\n", path, lineNum);
46f4442e
A
604 exit(U_PARSE_ERROR);
605 }
606 endTagExp[0] = 0;
b75a7d8f
A
607
608 tag = strtok(tag, WHITESPACE);
609 while (tag != NULL) {
610/* printf("Adding original tag \"%s\"\n", tag);*/
611
b75a7d8f 612 /* allocate a new entry in the tag table */
374ca955 613 atag = allocString(&tagBlock, tag, -1);
b75a7d8f
A
614
615 /* add the tag to the tag table */
616 tags[tagCount++].tag = (uint16_t)((atag - tagStore) >> 1);
617
618 /* The aliasList should already be set to 0's */
619
620 /* Get next tag */
621 tag = strtok(NULL, WHITESPACE);
622 }
623}
624
625static uint16_t
626addToKnownAliases(const char *alias) {
627/* uint32_t idx; */
628 /* strict matching */
629/* for (idx = 0; idx < knownAliasesCount; idx++) {
630 uint16_t num = GET_ALIAS_NUM(alias);
631 if (knownAliases[idx] != num
632 && uprv_strcmp(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
633 {
729e4ab9 634 fprintf(stderr, "%s:%d: warning: duplicate alias %s and %s found\n", path,
b75a7d8f
A
635 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
636 duplicateKnownAliasesCount++;
637 break;
638 }
639 else if (knownAliases[idx] != num
640 && ucnv_compareNames(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
641 {
642 if (verbose) {
729e4ab9 643 fprintf(stderr, "%s:%d: information: duplicate alias %s and %s found\n", path,
b75a7d8f
A
644 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
645 }
646 duplicateKnownAliasesCount++;
647 break;
648 }
649 }
650*/
651 if (knownAliasesCount >= MAX_ALIAS_COUNT) {
729e4ab9
A
652 fprintf(stderr, "%s:%d: warning: Too many aliases defined for all converters\n",
653 path, lineNum);
b75a7d8f
A
654 exit(U_BUFFER_OVERFLOW_ERROR);
655 }
656 /* TODO: We could try to unlist exact duplicates. */
657 return knownAliases[knownAliasesCount++] = GET_ALIAS_NUM(alias);
658}
659
660/*
661@param standard When standard is 0, then it's the "empty" tag.
662*/
663static uint16_t
664addAlias(const char *alias, uint16_t standard, uint16_t converter, UBool defaultName) {
665 uint32_t idx, idx2;
b75a7d8f
A
666 UBool startEmptyWithoutDefault = FALSE;
667 AliasList *aliasList;
668
669 if(standard>=MAX_TAG_COUNT) {
729e4ab9 670 fprintf(stderr, "%s:%d: error: too many standard tags\n", path, lineNum);
b75a7d8f
A
671 exit(U_BUFFER_OVERFLOW_ERROR);
672 }
673 if(converter>=MAX_CONV_COUNT) {
729e4ab9 674 fprintf(stderr, "%s:%d: error: too many converter names\n", path, lineNum);
b75a7d8f
A
675 exit(U_BUFFER_OVERFLOW_ERROR);
676 }
677 aliasList = &tags[standard].aliasList[converter];
678
679 if (strchr(alias, '}')) {
729e4ab9 680 fprintf(stderr, "%s:%d: error: unmatched } found\n", path,
b75a7d8f
A
681 lineNum);
682 }
683
684 if(aliasList->aliasCount + 1 >= MAX_TC_ALIAS_COUNT) {
729e4ab9 685 fprintf(stderr, "%s:%d: error: too many aliases for alias %s and converter %s\n", path,
b75a7d8f
A
686 lineNum, alias, GET_ALIAS_STR(converters[converter].converter));
687 exit(U_BUFFER_OVERFLOW_ERROR);
688 }
689
690 /* Show this warning only once. All aliases are added to the "ALL" tag. */
691 if (standard == ALL_TAG_NUM && GET_ALIAS_STR(converters[converter].converter) != alias) {
692 /* Normally these option values are parsed at runtime, and they can
693 be discarded when the alias is a default converter. Options should
694 only be on a converter and not an alias. */
695 if (uprv_strchr(alias, UCNV_OPTION_SEP_CHAR) != 0)
696 {
697 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",
698 lineNum, alias);
699 }
700 if (uprv_strchr(alias, UCNV_VALUE_SEP_CHAR) != 0)
701 {
702 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",
703 lineNum, alias);
704 }
705 }
706
73c04bcf 707 if (standard != ALL_TAG_NUM) {
b75a7d8f
A
708 /* Check for duplicate aliases for this tag on all converters */
709 for (idx = 0; idx < converterCount; idx++) {
710 for (idx2 = 0; idx2 < tags[standard].aliasList[idx].aliasCount; idx2++) {
711 uint16_t aliasNum = tags[standard].aliasList[idx].aliases[idx2];
712 if (aliasNum
713 && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
714 {
73c04bcf
A
715 if (idx == converter) {
716 /*
717 * (alias, standard) duplicates are harmless if they map to the same converter.
718 * Only print a warning in verbose mode, or if the alias is a precise duplicate,
719 * not just a lenient-match duplicate.
720 */
721 if (verbose || 0 == uprv_strcmp(alias, GET_ALIAS_STR(aliasNum))) {
729e4ab9 722 fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard %s and converter %s\n", path,
73c04bcf
A
723 lineNum, alias, GET_ALIAS_STR(aliasNum),
724 GET_TAG_STR(tags[standard].tag),
725 GET_ALIAS_STR(converters[converter].converter));
726 }
727 } else {
729e4ab9 728 fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard tag %s between converter %s and converter %s\n", path,
73c04bcf
A
729 lineNum, alias, GET_ALIAS_STR(aliasNum),
730 GET_TAG_STR(tags[standard].tag),
731 GET_ALIAS_STR(converters[converter].converter),
732 GET_ALIAS_STR(converters[idx].converter));
733 }
b75a7d8f
A
734 break;
735 }
736 }
737 }
738
739 /* Check for duplicate default aliases for this converter on all tags */
740 /* It's okay to have multiple standards prefer the same name */
741/* if (verbose && !dupFound) {
742 for (idx = 0; idx < tagCount; idx++) {
743 if (tags[idx].aliasList[converter].aliases) {
744 uint16_t aliasNum = tags[idx].aliasList[converter].aliases[0];
745 if (aliasNum
746 && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
747 {
729e4ab9 748 fprintf(stderr, "%s:%d: warning: duplicate alias %s found for converter %s and standard tag %s\n", path,
b75a7d8f
A
749 lineNum, alias, GET_ALIAS_STR(converters[converter].converter), GET_TAG_STR(tags[standard].tag));
750 break;
751 }
752 }
753 }
754 }*/
755 }
756
757 if (aliasList->aliasCount <= 0) {
758 aliasList->aliasCount++;
759 startEmptyWithoutDefault = TRUE;
760 }
761 aliasList->aliases = (uint16_t *)uprv_realloc(aliasList->aliases, (aliasList->aliasCount + 1) * sizeof(aliasList->aliases[0]));
762 if (startEmptyWithoutDefault) {
763 aliasList->aliases[0] = 0;
764 }
765 if (defaultName) {
766 if (aliasList->aliases[0] != 0) {
729e4ab9 767 fprintf(stderr, "%s:%d: error: Alias %s and %s cannot both be the default alias for standard tag %s and converter %s\n", path,
b75a7d8f
A
768 lineNum,
769 alias,
770 GET_ALIAS_STR(aliasList->aliases[0]),
771 GET_TAG_STR(tags[standard].tag),
772 GET_ALIAS_STR(converters[converter].converter));
773 exit(U_PARSE_ERROR);
774 }
775 aliasList->aliases[0] = GET_ALIAS_NUM(alias);
776 } else {
777 aliasList->aliases[aliasList->aliasCount++] = GET_ALIAS_NUM(alias);
778 }
779/* aliasList->converter = converter;*/
780
781 converters[converter].totalAliasCount++; /* One more to the column */
782 tags[standard].totalAliasCount++; /* One more to the row */
783
784 return aliasList->aliasCount;
785}
786
787static uint16_t
788addConverter(const char *converter) {
789 uint32_t idx;
790 if(converterCount>=MAX_CONV_COUNT) {
729e4ab9 791 fprintf(stderr, "%s:%d: error: too many converters\n", path, lineNum);
b75a7d8f
A
792 exit(U_BUFFER_OVERFLOW_ERROR);
793 }
794
795 for (idx = 0; idx < converterCount; idx++) {
796 if (ucnv_compareNames(converter, GET_ALIAS_STR(converters[idx].converter)) == 0) {
729e4ab9 797 fprintf(stderr, "%s:%d: error: duplicate converter %s found!\n", path, lineNum, converter);
b75a7d8f
A
798 exit(U_PARSE_ERROR);
799 break;
800 }
801 }
802
803 converters[converterCount].converter = GET_ALIAS_NUM(converter);
804 converters[converterCount].totalAliasCount = 0;
805
806 return converterCount++;
807}
808
809/* resolve this alias based on the prioritization of the standard tags. */
810static void
811resolveAliasToConverter(uint16_t alias, uint16_t *tagNum, uint16_t *converterNum) {
812 uint16_t idx, idx2, idx3;
813
814 for (idx = UCNV_NUM_RESERVED_TAGS; idx < tagCount; idx++) {
815 for (idx2 = 0; idx2 < converterCount; idx2++) {
816 for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) {
817 uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3];
818 if (aliasNum == alias) {
819 *tagNum = idx;
820 *converterNum = idx2;
821 return;
822 }
823 }
824 }
825 }
826 /* Do the leftovers last, just in case */
827 /* There is no need to do the ALL tag */
828 idx = 0;
829 for (idx2 = 0; idx2 < converterCount; idx2++) {
830 for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) {
831 uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3];
832 if (aliasNum == alias) {
833 *tagNum = idx;
834 *converterNum = idx2;
835 return;
836 }
837 }
838 }
839 *tagNum = UINT16_MAX;
840 *converterNum = UINT16_MAX;
729e4ab9
A
841 fprintf(stderr, "%s: warning: alias %s not found\n",
842 path,
b75a7d8f
A
843 GET_ALIAS_STR(alias));
844 return;
845}
846
847/* The knownAliases should be sorted before calling this function */
848static uint32_t
849resolveAliases(uint16_t *uniqueAliasArr, uint16_t *uniqueAliasToConverterArr, uint16_t aliasOffset) {
850 uint32_t uniqueAliasIdx = 0;
851 uint32_t idx;
852 uint16_t currTagNum, oldTagNum;
853 uint16_t currConvNum, oldConvNum;
854 const char *lastName;
855
3d1f044b
A
856 if (knownAliasesCount != 0) {
857 resolveAliasToConverter(knownAliases[0], &oldTagNum, &currConvNum);
858 uniqueAliasToConverterArr[uniqueAliasIdx] = currConvNum;
859 oldConvNum = currConvNum;
860 uniqueAliasArr[uniqueAliasIdx] = knownAliases[0] + aliasOffset;
861 uniqueAliasIdx++;
862 lastName = GET_ALIAS_STR(knownAliases[0]);
863
864 for (idx = 1; idx < knownAliasesCount; idx++) {
865 resolveAliasToConverter(knownAliases[idx], &currTagNum, &currConvNum);
866 if (ucnv_compareNames(lastName, GET_ALIAS_STR(knownAliases[idx])) == 0) {
867 /* duplicate found */
868 if ((currTagNum < oldTagNum && currTagNum >= UCNV_NUM_RESERVED_TAGS)
869 || oldTagNum == 0) {
870 oldTagNum = currTagNum;
871 uniqueAliasToConverterArr[uniqueAliasIdx - 1] = currConvNum;
872 uniqueAliasArr[uniqueAliasIdx - 1] = knownAliases[idx] + aliasOffset;
873 if (verbose) {
874 printf("using %s instead of %s -> %s",
875 GET_ALIAS_STR(knownAliases[idx]),
876 lastName,
877 GET_ALIAS_STR(converters[currConvNum].converter));
878 if (oldConvNum != currConvNum) {
879 printf(" (alias conflict)");
880 }
881 puts("");
882 }
883 }
884 else {
885 /* else ignore it */
886 if (verbose) {
887 printf("folding %s into %s -> %s",
888 GET_ALIAS_STR(knownAliases[idx]),
889 lastName,
890 GET_ALIAS_STR(converters[oldConvNum].converter));
891 if (oldConvNum != currConvNum) {
892 printf(" (alias conflict)");
893 }
894 puts("");
895 }
896 }
897 if (oldConvNum != currConvNum) {
898 uniqueAliasToConverterArr[uniqueAliasIdx - 1] |= UCNV_AMBIGUOUS_ALIAS_MAP_BIT;
899 }
900 }
901 else {
902 uniqueAliasToConverterArr[uniqueAliasIdx] = currConvNum;
903 oldConvNum = currConvNum;
904 uniqueAliasArr[uniqueAliasIdx] = knownAliases[idx] + aliasOffset;
905 uniqueAliasIdx++;
906 lastName = GET_ALIAS_STR(knownAliases[idx]);
907 oldTagNum = currTagNum;
908 /*printf("%s -> %s\n", GET_ALIAS_STR(knownAliases[idx]), GET_ALIAS_STR(converters[currConvNum].converter));*/
909 }
910 if (uprv_strchr(GET_ALIAS_STR(converters[currConvNum].converter), UCNV_OPTION_SEP_CHAR) != NULL) {
911 uniqueAliasToConverterArr[uniqueAliasIdx-1] |= UCNV_CONTAINS_OPTION_BIT;
912 }
913 }
b75a7d8f
A
914 }
915 return uniqueAliasIdx;
916}
917
918static void
919createOneAliasList(uint16_t *aliasArrLists, uint32_t tag, uint32_t converter, uint16_t offset) {
920 uint32_t aliasNum;
921 AliasList *aliasList = &tags[tag].aliasList[converter];
922
923 if (aliasList->aliasCount == 0) {
924 aliasArrLists[tag*converterCount + converter] = 0;
925 }
926 else {
927 aliasLists[aliasListsSize++] = aliasList->aliasCount;
928
929 /* write into the array area a 1's based index. */
930 aliasArrLists[tag*converterCount + converter] = aliasListsSize;
931
932/* printf("tag %s converter %s\n",
933 GET_TAG_STR(tags[tag].tag),
934 GET_ALIAS_STR(converters[converter].converter));*/
935 for (aliasNum = 0; aliasNum < aliasList->aliasCount; aliasNum++) {
936 uint16_t value;
937/* printf(" %s\n",
938 GET_ALIAS_STR(aliasList->aliases[aliasNum]));*/
939 if (aliasList->aliases[aliasNum]) {
940 value = aliasList->aliases[aliasNum] + offset;
941 } else {
942 value = 0;
2ca993e8 943 if (tag != 0 && !quiet) { /* Only show the warning when it's not the leftover tag. */
729e4ab9
A
944 fprintf(stderr, "%s: warning: tag %s does not have a default alias for %s\n",
945 path,
b75a7d8f
A
946 GET_TAG_STR(tags[tag].tag),
947 GET_ALIAS_STR(converters[converter].converter));
948 }
949 }
950 aliasLists[aliasListsSize++] = value;
951 if (aliasListsSize >= MAX_LIST_SIZE) {
729e4ab9 952 fprintf(stderr, "%s: error: Too many alias lists\n", path);
b75a7d8f
A
953 exit(U_BUFFER_OVERFLOW_ERROR);
954 }
955
956 }
957 }
958}
959
73c04bcf
A
960static void
961createNormalizedAliasStrings(char *normalizedStrings, const char *origStringBlock, int32_t stringBlockLength) {
962 int32_t currStrLen;
963 uprv_memcpy(normalizedStrings, origStringBlock, stringBlockLength);
964 while ((currStrLen = (int32_t)uprv_strlen(origStringBlock)) < stringBlockLength) {
965 int32_t currStrSize = currStrLen + 1;
966 if (currStrLen > 0) {
967 int32_t normStrLen;
968 ucnv_io_stripForCompare(normalizedStrings, origStringBlock);
3d1f044b 969 normStrLen = (int32_t)uprv_strlen(normalizedStrings);
73c04bcf
A
970 if (normStrLen > 0) {
971 uprv_memset(normalizedStrings + normStrLen, 0, currStrSize - normStrLen);
972 }
973 }
974 stringBlockLength -= currStrSize;
975 normalizedStrings += currStrSize;
976 origStringBlock += currStrSize;
977 }
978}
979
b75a7d8f
A
980static void
981writeAliasTable(UNewDataMemory *out) {
982 uint32_t i, j;
983 uint32_t uniqueAliasesSize;
984 uint16_t aliasOffset = (uint16_t)(tagBlock.top/sizeof(uint16_t));
985 uint16_t *aliasArrLists = (uint16_t *)uprv_malloc(tagCount * converterCount * sizeof(uint16_t));
986 uint16_t *uniqueAliases = (uint16_t *)uprv_malloc(knownAliasesCount * sizeof(uint16_t));
987 uint16_t *uniqueAliasesToConverter = (uint16_t *)uprv_malloc(knownAliasesCount * sizeof(uint16_t));
988
989 qsort(knownAliases, knownAliasesCount, sizeof(knownAliases[0]), compareAliases);
990 uniqueAliasesSize = resolveAliases(uniqueAliases, uniqueAliasesToConverter, aliasOffset);
991
992 /* Array index starts at 1. aliasLists[0] is the size of the lists section. */
993 aliasListsSize = 0;
994
995 /* write the offsets of all the aliases lists in a 2D array, and create the lists. */
996 for (i = 0; i < tagCount; ++i) {
997 for (j = 0; j < converterCount; ++j) {
998 createOneAliasList(aliasArrLists, i, j, aliasOffset);
999 }
1000 }
1001
1002 /* Write the size of the TOC */
73c04bcf
A
1003 if (tableOptions.stringNormalizationType == UCNV_IO_UNNORMALIZED) {
1004 udata_write32(out, 8);
1005 }
1006 else {
1007 udata_write32(out, 9);
1008 }
b75a7d8f
A
1009
1010 /* Write the sizes of each section */
1011 /* All sizes are the number of uint16_t units, not bytes */
1012 udata_write32(out, converterCount);
1013 udata_write32(out, tagCount);
1014 udata_write32(out, uniqueAliasesSize); /* list of aliases */
1015 udata_write32(out, uniqueAliasesSize); /* The preresolved form of mapping an untagged the alias to a converter */
1016 udata_write32(out, tagCount * converterCount);
1017 udata_write32(out, aliasListsSize + 1);
73c04bcf 1018 udata_write32(out, sizeof(tableOptions) / sizeof(uint16_t));
b75a7d8f 1019 udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t));
73c04bcf
A
1020 if (tableOptions.stringNormalizationType != UCNV_IO_UNNORMALIZED) {
1021 udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t));
1022 }
b75a7d8f
A
1023
1024 /* write the table of converters */
1025 /* Think of this as the column headers */
1026 for(i=0; i<converterCount; ++i) {
1027 udata_write16(out, (uint16_t)(converters[i].converter + aliasOffset));
1028 }
1029
1030 /* write the table of tags */
1031 /* Think of this as the row headers */
1032 for(i=UCNV_NUM_RESERVED_TAGS; i<tagCount; ++i) {
1033 udata_write16(out, tags[i].tag);
1034 }
1035 /* The empty tag is considered the leftover list, and put that at the end of the priority list. */
1036 udata_write16(out, tags[EMPTY_TAG_NUM].tag);
1037 udata_write16(out, tags[ALL_TAG_NUM].tag);
1038
1039 /* Write the unique list of aliases */
1040 udata_writeBlock(out, uniqueAliases, uniqueAliasesSize * sizeof(uint16_t));
1041
1042 /* Write the unique list of aliases */
1043 udata_writeBlock(out, uniqueAliasesToConverter, uniqueAliasesSize * sizeof(uint16_t));
1044
1045 /* Write the array to the lists */
1046 udata_writeBlock(out, (const void *)(aliasArrLists + (2*converterCount)), (((tagCount - 2) * converterCount) * sizeof(uint16_t)));
1047 /* Now write the leftover part of the array for the EMPTY and ALL lists */
1048 udata_writeBlock(out, (const void *)aliasArrLists, (2 * converterCount * sizeof(uint16_t)));
1049
1050 /* Offset the next array to make the index start at 1. */
1051 udata_write16(out, 0xDEAD);
1052
1053 /* Write the lists */
1054 udata_writeBlock(out, (const void *)aliasLists, aliasListsSize * sizeof(uint16_t));
1055
73c04bcf
A
1056 /* Write any options for the alias table. */
1057 udata_writeBlock(out, (const void *)&tableOptions, sizeof(tableOptions));
1058
b75a7d8f
A
1059 /* write the tags strings */
1060 udata_writeString(out, tagBlock.store, tagBlock.top);
1061
1062 /* write the aliases strings */
1063 udata_writeString(out, stringBlock.store, stringBlock.top);
1064
73c04bcf
A
1065 /* write the normalized aliases strings */
1066 if (tableOptions.stringNormalizationType != UCNV_IO_UNNORMALIZED) {
1067 char *normalizedStrings = (char *)uprv_malloc(tagBlock.top + stringBlock.top);
1068 createNormalizedAliasStrings(normalizedStrings, tagBlock.store, tagBlock.top);
1069 createNormalizedAliasStrings(normalizedStrings + tagBlock.top, stringBlock.store, stringBlock.top);
1070
1071 /* Write out the complete normalized array. */
1072 udata_writeString(out, normalizedStrings, tagBlock.top + stringBlock.top);
1073 uprv_free(normalizedStrings);
1074 }
1075
51004dcb 1076 uprv_free(uniqueAliasesToConverter);
b75a7d8f 1077 uprv_free(uniqueAliases);
51004dcb 1078 uprv_free(aliasArrLists);
b75a7d8f
A
1079}
1080
1081static char *
374ca955
A
1082allocString(StringBlock *block, const char *s, int32_t length) {
1083 uint32_t top;
b75a7d8f
A
1084 char *p;
1085
374ca955
A
1086 if(length<0) {
1087 length=(int32_t)uprv_strlen(s);
1088 }
1089
1090 /*
1091 * add 1 for the terminating NUL
1092 * and round up (+1 &~1)
1093 * to keep the addresses on a 16-bit boundary
1094 */
1095 top=block->top + (uint32_t)((length + 1 + 1) & ~1);
1096
b75a7d8f 1097 if(top >= block->max) {
729e4ab9 1098 fprintf(stderr, "%s:%d: error: out of memory\n", path, lineNum);
b75a7d8f
A
1099 exit(U_MEMORY_ALLOCATION_ERROR);
1100 }
374ca955
A
1101
1102 /* get the pointer and copy the string */
b75a7d8f 1103 p = block->store + block->top;
374ca955
A
1104 uprv_memcpy(p, s, length);
1105 p[length] = 0; /* NUL-terminate it */
1106 if((length & 1) == 0) {
1107 p[length + 1] = 0; /* set the padding byte */
1108 }
1109
1110 /* check for invariant characters now that we have a NUL-terminated string for easy output */
1111 if(!uprv_isInvariantString(p, length)) {
729e4ab9 1112 fprintf(stderr, "%s:%d: error: the name %s contains not just invariant characters\n", path, lineNum, p);
374ca955
A
1113 exit(U_INVALID_TABLE_FORMAT);
1114 }
1115
b75a7d8f
A
1116 block->top = top;
1117 return p;
1118}
1119
1120static int
1121compareAliases(const void *alias1, const void *alias2) {
1122 /* Names like IBM850 and ibm-850 need to be sorted together */
1123 int result = ucnv_compareNames(GET_ALIAS_STR(*(uint16_t*)alias1), GET_ALIAS_STR(*(uint16_t*)alias2));
1124 if (!result) {
1125 /* Sort the shortest first */
374ca955 1126 return (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias1)) - (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias2));
b75a7d8f
A
1127 }
1128 return result;
1129}
1130
1131/*
1132 * Hey, Emacs, please set the following:
1133 *
1134 * Local Variables:
1135 * indent-tabs-mode: nil
1136 * End:
1137 *
1138 */
1139