michael@0: /******************************************************************** michael@0: * COPYRIGHT: michael@0: * Copyright (C) 2001-2011 IBM, Inc. All Rights Reserved. michael@0: * michael@0: ********************************************************************/ michael@0: /******************************************************************************** michael@0: * michael@0: * File dumpce.cpp michael@0: * michael@0: * Modification History: michael@0: * Name Date Description michael@0: * synwee May 31 2001 Creation michael@0: * michael@0: ********************************************************************************* michael@0: */ michael@0: michael@0: /** michael@0: * This program outputs the collation elements used for a requested tailoring. michael@0: * michael@0: * Usage: michael@0: * dumpce options... please check main function. michael@0: */ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "ucol_tok.h" michael@0: #include "cstring.h" michael@0: #include "uoptions.h" michael@0: #include "ucol_imp.h" michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /** michael@0: * Command line option variables. michael@0: * These global variables are set according to the options specified on the michael@0: * command line by the user. michael@0: */ michael@0: static UOption options[]={ michael@0: /* 00 */ UOPTION_HELP_H, michael@0: /* 01 */ UOPTION_HELP_QUESTION_MARK, michael@0: /* 02 */ {"locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0}, michael@0: /* 03 */ {"serialize", NULL, NULL, NULL, 'z', UOPT_NO_ARG, 0}, michael@0: /* 04 */ UOPTION_DESTDIR, michael@0: /* 05 */ UOPTION_SOURCEDIR, michael@0: /* 06 */ {"attribute", NULL, NULL, NULL, 'a', UOPT_REQUIRES_ARG, 0}, michael@0: /* 07 */ {"rule", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0}, michael@0: /* 08 */ {"normalization", NULL, NULL, NULL, 'n', UOPT_REQUIRES_ARG, 0}, michael@0: /* 09 */ {"scripts", NULL, NULL, NULL, 't', UOPT_NO_ARG, 0}, michael@0: /* 10 */ {"reducehan", NULL, NULL, NULL, 'e', UOPT_NO_ARG, 0}, michael@0: /* 11 */ UOPTION_VERBOSE, michael@0: /* 12 */ {"wholescripts", NULL, NULL, NULL, 'W', UOPT_NO_ARG, 0} michael@0: }; michael@0: michael@0: /** michael@0: * Collator used in this program michael@0: */ michael@0: static UCollator *COLLATOR_; michael@0: /** michael@0: * Output strea, used in this program michael@0: */ michael@0: static FILE *OUTPUT_; michael@0: michael@0: static UColAttributeValue ATTRIBUTE_[UCOL_ATTRIBUTE_COUNT] = { michael@0: UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT, michael@0: UCOL_DEFAULT, UCOL_DEFAULT, UCOL_DEFAULT, michael@0: }; michael@0: michael@0: typedef struct { michael@0: int value; michael@0: char *name; michael@0: } EnumNameValuePair; michael@0: michael@0: static const EnumNameValuePair ATTRIBUTE_NAME_[] = { michael@0: {UCOL_FRENCH_COLLATION, "UCOL_FRENCH_COLLATION"}, michael@0: {UCOL_ALTERNATE_HANDLING, "UCOL_ALTERNATE_HANDLING"}, michael@0: {UCOL_CASE_FIRST, "UCOL_CASE_FIRST"}, michael@0: {UCOL_CASE_LEVEL, "UCOL_CASE_LEVEL"}, michael@0: {UCOL_NORMALIZATION_MODE, michael@0: "UCOL_NORMALIZATION_MODE|UCOL_DECOMPOSITION_MODE"}, michael@0: {UCOL_STRENGTH, "UCOL_STRENGTH"}, michael@0: {UCOL_HIRAGANA_QUATERNARY_MODE, "UCOL_HIRAGANA_QUATERNARY_MODE"}, michael@0: {UCOL_NUMERIC_COLLATION, "UCOL_NUMERIC_COLLATION"}, michael@0: NULL michael@0: }; michael@0: michael@0: static const EnumNameValuePair ATTRIBUTE_VALUE_[] = { michael@0: {UCOL_PRIMARY, "UCOL_PRIMARY"}, michael@0: {UCOL_SECONDARY, "UCOL_SECONDARY"}, michael@0: {UCOL_TERTIARY, "UCOL_TERTIARY|UCOL_DEFAULT_STRENGTH"}, michael@0: {UCOL_QUATERNARY, "UCOL_QUATERNARY"}, michael@0: {UCOL_IDENTICAL, "UCOL_IDENTICAL"}, michael@0: {UCOL_OFF, "UCOL_OFF"}, michael@0: {UCOL_ON, "UCOL_ON"}, michael@0: {UCOL_SHIFTED, "UCOL_SHIFTED"}, michael@0: {UCOL_NON_IGNORABLE, "UCOL_NON_IGNORABLE"}, michael@0: {UCOL_LOWER_FIRST, "UCOL_LOWER_FIRST"}, michael@0: {UCOL_UPPER_FIRST, "UCOL_UPPER_FIRST"}, michael@0: NULL michael@0: }; michael@0: michael@0: typedef struct { michael@0: UChar ch[32]; michael@0: int count; // number of codepoint michael@0: UBool tailored; michael@0: } ScriptElement; michael@0: michael@0: /** michael@0: * Writes the hexadecimal of a null-terminated array of codepoints into a michael@0: * file michael@0: * @param f UFILE instance to store michael@0: * @param c codepoints array michael@0: */ michael@0: void serialize(FILE *f, const UChar *c) michael@0: { michael@0: UChar cp = *(c ++); michael@0: michael@0: fprintf(f, " %04x", cp); michael@0: michael@0: while (*c != 0) { michael@0: cp = *(c ++); michael@0: fprintf(f, " %04x", cp); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Writes the hexadecimal of a non-null-terminated array of codepoints into a michael@0: * file michael@0: * @param f UFILE instance to store michael@0: * @param c codepoints array michael@0: * @param l codepoints array length michael@0: */ michael@0: void serialize(FILE *f, const UChar *c, int l) michael@0: { michael@0: int count = 1; michael@0: UChar cp = *(c ++); michael@0: michael@0: fprintf(f, " %04x", cp); michael@0: michael@0: while (count < l) { michael@0: cp = *(c ++); michael@0: fprintf(f, " %04x", cp); michael@0: count ++; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Sets the iterator to the argument string and outputs the collation elements. michael@0: * @param f file output stream michael@0: * @param iter collation element iterator michael@0: */ michael@0: void serialize(FILE *f, UCollationElements *iter) { michael@0: const UChar *codepoint = iter->iteratordata_.string; michael@0: // unlikely that sortkeys will be over this size michael@0: uint8_t sortkey[64]; michael@0: uint8_t *psortkey = sortkey; michael@0: int sortkeylength = 0; michael@0: michael@0: if (iter->iteratordata_.flags & UCOL_ITER_HASLEN) { michael@0: serialize(f, codepoint, iter->iteratordata_.endp - codepoint); michael@0: sortkeylength = ucol_getSortKey(iter->iteratordata_.coll, codepoint, michael@0: iter->iteratordata_.endp - codepoint, sortkey, 64); michael@0: } michael@0: else { michael@0: serialize(f, codepoint); michael@0: sortkeylength = ucol_getSortKey(iter->iteratordata_.coll, codepoint, michael@0: -1, sortkey, 64); michael@0: } michael@0: if (options[11].doesOccur) { michael@0: serialize(stdout, codepoint); michael@0: fprintf(stdout, "\n"); michael@0: } michael@0: michael@0: fprintf(f, "; "); michael@0: michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: uint32_t ce = ucol_next(iter, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(f, "Error retrieving collation elements\n"); michael@0: return; michael@0: } michael@0: michael@0: while (TRUE) { michael@0: fprintf(f, "["); michael@0: if (UCOL_PRIMARYORDER(ce) != 0) { michael@0: fprintf(f, "%04x", UCOL_PRIMARYORDER(ce)); michael@0: } michael@0: fprintf(f, ","); michael@0: if (UCOL_SECONDARYORDER(ce) != 0) { michael@0: fprintf(f, " %02x", UCOL_SECONDARYORDER(ce)); michael@0: } michael@0: fprintf(f, ","); michael@0: if (UCOL_TERTIARYORDER(ce) != 0) { michael@0: fprintf(f, " %02x", UCOL_TERTIARYORDER(ce)); michael@0: } michael@0: fprintf(f, "] "); michael@0: michael@0: ce = ucol_next(iter, &error); michael@0: if (ce == UCOL_NULLORDER) { michael@0: break; michael@0: } michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error retrieving collation elements"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (sortkeylength > 64) { michael@0: fprintf(f, "Sortkey exceeds pre-allocated size"); michael@0: } michael@0: michael@0: fprintf(f, "["); michael@0: while (TRUE) { michael@0: fprintf(f, "%02x", *psortkey); michael@0: psortkey ++; michael@0: if ((*psortkey) == 0) { michael@0: break; michael@0: } michael@0: fprintf(f, " "); michael@0: } michael@0: fprintf(f, "]\n"); michael@0: } michael@0: michael@0: /** michael@0: * Serializes the contraction within the given argument rule michael@0: * @param f file output stream michael@0: * @param r rule michael@0: * @param rlen rule length michael@0: * @param contractionsonly flag to indicate if only contractions are to be michael@0: * output or all collation elements michael@0: * @param iter iterator to iterate over collation elements michael@0: */ michael@0: void serialize(FILE *f, UChar *rule, int rlen, UBool contractiononly, michael@0: UCollationElements *iter) { michael@0: const UChar *current = NULL; michael@0: uint32_t strength = 0; michael@0: uint32_t chOffset = 0; michael@0: uint32_t chLen = 0; michael@0: uint32_t exOffset = 0; michael@0: uint32_t exLen = 0; michael@0: uint32_t prefixOffset = 0; michael@0: uint32_t prefixLen = 0; michael@0: uint8_t specs = 0; michael@0: UBool rstart = TRUE; michael@0: UColTokenParser src; michael@0: UColOptionSet opts; michael@0: UParseError parseError; michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: michael@0: src.opts = &opts; michael@0: michael@0: src.source = rule; michael@0: src.current = rule; michael@0: src.end = rule + rlen; michael@0: src.extraCurrent = src.end; michael@0: src.extraEnd = src.end + UCOL_TOK_EXTRA_RULE_SPACE_SIZE; michael@0: michael@0: michael@0: while ((current = ucol_tok_parseNextToken(&src, rstart, &parseError, michael@0: &error)) != NULL) { michael@0: chOffset = src.parsedToken.charsOffset; michael@0: chLen = src.parsedToken.charsLen; michael@0: // contractions handled here michael@0: if (!contractiononly || chLen > 1) { michael@0: ucol_setText(iter, rule + chOffset, chLen, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error setting text in iterator\n"); michael@0: return; michael@0: } michael@0: serialize(f, iter); michael@0: } michael@0: rstart = FALSE; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Prints the attribute values in the argument collator into the output stream michael@0: * @param collator michael@0: */ michael@0: void outputAttribute(UCollator *collator, UErrorCode *error) michael@0: { michael@0: UColAttribute attribute = UCOL_FRENCH_COLLATION; michael@0: while (attribute < UCOL_ATTRIBUTE_COUNT) { michael@0: int count = 0; michael@0: while (TRUE) { michael@0: // getting attribute name michael@0: if (ATTRIBUTE_NAME_[count].value == attribute) { michael@0: fprintf(OUTPUT_, "%s = ", ATTRIBUTE_NAME_[count].name); michael@0: break; michael@0: } michael@0: count ++; michael@0: } michael@0: count = 0; michael@0: int attributeval = ucol_getAttribute(collator, attribute, error); michael@0: if (U_FAILURE(*error)) { michael@0: fprintf(stdout, "Failure in reading collator attribute\n"); michael@0: return; michael@0: } michael@0: while (TRUE) { michael@0: // getting attribute value michael@0: if (ATTRIBUTE_VALUE_[count].value == attributeval) { michael@0: fprintf(OUTPUT_, "%s\n", ATTRIBUTE_VALUE_[count].name); michael@0: break; michael@0: } michael@0: count ++; michael@0: } michael@0: attribute = (UColAttribute)(attribute + 1); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Prints the normalization mode in the argument collator into the output stream michael@0: * @param collator michael@0: */ michael@0: void outputNormalization(UCollator *collator) michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: int normmode = ucol_getAttribute(collator, UCOL_NORMALIZATION_MODE, &status); michael@0: int count = 0; michael@0: while (TRUE) { michael@0: // getting attribute name michael@0: if (ATTRIBUTE_VALUE_[count].value == normmode) { michael@0: break; michael@0: } michael@0: count ++; michael@0: } michael@0: fprintf(OUTPUT_, "NORMALIZATION MODE = %s\n", michael@0: ATTRIBUTE_VALUE_[count].name); michael@0: } michael@0: michael@0: /** michael@0: * Output the collation element belonging to the locale into a file michael@0: * @param locale string michael@0: * @param fullrules flag to indicate if only tailored collation elements are to michael@0: * be output or all collation elements michael@0: */ michael@0: void serialize(const char *locale, UBool tailoredonly) { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: UChar str[128]; michael@0: int strlen = 0; michael@0: michael@0: fprintf(OUTPUT_, "# This file contains the serialized collation elements\n"); michael@0: fprintf(OUTPUT_, "# as of the collation version indicated below.\n"); michael@0: fprintf(OUTPUT_, "# Data format: xxxx xxxx..; [yyyy, yy, yy] [yyyy, yy, yy] ... [yyyy, yy, yy] [zz zz..\n"); michael@0: fprintf(OUTPUT_, "# where xxxx are codepoints in hexadecimals,\n"); michael@0: fprintf(OUTPUT_, "# yyyyyyyy are the corresponding\n"); michael@0: fprintf(OUTPUT_, "# collation elements in hexadecimals\n"); michael@0: fprintf(OUTPUT_, "# and zz are the sortkey values in hexadecimals\n"); michael@0: michael@0: fprintf(OUTPUT_, "\n# Collator information\n"); michael@0: michael@0: fprintf(OUTPUT_, "\nLocale: %s\n", locale); michael@0: fprintf(stdout, "Locale: %s\n", locale); michael@0: UVersionInfo version; michael@0: ucol_getVersion(COLLATOR_, version); michael@0: fprintf(OUTPUT_, "Version number: %d.%d.%d.%d\n", michael@0: version[0], version[1], version[2], version[3]); michael@0: outputAttribute(COLLATOR_, &error); michael@0: outputNormalization(COLLATOR_); michael@0: michael@0: UCollationElements *iter = ucol_openElements(COLLATOR_, str, strlen, michael@0: &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error creating iterator\n"); michael@0: return; michael@0: } michael@0: michael@0: if (!tailoredonly) { michael@0: fprintf(OUTPUT_, "\n# Range of unicode characters\n\n"); michael@0: UChar32 codepoint = 0; michael@0: while (codepoint <= UCHAR_MAX_VALUE) { michael@0: if (u_isdefined(codepoint)) { michael@0: strlen = 0; michael@0: UTF16_APPEND_CHAR_UNSAFE(str, strlen, codepoint); michael@0: str[strlen] = 0; michael@0: ucol_setText(iter, str, strlen, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error setting text in iterator\n"); michael@0: return; michael@0: } michael@0: serialize(OUTPUT_, iter); michael@0: } michael@0: codepoint ++; michael@0: } michael@0: } michael@0: michael@0: UChar ucarules[0x10000]; michael@0: UChar *rules; michael@0: int32_t rulelength = 0; michael@0: rules = ucarules; michael@0: michael@0: if (tailoredonly) { michael@0: int32_t rulelength = 0; michael@0: const UChar *temp = ucol_getRules(COLLATOR_, &rulelength); michael@0: if (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE > 0x10000) { michael@0: rules = (UChar *)malloc(sizeof(UChar) * michael@0: (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE)); michael@0: } michael@0: memcpy(rules, temp, rulelength * sizeof(UChar)); michael@0: rules[rulelength] = 0; michael@0: fprintf(OUTPUT_, "\n# Tailorings\n\n"); michael@0: serialize(OUTPUT_, rules, rulelength, FALSE, iter); michael@0: if (rules != ucarules) { michael@0: free(rules); michael@0: } michael@0: } michael@0: else { michael@0: rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, ucarules, michael@0: 0x10000); michael@0: if (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE > 0x10000) { michael@0: rules = (UChar *)malloc(sizeof(UChar) * michael@0: (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE)); michael@0: rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, rules, michael@0: rulelength); michael@0: } michael@0: fprintf(OUTPUT_, "\n# Contractions\n\n"); michael@0: serialize(OUTPUT_, rules, rulelength, TRUE, iter); michael@0: if (rules != ucarules) { michael@0: free(rules); michael@0: } michael@0: } michael@0: michael@0: ucol_closeElements(iter); michael@0: } michael@0: michael@0: /** michael@0: * Sets the collator with the attribute values michael@0: * @param collator michael@0: * @param error status michael@0: */ michael@0: void setAttributes(UCollator *collator, UErrorCode *error) michael@0: { michael@0: int count = 0; michael@0: while (count < UCOL_ATTRIBUTE_COUNT) { michael@0: if (ATTRIBUTE_[count] != UCOL_DEFAULT) { michael@0: ucol_setAttribute(collator, (UColAttribute)count, michael@0: ATTRIBUTE_[count], error); michael@0: if (U_FAILURE(*error)) { michael@0: return; michael@0: } michael@0: } michael@0: count ++; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Appends directory path with an ending seperator if necessary. michael@0: * @param path with enough space to append one seperator michael@0: * @return new directory path length michael@0: */ michael@0: int appendDirSeparator(char *dir) michael@0: { michael@0: int dirlength = strlen(dir); michael@0: char dirending = dir[dirlength - 1]; michael@0: if (dirending != U_FILE_SEP_CHAR) { michael@0: dir[dirlength] = U_FILE_SEP_CHAR; michael@0: dir[dirlength + 1] = 0; michael@0: return dirlength + 1; michael@0: } michael@0: return dirlength; michael@0: } michael@0: michael@0: /** michael@0: * Output the collation element into a file michael@0: */ michael@0: void serialize() { michael@0: char filename[128]; michael@0: int dirlength = 0; michael@0: michael@0: if (options[4].doesOccur) { michael@0: strcpy(filename, options[4].value); michael@0: dirlength = appendDirSeparator(filename); michael@0: } michael@0: michael@0: if (options[2].doesOccur) { michael@0: const char *locale = (char *)options[2].value; michael@0: int32_t localeindex = 0; michael@0: michael@0: if (strcmp(locale, "all") == 0) { michael@0: if (options[4].doesOccur) { michael@0: strcat(filename, "UCA.txt"); michael@0: OUTPUT_ = fopen(filename, "w"); michael@0: if (OUTPUT_ == NULL) { michael@0: fprintf(stdout, "Cannot open file:%s\n", filename); michael@0: return; michael@0: } michael@0: } michael@0: fprintf(stdout, "UCA\n"); michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: COLLATOR_ = ucol_open("en_US", &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator creation failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: goto CLOSEUCA; michael@0: return; michael@0: } michael@0: setAttributes(COLLATOR_, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator attribute setting failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: goto CLOSEUCA; michael@0: return; michael@0: } michael@0: michael@0: serialize("UCA", FALSE); michael@0: CLOSEUCA : michael@0: if (options[4].doesOccur) { michael@0: filename[dirlength] = 0; michael@0: fclose(OUTPUT_); michael@0: } michael@0: ucol_close(COLLATOR_); michael@0: localeindex = ucol_countAvailable() - 1; michael@0: fprintf(stdout, "Number of locales: %d\n", localeindex + 1); michael@0: locale = ucol_getAvailable(localeindex); michael@0: } michael@0: michael@0: while (TRUE) { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: COLLATOR_ = ucol_open(locale, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator creation failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: goto CLOSETAILOR; michael@0: return; michael@0: } michael@0: setAttributes(COLLATOR_, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator attribute setting failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: goto CLOSETAILOR; michael@0: return; michael@0: } michael@0: michael@0: if (options[4].doesOccur) { michael@0: strcat(filename, locale); michael@0: strcat(filename, ".txt"); michael@0: OUTPUT_ = fopen(filename, "w"); michael@0: if (OUTPUT_ == NULL) { michael@0: fprintf(stdout, "Cannot open file:%s\n", filename); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (options[3].doesOccur) { michael@0: serialize(locale, TRUE); michael@0: } michael@0: michael@0: ucol_close(COLLATOR_); michael@0: michael@0: CLOSETAILOR : michael@0: if (options[4].doesOccur) { michael@0: filename[dirlength] = 0; michael@0: fclose(OUTPUT_); michael@0: } michael@0: michael@0: localeindex --; michael@0: if (localeindex < 0) { michael@0: break; michael@0: } michael@0: locale = ucol_getAvailable(localeindex); michael@0: } michael@0: } michael@0: michael@0: if (options[7].doesOccur) { michael@0: char inputfilename[128] = ""; michael@0: // rules are to be used michael@0: if (options[5].doesOccur) { michael@0: strcpy(inputfilename, options[5].value); michael@0: appendDirSeparator(inputfilename); michael@0: } michael@0: strcat(inputfilename, options[7].value); michael@0: FILE *input = fopen(inputfilename, "r"); michael@0: if (input == NULL) { michael@0: fprintf(stdout, "Cannot open file:%s\n", filename); michael@0: return; michael@0: } michael@0: michael@0: char s[1024]; michael@0: UChar rule[1024]; michael@0: UChar *prule = rule; michael@0: int size = 1024; michael@0: // synwee TODO: make this part dynamic michael@0: while (fscanf(input, "%[^\n]s", s) != EOF) { michael@0: size -= u_unescape(s, prule, size); michael@0: prule = prule + u_strlen(prule); michael@0: } michael@0: fclose(input); michael@0: michael@0: if (options[4].doesOccur) { michael@0: strcat(filename, "Rules.txt"); michael@0: OUTPUT_ = fopen(filename, "w"); michael@0: if (OUTPUT_ == NULL) { michael@0: fprintf(stdout, "Cannot open file:%s\n", filename); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: fprintf(stdout, "Rules\n"); michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: UParseError parseError; michael@0: COLLATOR_ = ucol_openRules(rule, u_strlen(rule), UCOL_DEFAULT, michael@0: UCOL_DEFAULT_STRENGTH, &parseError, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator creation failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: goto CLOSERULES; michael@0: return; michael@0: } michael@0: setAttributes(COLLATOR_, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator attribute setting failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: goto CLOSERULES; michael@0: return; michael@0: } michael@0: michael@0: serialize("Rule-based", TRUE); michael@0: ucol_close(COLLATOR_); michael@0: michael@0: CLOSERULES : michael@0: if (options[4].doesOccur) { michael@0: filename[dirlength] = 0; michael@0: fclose(OUTPUT_); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Parse for enum values. michael@0: * Note this only works for positive enum values. michael@0: * @param enumarray array containing names of the enum values in string and michael@0: * their corresponding value. michael@0: * declared enum value. michael@0: * @param str string to be parsed michael@0: * @return corresponding integer enum value or -1 if value is not found. michael@0: */ michael@0: int parseEnums(const EnumNameValuePair enumarray[], const char *str) michael@0: { michael@0: const char *enumname = enumarray[0].name; michael@0: int result = atoi(str); michael@0: if (result == 0 && str[0] != '0') { michael@0: while (strcmp(enumname, str) != 0) { michael@0: // checking for multiple enum names sharing the same values michael@0: enumname = strstr(enumname, str); michael@0: if (enumname != NULL) { michael@0: int size = strchr(enumname, '|') - enumname; michael@0: if (size < 0) { michael@0: size = strlen(enumname); michael@0: } michael@0: if (size == (int)strlen(str)) { michael@0: return enumarray[result].value; michael@0: } michael@0: } michael@0: result ++; michael@0: if (&(enumarray[result]) == NULL) { michael@0: return -1; michael@0: } michael@0: enumname = enumarray[result].name; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: /** michael@0: * Parser for attribute name value pair michael@0: */ michael@0: void parseAttributes() { michael@0: char str[32]; michael@0: const char *pname = options[6].value; michael@0: const char *pend = options[6].value + strlen(options[6].value); michael@0: const char *pvalue; michael@0: michael@0: while (pname < pend) { michael@0: pvalue = strchr(pname, '='); michael@0: if (pvalue == NULL) { michael@0: fprintf(stdout, michael@0: "No matching value found for attribute argument %s\n", michael@0: pname); michael@0: return; michael@0: } michael@0: int count = pvalue - pname; michael@0: strncpy(str, pname, count); michael@0: str[count] = 0; michael@0: michael@0: int name = parseEnums(ATTRIBUTE_NAME_, str); michael@0: if (name == -1) { michael@0: fprintf(stdout, "Attribute name not found: %s\n", str); michael@0: return; michael@0: } michael@0: michael@0: pvalue ++; michael@0: // getting corresponding enum value michael@0: pname = strchr(pvalue, ','); michael@0: if (pname == NULL) { michael@0: pname = pend; michael@0: } michael@0: count = pname - pvalue; michael@0: strncpy(str, pvalue, count); michael@0: str[count] = 0; michael@0: int value = parseEnums(ATTRIBUTE_VALUE_, str); michael@0: if (value == -1) { michael@0: fprintf(stdout, "Attribute value not found: %s\n", str); michael@0: return; michael@0: } michael@0: ATTRIBUTE_[name] = (UColAttributeValue)value; michael@0: pname ++; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Checks if the locale argument is a base language michael@0: * @param locale to be checked michael@0: * @return TRUE if it is a base language michael@0: */ michael@0: inline UBool checkLocaleForLanguage(const char *locale) michael@0: { michael@0: return strlen(locale) <= 2; michael@0: } michael@0: michael@0: /** michael@0: * Converts a UChar array into its string form "xxxx xxxx" michael@0: * @param ch array of UChar characters michael@0: * @param count number of UChar characters michael@0: */ michael@0: void outputUChar(UChar ch[], int count) michael@0: { michael@0: for (int i = 0; i < count; i ++) { michael@0: fprintf(OUTPUT_, "%04X ", ch[i]); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * If it is a primary difference returns -1 or 1. michael@0: * If it is a secondary difference returns -2 or 2. michael@0: * If it is a tertiary difference returns -3 or 3. michael@0: * If equals returns 0. michael@0: */ michael@0: int compareSortKey(const void *elem1, const void *elem2) michael@0: { michael@0: // compare the 2 script element sort key michael@0: UChar *ch1 = ((ScriptElement *)elem1)->ch; michael@0: UChar *ch2 = ((ScriptElement *)elem2)->ch; michael@0: int size1 = ((ScriptElement *)elem1)->count; michael@0: int size2 = ((ScriptElement *)elem2)->count; michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: michael@0: ucol_setStrength(COLLATOR_, UCOL_PRIMARY); michael@0: int result = ucol_strcoll(COLLATOR_, ch1, size1, ch2, size2); michael@0: if (result == 0) { michael@0: ucol_setStrength(COLLATOR_, UCOL_SECONDARY); michael@0: result = ucol_strcoll(COLLATOR_, ch1, size1, ch2, size2); michael@0: if (result == 0) { michael@0: ucol_setStrength(COLLATOR_, UCOL_TERTIARY); michael@0: result = ucol_strcoll(COLLATOR_, ch1, size1, ch2, size2); michael@0: if (result < 0) { michael@0: return -3; michael@0: } michael@0: if (result > 0) { michael@0: return 3; michael@0: } michael@0: } michael@0: if (result < 0) { michael@0: return -2; michael@0: } michael@0: if (result > 0) { michael@0: return 2; michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Output serialized script elements michael@0: * @param element the element to output michael@0: * @param compare the comparison with the previous element michael@0: * @param expansion flags TRUE if element has an expansion michael@0: */ michael@0: void outputScriptElem(ScriptElement &element, int compare, UBool expansion) michael@0: { michael@0: switch (compare) { michael@0: case 0: michael@0: if (expansion) { michael@0: fprintf(OUTPUT_, ""); michael@0: michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: char utf8[64]; michael@0: UChar nfc[32]; michael@0: int32_t length = unorm_normalize(element.ch, element.count, UNORM_NFC, 0, nfc, michael@0: 32, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error normalizing contractions to NFC\n"); michael@0: } michael@0: u_strToUTF8(utf8, 64, &length, nfc, length, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error converting UChar to utf8\n"); michael@0: return; michael@0: } michael@0: michael@0: fprintf(OUTPUT_, "%s
", utf8); michael@0: fprintf(OUTPUT_, ""); michael@0: outputUChar(element.ch, element.count); michael@0: michael@0: if (compare == 0) { michael@0: fprintf(OUTPUT_, "   Q"); michael@0: } michael@0: else if (compare == -1) { michael@0: fprintf(OUTPUT_, "P   "); michael@0: } michael@0: else if (compare == -2) { michael@0: fprintf(OUTPUT_, " S  "); michael@0: } michael@0: else if (compare == -3) { michael@0: fprintf(OUTPUT_, "  T "); michael@0: } michael@0: michael@0: i = 0; michael@0: while (i < element.count) { michael@0: char str[128]; michael@0: UChar32 codepoint; michael@0: U16_NEXT(element.ch, i, element.count, codepoint); michael@0: int32_t temp = u_charName(codepoint, U_UNICODE_CHAR_NAME, str, 128, michael@0: &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error getting character name\n"); michael@0: return; michael@0: } michael@0: if (element.tailored) { michael@0: fprintf(OUTPUT_, ""); michael@0: } michael@0: fprintf(OUTPUT_, "%s", str); michael@0: if (element.tailored) { michael@0: fprintf(OUTPUT_, " *"); michael@0: } michael@0: if (i < element.count) { michael@0: fprintf(OUTPUT_, "
\n"); michael@0: } michael@0: } michael@0: michael@0: fprintf(OUTPUT_, "\n"); michael@0: } michael@0: michael@0: /** michael@0: * Checks if codepoint belongs to scripts michael@0: * @param script list michael@0: * @param scriptcount number of scripts michael@0: * @param codepoint to test michael@0: * @return TRUE if codepoint belongs to scripts michael@0: */ michael@0: UBool checkInScripts(UScriptCode script[], int scriptcount, michael@0: UChar32 codepoint) michael@0: { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: for (int i = 0; i < scriptcount; i ++) { michael@0: if (script[i] == USCRIPT_HAN && options[10].doesOccur) { michael@0: if ((codepoint >= 0x2E80 && codepoint <= 0x2EE4) || michael@0: (codepoint >= 0x2A672 && codepoint <= 0x2A6D6)) { michael@0: // reduce han michael@0: return TRUE; michael@0: } michael@0: } michael@0: else if (uscript_getScript(codepoint, &error) == script[i]) { michael@0: return TRUE; michael@0: } michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error checking character in scripts\n"); michael@0: return FALSE; michael@0: } michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * Checks if the set of codepoints belongs to the script michael@0: * @param script list michael@0: * @param scriptcount number of scripts michael@0: * @param scriptelem michael@0: * @return TRUE if all codepoints belongs to the script michael@0: */ michael@0: inline UBool checkInScripts(UScriptCode script[], int scriptcount, michael@0: ScriptElement scriptelem) michael@0: { michael@0: int i = 0; michael@0: while (i < scriptelem.count) { michael@0: UChar32 codepoint; michael@0: U16_NEXT(scriptelem.ch, i, scriptelem.count, codepoint); michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: if (checkInScripts(script, scriptcount, codepoint)) { michael@0: return TRUE; michael@0: } michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * Gets the script elements and contractions belonging to the script michael@0: * @param elems output list michael@0: * @param locale locale michael@0: * @return number of script elements michael@0: * Add by Richard michael@0: */ michael@0: int getScriptElementsFromExemplars(ScriptElement scriptelem[], const char* locale) { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: UChar32 codepoint = 0; michael@0: michael@0: UResourceBundle* ures = ures_open(NULL, locale, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Can not find resource bundle for locale: %s\n", locale); michael@0: return -1; michael@0: } michael@0: int32_t length; michael@0: const UChar* exemplarChars = ures_getStringByKey(ures, "ExemplarCharacters", &length, &error); michael@0: michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Can not find ExemplarCharacters in resource bundle\n"); michael@0: return -1; michael@0: } michael@0: michael@0: UChar* upperChars = new UChar[length * 2]; michael@0: if (upperChars == 0) { michael@0: fprintf(stdout, "Memory error\n"); michael@0: return -1; michael@0: } michael@0: michael@0: int32_t destLength = u_strToUpper(upperChars, length * 2, exemplarChars, -1, locale, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error when u_strToUpper() \n"); michael@0: return -1; michael@0: } michael@0: michael@0: UChar* pattern = new UChar[length + destLength + 10]; michael@0: UChar left[2] = {0x005b, 0x0}; michael@0: UChar right[2] = {0x005d, 0x0}; michael@0: pattern = u_strcpy(pattern, left); michael@0: pattern = u_strcat(pattern, exemplarChars); michael@0: pattern = u_strcat(pattern, upperChars); michael@0: pattern = u_strcat(pattern, right); michael@0: michael@0: UnicodeSet * uniset = new UnicodeSet(UnicodeString(pattern), error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Can not open USet \n"); michael@0: return -1; michael@0: } michael@0: michael@0: UnicodeSetIterator* usetiter = new UnicodeSetIterator(*uniset); michael@0: michael@0: int32_t count = 0; michael@0: michael@0: while (usetiter -> next()) { michael@0: if (usetiter -> isString()) { michael@0: UnicodeString strItem = usetiter -> getString(); michael@0: michael@0: scriptelem[count].count = 0; michael@0: for (int i = 0; i < strItem.length(); i++) { michael@0: codepoint = strItem.char32At(i); michael@0: UTF16_APPEND_CHAR_UNSAFE(scriptelem[count].ch, scriptelem[count].count, codepoint); michael@0: scriptelem[count].tailored = FALSE; michael@0: } michael@0: } else { michael@0: codepoint = usetiter -> getCodepoint(); michael@0: scriptelem[count].count = 0; michael@0: UTF16_APPEND_CHAR_UNSAFE(scriptelem[count].ch, scriptelem[count].count, codepoint); michael@0: scriptelem[count].tailored = FALSE; michael@0: } michael@0: michael@0: count++; michael@0: } michael@0: delete []pattern; michael@0: michael@0: return count; michael@0: } michael@0: michael@0: /** michael@0: * Gets the script elements and contractions belonging to the script michael@0: * @param script list michael@0: * @param scriptcount number of scripts michael@0: * @param elems output list michael@0: * @return number of script elements michael@0: */ michael@0: int getScriptElements(UScriptCode script[], int scriptcount, michael@0: ScriptElement scriptelem[]) michael@0: { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: UChar32 codepoint = 0; michael@0: int count = 0; michael@0: while (codepoint <= UCHAR_MAX_VALUE) { michael@0: if (checkInScripts(script, scriptcount, codepoint)) { michael@0: scriptelem[count].count = 0; michael@0: UTF16_APPEND_CHAR_UNSAFE(scriptelem[count].ch, michael@0: scriptelem[count].count, codepoint); michael@0: scriptelem[count].tailored = FALSE; michael@0: count ++; michael@0: } michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error determining codepoint in script\n"); michael@0: return -1; michael@0: } michael@0: codepoint ++; michael@0: } michael@0: michael@0: const UChar *current = NULL; michael@0: uint32_t strength = 0; michael@0: uint32_t chOffset = 0; michael@0: uint32_t chLen = 0; michael@0: uint32_t exOffset = 0; michael@0: uint32_t exLen = 0; michael@0: uint32_t prefixOffset = 0; michael@0: uint32_t prefixLen = 0; michael@0: uint8_t specs = 0; michael@0: UBool rstart = TRUE; michael@0: UColTokenParser src; michael@0: UColOptionSet opts; michael@0: UParseError parseError; michael@0: michael@0: int32_t rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, NULL, 0); michael@0: src.source = (UChar *)malloc(sizeof(UChar) * michael@0: (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE)); michael@0: rulelength = ucol_getRulesEx(COLLATOR_, UCOL_FULL_RULES, src.source, michael@0: rulelength); michael@0: src.current = src.source; michael@0: src.end = src.source + rulelength; michael@0: src.extraCurrent = src.end; michael@0: src.extraEnd = src.end + UCOL_TOK_EXTRA_RULE_SPACE_SIZE; michael@0: src.opts = &opts; michael@0: michael@0: /* michael@0: ucol_tok_parseNextToken(&src, &strength, &chOffset, michael@0: &chLen, &exOffset, &exLen, michael@0: &prefixOffset, &prefixLen, michael@0: &specs, rstart, &parseError, michael@0: &error) michael@0: */ michael@0: while ((current = ucol_tok_parseNextToken(&src, rstart, &parseError, michael@0: &error)) != NULL) { michael@0: // contractions handled here michael@0: if (chLen > 1) { michael@0: u_strncpy(scriptelem[count].ch, src.source + chOffset, chLen); michael@0: scriptelem[count].count = chLen; michael@0: if (checkInScripts(script, scriptcount, scriptelem[count])) { michael@0: scriptelem[count].tailored = FALSE; michael@0: count ++; michael@0: } michael@0: } michael@0: rstart = FALSE; michael@0: } michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error parsing rules: %s\n", u_errorName(error)); michael@0: } michael@0: // rule might have been reallocated, so delete this instead michael@0: free(src.source); michael@0: return count; michael@0: } michael@0: michael@0: int compareCodepoints(const void *elem1, const void *elem2) michael@0: { michael@0: UChar *ch1 = ((ScriptElement *)elem1)->ch; // key michael@0: UChar *ch2 = ((ScriptElement *)elem2)->ch; michael@0: ch1[((ScriptElement *)elem1)->count] = 0; michael@0: ch2[((ScriptElement *)elem2)->count] = 0; michael@0: michael@0: // compare the 2 codepoints michael@0: return u_strcmp(ch1, ch2); michael@0: } michael@0: michael@0: UBool hasSubNFD(ScriptElement &se, ScriptElement &key) michael@0: { michael@0: UChar *ch1 = se.ch; michael@0: UChar *ch2 = key.ch; // key michael@0: ch1[se.count] = 0; michael@0: ch2[key.count] = 0; michael@0: michael@0: // compare the 2 codepoints michael@0: if (u_strstr(ch1, ch2) != NULL) { michael@0: return TRUE; michael@0: } michael@0: michael@0: // check the decomposition michael@0: UChar norm[32]; michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: int size = unorm_normalize(ch1, se.count, UNORM_NFD, 0, norm, 32, michael@0: &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error normalizing\n"); michael@0: } michael@0: if (u_strstr(norm, ch2) != NULL) { michael@0: return TRUE; michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * Marks tailored elements michael@0: * @param script list michael@0: * @param scriptcount number of scripts michael@0: * @param scriptelem script element list michael@0: * @param scriptelemlength size of the script element list michael@0: */ michael@0: void markTailored(UScriptCode script[], int scriptcount, michael@0: ScriptElement scriptelem[], int scriptelemlength) michael@0: { michael@0: int32_t rulelength; michael@0: const UChar *rule = ucol_getRules(COLLATOR_, &rulelength); michael@0: michael@0: const UChar *current = NULL; michael@0: uint32_t strength = 0; michael@0: uint32_t chOffset = 0; michael@0: uint32_t chLen = 0; michael@0: uint32_t exOffset = 0; michael@0: uint32_t exLen = 0; michael@0: uint32_t prefixOffset = 0; michael@0: uint32_t prefixLen = 0; michael@0: uint8_t specs = 0; michael@0: UBool rstart = TRUE; michael@0: UColTokenParser src; michael@0: UColOptionSet opts; michael@0: UParseError parseError; michael@0: michael@0: src.opts = &opts; michael@0: src.source = (UChar *)malloc( michael@0: (rulelength + UCOL_TOK_EXTRA_RULE_SPACE_SIZE) * sizeof(UChar)); michael@0: memcpy(src.source, rule, rulelength * sizeof(UChar)); michael@0: src.current = src.source; michael@0: src.end = (UChar *)src.source + rulelength; michael@0: src.extraCurrent = src.end; michael@0: src.extraEnd = src.end + UCOL_TOK_EXTRA_RULE_SPACE_SIZE; michael@0: michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: michael@0: while ((current = ucol_tok_parseNextToken(&src, rstart, &parseError, michael@0: &error)) != NULL) { michael@0: if (chLen >= 1 && strength != UCOL_TOK_RESET) { michael@0: // skipping the reset characters and non useful stuff. michael@0: ScriptElement se; michael@0: u_strncpy(se.ch, src.source + chOffset, chLen); michael@0: se.count = chLen; michael@0: michael@0: if (checkInScripts(script, scriptcount, se)) { michael@0: /* michael@0: ScriptElement *tse = (ScriptElement *)bsearch(&se, scriptelem, michael@0: scriptelemlength, michael@0: sizeof(ScriptElement), michael@0: compareCodepoints); michael@0: */ michael@0: for (int i = 0; i < scriptelemlength; i ++) { michael@0: if (!scriptelem[i].tailored && michael@0: hasSubNFD(scriptelem[i], se)) { michael@0: scriptelem[i].tailored = TRUE; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: rstart = FALSE; michael@0: } michael@0: free(src.source); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error parsing rules\n"); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Checks if the collation iterator has more than 1 collation element michael@0: * @parem coleiter collation element iterator michael@0: * @return TRUE if collation iterator has more than 1 collation element michael@0: */ michael@0: UBool hasExpansions(UCollationElements *coleiter) michael@0: { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: int32_t ce = ucol_next(coleiter, &error); michael@0: int count = 0; michael@0: michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error getting next collation element\n"); michael@0: } michael@0: while (ce != UCOL_NULLORDER) { michael@0: if ((UCOL_PRIMARYORDER(ce) != 0) && !isContinuation(ce)) { michael@0: count ++; michael@0: if (count == 2) { michael@0: return TRUE; michael@0: } michael@0: } michael@0: ce = ucol_next(coleiter, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error getting next collation element\n"); michael@0: } michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * Prints the footer for index.html michael@0: * @param file output file michael@0: */ michael@0: void outputHTMLFooter() michael@0: { michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: } michael@0: michael@0: /** michael@0: * Serialize the codepoints from start to end into an html file. michael@0: * Arranging them into ascending collation order. michael@0: * @param script code list michael@0: * @param scriptcount number of scripts michael@0: */ michael@0: //void serializeScripts(UScriptCode script[], int scriptcount) michael@0: //Richard michael@0: void serializeScripts(UScriptCode script[], int scriptcount, const char* locale = NULL) michael@0: { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: michael@0: ScriptElement *scriptelem = michael@0: (ScriptElement *)malloc(sizeof(ScriptElement) * 0x20000); michael@0: if (scriptelem == NULL) { michael@0: fprintf(stdout, "Memory error\n"); michael@0: return; michael@0: } michael@0: int count = 0; michael@0: if(locale) { michael@0: count = getScriptElementsFromExemplars(scriptelem, locale); michael@0: } else { michael@0: count = getScriptElements(script, scriptcount, scriptelem); michael@0: } michael@0: michael@0: // Sort script elements using Quicksort algorithm: michael@0: qsort(scriptelem, count, sizeof(ScriptElement), compareCodepoints); michael@0: markTailored(script, scriptcount, scriptelem, count); michael@0: // Sort script elements using Quicksort algorithm: michael@0: qsort(scriptelem, count, sizeof(ScriptElement), compareSortKey); michael@0: michael@0: UCollationElements* coleiter = ucol_openElements(COLLATOR_, michael@0: scriptelem[0].ch, michael@0: scriptelem[0].count, michael@0: &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error creating collation element iterator\n"); michael@0: return; michael@0: } michael@0: michael@0: outputScriptElem(scriptelem[0], -1, hasExpansions(coleiter)); michael@0: for (int i = 0; i < count - 1; i ++) { michael@0: ucol_setText(coleiter, scriptelem[i + 1].ch, scriptelem[i + 1].count, michael@0: &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error setting text in collation element iterator\n"); michael@0: return; michael@0: } michael@0: outputScriptElem(scriptelem[i + 1], michael@0: compareSortKey(scriptelem + i, scriptelem + i + 1), michael@0: hasExpansions(coleiter)); michael@0: } michael@0: free(scriptelem); michael@0: outputHTMLFooter(); michael@0: } michael@0: michael@0: /** michael@0: * Prints the header for the html michael@0: * @param locale name michael@0: * @param script michael@0: * @param scriptcount number of scripts michael@0: */ michael@0: void outputHTMLHeader(const char *locale, UScriptCode script[], michael@0: int scriptcount) michael@0: { michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "ICU Collation charts\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: michael@0: fprintf(OUTPUT_, "\n"); michael@0: fprintf(OUTPUT_, "\n"); michael@0: michael@0: fprintf(OUTPUT_, "\n"); michael@0: UChar displayname[64]; michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: int32_t size = uloc_getDisplayName(locale, "en_US", displayname, 64, &error); michael@0: char utf8displayname[128]; michael@0: if (U_FAILURE(error)) { michael@0: utf8displayname[0] = 0; michael@0: } michael@0: else { michael@0: int32_t utf8size = 0; michael@0: u_strToUTF8(utf8displayname, 128, &utf8size, displayname, size, &error); michael@0: } michael@0: michael@0: fprintf(OUTPUT_, "\n", utf8displayname); michael@0: fprintf(OUTPUT_, ""); michael@0: fprintf(OUTPUT_, "\n"); michael@0: michael@0: fprintf(OUTPUT_, "\n", locale, locale); michael@0: michael@0: UVersionInfo version; michael@0: ucol_getVersion(COLLATOR_, version); michael@0: fprintf(OUTPUT_, "\n", michael@0: version[0], version[1], version[2], version[3]); michael@0: michael@0: UColAttribute attr = UCOL_FRENCH_COLLATION; michael@0: while (attr < UCOL_ATTRIBUTE_COUNT) { michael@0: UColAttributeValue value = ucol_getAttribute(COLLATOR_, attr, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error getting attribute\n"); michael@0: return; michael@0: } michael@0: if (value != UCOL_DEFAULT) { michael@0: if (attr == UCOL_FRENCH_COLLATION && value != UCOL_OFF) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: if (attr == UCOL_ALTERNATE_HANDLING && value != UCOL_NON_IGNORABLE) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: if (attr == UCOL_CASE_FIRST && value != UCOL_OFF) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: if (attr == UCOL_CASE_LEVEL && value != UCOL_OFF) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: if (attr == UCOL_NORMALIZATION_MODE && value != UCOL_OFF) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: if (attr == UCOL_STRENGTH && value != UCOL_TERTIARY) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: if (attr == UCOL_HIRAGANA_QUATERNARY_MODE && value != UCOL_OFF) { michael@0: fprintf(OUTPUT_, "\n", value); michael@0: } michael@0: } michael@0: attr = (UColAttribute)(attr + 1); michael@0: } michael@0: michael@0: // Get UNIX-style time and display as number and string. michael@0: time_t ltime; michael@0: time( <ime ); michael@0: fprintf(OUTPUT_, "", ctime(<ime)); michael@0: michael@0: fprintf(OUTPUT_, "
Locale%s
Script(s)"); michael@0: for (int i = 0; i < scriptcount; i ++) { michael@0: fprintf(OUTPUT_, "%s", uscript_getName(script[i])); michael@0: if (i + 1 != scriptcount) { michael@0: fprintf(OUTPUT_, ", "); michael@0: } michael@0: } michael@0: fprintf(OUTPUT_, "
Rules%s.txt
Collator version%d.%d.%d.%d
French Collationon, code %d
Alternate Handlingshifted, code%d
Case Firston, code %d
Case Levelon, code %d
Normalizationon, code %d
Strengthcode %d
Hiragana Quaternaryon, code %d
Date Generated%s
\n"); michael@0: michael@0: fprintf(OUTPUT_, "

