]> git.saurik.com Git - apple/icu.git/blame - icuSources/tools/dumpce/dumpce.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / tools / dumpce / dumpce.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2001-2003 IBM, Inc. All Rights Reserved.
4 *
5 ********************************************************************/
6/********************************************************************************
7*
8* File dumpce.cpp
9*
10* Modification History:
11* Name Date Description
12* synwee May 31 2001 Creation
13*
14*********************************************************************************
15*/
16
17/**
18* This program outputs the collation elements used for a requested tailoring.
19*
20* Usage:
21* dumpce options... please check main function.
22*/
23#include <unicode/utypes.h>
24#include <unicode/ucol.h>
25#include <unicode/uloc.h>
26#include <unicode/ucoleitr.h>
27#include <unicode/uchar.h>
28#include <unicode/uscript.h>
29#include <unicode/utf16.h>
30#include <unicode/putil.h>
31#include <unicode/ustring.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <time.h>
36#include "ucol_tok.h"
37#include "cstring.h"
38#include "uoptions.h"
39#include "ucol_imp.h"
40#include <unicode/ures.h>
41#include <unicode/uniset.h>
42#include <unicode/usetiter.h>
43
44/**
45* Command line option variables.
46* These global variables are set according to the options specified on the
47* command line by the user.
48*/
49static UOption options[]={
50 /* 00 */ UOPTION_HELP_H,
51 /* 01 */ UOPTION_HELP_QUESTION_MARK,
52 /* 02 */ {"locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0},
53 /* 03 */ {"serialize", NULL, NULL, NULL, 'z', UOPT_NO_ARG, 0},
54 /* 04 */ UOPTION_DESTDIR,
55 /* 05 */ UOPTION_SOURCEDIR,
56 /* 06 */ {"attribute", NULL, NULL, NULL, 'a', UOPT_REQUIRES_ARG, 0},
57 /* 07 */ {"rule", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0},
58 /* 08 */ {"normalization", NULL, NULL, NULL, 'n', UOPT_REQUIRES_ARG, 0},
59 /* 09 */ {"scripts", NULL, NULL, NULL, 't', UOPT_NO_ARG, 0},
60 /* 10 */ {"reducehan", NULL, NULL, NULL, 'e', UOPT_NO_ARG, 0},
61 /* 11 */ UOPTION_VERBOSE,
62 /* 12 */ {"wholescripts", NULL, NULL, NULL, 'W', UOPT_NO_ARG, 0}
63};
64
65/**
66* Collator used in this program
67*/
68static UCollator *COLLATOR_;
69/**
70* Output strea, used in this program
71*/
72static FILE *OUTPUT_;
73
74static UColAttributeValue ATTRIBUTE_[UCOL_ATTRIBUTE_COUNT] = {
75 UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT,
76 UCOL_DEFAULT, UCOL_DEFAULT
77};
78
79typedef struct {
80 int value;
81 char *name;
82} EnumNameValuePair;
83
84static const EnumNameValuePair ATTRIBUTE_NAME_[] = {
85 {UCOL_FRENCH_COLLATION, "UCOL_FRENCH_COLLATION"},
86 {UCOL_ALTERNATE_HANDLING, "UCOL_ALTERNATE_HANDLING"},
87 {UCOL_CASE_FIRST, "UCOL_CASE_FIRST"},
88 {UCOL_CASE_LEVEL, "UCOL_CASE_LEVEL"},
89 {UCOL_NORMALIZATION_MODE,
90 "UCOL_NORMALIZATION_MODE|UCOL_DECOMPOSITION_MODE"},
91 {UCOL_STRENGTH, "UCOL_STRENGTH"},
92 {UCOL_HIRAGANA_QUATERNARY_MODE, "UCOL_HIRAGANA_QUATERNARY_MODE"},
93 NULL
94};
95
96static const EnumNameValuePair ATTRIBUTE_VALUE_[] = {
97 {UCOL_PRIMARY, "UCOL_PRIMARY"},
98 {UCOL_SECONDARY, "UCOL_SECONDARY"},
99 {UCOL_TERTIARY, "UCOL_TERTIARY|UCOL_DEFAULT_STRENGTH"},
100 {UCOL_QUATERNARY, "UCOL_QUATERNARY"},
101 {UCOL_IDENTICAL, "UCOL_IDENTICAL"},
102 {UCOL_OFF, "UCOL_OFF"},
103 {UCOL_ON, "UCOL_ON"},
104 {UCOL_SHIFTED, "UCOL_SHIFTED"},
105 {UCOL_NON_IGNORABLE, "UCOL_NON_IGNORABLE"},
106 {UCOL_LOWER_FIRST, "UCOL_LOWER_FIRST"},
107 {UCOL_UPPER_FIRST, "UCOL_UPPER_FIRST"},
108 NULL
109};
110
111typedef struct {
112 UChar ch[32];
113 int count; // number of codepoint
114 UBool tailored;
115} ScriptElement;
116
117/**
118* Writes the hexadecimal of a null-terminated array of codepoints into a
119* file
120* @param f UFILE instance to store
121* @param c codepoints array
122*/
123void serialize(FILE *f, const UChar *c)
124{
125 UChar cp = *(c ++);
126
127 fprintf(f, " %04x", cp);
128
129 while (*c != 0) {
130 cp = *(c ++);
131 fprintf(f, " %04x", cp);
132 }
133}
134
135/**
136* Writes the hexadecimal of a non-null-terminated array of codepoints into a
137* file
138* @param f UFILE instance to store
139* @param c codepoints array
140* @param l codepoints array length
141*/
142void serialize(FILE *f, const UChar *c, int l)
143{
144 int count = 1;
145 UChar cp = *(c ++);
146
147 fprintf(f, " %04x", cp);
148
149 while (count < l) {
150 cp = *(c ++);
151 fprintf(f, " %04x", cp);
152 count ++;
153 }
154}
155
156/**
157* Sets the iterator to the argument string and outputs the collation elements.
158* @param f file output stream
159* @param iter collation element iterator
160*/
161void serialize(FILE *f, UCollationElements *iter) {
162 UChar *codepoint = iter->iteratordata_.string;
163 // unlikely that sortkeys will be over this size
164 uint8_t sortkey[64];
165 uint8_t *psortkey = sortkey;
166 int sortkeylength = 0;
167
168 if (iter->iteratordata_.flags & UCOL_ITER_HASLEN) {
169 serialize(f, codepoint, iter->iteratordata_.endp - codepoint);
170 sortkeylength = ucol_getSortKey(iter->iteratordata_.coll, codepoint,
171 iter->iteratordata_.endp - codepoint, sortkey, 64);
172 }
173 else {
174 serialize(f, codepoint);
175 sortkeylength = ucol_getSortKey(iter->iteratordata_.coll, codepoint,
176 -1, sortkey, 64);
177 }
178 if (options[11].doesOccur) {
179 serialize(stdout, codepoint);
180 fprintf(stdout, "\n");
181 }
182
183 fprintf(f, "; ");
184
185 UErrorCode error = U_ZERO_ERROR;
186 uint32_t ce = ucol_next(iter, &error);
187 if (U_FAILURE(error)) {
188 fprintf(f, "Error retrieving collation elements\n");
189 return;
190 }
191
192 while (TRUE) {
193 fprintf(f, "[");
194 if (UCOL_PRIMARYORDER(ce) != 0) {
195 fprintf(f, "%04x", UCOL_PRIMARYORDER(ce));
196 }
197 fprintf(f, ",");
198 if (UCOL_SECONDARYORDER(ce) != 0) {
199 fprintf(f, " %02x", UCOL_SECONDARYORDER(ce));
200 }
201 fprintf(f, ",");
202 if (UCOL_TERTIARYORDER(ce) != 0) {
203 fprintf(f, " %02x", UCOL_TERTIARYORDER(ce));
204 }
205 fprintf(f, "] ");
206
207 ce = ucol_next(iter, &error);
208 if (ce == UCOL_NULLORDER) {
209 break;
210 }
211 if (U_FAILURE(error)) {
212 fprintf(stdout, "Error retrieving collation elements");
213 return;
214 }
215 }
216
217 if (sortkeylength > 64) {
218 fprintf(f, "Sortkey exceeds pre-allocated size");
219 }
220
221 fprintf(f, "[");
222 while (TRUE) {
223 fprintf(f, "%02x", *psortkey);
224 psortkey ++;
225 if ((*psortkey) == 0) {
226 break;
227 }
228 fprintf(f, " ");
229 }
230 fprintf(f, "]\n");
231}
232
233/**
234* Serializes the contraction within the given argument rule
235* @param f file output stream
236* @param r rule
237* @param rlen rule length
238* @param contractionsonly flag to indicate if only contractions are to be
239* output or all collation elements
240* @param iter iterator to iterate over collation elements
241*/
242void serialize(FILE *f, UChar *rule, int rlen, UBool contractiononly,
243 UCollationElements *iter) {
244 const UChar *current = NULL;
245 uint32_t strength = 0;
246 uint32_t chOffset = 0;
247 uint32_t chLen = 0;
248 uint32_t exOffset = 0;
249 uint32_t exLen = 0;
250 uint32_t prefixOffset = 0;
251 uint32_t prefixLen = 0;
252 uint8_t specs = 0;
253 UBool rstart = TRUE;
254 UColTokenParser src;
255 UColOptionSet opts;
256 UParseError parseError;
257 UErrorCode error = U_ZERO_ERROR;
258
259 src.opts = &opts;
260
261 src.source = rule;
262 src.current = rule;
263 src.end = rule + rlen;
264 src.extraCurrent = src.end;
265 src.extraEnd = src.end + UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
266
267
268 while ((current = ucol_tok_parseNextToken(&src, rstart, &parseError,
269 &error)) != NULL) {
270 chOffset = src.parsedToken.charsOffset;
271 chLen = src.parsedToken.charsLen;
272 // contractions handled here
273 if (!contractiononly || chLen > 1) {
274 ucol_setText(iter, rule + chOffset, chLen, &error);
275 if (U_FAILURE(error)) {
276 fprintf(stdout, "Error setting text in iterator\n");
277 return;
278 }
279 serialize(f, iter);
280 }
281 rstart = FALSE;
282 }
283}
284
285/**
286* Prints the attribute values in the argument collator into the output stream
287* @param collator
288*/
289void outputAttribute(UCollator *collator, UErrorCode *error)
290{
291 UColAttribute attribute = UCOL_FRENCH_COLLATION;
292 while (attribute < UCOL_ATTRIBUTE_COUNT) {
293 int count = 0;
294 while (TRUE) {
295 // getting attribute name
296 if (ATTRIBUTE_NAME_[count].value == attribute) {
297 fprintf(OUTPUT_, "%s = ", ATTRIBUTE_NAME_[count].name);
298 break;
299 }
300 count ++;
301 }
302 count = 0;
303 int attributeval = ucol_getAttribute(collator, attribute, error);
304 if (U_FAILURE(*error)) {
305 fprintf(stdout, "Failure in reading collator attribute\n");
306 return;
307 }
308 while (TRUE) {
309 // getting attribute value
310 if (ATTRIBUTE_VALUE_[count].value == attributeval) {
311 fprintf(OUTPUT_, "%s\n", ATTRIBUTE_VALUE_[count].name);
312 break;
313 }
314 count ++;
315 }
316 attribute = (UColAttribute)(attribute + 1);
317 }
318}
319
320/**
321* Prints the normalization mode in the argument collator into the output stream
322* @param collator
323*/
324void outputNormalization(UCollator *collator)
325{
326 UErrorCode status = U_ZERO_ERROR;
327 int normmode = ucol_getAttribute(collator, UCOL_NORMALIZATION_MODE, &status);
328 int count = 0;
329 while (TRUE) {
330 // getting attribute name
331 if (ATTRIBUTE_VALUE_[count].value == normmode) {
332 break;
333 }
334 count ++;
335 }
336 fprintf(OUTPUT_, "NORMALIZATION MODE = %s\n",
337 ATTRIBUTE_VALUE_[count].name);
338}
339
340/**
341* Output the collation element belonging to the locale into a file
342* @param locale string
343* @param fullrules flag to indicate if only tailored collation elements are to
344* be output or all collation elements
345*/
346void serialize(const char *locale, UBool tailoredonly) {
347 UErrorCode error = U_ZERO_ERROR;
348 UChar str[128];
349 int strlen = 0;
350
351 fprintf(OUTPUT_, "# This file contains the serialized collation elements\n");
352 fprintf(OUTPUT_, "# as of the collation version indicated below.\n");
353 fprintf(OUTPUT_, "# Data format: xxxx xxxx..; [yyyy, yy, yy] [yyyy, yy, yy] ... [yyyy, yy, yy] [zz zz..\n");
354 fprintf(OUTPUT_, "# where xxxx are codepoints in hexadecimals,\n");
355 fprintf(OUTPUT_, "# yyyyyyyy are the corresponding\n");
356 fprintf(OUTPUT_, "# collation elements in hexadecimals\n");
357 fprintf(OUTPUT_, "# and zz are the sortkey values in hexadecimals\n");
358
359 fprintf(OUTPUT_, "\n# Collator information\n");
360
361 fprintf(OUTPUT_, "\nLocale: %s\n", locale);
362 fprintf(stdout, "Locale: %s\n", locale);
363 UVersionInfo version;
364 ucol_getVersion(COLLATOR_, version);
365 fprintf(OUTPUT_, "Version number: %d.%d.%d.%d\n",
366 version[0], version[1], version[2], version[3]);
367 outputAttribute(COLLATOR_, &error);
368 outputNormalization(COLLATOR_);
369
370 UCollationElements *iter = ucol_openElements(COLLATOR_, str, strlen,
371 &error);
372 if (U_FAILURE(error)) {
373 fprintf(stdout, "Error creating iterator\n");
374 return;
375 }
376
377 if (!tailoredonly) {
378 fprintf(OUTPUT_, "\n# Range of unicode characters\n\n");
379 UChar32 codepoint = 0;
380 while (codepoint <= UCHAR_MAX_VALUE) {
381 if (u_isdefined(codepoint)) {
382 strlen = 0;
383 UTF16_APPEND_CHAR_UNSAFE(str, strlen, codepoint);
384 str[strlen] = 0;
385 ucol_setText(iter, str, strlen, &error);
386 if (U_FAILURE(error)) {
387 fprintf(stdout, "Error setting text in iterator\n");
388 return;
389 }
390 serialize(OUTPUT_, iter);
391 }
392 codepoint ++;
393 }
394 }
395
396 UChar ucarules[0x10000];
397 UChar *rules;
398 int32_t rulelength = 0;
399 rules = ucarules;
400
401 if (tailoredonly) {
402 int32_t rulelength = 0;
403 const UChar *temp = ucol_getRules(COLLATOR_, &rulelength);
404 if (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE > 0x10000) {
405 rules = (UChar *)malloc(sizeof(UChar) *
406 (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE));
407 }
408 memcpy(rules, temp, rulelength * sizeof(UChar));
409 rules[rulelength] = 0;
410 fprintf(OUTPUT_, "\n# Tailorings\n\n");
411 serialize(OUTPUT_, rules, rulelength, FALSE, iter);
412 if (rules != ucarules) {
413 free(rules);
414 }
415 }
416 else {
417 rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, ucarules,
418 0x10000);
419 if (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE > 0x10000) {
420 rules = (UChar *)malloc(sizeof(UChar) *
421 (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE));
422 rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, rules,
423 rulelength);
424 }
425 fprintf(OUTPUT_, "\n# Contractions\n\n");
426 serialize(OUTPUT_, rules, rulelength, TRUE, iter);
427 if (rules != ucarules) {
428 free(rules);
429 }
430 }
431
432 ucol_closeElements(iter);
433}
434
435/**
436* Sets the collator with the attribute values
437* @param collator
438* @param error status
439*/
440void setAttributes(UCollator *collator, UErrorCode *error)
441{
442 int count = 0;
443 while (count < UCOL_ATTRIBUTE_COUNT) {
444 if (ATTRIBUTE_[count] != UCOL_DEFAULT) {
445 ucol_setAttribute(collator, (UColAttribute)count,
446 ATTRIBUTE_[count], error);
447 if (U_FAILURE(*error)) {
448 return;
449 }
450 }
451 count ++;
452 }
453}
454
455/**
456* Appends directory path with an ending seperator if necessary.
457* @param path with enough space to append one seperator
458* @return new directory path length
459*/
460int appendDirSeparator(char *dir)
461{
462 int dirlength = strlen(dir);
463 char dirending = dir[dirlength - 1];
464 if (dirending != U_FILE_SEP_CHAR) {
465 dir[dirlength] = U_FILE_SEP_CHAR;
466 dir[dirlength + 1] = 0;
467 return dirlength + 1;
468 }
469 return dirlength;
470}
471
472/**
473* Output the collation element into a file
474*/
475void serialize() {
476 char filename[128];
477 int dirlength = 0;
478
479 if (options[4].doesOccur) {
480 strcpy(filename, options[4].value);
481 dirlength = appendDirSeparator(filename);
482 }
483
484 if (options[2].doesOccur) {
485 const char *locale = (char *)options[2].value;
486 int32_t localeindex = 0;
487
488 if (strcmp(locale, "all") == 0) {
489 if (options[4].doesOccur) {
490 strcat(filename, "UCA.txt");
491 OUTPUT_ = fopen(filename, "w");
492 if (OUTPUT_ == NULL) {
493 fprintf(stdout, "Cannot open file:%s\n", filename);
494 return;
495 }
496 }
497 fprintf(stdout, "UCA\n");
498 UErrorCode error = U_ZERO_ERROR;
499 COLLATOR_ = ucol_open("en_US", &error);
500 if (U_FAILURE(error)) {
501 fprintf(stdout, "Collator creation failed:");
502 fprintf(stdout, u_errorName(error));
503 goto CLOSEUCA;
504 return;
505 }
506 setAttributes(COLLATOR_, &error);
507 if (U_FAILURE(error)) {
508 fprintf(stdout, "Collator attribute setting failed:");
509 fprintf(stdout, u_errorName(error));
510 goto CLOSEUCA;
511 return;
512 }
513
514 serialize("UCA", FALSE);
515CLOSEUCA :
516 if (options[4].doesOccur) {
517 filename[dirlength] = 0;
518 fclose(OUTPUT_);
519 }
520 ucol_close(COLLATOR_);
521 localeindex = ucol_countAvailable() - 1;
522 fprintf(stdout, "Number of locales: %d\n", localeindex + 1);
523 locale = ucol_getAvailable(localeindex);
524 }
525
526 while (TRUE) {
527 UErrorCode error = U_ZERO_ERROR;
528 COLLATOR_ = ucol_open(locale, &error);
529 if (U_FAILURE(error)) {
530 fprintf(stdout, "Collator creation failed:");
531 fprintf(stdout, u_errorName(error));
532 goto CLOSETAILOR;
533 return;
534 }
535 setAttributes(COLLATOR_, &error);
536 if (U_FAILURE(error)) {
537 fprintf(stdout, "Collator attribute setting failed:");
538 fprintf(stdout, u_errorName(error));
539 goto CLOSETAILOR;
540 return;
541 }
542
543 if (options[4].doesOccur) {
544 strcat(filename, locale);
545 strcat(filename, ".txt");
546 OUTPUT_ = fopen(filename, "w");
547 if (OUTPUT_ == NULL) {
548 fprintf(stdout, "Cannot open file:%s\n", filename);
549 return;
550 }
551 }
552
553 if (options[3].doesOccur) {
554 serialize(locale, TRUE);
555 }
556
557 ucol_close(COLLATOR_);
558
559CLOSETAILOR :
560 if (options[4].doesOccur) {
561 filename[dirlength] = 0;
562 fclose(OUTPUT_);
563 }
564
565 localeindex --;
566 if (localeindex < 0) {
567 break;
568 }
569 locale = ucol_getAvailable(localeindex);
570 }
571 }
572
573 if (options[7].doesOccur) {
574 char inputfilename[128];
575 // rules are to be used
576 if (options[5].doesOccur) {
577 strcpy(inputfilename, options[5].value);
578 appendDirSeparator(inputfilename);
579 }
580 strcat(inputfilename, options[7].value);
581 FILE *input = fopen(inputfilename, "r");
582 if (input == NULL) {
583 fprintf(stdout, "Cannot open file:%s\n", filename);
584 return;
585 }
586
587 char s[1024];
588 UChar rule[1024];
589 UChar *prule = rule;
590 int size = 1024;
591 // synwee TODO: make this part dynamic
592 while (fscanf(input, "%[^\n]s", s) != EOF) {
593 size -= u_unescape(s, prule, size);
594 prule = prule + u_strlen(prule);
595 }
596 fclose(input);
597
598 if (options[4].doesOccur) {
599 strcat(filename, "Rules.txt");
600 OUTPUT_ = fopen(filename, "w");
601 if (OUTPUT_ == NULL) {
602 fprintf(stdout, "Cannot open file:%s\n", filename);
603 return;
604 }
605 }
606
607 fprintf(stdout, "Rules\n");
608 UErrorCode error = U_ZERO_ERROR;
609 UParseError parseError;
610 COLLATOR_ = ucol_openRules(rule, u_strlen(rule), UCOL_DEFAULT,
611 UCOL_DEFAULT_STRENGTH, &parseError, &error);
612 if (U_FAILURE(error)) {
613 fprintf(stdout, "Collator creation failed:");
614 fprintf(stdout, u_errorName(error));
615 goto CLOSERULES;
616 return;
617 }
618 setAttributes(COLLATOR_, &error);
619 if (U_FAILURE(error)) {
620 fprintf(stdout, "Collator attribute setting failed:");
621 fprintf(stdout, u_errorName(error));
622 goto CLOSERULES;
623 return;
624 }
625
626 serialize("Rule-based", TRUE);
627 ucol_close(COLLATOR_);
628
629CLOSERULES :
630 if (options[4].doesOccur) {
631 filename[dirlength] = 0;
632 fclose(OUTPUT_);
633 }
634 }
635}
636
637/**
638* Parse for enum values.
639* Note this only works for positive enum values.
640* @param enumarray array containing names of the enum values in string and
641* their corresponding value.
642* declared enum value.
643* @param str string to be parsed
644* @return corresponding integer enum value or -1 if value is not found.
645*/
646int parseEnums(const EnumNameValuePair enumarray[], const char *str)
647{
648 const char *enumname = enumarray[0].name;
649 int result = atoi(str);
650 if (result == 0 && str[0] != '0') {
651 while (strcmp(enumname, str) != 0) {
652 // checking for multiple enum names sharing the same values
653 enumname = strstr(enumname, str);
654 if (enumname != NULL) {
655 int size = strchr(enumname, '|') - enumname;
656 if (size < 0) {
657 size = strlen(enumname);
658 }
659 if (size == (int)strlen(str)) {
660 return enumarray[result].value;
661 }
662 }
663 result ++;
664 if (&(enumarray[result]) == NULL) {
665 return -1;
666 }
667 enumname = enumarray[result].name;
668 }
669 }
670 return -1;
671}
672
673/**
674* Parser for attribute name value pair
675*/
676void parseAttributes() {
677 char str[32];
678 const char *pname = options[6].value;
679 const char *pend = options[6].value + strlen(options[6].value);
680 const char *pvalue;
681
682 while (pname < pend) {
683 pvalue = strchr(pname, '=');
684 if (pvalue == NULL) {
685 fprintf(stdout,
686 "No matching value found for attribute argument %s\n",
687 pname);
688 return;
689 }
690 int count = pvalue - pname;
691 strncpy(str, pname, count);
692 str[count] = 0;
693
694 int name = parseEnums(ATTRIBUTE_NAME_, str);
695 if (name == -1) {
696 fprintf(stdout, "Attribute name not found: %s\n", str);
697 return;
698 }
699
700 pvalue ++;
701 // getting corresponding enum value
702 pname = strchr(pvalue, ',');
703 if (pname == NULL) {
704 pname = pend;
705 }
706 count = pname - pvalue;
707 strncpy(str, pvalue, count);
708 str[count] = 0;
709 int value = parseEnums(ATTRIBUTE_VALUE_, str);
710 if (value == -1) {
711 fprintf(stdout, "Attribute value not found: %s\n", str);
712 return;
713 }
714 ATTRIBUTE_[name] = (UColAttributeValue)value;
715 pname ++;
716 }
717}
718
719/**
720* Checks if the locale argument is a base language
721* @param locale to be checked
722* @return TRUE if it is a base language
723*/
724inline UBool checkLocaleForLanguage(const char *locale)
725{
726 return strlen(locale) <= 2;
727}
728
729/**
730* Converts a UChar array into its string form "xxxx xxxx"
731* @param ch array of UChar characters
732* @param count number of UChar characters
733*/
734void outputUChar(UChar ch[], int count)
735{
736 for (int i = 0; i < count; i ++) {
737 fprintf(OUTPUT_, "%04X ", ch[i]);
738 }
739}
740
741/**
742* If it is a primary difference returns -1 or 1.
743* If it is a secondary difference returns -2 or 2.
744* If it is a tertiary difference returns -3 or 3.
745* If equals returns 0.
746*/
747int compareSortKey(const void *elem1, const void *elem2)
748{
749 // compare the 2 script element sort key
750 UChar *ch1 = ((ScriptElement *)elem1)->ch;
751 UChar *ch2 = ((ScriptElement *)elem2)->ch;
752 int size1 = ((ScriptElement *)elem1)->count;
753 int size2 = ((ScriptElement *)elem2)->count;
754 UErrorCode error = U_ZERO_ERROR;
755
756 ucol_setStrength(COLLATOR_, UCOL_PRIMARY);
757 int result = ucol_strcoll(COLLATOR_, ch1, size1, ch2, size2);
758 if (result == 0) {
759 ucol_setStrength(COLLATOR_, UCOL_SECONDARY);
760 result = ucol_strcoll(COLLATOR_, ch1, size1, ch2, size2);
761 if (result == 0) {
762 ucol_setStrength(COLLATOR_, UCOL_TERTIARY);
763 result = ucol_strcoll(COLLATOR_, ch1, size1, ch2, size2);
764 if (result < 0) {
765 return -3;
766 }
767 if (result > 0) {
768 return 3;
769 }
770 }
771 if (result < 0) {
772 return -2;
773 }
774 if (result > 0) {
775 return 2;
776 }
777 }
778 return result;
779}
780
781/**
782* Output serialized script elements
783* @param element the element to output
784* @param compare the comparison with the previous element
785* @param expansion flags TRUE if element has an expansion
786*/
787void outputScriptElem(ScriptElement &element, int compare, UBool expansion)
788{
789 switch (compare) {
790 case 0:
791 if (expansion) {
792 fprintf(OUTPUT_, "<tr><td class='eq' title='[");
793 }
794 else {
795 fprintf(OUTPUT_, "<tr><td class='q' title='[");
796 }
797 break;
798 case -1:
799 if (expansion) {
800 fprintf(OUTPUT_, "<tr><td class='ep' title='[");
801 }
802 else {
803 fprintf(OUTPUT_, "<tr><td class='p' title='[");
804 }
805 break;
806 case -2:
807 if (expansion) {
808 fprintf(OUTPUT_, "<tr><td class='es' title='[");
809 }
810 else {
811 fprintf(OUTPUT_, "<tr><td class='s' title='[");
812 }
813 break;
814 default:
815 if (expansion) {
816 fprintf(OUTPUT_, "<tr><td class='et' title='[");
817 }
818 else {
819 fprintf(OUTPUT_, "<tr><td class='t' title='[");
820 }
821 }
822
823 uint8_t sortkey[32];
824 ucol_setStrength(COLLATOR_, UCOL_TERTIARY);
825 ucol_getSortKey(COLLATOR_, element.ch, element.count, sortkey, 32);
826 int i = 0;
827 while (sortkey[i] != 0) {
828 if (sortkey[i] == 1) {
829 fprintf(OUTPUT_, " | ");
830 }
831 else {
832 fprintf(OUTPUT_, "%02x", sortkey[i]);
833 }
834
835 i ++;
836 }
837
838 fprintf(OUTPUT_, "]'>");
839
840 UErrorCode error = U_ZERO_ERROR;
841 char utf8[64];
842 UChar nfc[32];
843 int32_t length = unorm_normalize(element.ch, element.count, UNORM_NFC, 0, nfc,
844 32, &error);
845 if (U_FAILURE(error)) {
846 fprintf(stdout, "Error normalizing contractions to NFC\n");
847 }
848 u_strToUTF8(utf8, 64, &length, nfc, length, &error);
849 if (U_FAILURE(error)) {
850 fprintf(stdout, "Error converting UChar to utf8\n");
851 return;
852 }
853
854 fprintf(OUTPUT_, "%s<br>", utf8);
855 fprintf(OUTPUT_, "<tt>");
856 outputUChar(element.ch, element.count);
857
858 if (compare == 0) {
859 fprintf(OUTPUT_, "</tt></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>Q</td><td>");
860 }
861 else if (compare == -1) {
862 fprintf(OUTPUT_, "</tt></td><td>P</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>");
863 }
864 else if (compare == -2) {
865 fprintf(OUTPUT_, "</tt></td><td>&nbsp;</td><td>S</td><td>&nbsp;</td><td>&nbsp;</td><td>");
866 }
867 else if (compare == -3) {
868 fprintf(OUTPUT_, "</tt></td><td>&nbsp;</td><td>&nbsp;</td><td>T</td><td>&nbsp;</td><td>");
869 }
870
871 i = 0;
872 while (i < element.count) {
873 char str[128];
874 UChar32 codepoint;
875 UTF_NEXT_CHAR(element.ch, i, element.count, codepoint);
876 int32_t temp = u_charName(codepoint, U_UNICODE_CHAR_NAME, str, 128,
877 &error);
878 if (U_FAILURE(error)) {
879 fprintf(stdout, "Error getting character name\n");
880 return;
881 }
882 if (element.tailored) {
883 fprintf(OUTPUT_, "<b>");
884 }
885 fprintf(OUTPUT_, "%s", str);
886 if (element.tailored) {
887 fprintf(OUTPUT_, " *</b>");
888 }
889 if (i < element.count) {
890 fprintf(OUTPUT_, "<br>\n");
891 }
892 }
893
894 fprintf(OUTPUT_, "</td></tr>\n");
895}
896
897/**
898* Checks if codepoint belongs to scripts
899* @param script list
900* @param scriptcount number of scripts
901* @param codepoint to test
902* @return TRUE if codepoint belongs to scripts
903*/
904UBool checkInScripts(UScriptCode script[], int scriptcount,
905 UChar32 codepoint)
906{
907 UErrorCode error = U_ZERO_ERROR;
908 for (int i = 0; i < scriptcount; i ++) {
909 if (script[i] == USCRIPT_HAN && options[10].doesOccur) {
910 if ((codepoint >= 0x2E80 && codepoint <= 0x2EE4) ||
911 (codepoint >= 0x2A672 && codepoint <= 0x2A6D6)) {
912 // reduce han
913 return TRUE;
914 }
915 }
916 else if (uscript_getScript(codepoint, &error) == script[i]) {
917 return TRUE;
918 }
919 if (U_FAILURE(error)) {
920 fprintf(stdout, "Error checking character in scripts\n");
921 return FALSE;
922 }
923 }
924 return FALSE;
925}
926
927/**
928* Checks if the set of codepoints belongs to the script
929* @param script list
930* @param scriptcount number of scripts
931* @param scriptelem
932* @return TRUE if all codepoints belongs to the script
933*/
934inline UBool checkInScripts(UScriptCode script[], int scriptcount,
935 ScriptElement scriptelem)
936{
937 int i = 0;
938 while (i < scriptelem.count) {
939 UChar32 codepoint;
940 UTF_NEXT_CHAR(scriptelem.ch, i, scriptelem.count, codepoint);
941 UErrorCode error = U_ZERO_ERROR;
942 if (checkInScripts(script, scriptcount, codepoint)) {
943 return TRUE;
944 }
945 }
946 return FALSE;
947}
948
949/**
950* Gets the script elements and contractions belonging to the script
951* @param elems output list
952* @param locale locale
953* @return number of script elements
954* Add by Richard
955*/
956int getScriptElementsFromExemplars(ScriptElement scriptelem[], const char* locale) {
957 UErrorCode error = U_ZERO_ERROR;
958 UChar32 codepoint = 0;
959
960 UResourceBundle* ures = ures_open(NULL, locale, &error);
961 if (U_FAILURE(error)) {
962 fprintf(stdout, "Can not find resource bundle for locale: %s\n", locale);
963 return -1;
964 }
965 int32_t length;
966 const UChar* exemplarChars = ures_getStringByKey(ures, "ExemplarCharacters", &length, &error);
967
968 if (U_FAILURE(error)) {
969 fprintf(stdout, "Can not find ExemplarCharacters in resource bundle\n");
970 return -1;
971 }
972
973 UChar* upperChars = new UChar[length*2];
974 if (upperChars == 0) {
975 fprintf(stdout, "Memory error\n");
976 return -1;
977 }
978
979 int32_t destLength = u_strToUpper(upperChars, length*2, exemplarChars, -1, locale, &error);
980 if (U_FAILURE(error)) {
981 fprintf(stdout, "Error when u_strToUpper() \n");
982 return -1;
983 }
984
985 UChar* pattern = new UChar[length + destLength + 10];
986 UChar left[2] = {0x005b, 0x0};
987 UChar right[2] = {0x005d, 0x0};
988 pattern = u_strcpy(pattern, left);
989 pattern = u_strcat(pattern, exemplarChars);
990 pattern = u_strcat(pattern, upperChars);
991 pattern = u_strcat(pattern, right);
992
993 UnicodeSet * uniset = new UnicodeSet(UnicodeString(pattern), error);
994 if (U_FAILURE(error)) {
995 fprintf(stdout, "Can not open USet \n");
996 return -1;
997 }
998
999 UnicodeSetIterator* usetiter = new UnicodeSetIterator(*uniset);
1000
1001 int32_t count = 0;
1002
1003 while (usetiter -> next()) {
1004 if (usetiter -> isString()) {
1005 UnicodeString strItem = usetiter -> getString();
1006
1007 scriptelem[count].count = 0;
1008 for (int i = 0; i < strItem.length(); i++) {
1009 codepoint = strItem.char32At(i);
1010 UTF16_APPEND_CHAR_UNSAFE(scriptelem[count].ch,
1011 scriptelem[count].count, codepoint);
1012 scriptelem[count].tailored = FALSE;
1013 }
1014 } else {
1015 codepoint = usetiter -> getCodepoint();
1016 scriptelem[count].count = 0;
1017 UTF16_APPEND_CHAR_UNSAFE(scriptelem[count].ch,
1018 scriptelem[count].count, codepoint);
1019 scriptelem[count].tailored = FALSE;
1020 }
1021
1022 count++;
1023 }
1024
1025 return count;
1026}
1027
1028/**
1029* Gets the script elements and contractions belonging to the script
1030* @param script list
1031* @param scriptcount number of scripts
1032* @param elems output list
1033* @return number of script elements
1034*/
1035int getScriptElements(UScriptCode script[], int scriptcount,
1036 ScriptElement scriptelem[])
1037{
1038 UErrorCode error = U_ZERO_ERROR;
1039 UChar32 codepoint = 0;
1040 int count = 0;
1041 while (codepoint <= UCHAR_MAX_VALUE) {
1042 if (checkInScripts(script, scriptcount, codepoint)) {
1043 scriptelem[count].count = 0;
1044 UTF16_APPEND_CHAR_UNSAFE(scriptelem[count].ch,
1045 scriptelem[count].count, codepoint);
1046 scriptelem[count].tailored = FALSE;
1047 count ++;
1048 }
1049 if (U_FAILURE(error)) {
1050 fprintf(stdout, "Error determining codepoint in script\n");
1051 return -1;
1052 }
1053 codepoint ++;
1054 }
1055
1056 const UChar *current = NULL;
1057 uint32_t strength = 0;
1058 uint32_t chOffset = 0;
1059 uint32_t chLen = 0;
1060 uint32_t exOffset = 0;
1061 uint32_t exLen = 0;
1062 uint32_t prefixOffset = 0;
1063 uint32_t prefixLen = 0;
1064 uint8_t specs = 0;
1065 UBool rstart = TRUE;
1066 UColTokenParser src;
1067 UColOptionSet opts;
1068 UParseError parseError;
1069
1070 int32_t rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, NULL, 0);
1071 src.source = (UChar *)malloc(sizeof(UChar) *
1072 (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE));
1073 rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, src.source,
1074 rulelength);
1075 src.current = src.source;
1076 src.end = src.source + rulelength;
1077 src.extraCurrent = src.end;
1078 src.extraEnd = src.end + UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
1079 src.opts = &opts;
1080
1081 /*
1082 ucol_tok_parseNextToken(&src, &strength, &chOffset,
1083 &chLen, &exOffset, &exLen,
1084 &prefixOffset, &prefixLen,
1085 &specs, rstart, &parseError,
1086 &error)
1087 */
1088 while ((current = ucol_tok_parseNextToken(&src, rstart, &parseError,
1089 &error)) != NULL) {
1090 // contractions handled here
1091 if (chLen > 1) {
1092 u_strncpy(scriptelem[count].ch, src.source + chOffset, chLen);
1093 scriptelem[count].count = chLen;
1094 if (checkInScripts(script, scriptcount, scriptelem[count])) {
1095 scriptelem[count].tailored = FALSE;
1096 count ++;
1097 }
1098 }
1099 rstart = FALSE;
1100 }
1101 if (U_FAILURE(error)) {
1102 fprintf(stdout, "Error parsing rules: %s\n", u_errorName(error));
1103 }
1104 // rule might have been reallocated, so delete this instead
1105 free(src.source);
1106 return count;
1107}
1108
1109int compareCodepoints(const void *elem1, const void *elem2)
1110{
1111 UChar *ch1 = ((ScriptElement *)elem1)->ch; // key
1112 UChar *ch2 = ((ScriptElement *)elem2)->ch;
1113 ch1[((ScriptElement *)elem1)->count] = 0;
1114 ch2[((ScriptElement *)elem2)->count] = 0;
1115
1116 // compare the 2 codepoints
1117 return u_strcmp(ch1, ch2);
1118}
1119
1120UBool hasSubNFD(ScriptElement &se, ScriptElement &key)
1121{
1122 UChar *ch1 = se.ch;
1123 UChar *ch2 = key.ch; // key
1124 ch1[se.count] = 0;
1125 ch2[key.count] = 0;
1126
1127 // compare the 2 codepoints
1128 if (u_strstr(ch1, ch2) != NULL) {
1129 return TRUE;
1130 }
1131
1132 // check the decomposition
1133 UChar norm[32];
1134 UErrorCode error = U_ZERO_ERROR;
1135 int size = unorm_normalize(ch1, se.count, UNORM_NFD, 0, norm, 32,
1136 &error);
1137 if (U_FAILURE(error)) {
1138 fprintf(stdout, "Error normalizing\n");
1139 }
1140 if (u_strstr(norm, ch2) != NULL) {
1141 return TRUE;
1142 }
1143 return FALSE;
1144}
1145
1146/**
1147* Marks tailored elements
1148* @param script list
1149* @param scriptcount number of scripts
1150* @param scriptelem script element list
1151* @param scriptelemlength size of the script element list
1152*/
1153void markTailored(UScriptCode script[], int scriptcount,
1154 ScriptElement scriptelem[], int scriptelemlength)
1155{
1156 int32_t rulelength;
1157 const UChar *rule = ucol_getRules(COLLATOR_, &rulelength);
1158
1159 const UChar *current = NULL;
1160 uint32_t strength = 0;
1161 uint32_t chOffset = 0;
1162 uint32_t chLen = 0;
1163 uint32_t exOffset = 0;
1164 uint32_t exLen = 0;
1165 uint32_t prefixOffset = 0;
1166 uint32_t prefixLen = 0;
1167 uint8_t specs = 0;
1168 UBool rstart = TRUE;
1169 UColTokenParser src;
1170 UColOptionSet opts;
1171 UParseError parseError;
1172
1173 src.opts = &opts;
1174 src.source = (UChar *)malloc(
1175 (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE) * sizeof(UChar));
1176 memcpy(src.source, rule, rulelength * sizeof(UChar));
1177 src.current = src.source;
1178 src.end = (UChar *)src.source + rulelength;
1179 src.extraCurrent = src.end;
1180 src.extraEnd = src.end + UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
1181
1182 UErrorCode error = U_ZERO_ERROR;
1183
1184 while ((current = ucol_tok_parseNextToken(&src, rstart, &parseError,
1185 &error)) != NULL) {
1186 if (chLen >= 1 && strength != UCOL_TOK_RESET) {
1187 // skipping the reset characters and non useful stuff.
1188 ScriptElement se;
1189 u_strncpy(se.ch, src.source + chOffset, chLen);
1190 se.count = chLen;
1191
1192 if (checkInScripts(script, scriptcount, se)) {
1193 /*
1194 ScriptElement *tse = (ScriptElement *)bsearch(&se, scriptelem,
1195 scriptelemlength,
1196 sizeof(ScriptElement),
1197 compareCodepoints);
1198 */
1199 for (int i = 0; i < scriptelemlength; i ++) {
1200 if (!scriptelem[i].tailored &&
1201 hasSubNFD(scriptelem[i], se)) {
1202 scriptelem[i].tailored = TRUE;
1203 }
1204 }
1205 }
1206 }
1207 rstart = FALSE;
1208 }
1209 free(src.source);
1210 if (U_FAILURE(error)) {
1211 fprintf(stdout, "Error parsing rules\n");
1212 }
1213}
1214
1215/**
1216* Checks if the collation iterator has more than 1 collation element
1217* @parem coleiter collation element iterator
1218* @return TRUE if collation iterator has more than 1 collation element
1219*/
1220UBool hasExpansions(UCollationElements *coleiter)
1221{
1222 UErrorCode error = U_ZERO_ERROR;
1223 int32_t ce = ucol_next(coleiter, &error);
1224 int count = 0;
1225
1226 if (U_FAILURE(error)) {
1227 fprintf(stdout, "Error getting next collation element\n");
1228 }
1229 while (ce != UCOL_NULLORDER) {
1230 if ((UCOL_PRIMARYORDER(ce) != 0) && !isContinuation(ce)) {
1231 count ++;
1232 if (count == 2) {
1233 return TRUE;
1234 }
1235 }
1236 ce = ucol_next(coleiter, &error);
1237 if (U_FAILURE(error)) {
1238 fprintf(stdout, "Error getting next collation element\n");
1239 }
1240 }
1241 return FALSE;
1242}
1243
1244/**
1245* Prints the footer for index.html
1246* @param file output file
1247*/
1248void outputHTMLFooter()
1249{
1250 fprintf(OUTPUT_, "</table>\n");
1251 fprintf(OUTPUT_, "</body>\n");
1252 fprintf(OUTPUT_, "</html>\n");
1253}
1254
1255/**
1256* Serialize the codepoints from start to end into an html file.
1257* Arranging them into ascending collation order.
1258* @param script code list
1259* @param scriptcount number of scripts
1260*/
1261//void serializeScripts(UScriptCode script[], int scriptcount)
1262//Richard
1263void serializeScripts(UScriptCode script[], int scriptcount, const char* locale = NULL)
1264{
1265 UErrorCode error = U_ZERO_ERROR;
1266
1267 ScriptElement *scriptelem =
1268 (ScriptElement *)malloc(sizeof(ScriptElement) * 0x20000);
1269 if (scriptelem == NULL) {
1270 fprintf(stdout, "Memory error\n");
1271 return;
1272 }
1273 int count = 0;
1274 if(locale) {
1275 count = getScriptElementsFromExemplars(scriptelem, locale);
1276 } else {
1277 count = getScriptElements(script, scriptcount, scriptelem);
1278 }
1279
1280 // Sort script elements using Quicksort algorithm:
1281 qsort(scriptelem, count, sizeof(ScriptElement), compareCodepoints);
1282 markTailored(script, scriptcount, scriptelem, count);
1283 // Sort script elements using Quicksort algorithm:
1284 qsort(scriptelem, count, sizeof(ScriptElement), compareSortKey);
1285
1286 UCollationElements* coleiter = ucol_openElements(COLLATOR_,
1287 scriptelem[0].ch,
1288 scriptelem[0].count,
1289 &error);
1290 if (U_FAILURE(error)) {
1291 fprintf(stdout, "Error creating collation element iterator\n");
1292 return;
1293 }
1294
1295 outputScriptElem(scriptelem[0], -1, hasExpansions(coleiter));
1296 for (int i = 0; i < count - 1; i ++) {
1297 ucol_setText(coleiter, scriptelem[i + 1].ch, scriptelem[i + 1].count,
1298 &error);
1299 if (U_FAILURE(error)) {
1300 fprintf(stdout, "Error setting text in collation element iterator\n");
1301 return;
1302 }
1303 outputScriptElem(scriptelem[i + 1],
1304 compareSortKey(scriptelem + i, scriptelem + i + 1),
1305 hasExpansions(coleiter));
1306 }
1307 free(scriptelem);
1308 outputHTMLFooter();
1309}
1310
1311/**
1312* Prints the header for the html
1313* @param locale name
1314* @param script
1315* @param scriptcount number of scripts
1316*/
1317void outputHTMLHeader(const char *locale, UScriptCode script[],
1318 int scriptcount)
1319{
1320 fprintf(OUTPUT_, "<html>\n");
1321 fprintf(OUTPUT_, "<head>\n");
1322 fprintf(OUTPUT_, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
1323 fprintf(OUTPUT_, "<meta http-equiv=\"Content-Language\" content=\"en-us\">\n");
1324 fprintf(OUTPUT_, "<link rel=\"stylesheet\" href=\"charts.css\" type=\"text/css\">\n");
1325 fprintf(OUTPUT_, "<title>ICU Collation charts</title>\n");
1326 fprintf(OUTPUT_, "<base target=\"main\">\n");
1327 fprintf(OUTPUT_, "</head>\n");
1328
1329 fprintf(OUTPUT_, "<body bgcolor=#FFFFFF>\n");
1330 fprintf(OUTPUT_, "<!--\n");
1331 fprintf(OUTPUT_, "This file contains sorted characters in ascending order according to the locale stated\n");
1332 fprintf(OUTPUT_, "If the character is in red, it is tailored in the collation rules.\n");
1333 fprintf(OUTPUT_, "Background colours have certain meanings:\n");
1334 fprintf(OUTPUT_, "White - equals the previous character\n");
1335 fprintf(OUTPUT_, "dark blue - primary greater than the previous character\n");
1336 fprintf(OUTPUT_, "blue - secondary greater than the previous character\n");
1337 fprintf(OUTPUT_, "light blue - tertiary greater than the previous character\n");
1338 fprintf(OUTPUT_, "--!>\n");
1339
1340 fprintf(OUTPUT_, "<table border=0>\n");
1341 UChar displayname[64];
1342 UErrorCode error = U_ZERO_ERROR;
1343 int32_t size = uloc_getDisplayName(locale, "en_US", displayname, 64, &error);
1344 char utf8displayname[128];
1345 if (U_FAILURE(error)) {
1346 utf8displayname[0] = 0;
1347 }
1348 else {
1349 int32_t utf8size = 0;
1350 u_strToUTF8(utf8displayname, 128, &utf8size, displayname, size, &error);
1351 }
1352
1353 fprintf(OUTPUT_, "<tr><th>Locale</th><td class='noborder'>%s</td></tr>\n", utf8displayname);
1354 fprintf(OUTPUT_, "<tr><th>Script(s)</th>");
1355 fprintf(OUTPUT_, "<td class='noborder'>");
1356 for (int i = 0; i < scriptcount; i ++) {
1357 fprintf(OUTPUT_, "%s", uscript_getName(script[i]));
1358 if (i + 1 != scriptcount) {
1359 fprintf(OUTPUT_, ", ");
1360 }
1361 }
1362 fprintf(OUTPUT_, "</td></tr>\n");
1363
1364 fprintf(OUTPUT_, "<tr><th>Rules</th><td class='noborder'><a href=http://oss.software.ibm.com/cvs/icu/~checkout~/icu/source/data/locales/%s.txt>%s.txt</a></td></tr>\n", locale, locale);
1365
1366 UVersionInfo version;
1367 ucol_getVersion(COLLATOR_, version);
1368 fprintf(OUTPUT_, "<tr><th>Collator version</th><td class='noborder'>%d.%d.%d.%d</td></tr>\n",
1369 version[0], version[1], version[2], version[3]);
1370
1371 UColAttribute attr = UCOL_FRENCH_COLLATION;
1372 while (attr < UCOL_ATTRIBUTE_COUNT) {
1373 UColAttributeValue value = ucol_getAttribute(COLLATOR_, attr, &error);
1374 if (U_FAILURE(error)) {
1375 fprintf(stdout, "Error getting attribute\n");
1376 return;
1377 }
1378 if (value != UCOL_DEFAULT) {
1379 if (attr == UCOL_FRENCH_COLLATION && value != UCOL_OFF) {
1380 fprintf(OUTPUT_, "<tr><th>French Collation</th><td class='noborder'>on, code %d</td></tr>\n", value);
1381 }
1382 if (attr == UCOL_ALTERNATE_HANDLING && value != UCOL_NON_IGNORABLE) {
1383 fprintf(OUTPUT_, "<tr><th>Alternate Handling</th><td class='noborder'>shifted, code%d</td></tr>\n", value);
1384 }
1385 if (attr == UCOL_CASE_FIRST && value != UCOL_OFF) {
1386 fprintf(OUTPUT_, "<tr><th>Case First</th><td class='noborder'>on, code %d</td></tr>\n", value);
1387 }
1388 if (attr == UCOL_CASE_LEVEL && value != UCOL_OFF) {
1389 fprintf(OUTPUT_, "<tr><th>Case Level</th><td class='noborder'>on, code %d</td></tr>\n", value);
1390 }
1391 if (attr == UCOL_NORMALIZATION_MODE && value != UCOL_OFF) {
1392 fprintf(OUTPUT_, "<tr><th>Normalization</th><td class='noborder'>on, code %d</td></tr>\n", value);
1393 }
1394 if (attr == UCOL_STRENGTH && value != UCOL_TERTIARY) {
1395 fprintf(OUTPUT_, "<tr><th>Strength</th><td class='noborder'>code %d</td></tr>\n", value);
1396 }
1397 if (attr == UCOL_HIRAGANA_QUATERNARY_MODE && value != UCOL_OFF) {
1398 fprintf(OUTPUT_, "<tr><th>Hiragana Quaternary</th><td class='noborder'>on, code %d</td></tr>\n", value);
1399 }
1400 }
1401 attr = (UColAttribute)(attr + 1);
1402 }
1403
1404 // Get UNIX-style time and display as number and string.
1405 time_t ltime;
1406 time( &ltime );
1407 fprintf(OUTPUT_, "<tr><th>Date Generated</th><td class='noborder'>%s</td></tr>", ctime(&ltime));
1408
1409 fprintf(OUTPUT_, "</table>\n");
1410
1411 fprintf(OUTPUT_, "<p><a href=help.html>How to read the table</a><br>\n");
1412 fprintf(OUTPUT_, "<a href=http://www.jtcsv.com/cgi-bin/icu-bugs/ target=new>Submit a bug</a></p>\n");
1413 fprintf(OUTPUT_, "\n<table>\n");
1414 fprintf(OUTPUT_, "\n<tr><th>Codepoint</th><th>P</th><th>S</th><th>T</th><th>Q</th><th>Name</th></tr>\n");
1415}
1416
1417/**
1418* Prints the header for index.html
1419* @param file output file
1420*/
1421void outputListHTMLHeader(FILE *file)
1422{
1423 fprintf(file, "<html>\n");
1424 fprintf(file, "<head>\n");
1425 fprintf(file, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
1426 fprintf(file, "<meta http-equiv=\"Content-Language\" content=\"en-us\">\n");
1427 fprintf(file, "<title>ICU Collation Charts</title>\n");
1428 fprintf(file, "<base target=\"main\">\n");
1429 fprintf(file, "</head>\n");
1430 fprintf(file, "<body bgcolor=#FFFFFF>\n");
1431 fprintf(file, "<h2 align=center>ICU Collation Charts</h2>\n");
1432 fprintf(file, "<p align=center>\n");
1433 fprintf(file, "<a href=http://www.unicode.org/charts/collation/ target=new>UCA Charts</a><br>");
1434}
1435
1436/**
1437* Prints the footer for index.html
1438* @param file output file
1439*/
1440void outputListHTMLFooter(FILE *file)
1441{
1442 fprintf(file, "</p>\n");
1443 fprintf(file, "<center><image src=http://oss.software.ibm.com/icu/images/w24.gif></center>\n");
1444 fprintf(file, "</body>\n");
1445 fprintf(file, "</html>\n");
1446}
1447
1448/**
1449* Gets all scripts and serialize their codepoints into an html file.
1450*/
1451void serializeScripts() {
1452 char filename[128];
1453 int dirlength = 0;
1454
1455 if (options[4].doesOccur) {
1456 strcpy(filename, options[4].value);
1457 dirlength = appendDirSeparator(filename);
1458 } else {
1459 filename[0] = 0;
1460 }
1461
1462 const char *locale;
1463 int32_t localelist = 0;
1464 int32_t localesize;
1465
1466 localesize = ucol_countAvailable();
1467 locale = ucol_getAvailable(localelist);
1468
1469 strcat(filename, "list.html");
1470 FILE *list = fopen(filename, "w");
1471 filename[dirlength] = 0;
1472 if (list == NULL) {
1473 fprintf(stdout, "Cannot open file: %s\n", filename);
1474 return;
1475 }
1476
1477 outputListHTMLHeader(list);
1478 fprintf(list, "<blockquote>\n");
1479 while (TRUE) {
1480 UErrorCode error = U_ZERO_ERROR;
1481 COLLATOR_ = ucol_open(locale, &error);
1482 if (U_FAILURE(error)) {
1483 fprintf(stdout, "Collator creation failed:");
1484 fprintf(stdout, u_errorName(error));
1485 return;
1486 }
1487 if ((error != U_USING_FALLBACK_WARNING && // not tailored
1488 error != U_USING_DEFAULT_WARNING) ||
1489 checkLocaleForLanguage(locale)) {
1490 fprintf(list, "<a href=%s.html>%s</a> ", locale, locale);
1491 setAttributes(COLLATOR_, &error);
1492 if (U_FAILURE(error)) {
1493 fprintf(stdout, "Collator attribute setting failed:");
1494 fprintf(stdout, u_errorName(error));
1495 return;
1496 }
1497
1498 UScriptCode scriptcode[32];
1499 uint32_t scriptcount = uscript_getCode(locale, scriptcode, 32,
1500 &error);
1501 if (U_FAILURE(error)) {
1502 fprintf(stdout, "Error getting lcale scripts\n");
1503 return;
1504 }
1505
1506 strcat(filename, locale);
1507 strcat(filename, ".html");
1508 OUTPUT_ = fopen(filename, "w");
1509 if (OUTPUT_ == NULL) {
1510 fprintf(stdout, "Cannot open file:%s\n", filename);
1511 return;
1512 }
1513 outputHTMLHeader(locale, scriptcode, scriptcount);
1514 fprintf(stdout, "%s\n", locale);
1515
1516 if(options[12].doesOccur) {
1517 // use whole scripts
1518 serializeScripts(scriptcode, scriptcount);
1519 } else {
1520 // use exemplar chars
1521 serializeScripts(scriptcode, scriptcount, locale);
1522 }
1523 fclose(OUTPUT_);
1524 }
1525 ucol_close(COLLATOR_);
1526
1527 filename[dirlength] = 0;
1528 localelist ++;
1529 if (localelist == localesize) {
1530 break;
1531 }
1532 locale = ucol_getAvailable(localelist);
1533 }
1534 fprintf(list, "<br><a href=help.html>help</a><br>");
1535 fprintf(list, "</blockquote>\n");
1536 outputListHTMLFooter(list);
1537 fclose(list);
1538}
1539
1540/**
1541* Main -- process command line, read in and pre-process the test file,
1542* call other functions to do the actual tests.
1543*/
1544int main(int argc, char *argv[]) {
1545
1546 argc = u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]),
1547 options);
1548
1549 // error handling, printing usage message
1550 if (argc < 0) {
1551 fprintf(stdout, "error in command line argument: ");
1552 fprintf(stdout, argv[-argc]);
1553 fprintf(stdout, "\n");
1554 }
1555 if (argc < 0 || options[0].doesOccur || options[1].doesOccur) {
1556 fprintf(stdout, "Usage: dumpce options...\n"
1557 "--help\n"
1558 " Display this message.\n"
1559 "--locale name|all\n"
1560 " ICU locale to use. Default is en_US\n"
1561 "--serialize\n"
1562 " Serializes the collation elements in -locale or all locales available and outputs them into --outputdir/locale_ce.txt\n"
1563 "--destdir dir_name\n"
1564 " Path for outputing the serialized collation elements. Defaults to stdout if no defined\n"
1565 "--sourcedir dir_name\n"
1566 " Path for the input rule file for collation\n"
1567 "--attribute name=value,name=value...\n"
1568 " Pairs of attribute names and values for setting\n"
1569 "--rule filename\n"
1570 " Name of file containing the collation rules.\n"
1571 "--normalizaton mode\n"
1572 " UNormalizationMode mode to be used.\n"
1573 "--scripts\n"
1574 " Codepoints from all scripts are sorted and serialized.\n"
1575 "--reducehan\n"
1576 " Only 200 Han script characters will be displayed with the use of --scripts.\n"
1577 "--wholescripts\n"
1578 " Show collation order for whole scripts instead of just for exemplar characters of a locale\n\n");
1579
1580 fprintf(stdout, "Example to generate *.txt files : dumpce --serialize --locale af --destdir /temp --attribute UCOL_STRENGTH=UCOL_DEFAULT_STRENGTH,4=17\n\n");
1581 fprintf(stdout, "Example to generate *.html files for oss web display: dumpce --scripts --destdir /temp --reducehan\n");
1582 return argc < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
1583 }
1584
1585 OUTPUT_ = stdout;
1586 if (options[6].doesOccur) {
1587 fprintf(stdout, "attributes %s\n", options[6].value);
1588 parseAttributes();
1589 }
1590 if (options[3].doesOccur) {
1591 serialize();
1592 }
1593 if (options[9].doesOccur) {
1594 serializeScripts();
1595 }
1596 return 0;
1597}