]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/gennorm2/gennorm2.cpp
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / tools / gennorm2 / gennorm2.cpp
index f0d981ec53b46b77dfac45f9a65e61a3b25a7a81..237d8de7c746f5b46c2e1a30c224fab61891e9f5 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2009-2010, International Business Machines
+*   Copyright (C) 2009-2014, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -38,8 +38,6 @@
 #include "unewdata.h"
 #endif
 
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
 U_NAMESPACE_BEGIN
 
 UBool beVerbose=FALSE, haveCopyright=TRUE;
@@ -60,6 +58,7 @@ enum {
     SOURCEDIR,
     OUTPUT_FILENAME,
     UNICODE_VERSION,
+    WRITE_C_SOURCE,
     OPT_FAST
 };
 
@@ -71,6 +70,7 @@ static UOption options[]={
     UOPTION_SOURCEDIR,
     UOPTION_DEF("output", 'o', UOPT_REQUIRES_ARG),
     UOPTION_DEF("unicode", 'u', UOPT_REQUIRES_ARG),
+    UOPTION_DEF("csource", '\1', UOPT_NO_ARG),
     UOPTION_DEF("fast", '\1', UOPT_NO_ARG)
 };
 
@@ -80,7 +80,6 @@ main(int argc, char* argv[]) {
 
     /* preset then read command line options */
     options[SOURCEDIR].value="";
-    options[UNICODE_VERSION].value=U_UNICODE_VERSION;
     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[HELP_H]), options);
 
     /* error handling, printing usage message */
@@ -103,7 +102,7 @@ main(int argc, char* argv[]) {
             "Usage: %s [-options] infiles+ -o outputfilename\n"
             "\n"
             "Reads the infiles with normalization data and\n"
-            "creates a binary file (outputfilename) with the data.\n"
+            "creates a binary or C source file (outputfilename) with the data.\n"
             "\n",
             argv[0]);
         fprintf(stderr,
@@ -114,9 +113,10 @@ main(int argc, char* argv[]) {
             "\t-u or --unicode     Unicode version, followed by the version like 5.2.0\n");
         fprintf(stderr,
             "\t-s or --sourcedir   source directory, followed by the path\n"
-            "\t-o or --output      output filename\n");
+            "\t-o or --output      output filename\n"
+            "\t      --csource     writes a C source file with initializers\n");
         fprintf(stderr,
-            "\t      --fast        optimize the .nrm file for fast normalization,\n"
+            "\t      --fast        optimize the data for fast normalization,\n"
             "\t                    which might increase its size  (Writes fully decomposed\n"
             "\t                    regular mappings instead of delta mappings.\n"
             "\t                    You should measure the runtime speed to make sure that\n"
@@ -142,10 +142,12 @@ main(int argc, char* argv[]) {
 
 #else
 
-    LocalPointer<Normalizer2DataBuilder> builder(new Normalizer2DataBuilder(errorCode));
+    LocalPointer<Normalizer2DataBuilder> builder(new Normalizer2DataBuilder(errorCode), errorCode);
     errorCode.assertSuccess();
 
-    builder->setUnicodeVersion(options[UNICODE_VERSION].value);
+    if(options[UNICODE_VERSION].doesOccur) {
+        builder->setUnicodeVersion(options[UNICODE_VERSION].value);
+    }
 
     if(options[OPT_FAST].doesOccur) {
         builder->setOptimization(Normalizer2DataBuilder::OPTIMIZE_FAST);
@@ -175,7 +177,11 @@ main(int argc, char* argv[]) {
         filename.truncate(pathLength);
     }
 
-    builder->writeBinaryFile(options[OUTPUT_FILENAME].value);
+    if(options[WRITE_C_SOURCE].doesOccur) {
+        builder->writeCSourceFile(options[OUTPUT_FILENAME].value);
+    } else {
+        builder->writeBinaryFile(options[OUTPUT_FILENAME].value);
+    }
 
     return errorCode.get();
 
@@ -198,6 +204,11 @@ void parseFile(FILE *f, Normalizer2DataBuilder &builder) {
             continue;  // skip empty and comment-only lines
         }
         if(line[0]=='*') {
+            const char *s=u_skipWhitespace(line+1);
+            if(0==strncmp(s, "Unicode", 7)) {
+                s=u_skipWhitespace(s+7);
+                builder.setUnicodeVersion(s);
+            }
             continue;  // reserved syntax
         }
         const char *delimiter;
@@ -233,7 +244,7 @@ void parseFile(FILE *f, Normalizer2DataBuilder &builder) {
         }
         if(*delimiter=='=' || *delimiter=='>') {
             UChar uchars[Normalizer2Impl::MAPPING_LENGTH_MASK];
-            int32_t length=u_parseString(delimiter+1, uchars, LENGTHOF(uchars), NULL, errorCode);
+            int32_t length=u_parseString(delimiter+1, uchars, UPRV_LENGTHOF(uchars), NULL, errorCode);
             if(errorCode.isFailure()) {
                 fprintf(stderr, "gennorm2 error: parsing mapping string from %s\n", line);
                 exit(errorCode.reset());