How to read the table
\n"); michael@0: fprintf(OUTPUT_, "Submit a bug

\n"); michael@0: fprintf(OUTPUT_, "\n\n"); michael@0: fprintf(OUTPUT_, "\n\n"); michael@0: } michael@0: michael@0: /** michael@0: * Prints the header for index.html michael@0: * @param file output file michael@0: */ michael@0: void outputListHTMLHeader(FILE *file) michael@0: { michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "ICU Collation Charts\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "

ICU Collation Charts

\n"); michael@0: fprintf(file, "

\n"); michael@0: fprintf(file, "UCA Charts
"); michael@0: } michael@0: michael@0: /** michael@0: * Prints the footer for index.html michael@0: * @param file output file michael@0: */ michael@0: void outputListHTMLFooter(FILE *file) michael@0: { michael@0: fprintf(file, "

\n"); michael@0: //fprintf(file, "
\n"); michael@0: fprintf(file, "\n"); michael@0: fprintf(file, "\n"); michael@0: } michael@0: michael@0: /** michael@0: * Gets all scripts and serialize their codepoints into an html file. michael@0: */ michael@0: void serializeScripts() { michael@0: char filename[128]; michael@0: int dirlength = 0; michael@0: michael@0: if (options[4].doesOccur) { michael@0: strcpy(filename, options[4].value); michael@0: dirlength = appendDirSeparator(filename); michael@0: } else { michael@0: filename[0] = 0; michael@0: } michael@0: michael@0: const char *locale; michael@0: int32_t localelist = 0; michael@0: int32_t localesize; michael@0: michael@0: localesize = ucol_countAvailable(); michael@0: locale = ucol_getAvailable(localelist); michael@0: michael@0: strcat(filename, "list.html"); michael@0: FILE *list = fopen(filename, "w"); michael@0: filename[dirlength] = 0; michael@0: if (list == NULL) { michael@0: fprintf(stdout, "Cannot open file: %s\n", filename); michael@0: return; michael@0: } michael@0: michael@0: outputListHTMLHeader(list); michael@0: fprintf(list, "
\n"); michael@0: while (TRUE) { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: COLLATOR_ = ucol_open(locale, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator creation failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: break; michael@0: } michael@0: if ((error != U_USING_FALLBACK_WARNING && // not tailored michael@0: error != U_USING_DEFAULT_WARNING) || michael@0: checkLocaleForLanguage(locale)) { michael@0: fprintf(list, "%s ", locale, locale); michael@0: setAttributes(COLLATOR_, &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Collator attribute setting failed:"); michael@0: fprintf(stdout, u_errorName(error)); michael@0: break; michael@0: } michael@0: michael@0: UScriptCode scriptcode[32]; michael@0: uint32_t scriptcount = uscript_getCode(locale, scriptcode, 32, michael@0: &error); michael@0: if (U_FAILURE(error)) { michael@0: fprintf(stdout, "Error getting lcale scripts\n"); michael@0: break; michael@0: } michael@0: michael@0: strcat(filename, locale); michael@0: strcat(filename, ".html"); michael@0: OUTPUT_ = fopen(filename, "w"); michael@0: if (OUTPUT_ == NULL) { michael@0: fprintf(stdout, "Cannot open file:%s\n", filename); michael@0: break; michael@0: } michael@0: outputHTMLHeader(locale, scriptcode, scriptcount); michael@0: fprintf(stdout, "%s\n", locale); michael@0: michael@0: if(options[12].doesOccur) { michael@0: // use whole scripts michael@0: serializeScripts(scriptcode, scriptcount); michael@0: } else { michael@0: // use exemplar chars michael@0: serializeScripts(scriptcode, scriptcount, locale); michael@0: } michael@0: fclose(OUTPUT_); michael@0: } michael@0: ucol_close(COLLATOR_); michael@0: michael@0: filename[dirlength] = 0; michael@0: localelist ++; michael@0: if (localelist == localesize) { michael@0: break; michael@0: } michael@0: locale = ucol_getAvailable(localelist); michael@0: } michael@0: fprintf(list, "
help
"); michael@0: fprintf(list, "
\n"); michael@0: outputListHTMLFooter(list); michael@0: fclose(list); michael@0: } michael@0: michael@0: /** michael@0: * Main -- process command line, read in and pre-process the test file, michael@0: * call other functions to do the actual tests. michael@0: */ michael@0: int main(int argc, char *argv[]) { michael@0: michael@0: argc = u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), michael@0: options); michael@0: michael@0: // error handling, printing usage message michael@0: if (argc < 0) { michael@0: fprintf(stdout, "error in command line argument: "); michael@0: fprintf(stdout, argv[-argc]); michael@0: fprintf(stdout, "\n"); michael@0: } michael@0: if (argc < 0 || options[0].doesOccur || options[1].doesOccur) { michael@0: fprintf(stdout, "Usage: dumpce options...\n" michael@0: "--help\n" michael@0: " Display this message.\n" michael@0: "--locale name|all\n" michael@0: " ICU locale to use. Default is en_US\n" michael@0: "--serialize\n" michael@0: " Serializes the collation elements in -locale or all locales available and outputs them into --outputdir/locale_ce.txt\n" michael@0: "--destdir dir_name\n" michael@0: " Path for outputing the serialized collation elements. Defaults to stdout if no defined\n" michael@0: "--sourcedir dir_name\n" michael@0: " Path for the input rule file for collation\n" michael@0: "--attribute name=value,name=value...\n" michael@0: " Pairs of attribute names and values for setting\n" michael@0: "--rule filename\n" michael@0: " Name of file containing the collation rules.\n" michael@0: "--normalizaton mode\n" michael@0: " UNormalizationMode mode to be used.\n" michael@0: "--scripts\n" michael@0: " Codepoints from all scripts are sorted and serialized.\n" michael@0: "--reducehan\n" michael@0: " Only 200 Han script characters will be displayed with the use of --scripts.\n" michael@0: "--wholescripts\n" michael@0: " Show collation order for whole scripts instead of just for exemplar characters of a locale\n\n"); michael@0: michael@0: fprintf(stdout, "Example to generate *.txt files : dumpce --serialize --locale af --destdir /temp --attribute UCOL_STRENGTH=UCOL_DEFAULT_STRENGTH,4=17\n\n"); michael@0: fprintf(stdout, "Example to generate *.html files for oss web display: dumpce --scripts --destdir /temp --reducehan\n"); michael@0: return argc < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; michael@0: } michael@0: michael@0: OUTPUT_ = stdout; michael@0: if (options[6].doesOccur) { michael@0: fprintf(stdout, "attributes %s\n", options[6].value); michael@0: parseAttributes(); michael@0: } michael@0: if (options[3].doesOccur) { michael@0: serialize(); michael@0: } michael@0: if (options[9].doesOccur) { michael@0: serializeScripts(); michael@0: } michael@0: return 0; michael@0: }
CodepointPSTQName