]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
73c04bcf | 4 | * Copyright (C) 2000-2006, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ******************************************************************************* | |
8 | * file name: genuca.cpp | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created at the end of XX century | |
14 | * created by: Vladimir Weinstein | |
15 | * | |
16 | * This program reads the Franctional UCA table and generates | |
17 | * internal format for UCA table as well as inverse UCA table. | |
18 | * It then writes binary files containing the data: ucadata.dat | |
19 | * & invuca.dat | |
20 | * Change history: | |
21 | * 02/23/2001 grhoten Made it into a tool | |
22 | * 02/23/2001 weiv Moved element & table handling code to i18n | |
23 | * 05/09/2001 weiv Case bits are now in the CEs, not in front | |
24 | */ | |
25 | ||
b75a7d8f | 26 | #include "unicode/utypes.h" |
374ca955 | 27 | #include "unicode/putil.h" |
b75a7d8f | 28 | #include "unicode/udata.h" |
374ca955 | 29 | #include "unicode/uclean.h" |
b75a7d8f A |
30 | #include "ucol_imp.h" |
31 | #include "genuca.h" | |
32 | #include "uoptions.h" | |
33 | #include "toolutil.h" | |
34 | #include "unewdata.h" | |
35 | #include "cstring.h" | |
36 | #include "cmemory.h" | |
37 | ||
374ca955 A |
38 | #include <stdio.h> |
39 | ||
b75a7d8f A |
40 | /* |
41 | * Global - verbosity | |
42 | */ | |
43 | UBool VERBOSE = FALSE; | |
44 | ||
45 | static UVersionInfo UCAVersion; | |
46 | ||
47 | #if UCONFIG_NO_COLLATION | |
48 | ||
49 | /* dummy UDataInfo cf. udata.h */ | |
50 | static UDataInfo dummyDataInfo = { | |
51 | sizeof(UDataInfo), | |
52 | 0, | |
53 | ||
54 | U_IS_BIG_ENDIAN, | |
55 | U_CHARSET_FAMILY, | |
56 | U_SIZEOF_UCHAR, | |
57 | 0, | |
58 | ||
59 | { 0, 0, 0, 0 }, /* dummy dataFormat */ | |
60 | { 0, 0, 0, 0 }, /* dummy formatVersion */ | |
61 | { 0, 0, 0, 0 } /* dummy dataVersion */ | |
62 | }; | |
63 | ||
64 | #else | |
65 | ||
374ca955 A |
66 | static const UDataInfo ucaDataInfo={ |
67 | sizeof(UDataInfo), | |
68 | 0, | |
69 | ||
70 | U_IS_BIG_ENDIAN, | |
71 | U_CHARSET_FAMILY, | |
72 | sizeof(UChar), | |
73 | 0, | |
74 | ||
75 | {UCA_DATA_FORMAT_0, UCA_DATA_FORMAT_1, UCA_DATA_FORMAT_2, UCA_DATA_FORMAT_3}, /* dataFormat="UCol" */ | |
76 | /* 03/26/2002 bumped up version since format has changed */ | |
77 | /* 09/16/2002 bumped up version since we went from UColAttributeValue */ | |
78 | /* to int32_t in UColOptionSet */ | |
79 | /* 05/13/2003 This one also updated since we added UCA and UCD versions */ | |
80 | /* to header */ | |
81 | /* 09/11/2003 Adding information required by data swapper */ | |
82 | {UCA_FORMAT_VERSION_0, UCA_FORMAT_VERSION_1, UCA_FORMAT_VERSION_2, UCA_FORMAT_VERSION_3}, /* formatVersion */ | |
83 | {0, 0, 0, 0} /* dataVersion = Unicode Version*/ | |
84 | }; | |
85 | ||
86 | static const UDataInfo invUcaDataInfo={ | |
87 | sizeof(UDataInfo), | |
88 | 0, | |
89 | ||
90 | U_IS_BIG_ENDIAN, | |
91 | U_CHARSET_FAMILY, | |
92 | sizeof(UChar), | |
93 | 0, | |
94 | ||
95 | {INVUCA_DATA_FORMAT_0, INVUCA_DATA_FORMAT_1, INVUCA_DATA_FORMAT_2, INVUCA_DATA_FORMAT_3}, /* dataFormat="InvC" */ | |
96 | /* 03/26/2002 bumped up version since format has changed */ | |
97 | /* 04/29/2003 2.1 format - we have added UCA version to header */ | |
98 | {INVUCA_FORMAT_VERSION_0, INVUCA_FORMAT_VERSION_1, INVUCA_FORMAT_VERSION_2, INVUCA_FORMAT_VERSION_3}, /* formatVersion */ | |
99 | {0, 0, 0, 0} /* dataVersion = Unicode Version*/ | |
100 | }; | |
101 | ||
b75a7d8f A |
102 | UCAElements le; |
103 | ||
104 | int32_t readElement(char **from, char *to, char separator, UErrorCode *status) { | |
105 | if(U_FAILURE(*status)) { | |
106 | return 0; | |
107 | } | |
108 | char buffer[1024]; | |
109 | int32_t i = 0; | |
110 | while(**from != separator) { | |
111 | if(**from != ' ') { | |
112 | *(buffer+i++) = **from; | |
113 | } | |
114 | (*from)++; | |
115 | } | |
116 | (*from)++; | |
117 | *(buffer + i) = 0; | |
118 | //*to = (char *)malloc(strlen(buffer)+1); | |
119 | strcpy(to, buffer); | |
120 | return i/2; | |
121 | } | |
122 | ||
123 | ||
124 | uint32_t getSingleCEValue(char *primary, char *secondary, char *tertiary, UErrorCode *status) { | |
125 | if(U_FAILURE(*status)) { | |
126 | return 0; | |
127 | } | |
128 | uint32_t value = 0; | |
129 | char primsave = '\0'; | |
130 | char secsave = '\0'; | |
131 | char tersave = '\0'; | |
132 | char *primend = primary+4; | |
133 | if(strlen(primary) > 4) { | |
134 | primsave = *primend; | |
135 | *primend = '\0'; | |
136 | } | |
137 | char *secend = secondary+2; | |
138 | if(strlen(secondary) > 2) { | |
139 | secsave = *secend; | |
140 | *secend = '\0'; | |
141 | } | |
142 | char *terend = tertiary+2; | |
143 | if(strlen(tertiary) > 2) { | |
144 | tersave = *terend; | |
145 | *terend = '\0'; | |
146 | } | |
147 | uint32_t primvalue = (uint32_t)((*primary!='\0')?strtoul(primary, &primend, 16):0); | |
148 | uint32_t secvalue = (uint32_t)((*secondary!='\0')?strtoul(secondary, &secend, 16):0); | |
149 | uint32_t tervalue = (uint32_t)((*tertiary!='\0')?strtoul(tertiary, &terend, 16):0); | |
150 | if(primvalue <= 0xFF) { | |
151 | primvalue <<= 8; | |
152 | } | |
153 | ||
154 | value = ((primvalue<<UCOL_PRIMARYORDERSHIFT)&UCOL_PRIMARYORDERMASK)| | |
155 | ((secvalue<<UCOL_SECONDARYORDERSHIFT)&UCOL_SECONDARYORDERMASK)| | |
156 | (tervalue&UCOL_TERTIARYORDERMASK); | |
157 | ||
158 | if(primsave!='\0') { | |
159 | *primend = primsave; | |
160 | } | |
161 | if(secsave!='\0') { | |
162 | *secend = secsave; | |
163 | } | |
164 | if(tersave!='\0') { | |
165 | *terend = tersave; | |
166 | } | |
167 | return value; | |
168 | } | |
169 | ||
170 | static uint32_t inverseTable[0xFFFF][3]; | |
171 | static uint32_t inversePos = 0; | |
172 | static UChar stringContinue[0xFFFF]; | |
173 | static uint32_t sContPos = 0; | |
174 | ||
175 | static void addNewInverse(UCAElements *element, UErrorCode *status) { | |
176 | if(U_FAILURE(*status)) { | |
177 | return; | |
178 | } | |
179 | if(VERBOSE && isContinuation(element->CEs[1])) { | |
180 | //fprintf(stdout, "+"); | |
181 | } | |
182 | inversePos++; | |
183 | inverseTable[inversePos][0] = element->CEs[0]; | |
184 | if(element->noOfCEs > 1 && isContinuation(element->CEs[1])) { | |
185 | inverseTable[inversePos][1] = element->CEs[1]; | |
186 | } else { | |
187 | inverseTable[inversePos][1] = 0; | |
188 | } | |
189 | if(element->cSize < 2) { | |
190 | inverseTable[inversePos][2] = element->cPoints[0]; | |
191 | } else { /* add a new store of cruft */ | |
192 | inverseTable[inversePos][2] = ((element->cSize+1) << UCOL_INV_SHIFTVALUE) | sContPos; | |
193 | memcpy(stringContinue+sContPos, element->cPoints, element->cSize*sizeof(UChar)); | |
194 | sContPos += element->cSize+1; | |
195 | } | |
196 | } | |
197 | ||
198 | static void insertInverse(UCAElements *element, uint32_t position, UErrorCode *status) { | |
199 | if(U_FAILURE(*status)) { | |
200 | return; | |
201 | } | |
202 | ||
203 | if(VERBOSE && isContinuation(element->CEs[1])) { | |
204 | //fprintf(stdout, "+"); | |
205 | } | |
206 | if(position <= inversePos) { | |
207 | /*move stuff around */ | |
208 | uint32_t amountToMove = (inversePos - position+1)*sizeof(inverseTable[0]); | |
209 | uprv_memmove(inverseTable[position+1], inverseTable[position], amountToMove); | |
210 | } | |
211 | inverseTable[position][0] = element->CEs[0]; | |
212 | if(element->noOfCEs > 1 && isContinuation(element->CEs[1])) { | |
213 | inverseTable[position][1] = element->CEs[1]; | |
214 | } else { | |
215 | inverseTable[position][1] = 0; | |
216 | } | |
217 | if(element->cSize < 2) { | |
218 | inverseTable[position][2] = element->cPoints[0]; | |
219 | } else { /* add a new store of cruft */ | |
220 | inverseTable[position][2] = ((element->cSize+1) << UCOL_INV_SHIFTVALUE) | sContPos; | |
221 | memcpy(stringContinue+sContPos, element->cPoints, element->cSize*sizeof(UChar)); | |
222 | sContPos += element->cSize+1; | |
223 | } | |
224 | inversePos++; | |
225 | } | |
226 | ||
227 | static void addToExistingInverse(UCAElements *element, uint32_t position, UErrorCode *status) { | |
228 | ||
229 | if(U_FAILURE(*status)) { | |
230 | return; | |
231 | } | |
232 | ||
233 | if((inverseTable[position][2] & UCOL_INV_SIZEMASK) == 0) { /* single element, have to make new extension place and put both guys there */ | |
234 | stringContinue[sContPos] = (UChar)inverseTable[position][2]; | |
235 | inverseTable[position][2] = ((element->cSize+3) << UCOL_INV_SHIFTVALUE) | sContPos; | |
236 | sContPos++; | |
237 | stringContinue[sContPos++] = 0xFFFF; | |
238 | memcpy(stringContinue+sContPos, element->cPoints, element->cSize*sizeof(UChar)); | |
239 | sContPos += element->cSize; | |
240 | stringContinue[sContPos++] = 0xFFFE; | |
241 | } else { /* adding to the already existing continuing table */ | |
242 | uint32_t contIndex = inverseTable[position][2] & UCOL_INV_OFFSETMASK; | |
243 | uint32_t contSize = (inverseTable[position][2] & UCOL_INV_SIZEMASK) >> UCOL_INV_SHIFTVALUE; | |
244 | ||
245 | if(contIndex+contSize < sContPos) { | |
246 | /*fprintf(stderr, ".", sContPos, contIndex+contSize);*/ | |
247 | memcpy(stringContinue+contIndex+contSize+element->cSize+1, stringContinue+contIndex+contSize, (element->cSize+1)*sizeof(UChar)); | |
248 | } | |
249 | ||
250 | stringContinue[contIndex+contSize-1] = 0xFFFF; | |
251 | memcpy(stringContinue+contIndex+contSize, element->cPoints, element->cSize*sizeof(UChar)); | |
252 | sContPos += element->cSize+1; | |
253 | stringContinue[contIndex+contSize+element->cSize] = 0xFFFE; | |
254 | ||
255 | inverseTable[position][2] = ((contSize+element->cSize+1) << UCOL_INV_SHIFTVALUE) | contIndex; | |
256 | } | |
257 | } | |
258 | ||
374ca955 A |
259 | /* |
260 | * Takes two CEs (lead and continuation) and | |
261 | * compares them as CEs should be compared: | |
262 | * primary vs. primary, secondary vs. secondary | |
263 | * tertiary vs. tertiary | |
264 | */ | |
265 | static int32_t compareCEs(uint32_t *source, uint32_t *target) { | |
266 | uint32_t s1 = source[0], s2, t1 = target[0], t2; | |
267 | if(isContinuation(source[1])) { | |
268 | s2 = source[1]; | |
269 | } else { | |
270 | s2 = 0; | |
271 | } | |
272 | if(isContinuation(target[1])) { | |
273 | t2 = target[1]; | |
274 | } else { | |
275 | t2 = 0; | |
276 | } | |
277 | ||
278 | uint32_t s = 0, t = 0; | |
279 | if(s1 == t1 && s2 == t2) { | |
280 | return 0; | |
281 | } | |
282 | s = (s1 & 0xFFFF0000)|((s2 & 0xFFFF0000)>>16); | |
283 | t = (t1 & 0xFFFF0000)|((t2 & 0xFFFF0000)>>16); | |
284 | if(s < t) { | |
285 | return -1; | |
286 | } else if(s > t) { | |
287 | return 1; | |
288 | } else { | |
289 | s = (s1 & 0x0000FF00) | (s2 & 0x0000FF00)>>8; | |
290 | t = (t1 & 0x0000FF00) | (t2 & 0x0000FF00)>>8; | |
291 | if(s < t) { | |
292 | return -1; | |
293 | } else if(s > t) { | |
294 | return 1; | |
295 | } else { | |
296 | s = (s1 & 0x000000FF)<<8 | (s2 & 0x000000FF); | |
297 | t = (t1 & 0x000000FF)<<8 | (t2 & 0x000000FF); | |
298 | if(s < t) { | |
299 | return -1; | |
300 | } else { | |
301 | return 1; | |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
b75a7d8f | 307 | static uint32_t addToInverse(UCAElements *element, UErrorCode *status) { |
b75a7d8f A |
308 | uint32_t position = inversePos; |
309 | uint32_t saveElement = element->CEs[0]; | |
374ca955 | 310 | int32_t compResult = 0; |
b75a7d8f A |
311 | element->CEs[0] &= 0xFFFFFF3F; |
312 | if(element->noOfCEs == 1) { | |
313 | element->CEs[1] = 0; | |
314 | } | |
315 | if(inversePos == 0) { | |
316 | inverseTable[0][0] = inverseTable[0][1] = inverseTable[0][2] = 0; | |
317 | addNewInverse(element, status); | |
374ca955 A |
318 | } else if(compareCEs(inverseTable[inversePos], element->CEs) > 0) { |
319 | while((compResult = compareCEs(inverseTable[--position], element->CEs)) > 0); | |
320 | if(VERBOSE) { fprintf(stdout, "p:%u ", (int)position); } | |
321 | if(compResult == 0) { | |
322 | addToExistingInverse(element, position, status); | |
323 | } else { | |
b75a7d8f A |
324 | insertInverse(element, position+1, status); |
325 | } | |
374ca955 A |
326 | } else if(compareCEs(inverseTable[inversePos], element->CEs) == 0) { |
327 | addToExistingInverse(element, inversePos, status); | |
328 | } else { | |
b75a7d8f A |
329 | addNewInverse(element, status); |
330 | } | |
331 | element->CEs[0] = saveElement; | |
332 | if(VERBOSE) { fprintf(stdout, "+"); } | |
333 | return inversePos; | |
334 | } | |
335 | ||
336 | static InverseUCATableHeader *assembleInverseTable(UErrorCode *status) | |
337 | { | |
338 | InverseUCATableHeader *result = NULL; | |
339 | uint32_t headerByteSize = paddedsize(sizeof(InverseUCATableHeader)); | |
340 | uint32_t inverseTableByteSize = (inversePos+2)*sizeof(uint32_t)*3; | |
341 | uint32_t contsByteSize = sContPos * sizeof(UChar); | |
342 | uint32_t i = 0; | |
343 | ||
344 | result = (InverseUCATableHeader *)uprv_malloc(headerByteSize + inverseTableByteSize + contsByteSize); | |
374ca955 | 345 | uprv_memset(result, 0, headerByteSize + inverseTableByteSize + contsByteSize); |
b75a7d8f A |
346 | if(result != NULL) { |
347 | result->byteSize = headerByteSize + inverseTableByteSize + contsByteSize; | |
348 | ||
349 | inversePos++; | |
350 | inverseTable[inversePos][0] = 0xFFFFFFFF; | |
351 | inverseTable[inversePos][1] = 0xFFFFFFFF; | |
352 | inverseTable[inversePos][2] = 0x0000FFFF; | |
353 | inversePos++; | |
354 | ||
355 | for(i = 2; i<inversePos; i++) { | |
374ca955 A |
356 | if(compareCEs(inverseTable[i-1], inverseTable[i]) > 0) { |
357 | fprintf(stderr, "Error at %i: %08X & %08X\n", (int)i, (int)inverseTable[i-1][0], (int)inverseTable[i][0]); | |
b75a7d8f | 358 | } else if(inverseTable[i-1][0] == inverseTable[i][0] && !(inverseTable[i-1][1] < inverseTable[i][1])) { |
374ca955 | 359 | fprintf(stderr, "Continuation error at %i: %08X %08X & %08X %08X\n", (int)i, (int)inverseTable[i-1][0], (int)inverseTable[i-1][1], (int)inverseTable[i][0], (int)inverseTable[i][1]); |
b75a7d8f A |
360 | } |
361 | } | |
362 | ||
363 | result->tableSize = inversePos; | |
364 | result->contsSize = sContPos; | |
365 | ||
366 | result->table = headerByteSize; | |
367 | result->conts = headerByteSize + inverseTableByteSize; | |
368 | ||
369 | memcpy((uint8_t *)result + result->table, inverseTable, inverseTableByteSize); | |
370 | memcpy((uint8_t *)result + result->conts, stringContinue, contsByteSize); | |
371 | ||
372 | } else { | |
373 | *status = U_MEMORY_ALLOCATION_ERROR; | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | return result; | |
378 | } | |
379 | ||
380 | ||
381 | static void writeOutInverseData(InverseUCATableHeader *data, | |
382 | const char *outputDir, | |
383 | const char *copyright, | |
384 | UErrorCode *status) | |
385 | { | |
386 | UNewDataMemory *pData; | |
387 | ||
388 | long dataLength; | |
389 | ||
390 | UDataInfo invUcaInfo; | |
391 | uprv_memcpy(&invUcaInfo, &invUcaDataInfo, sizeof(UDataInfo)); | |
392 | u_getUnicodeVersion(invUcaInfo.dataVersion); | |
393 | ||
374ca955 | 394 | pData=udata_create(outputDir, INVC_DATA_TYPE, INVC_DATA_NAME, &invUcaInfo, |
b75a7d8f A |
395 | copyright, status); |
396 | ||
397 | if(U_FAILURE(*status)) { | |
374ca955 | 398 | fprintf(stderr, "Error: unable to create %s"INVC_DATA_NAME", error %s\n", outputDir, u_errorName(*status)); |
b75a7d8f A |
399 | return; |
400 | } | |
401 | ||
402 | /* write the data to the file */ | |
403 | if (VERBOSE) { | |
404 | fprintf(stdout, "Writing out inverse UCA table: %s%c%s.%s\n", outputDir, U_FILE_SEP_CHAR, | |
374ca955 | 405 | INVC_DATA_NAME, |
b75a7d8f A |
406 | INVC_DATA_TYPE); |
407 | } | |
408 | udata_writeBlock(pData, data, data->byteSize); | |
409 | ||
410 | /* finish up */ | |
411 | dataLength=udata_finish(pData, status); | |
412 | if(U_FAILURE(*status)) { | |
413 | fprintf(stderr, "Error: error %d writing the output file\n", *status); | |
414 | return; | |
415 | } | |
416 | } | |
417 | ||
418 | ||
419 | ||
420 | static int32_t hex2num(char hex) { | |
421 | if(hex>='0' && hex <='9') { | |
422 | return hex-'0'; | |
423 | } else if(hex>='a' && hex<='f') { | |
424 | return hex-'a'+10; | |
425 | } else if(hex>='A' && hex<='F') { | |
426 | return hex-'A'+10; | |
427 | } else { | |
428 | return 0; | |
429 | } | |
430 | } | |
431 | ||
432 | UCAElements *readAnElement(FILE *data, tempUCATable *t, UCAConstants *consts, UErrorCode *status) { | |
433 | char buffer[2048], primary[100], secondary[100], tertiary[100]; | |
434 | UBool detectedContraction; | |
435 | int32_t i = 0; | |
436 | unsigned int theValue; | |
437 | char *pointer = NULL; | |
438 | char *commentStart = NULL; | |
439 | char *startCodePoint = NULL; | |
440 | char *endCodePoint = NULL; | |
441 | char *spacePointer = NULL; | |
442 | char *result = fgets(buffer, 2048, data); | |
374ca955 | 443 | int32_t buflen = (int32_t)uprv_strlen(buffer); |
b75a7d8f A |
444 | if(U_FAILURE(*status)) { |
445 | return 0; | |
446 | } | |
447 | *primary = *secondary = *tertiary = '\0'; | |
448 | if(result == NULL) { | |
449 | if(feof(data)) { | |
450 | return NULL; | |
451 | } else { | |
452 | fprintf(stderr, "empty line but no EOF!\n"); | |
453 | *status = U_INVALID_FORMAT_ERROR; | |
454 | return NULL; | |
455 | } | |
456 | } | |
457 | while(buflen>0 && (buffer[buflen-1] == '\r' || buffer[buflen-1] == '\n')) { | |
458 | buffer[--buflen] = 0; | |
459 | } | |
460 | ||
461 | if(buffer[0] == 0 || buffer[0] == '#') { | |
462 | return NULL; // just a comment, skip whole line | |
463 | } | |
464 | ||
465 | UCAElements *element = ≤ //(UCAElements *)malloc(sizeof(UCAElements)); | |
466 | ||
467 | enum ActionType { | |
468 | READCE, | |
469 | READHEX, | |
470 | READUCAVERSION | |
471 | }; | |
472 | ||
473 | // Directives. | |
474 | if(buffer[0] == '[') { | |
475 | uint32_t cnt = 0; | |
476 | struct { | |
477 | char name[256]; | |
478 | uint32_t *what; | |
479 | ActionType what_to_do; | |
480 | } vt[] = { {"[first tertiary ignorable", consts->UCA_FIRST_TERTIARY_IGNORABLE, READCE}, | |
481 | {"[last tertiary ignorable", consts->UCA_LAST_TERTIARY_IGNORABLE, READCE}, | |
482 | {"[first secondary ignorable", consts->UCA_FIRST_SECONDARY_IGNORABLE, READCE}, | |
483 | {"[last secondary ignorable", consts->UCA_LAST_SECONDARY_IGNORABLE, READCE}, | |
484 | {"[first primary ignorable", consts->UCA_FIRST_PRIMARY_IGNORABLE, READCE}, | |
485 | {"[last primary ignorable", consts->UCA_LAST_PRIMARY_IGNORABLE, READCE}, | |
486 | {"[first variable", consts->UCA_FIRST_VARIABLE, READCE}, | |
487 | {"[last variable", consts->UCA_LAST_VARIABLE, READCE}, | |
488 | {"[first regular", consts->UCA_FIRST_NON_VARIABLE, READCE}, | |
489 | {"[last regular", consts->UCA_LAST_NON_VARIABLE, READCE}, | |
490 | {"[first implicit", consts->UCA_FIRST_IMPLICIT, READCE}, | |
491 | {"[last implicit", consts->UCA_LAST_IMPLICIT, READCE}, | |
492 | {"[first trailing", consts->UCA_FIRST_TRAILING, READCE}, | |
493 | {"[last trailing", consts->UCA_LAST_TRAILING, READCE}, | |
494 | ||
495 | {"[fixed top", &consts->UCA_PRIMARY_TOP_MIN, READHEX}, | |
496 | {"[fixed first implicit byte", &consts->UCA_PRIMARY_IMPLICIT_MIN, READHEX}, | |
497 | {"[fixed last implicit byte", &consts->UCA_PRIMARY_IMPLICIT_MAX, READHEX}, | |
498 | {"[fixed first trail byte", &consts->UCA_PRIMARY_TRAILING_MIN, READHEX}, | |
499 | {"[fixed last trail byte", &consts->UCA_PRIMARY_TRAILING_MAX, READHEX}, | |
500 | {"[fixed first special byte", &consts->UCA_PRIMARY_SPECIAL_MIN, READHEX}, | |
501 | {"[fixed last special byte", &consts->UCA_PRIMARY_SPECIAL_MAX, READHEX}, | |
502 | {"[variable top = ", &t->options->variableTopValue, READHEX}, | |
503 | {"[UCA version = ", NULL, READUCAVERSION} | |
504 | }; | |
505 | for (cnt = 0; cnt<sizeof(vt)/sizeof(vt[0]); cnt++) { | |
506 | uint32_t vtLen = (uint32_t)uprv_strlen(vt[cnt].name); | |
507 | if(uprv_strncmp(buffer, vt[cnt].name, vtLen) == 0) { | |
508 | element->variableTop = TRUE; | |
509 | if(vt[cnt].what_to_do == READHEX) { | |
510 | if(sscanf(buffer+vtLen, "%4x", &theValue) != 1) /* read first code point */ | |
511 | { | |
512 | fprintf(stderr, " scanf(hex) failed on !\n "); | |
513 | } | |
514 | *(vt[cnt].what) = (UChar)theValue; | |
515 | //if(cnt == 1) { // first implicit | |
516 | // we need to set the value for top next | |
517 | //uint32_t nextTop = ucol_prv_calculateImplicitPrimary(0x4E00); // CJK base | |
518 | //consts->UCA_NEXT_TOP_VALUE = theValue<<24 | 0x030303; | |
519 | //} | |
520 | } else if (vt[cnt].what_to_do == READCE) { /* vt[cnt].what_to_do == READCE */ | |
521 | pointer = strchr(buffer+vtLen, '['); | |
522 | if(pointer) { | |
523 | pointer++; | |
524 | element->sizePrim[0]=readElement(&pointer, primary, ',', status); | |
525 | element->sizeSec[0]=readElement(&pointer, secondary, ',', status); | |
526 | element->sizeTer[0]=readElement(&pointer, tertiary, ']', status); | |
527 | ||
528 | vt[cnt].what[0] = getSingleCEValue(primary, secondary, tertiary, status); | |
529 | if(element->sizePrim[0] > 2 || element->sizeSec[0] > 1 || element->sizeTer[0] > 1) { | |
530 | uint32_t CEi = 1; | |
531 | uint32_t value = UCOL_CONTINUATION_MARKER; /* Continuation marker */ | |
532 | if(2*CEi<element->sizePrim[i]) { | |
533 | value |= ((hex2num(*(primary+4*CEi))&0xF)<<28); | |
534 | value |= ((hex2num(*(primary+4*CEi+1))&0xF)<<24); | |
535 | } | |
536 | ||
537 | if(2*CEi+1<element->sizePrim[i]) { | |
538 | value |= ((hex2num(*(primary+4*CEi+2))&0xF)<<20); | |
539 | value |= ((hex2num(*(primary+4*CEi+3))&0xF)<<16); | |
540 | } | |
541 | ||
542 | if(CEi<element->sizeSec[i]) { | |
543 | value |= ((hex2num(*(secondary+2*CEi))&0xF)<<12); | |
544 | value |= ((hex2num(*(secondary+2*CEi+1))&0xF)<<8); | |
545 | } | |
546 | ||
547 | if(CEi<element->sizeTer[i]) { | |
548 | value |= ((hex2num(*(tertiary+2*CEi))&0x3)<<4); | |
549 | value |= (hex2num(*(tertiary+2*CEi+1))&0xF); | |
550 | } | |
551 | ||
552 | CEi++; | |
553 | ||
554 | vt[cnt].what[1] = value; | |
555 | //element->CEs[CEindex++] = value; | |
556 | } else { | |
557 | vt[cnt].what[1] = 0; | |
558 | } | |
559 | } else { | |
560 | fprintf(stderr, "Failed to read a CE from line %s\n", buffer); | |
561 | } | |
562 | } else { //vt[cnt].what_to_do == READUCAVERSION | |
563 | u_versionFromString(UCAVersion, buffer+vtLen); | |
564 | if(VERBOSE) { | |
565 | fprintf(stdout, "UCA version [%hu.%hu.%hu.%hu]\n", UCAVersion[0], UCAVersion[1], UCAVersion[2], UCAVersion[3]); | |
566 | } | |
567 | } | |
568 | //element->cPoints[0] = (UChar)theValue; | |
569 | //return element; | |
570 | return NULL; | |
571 | } | |
572 | } | |
573 | fprintf(stderr, "Warning: unrecognized option: %s\n", buffer); | |
574 | //*status = U_INVALID_FORMAT_ERROR; | |
575 | return NULL; | |
576 | } | |
577 | element->variableTop = FALSE; | |
578 | ||
579 | startCodePoint = buffer; | |
580 | endCodePoint = strchr(startCodePoint, ';'); | |
581 | ||
582 | if(endCodePoint == 0) { | |
583 | fprintf(stderr, "error - line with no code point!\n"); | |
584 | *status = U_INVALID_FORMAT_ERROR; /* No code point - could be an error, but probably only an empty line */ | |
585 | return NULL; | |
586 | } else { | |
587 | *(endCodePoint) = 0; | |
588 | } | |
589 | ||
590 | if(element != NULL) { | |
591 | memset(element, 0, sizeof(*element)); | |
592 | } else { | |
593 | *status = U_MEMORY_ALLOCATION_ERROR; | |
594 | return NULL; | |
595 | } | |
596 | ||
597 | element->cPoints = element->uchars; | |
598 | ||
599 | spacePointer = strchr(buffer, ' '); | |
600 | if(sscanf(buffer, "%4x", &theValue) != 1) /* read first code point */ | |
601 | { | |
602 | fprintf(stderr, " scanf(hex) failed!\n "); | |
603 | } | |
604 | element->cPoints[0] = (UChar)theValue; | |
605 | ||
606 | if(spacePointer == 0) { | |
607 | detectedContraction = FALSE; | |
608 | element->cSize = 1; | |
609 | } else { | |
610 | i = 1; | |
611 | detectedContraction = TRUE; | |
612 | while(spacePointer != NULL) { | |
613 | sscanf(spacePointer+1, "%4x", &theValue); | |
614 | element->cPoints[i++] = (UChar)theValue; | |
615 | spacePointer = strchr(spacePointer+1, ' '); | |
616 | } | |
617 | ||
618 | element->cSize = i; | |
619 | ||
620 | //fprintf(stderr, "Number of codepoints in contraction: %i\n", i); | |
621 | } | |
622 | ||
623 | startCodePoint = endCodePoint+1; | |
624 | ||
625 | commentStart = strchr(startCodePoint, '#'); | |
626 | if(commentStart == NULL) { | |
627 | commentStart = strlen(startCodePoint) + startCodePoint; | |
628 | } | |
629 | ||
630 | i = 0; | |
631 | uint32_t CEindex = 0; | |
632 | element->noOfCEs = 0; | |
633 | for(;;) { | |
634 | endCodePoint = strchr(startCodePoint, ']'); | |
635 | if(endCodePoint == NULL || endCodePoint >= commentStart) { | |
636 | break; | |
637 | } | |
638 | pointer = strchr(startCodePoint, '['); | |
639 | pointer++; | |
640 | ||
641 | element->sizePrim[i]=readElement(&pointer, primary, ',', status); | |
642 | element->sizeSec[i]=readElement(&pointer, secondary, ',', status); | |
643 | element->sizeTer[i]=readElement(&pointer, tertiary, ']', status); | |
644 | ||
645 | ||
646 | /* I want to get the CEs entered right here, including continuation */ | |
647 | element->CEs[CEindex++] = getSingleCEValue(primary, secondary, tertiary, status); | |
648 | ||
649 | uint32_t CEi = 1; | |
650 | while(2*CEi<element->sizePrim[i] || CEi<element->sizeSec[i] || CEi<element->sizeTer[i]) { | |
651 | uint32_t value = UCOL_CONTINUATION_MARKER; /* Continuation marker */ | |
652 | if(2*CEi<element->sizePrim[i]) { | |
653 | value |= ((hex2num(*(primary+4*CEi))&0xF)<<28); | |
654 | value |= ((hex2num(*(primary+4*CEi+1))&0xF)<<24); | |
655 | } | |
656 | ||
657 | if(2*CEi+1<element->sizePrim[i]) { | |
658 | value |= ((hex2num(*(primary+4*CEi+2))&0xF)<<20); | |
659 | value |= ((hex2num(*(primary+4*CEi+3))&0xF)<<16); | |
660 | } | |
661 | ||
662 | if(CEi<element->sizeSec[i]) { | |
663 | value |= ((hex2num(*(secondary+2*CEi))&0xF)<<12); | |
664 | value |= ((hex2num(*(secondary+2*CEi+1))&0xF)<<8); | |
665 | } | |
666 | ||
667 | if(CEi<element->sizeTer[i]) { | |
668 | value |= ((hex2num(*(tertiary+2*CEi))&0x3)<<4); | |
669 | value |= (hex2num(*(tertiary+2*CEi+1))&0xF); | |
670 | } | |
671 | ||
672 | CEi++; | |
673 | ||
674 | element->CEs[CEindex++] = value; | |
675 | } | |
676 | ||
677 | startCodePoint = endCodePoint+1; | |
678 | i++; | |
679 | } | |
680 | element->noOfCEs = CEindex; | |
73c04bcf | 681 | #if 0 |
b75a7d8f | 682 | element->isThai = UCOL_ISTHAIPREVOWEL(element->cPoints[0]); |
73c04bcf | 683 | #endif |
b75a7d8f A |
684 | // we don't want any strange stuff after useful data! |
685 | while(pointer < commentStart) { | |
374ca955 A |
686 | if(*pointer != ' ' && *pointer != '\t') |
687 | { | |
b75a7d8f A |
688 | *status=U_INVALID_FORMAT_ERROR; |
689 | break; | |
690 | } | |
691 | pointer++; | |
692 | } | |
693 | ||
694 | if(U_FAILURE(*status)) { | |
374ca955 | 695 | fprintf(stderr, "problem putting stuff in hash table %s\n", u_errorName(*status)); |
b75a7d8f A |
696 | *status = U_INTERNAL_PROGRAM_ERROR; |
697 | return NULL; | |
698 | } | |
699 | ||
700 | return element; | |
701 | } | |
702 | ||
703 | ||
704 | void writeOutData(UCATableHeader *data, | |
705 | UCAConstants *consts, | |
706 | UChar contractions[][3], | |
707 | uint32_t noOfcontractions, | |
708 | const char *outputDir, | |
709 | const char *copyright, | |
710 | UErrorCode *status) | |
711 | { | |
712 | if(U_FAILURE(*status)) { | |
713 | return; | |
714 | } | |
715 | ||
716 | uint32_t size = data->size; | |
717 | ||
374ca955 A |
718 | data->UCAConsts = data->size; |
719 | data->size += paddedsize(sizeof(UCAConstants)); | |
720 | ||
b75a7d8f A |
721 | if(noOfcontractions != 0) { |
722 | contractions[noOfcontractions][0] = 0; | |
723 | contractions[noOfcontractions][1] = 0; | |
724 | contractions[noOfcontractions][2] = 0; | |
725 | noOfcontractions++; | |
726 | ||
727 | ||
b75a7d8f | 728 | data->contractionUCACombos = data->size; |
374ca955 A |
729 | data->contractionUCACombosWidth = 3; |
730 | data->contractionUCACombosSize = noOfcontractions; | |
b75a7d8f A |
731 | data->size += paddedsize((noOfcontractions*3*sizeof(UChar))); |
732 | } | |
733 | ||
734 | UNewDataMemory *pData; | |
735 | ||
736 | long dataLength; | |
737 | UDataInfo ucaInfo; | |
738 | uprv_memcpy(&ucaInfo, &ucaDataInfo, sizeof(UDataInfo)); | |
739 | u_getUnicodeVersion(ucaInfo.dataVersion); | |
740 | ||
374ca955 | 741 | pData=udata_create(outputDir, UCA_DATA_TYPE, UCA_DATA_NAME, &ucaInfo, |
b75a7d8f A |
742 | copyright, status); |
743 | ||
744 | if(U_FAILURE(*status)) { | |
374ca955 | 745 | fprintf(stderr, "Error: unable to create %s"UCA_DATA_NAME", error %s\n", outputDir, u_errorName(*status)); |
b75a7d8f A |
746 | return; |
747 | } | |
748 | ||
749 | /* write the data to the file */ | |
750 | if (VERBOSE) { | |
751 | fprintf(stdout, "Writing out UCA table: %s%c%s.%s\n", outputDir, | |
752 | U_FILE_SEP_CHAR, | |
753 | U_ICUDATA_NAME "_" UCA_DATA_NAME, | |
754 | UCA_DATA_TYPE); | |
755 | } | |
756 | udata_writeBlock(pData, data, size); | |
757 | ||
758 | // output the constants here | |
759 | udata_writeBlock(pData, consts, sizeof(UCAConstants)); | |
760 | ||
761 | if(noOfcontractions != 0) { | |
762 | udata_writeBlock(pData, contractions, noOfcontractions*3*sizeof(UChar)); | |
763 | udata_writePadding(pData, paddedsize((noOfcontractions*3*sizeof(UChar))) - noOfcontractions*3*sizeof(uint16_t)); | |
764 | } | |
765 | ||
766 | /* finish up */ | |
767 | dataLength=udata_finish(pData, status); | |
768 | if(U_FAILURE(*status)) { | |
769 | fprintf(stderr, "Error: error %d writing the output file\n", *status); | |
770 | return; | |
771 | } | |
772 | } | |
773 | ||
774 | static int32_t | |
775 | write_uca_table(const char *filename, | |
776 | const char *outputDir, | |
777 | const char *copyright, | |
778 | UErrorCode *status) | |
779 | { | |
780 | FILE *data = fopen(filename, "r"); | |
73c04bcf A |
781 | if(data == NULL) { |
782 | fprintf(stderr, "Couldn't open file: %s\n", filename); | |
783 | return -1; | |
784 | } | |
b75a7d8f A |
785 | uint32_t line = 0; |
786 | UCAElements *element = NULL; | |
787 | UChar variableTopValue = 0; | |
788 | UCATableHeader *myD = (UCATableHeader *)uprv_malloc(sizeof(UCATableHeader)); | |
789 | /* test for NULL */ | |
374ca955 A |
790 | if(myD == NULL) { |
791 | *status = U_MEMORY_ALLOCATION_ERROR; | |
792 | fclose(data); | |
793 | return 0; | |
794 | } | |
795 | uprv_memset(myD, 0, sizeof(UCATableHeader)); | |
b75a7d8f A |
796 | UColOptionSet *opts = (UColOptionSet *)uprv_malloc(sizeof(UColOptionSet)); |
797 | /* test for NULL */ | |
374ca955 A |
798 | if(opts == NULL) { |
799 | *status = U_MEMORY_ALLOCATION_ERROR; | |
800 | uprv_free(myD); | |
801 | fclose(data); | |
802 | return 0; | |
803 | } | |
804 | uprv_memset(opts, 0, sizeof(UColOptionSet)); | |
73c04bcf A |
805 | UChar contractionCEs[512][3]; |
806 | uprv_memset(contractionCEs, 0, 512*3*sizeof(UChar)); | |
b75a7d8f A |
807 | uint32_t noOfContractions = 0; |
808 | UCAConstants consts; | |
374ca955 | 809 | uprv_memset(&consts, 0, sizeof(consts)); |
b75a7d8f A |
810 | #if 0 |
811 | UCAConstants consts = { | |
812 | UCOL_RESET_TOP_VALUE, | |
813 | UCOL_FIRST_PRIMARY_IGNORABLE, | |
814 | UCOL_LAST_PRIMARY_IGNORABLE, | |
815 | UCOL_LAST_PRIMARY_IGNORABLE_CONT, | |
816 | UCOL_FIRST_SECONDARY_IGNORABLE, | |
817 | UCOL_LAST_SECONDARY_IGNORABLE, | |
818 | UCOL_FIRST_TERTIARY_IGNORABLE, | |
819 | UCOL_LAST_TERTIARY_IGNORABLE, | |
820 | UCOL_FIRST_VARIABLE, | |
821 | UCOL_LAST_VARIABLE, | |
822 | UCOL_FIRST_NON_VARIABLE, | |
823 | UCOL_LAST_NON_VARIABLE, | |
824 | ||
825 | UCOL_NEXT_TOP_VALUE, | |
826 | /* | |
827 | UCOL_NEXT_FIRST_PRIMARY_IGNORABLE, | |
828 | UCOL_NEXT_LAST_PRIMARY_IGNORABLE, | |
829 | UCOL_NEXT_FIRST_SECONDARY_IGNORABLE, | |
830 | UCOL_NEXT_LAST_SECONDARY_IGNORABLE, | |
831 | UCOL_NEXT_FIRST_TERTIARY_IGNORABLE, | |
832 | UCOL_NEXT_LAST_TERTIARY_IGNORABLE, | |
833 | UCOL_NEXT_FIRST_VARIABLE, | |
834 | UCOL_NEXT_LAST_VARIABLE, | |
835 | */ | |
836 | ||
837 | PRIMARY_IMPLICIT_MIN, | |
838 | PRIMARY_IMPLICIT_MAX | |
839 | }; | |
840 | #endif | |
841 | ||
842 | ||
374ca955 | 843 | uprv_memset(inverseTable, 0xDA, sizeof(int32_t)*3*0xFFFF); |
b75a7d8f A |
844 | |
845 | opts->variableTopValue = variableTopValue; | |
846 | opts->strength = UCOL_TERTIARY; | |
847 | opts->frenchCollation = UCOL_OFF; | |
848 | opts->alternateHandling = UCOL_NON_IGNORABLE; /* attribute for handling variable elements*/ | |
849 | opts->caseFirst = UCOL_OFF; /* who goes first, lower case or uppercase */ | |
850 | opts->caseLevel = UCOL_OFF; /* do we have an extra case level */ | |
851 | opts->normalizationMode = UCOL_OFF; /* attribute for normalization */ | |
852 | opts->hiraganaQ = UCOL_OFF; /* attribute for JIS X 4061, used only in Japanese */ | |
374ca955 | 853 | opts->numericCollation = UCOL_OFF; |
b75a7d8f A |
854 | myD->jamoSpecial = FALSE; |
855 | ||
374ca955 | 856 | tempUCATable *t = uprv_uca_initTempTable(myD, opts, NULL, IMPLICIT_TAG, LEAD_SURROGATE_TAG, status); |
b75a7d8f A |
857 | if(U_FAILURE(*status)) |
858 | { | |
859 | fprintf(stderr, "Failed to init UCA temp table: %s\n", u_errorName(*status)); | |
73c04bcf A |
860 | uprv_free(opts); |
861 | uprv_free(myD); | |
862 | fclose(data); | |
b75a7d8f A |
863 | return -1; |
864 | } | |
865 | ||
866 | #if 0 | |
867 | IMPLICIT_TAG = 9, | |
868 | /* | |
869 | ***************************************************************************************** | |
870 | * NON_CHARACTER FDD0 - FDEF, FFFE, FFFF, 1FFFE, 1FFFF, 2FFFE, 2FFFF,...e.g. **FFFE, **FFFF | |
871 | ****************************************************************************************** | |
872 | */ | |
873 | #endif | |
874 | ||
875 | // * set to zero | |
876 | struct { | |
877 | UChar32 start; | |
878 | UChar32 end; | |
879 | int32_t value; | |
880 | } ranges[] = | |
881 | { | |
882 | #if 0 | |
883 | {0xAC00, 0xD7AF, UCOL_SPECIAL_FLAG | (HANGUL_SYLLABLE_TAG << 24) }, //0 HANGUL_SYLLABLE_TAG,/* AC00-D7AF*/ | |
884 | {0xD800, 0xDBFF, UCOL_SPECIAL_FLAG | (LEAD_SURROGATE_TAG << 24) }, //1 LEAD_SURROGATE_TAG, /* D800-DBFF*/ | |
885 | {0xDC00, 0xDFFF, UCOL_SPECIAL_FLAG | (TRAIL_SURROGATE_TAG << 24) }, //2 TRAIL_SURROGATE DC00-DFFF | |
886 | {0x3400, 0x4DB5, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //3 CJK_IMPLICIT_TAG, /* 0x3400-0x4DB5*/ | |
887 | {0x4E00, 0x9FA5, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //4 CJK_IMPLICIT_TAG, /* 0x4E00-0x9FA5*/ | |
888 | {0xF900, 0xFA2D, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //5 CJK_IMPLICIT_TAG, /* 0xF900-0xFA2D*/ | |
889 | {0x20000, 0x2A6D6, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //6 CJK_IMPLICIT_TAG, /* 0x20000-0x2A6D6*/ | |
890 | {0x2F800, 0x2FA1D, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //7 CJK_IMPLICIT_TAG, /* 0x2F800-0x2FA1D*/ | |
891 | #endif | |
892 | {0xAC00, 0xD7B0, UCOL_SPECIAL_FLAG | (HANGUL_SYLLABLE_TAG << 24) }, //0 HANGUL_SYLLABLE_TAG,/* AC00-D7AF*/ | |
374ca955 | 893 | //{0xD800, 0xDC00, UCOL_SPECIAL_FLAG | (LEAD_SURROGATE_TAG << 24) }, //1 LEAD_SURROGATE_TAG, /* D800-DBFF*/ |
b75a7d8f A |
894 | {0xDC00, 0xE000, UCOL_SPECIAL_FLAG | (TRAIL_SURROGATE_TAG << 24) }, //2 TRAIL_SURROGATE DC00-DFFF |
895 | // Now directly handled in the collation code by the swapCJK function. | |
896 | //{0x3400, 0x4DB6, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //3 CJK_IMPLICIT_TAG, /* 0x3400-0x4DB5*/ | |
897 | //{0x4E00, 0x9FA6, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //4 CJK_IMPLICIT_TAG, /* 0x4E00-0x9FA5*/ | |
898 | //{0xF900, 0xFA2E, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //5 CJK_IMPLICIT_TAG, /* 0xF900-0xFA2D*/ | |
899 | //{0x20000, 0x2A6D7, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //6 CJK_IMPLICIT_TAG, /* 0x20000-0x2A6D6*/ | |
900 | //{0x2F800, 0x2FA1E, UCOL_SPECIAL_FLAG | (CJK_IMPLICIT_TAG << 24) }, //7 CJK_IMPLICIT_TAG, /* 0x2F800-0x2FA1D*/ | |
901 | }; | |
902 | uint32_t i = 0; | |
903 | ||
904 | for(i = 0; i<sizeof(ranges)/sizeof(ranges[0]); i++) { | |
905 | /*ucmpe32_setRange32(t->mapping, ranges[i].start, ranges[i].end, ranges[i].value); */ | |
906 | utrie_setRange32(t->mapping, ranges[i].start, ranges[i].end, ranges[i].value, TRUE); | |
907 | } | |
908 | ||
909 | ||
910 | int32_t surrogateCount = 0; | |
911 | while(!feof(data)) { | |
912 | if(U_FAILURE(*status)) { | |
374ca955 A |
913 | fprintf(stderr, "Something returned an error %i (%s) while processing line %u of %s. Exiting...\n", |
914 | *status, u_errorName(*status), (int)line, filename); | |
b75a7d8f A |
915 | exit(*status); |
916 | } | |
917 | ||
918 | element = readAnElement(data, t, &consts, status); | |
919 | line++; | |
920 | if(VERBOSE) { | |
374ca955 | 921 | fprintf(stdout, "%u ", (int)line); |
b75a7d8f A |
922 | } |
923 | if(element != NULL) { | |
924 | // we have read the line, now do something sensible with the read data! | |
925 | ||
926 | // Below stuff was taken care of in readAnElement | |
927 | //if(element->variableTop == TRUE && variableTopValue == 0) { | |
928 | // t->options->variableTopValue = element->cPoints[0]; | |
929 | //} | |
930 | ||
931 | // if element is a contraction, we want to add it to contractions | |
932 | if(element->cSize > 1 && element->cPoints[0] != 0xFDD0) { // this is a contraction | |
933 | if(UTF_IS_LEAD(element->cPoints[0]) && UTF_IS_TRAIL(element->cPoints[1]) && element->cSize == 2) { | |
934 | surrogateCount++; | |
935 | } else { | |
936 | contractionCEs[noOfContractions][0] = element->cPoints[0]; | |
937 | contractionCEs[noOfContractions][1] = element->cPoints[1]; | |
938 | if(element->cSize > 2) { // the third one | |
939 | contractionCEs[noOfContractions][2] = element->cPoints[2]; | |
940 | } else { | |
941 | contractionCEs[noOfContractions][2] = 0; | |
942 | } | |
943 | noOfContractions++; | |
944 | } | |
945 | } | |
946 | ||
947 | /* we're first adding to inverse, because addAnElement will reverse the order */ | |
948 | /* of code points and stuff... we don't want that to happen */ | |
949 | addToInverse(element, status); | |
950 | if(!(element->cSize > 1 && element->cPoints[0] == 0xFDD0)) { | |
951 | uprv_uca_addAnElement(t, element, status); | |
952 | } | |
953 | } | |
954 | } | |
955 | ||
956 | if(UCAVersion[0] == 0 && UCAVersion[1] == 0 && UCAVersion[2] == 0 && UCAVersion[3] == 0) { | |
73c04bcf A |
957 | fprintf(stderr, "UCA version not specified. Cannot create data file!\n"); |
958 | uprv_uca_closeTempTable(t); | |
959 | uprv_free(opts); | |
960 | uprv_free(myD); | |
961 | fclose(data); | |
962 | return -1; | |
b75a7d8f | 963 | } |
374ca955 A |
964 | /* { |
965 | uint32_t trieWord = utrie_get32(t->mapping, 0xDC01, NULL); | |
966 | }*/ | |
b75a7d8f A |
967 | |
968 | if (VERBOSE) { | |
374ca955 A |
969 | fprintf(stdout, "\nLines read: %u\n", (int)line); |
970 | fprintf(stdout, "Surrogate count: %i\n", (int)surrogateCount); | |
b75a7d8f A |
971 | fprintf(stdout, "Raw data breakdown:\n"); |
972 | /*fprintf(stdout, "Compact array stage1 top: %i, stage2 top: %i\n", t->mapping->stage1Top, t->mapping->stage2Top);*/ | |
374ca955 A |
973 | fprintf(stdout, "Number of contractions: %u\n", (int)noOfContractions); |
974 | fprintf(stdout, "Contraction image size: %u\n", (int)t->image->contractionSize); | |
975 | fprintf(stdout, "Expansions size: %i\n", (int)t->expansions->position); | |
b75a7d8f A |
976 | } |
977 | ||
978 | ||
979 | /* produce canonical closure for table */ | |
980 | /* first set up constants for implicit calculation */ | |
374ca955 | 981 | uprv_uca_initImplicitConstants(consts.UCA_PRIMARY_IMPLICIT_MIN, consts.UCA_PRIMARY_IMPLICIT_MAX, status); |
b75a7d8f A |
982 | /* do the closure */ |
983 | int32_t noOfClosures = uprv_uca_canonicalClosure(t, status); | |
984 | if(noOfClosures != 0) { | |
374ca955 | 985 | fprintf(stderr, "Warning: %i canonical closures occured!\n", (int)noOfClosures); |
b75a7d8f A |
986 | } |
987 | ||
988 | /* test */ | |
989 | UCATableHeader *myData = uprv_uca_assembleTable(t, status); | |
990 | ||
991 | if (VERBOSE) { | |
992 | fprintf(stdout, "Compacted data breakdown:\n"); | |
993 | /*fprintf(stdout, "Compact array stage1 top: %i, stage2 top: %i\n", t->mapping->stage1Top, t->mapping->stage2Top);*/ | |
374ca955 A |
994 | fprintf(stdout, "Number of contractions: %u\n", (int)noOfContractions); |
995 | fprintf(stdout, "Contraction image size: %u\n", (int)t->image->contractionSize); | |
996 | fprintf(stdout, "Expansions size: %i\n", (int)t->expansions->position); | |
997 | } | |
998 | ||
999 | if(U_FAILURE(*status)) { | |
1000 | fprintf(stderr, "Error creating table: %s\n", u_errorName(*status)); | |
73c04bcf A |
1001 | uprv_uca_closeTempTable(t); |
1002 | uprv_free(opts); | |
1003 | uprv_free(myD); | |
1004 | fclose(data); | |
374ca955 | 1005 | return -1; |
b75a7d8f A |
1006 | } |
1007 | ||
1008 | /* populate the version info struct with version info*/ | |
1009 | myData->version[0] = UCOL_BUILDER_VERSION; | |
1010 | myData->version[1] = UCAVersion[0]; | |
1011 | myData->version[2] = UCAVersion[1]; | |
1012 | myData->version[3] = UCAVersion[2]; | |
1013 | /*TODO:The fractional rules version should be taken from FractionalUCA.txt*/ | |
1014 | // Removed this macro. Instead, we use the fields below | |
1015 | //myD->version[1] = UCOL_FRACTIONAL_UCA_VERSION; | |
1016 | //myD->UCAVersion = UCAVersion; // out of FractionalUCA.txt | |
1017 | uprv_memcpy(myData->UCAVersion, UCAVersion, sizeof(UVersionInfo)); | |
1018 | u_getUnicodeVersion(myData->UCDVersion); | |
1019 | ||
1020 | writeOutData(myData, &consts, contractionCEs, noOfContractions, outputDir, copyright, status); | |
1021 | ||
1022 | InverseUCATableHeader *inverse = assembleInverseTable(status); | |
1023 | uprv_memcpy(inverse->UCAVersion, UCAVersion, sizeof(UVersionInfo)); | |
1024 | writeOutInverseData(inverse, outputDir, copyright, status); | |
1025 | ||
73c04bcf | 1026 | uprv_uca_closeTempTable(t); |
b75a7d8f A |
1027 | uprv_free(myD); |
1028 | uprv_free(opts); | |
1029 | ||
1030 | ||
1031 | uprv_free(myData); | |
1032 | uprv_free(inverse); | |
1033 | fclose(data); | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | #endif /* #if !UCONFIG_NO_COLLATION */ | |
1039 | ||
1040 | static UOption options[]={ | |
1041 | UOPTION_HELP_H, /* 0 Numbers for those who*/ | |
1042 | UOPTION_HELP_QUESTION_MARK, /* 1 can't count. */ | |
1043 | UOPTION_COPYRIGHT, /* 2 */ | |
1044 | UOPTION_VERSION, /* 3 */ | |
1045 | UOPTION_DESTDIR, /* 4 */ | |
1046 | UOPTION_SOURCEDIR, /* 5 */ | |
1047 | UOPTION_VERBOSE, /* 6 */ | |
1048 | UOPTION_ICUDATADIR /* 7 */ | |
1049 | /* weiv can't count :))))) */ | |
1050 | }; | |
1051 | ||
1052 | int main(int argc, char* argv[]) { | |
1053 | UErrorCode status = U_ZERO_ERROR; | |
1054 | const char* destdir = NULL; | |
1055 | const char* srcDir = NULL; | |
1056 | char filename[300]; | |
1057 | char *basename = NULL; | |
1058 | const char *copyright = NULL; | |
1059 | uprv_memset(&UCAVersion, 0, 4); | |
1060 | ||
1061 | U_MAIN_INIT_ARGS(argc, argv); | |
1062 | ||
1063 | /* preset then read command line options */ | |
1064 | options[4].value=u_getDataDirectory(); | |
1065 | options[5].value=""; | |
1066 | argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); | |
1067 | ||
1068 | /* error handling, printing usage message */ | |
1069 | if(argc<0) { | |
1070 | fprintf(stderr, | |
1071 | "error in command line argument \"%s\"\n", | |
1072 | argv[-argc]); | |
1073 | } else if(argc<2) { | |
1074 | argc=-1; | |
1075 | } | |
1076 | if(options[0].doesOccur || options[1].doesOccur) { | |
1077 | fprintf(stderr, | |
1078 | "usage: %s [-options] file\n" | |
1079 | "\tRead in UCA collation text data and write out the binary collation data\n" | |
1080 | "options:\n" | |
1081 | "\t-h or -? or --help this usage text\n" | |
1082 | "\t-V or --version show a version message\n" | |
1083 | "\t-c or --copyright include a copyright notice\n" | |
1084 | "\t-d or --destdir destination directory, followed by the path\n" | |
1085 | "\t-s or --sourcedir source directory, followed by the path\n" | |
1086 | "\t-v or --verbose turn on verbose output\n" | |
1087 | "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" | |
1088 | "\t followed by path, defaults to %s\n", | |
1089 | argv[0], u_getDataDirectory()); | |
1090 | return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; | |
1091 | } | |
b75a7d8f | 1092 | if(options[3].doesOccur) { |
374ca955 | 1093 | fprintf(stdout, "genuca version %hu.%hu, ICU tool to read UCA text data and create UCA data tables for collation.\n", |
b75a7d8f A |
1094 | #if UCONFIG_NO_COLLATION |
1095 | 0, 0 | |
1096 | #else | |
374ca955 | 1097 | UCA_FORMAT_VERSION_0, UCA_FORMAT_VERSION_1 |
b75a7d8f A |
1098 | #endif |
1099 | ); | |
374ca955 | 1100 | fprintf(stdout, U_COPYRIGHT_STRING"\n"); |
b75a7d8f A |
1101 | exit(0); |
1102 | } | |
1103 | ||
1104 | /* get the options values */ | |
1105 | destdir = options[4].value; | |
1106 | srcDir = options[5].value; | |
1107 | VERBOSE = options[6].doesOccur; | |
1108 | ||
1109 | if (options[2].doesOccur) { | |
1110 | copyright = U_COPYRIGHT_STRING; | |
1111 | } | |
1112 | ||
1113 | if (options[7].doesOccur) { | |
1114 | u_setDataDirectory(options[7].value); | |
1115 | } | |
374ca955 A |
1116 | /* Initialize ICU */ |
1117 | u_init(&status); | |
1118 | if (U_FAILURE(status) && status != U_FILE_ACCESS_ERROR) { | |
1119 | fprintf(stderr, "%s: can not initialize ICU. status = %s\n", | |
1120 | argv[0], u_errorName(status)); | |
1121 | exit(1); | |
1122 | } | |
1123 | status = U_ZERO_ERROR; | |
1124 | ||
b75a7d8f A |
1125 | |
1126 | /* prepare the filename beginning with the source dir */ | |
1127 | uprv_strcpy(filename, srcDir); | |
1128 | basename=filename+uprv_strlen(filename); | |
1129 | ||
1130 | if(basename>filename && *(basename-1)!=U_FILE_SEP_CHAR) { | |
1131 | *basename++ = U_FILE_SEP_CHAR; | |
1132 | } | |
1133 | ||
1134 | if(argc < 0) { | |
1135 | uprv_strcpy(basename, "FractionalUCA.txt"); | |
1136 | } else { | |
1137 | argv++; | |
1138 | uprv_strcpy(basename, getLongPathname(*argv)); | |
1139 | } | |
1140 | ||
1141 | #if 0 | |
1142 | if(u_getCombiningClass(0x0053) == 0) | |
1143 | { | |
1144 | fprintf(stderr, "SEVERE ERROR: Normalization data is not functioning! Bailing out. Was not able to load unorm.dat.\n"); | |
1145 | exit(1); | |
1146 | } | |
1147 | #endif | |
1148 | ||
1149 | #if UCONFIG_NO_COLLATION | |
1150 | ||
1151 | UNewDataMemory *pData; | |
1152 | const char *msg; | |
1153 | ||
374ca955 | 1154 | msg = "genuca writes dummy " UCA_DATA_NAME "." UCA_DATA_TYPE " because of UCONFIG_NO_COLLATION, see uconfig.h"; |
b75a7d8f | 1155 | fprintf(stderr, "%s\n", msg); |
374ca955 | 1156 | pData = udata_create(destdir, UCA_DATA_TYPE, UCA_DATA_NAME, &dummyDataInfo, |
b75a7d8f A |
1157 | NULL, &status); |
1158 | udata_writeBlock(pData, msg, strlen(msg)); | |
1159 | udata_finish(pData, &status); | |
1160 | ||
374ca955 | 1161 | msg = "genuca writes dummy " INVC_DATA_NAME "." INVC_DATA_TYPE " because of UCONFIG_NO_COLLATION, see uconfig.h"; |
b75a7d8f | 1162 | fprintf(stderr, "%s\n", msg); |
374ca955 | 1163 | pData = udata_create(destdir, INVC_DATA_TYPE, INVC_DATA_NAME, &dummyDataInfo, |
b75a7d8f A |
1164 | NULL, &status); |
1165 | udata_writeBlock(pData, msg, strlen(msg)); | |
1166 | udata_finish(pData, &status); | |
1167 | ||
1168 | return (int)status; | |
1169 | ||
1170 | #else | |
1171 | ||
1172 | return write_uca_table(filename, destdir, copyright, &status); | |
1173 | ||
1174 | #endif | |
1175 | } | |
1176 | ||
1177 | /* | |
1178 | * Hey, Emacs, please set the following: | |
1179 | * | |
1180 | * Local Variables: | |
1181 | * indent-tabs-mode: nil | |
1182 | * End: | |
1183 | * | |
1184 | */ |