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