]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/toolutil/writesrc.c
ICU-511.25.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / writesrc.c
index 28303c733e5b5ea80629ea3b397a0c865fb05314..6db9087e4512ff1f470ab8645029c38504ff7cd8 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2005-2007, International Business Machines
+*   Copyright (C) 2005-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 #include <time.h>
 #include "unicode/utypes.h"
 #include "unicode/putil.h"
-#include "utrie.h"
+#include "utrie2.h"
 #include "cstring.h"
 #include "writesrc.h"
 
-U_CAPI FILE * U_EXPORT2
-usrc_create(const char *path, const char *filename) {
+static FILE *
+usrc_createWithHeader(const char *path, const char *filename,
+                      const char *generator, const char *header) {
     char buffer[1024];
     const char *p;
     char *q;
@@ -54,20 +55,12 @@ usrc_create(const char *path, const char *filename) {
         time(&t);
         lt=localtime(&t);
         strftime(year, sizeof(year), "%Y", lt);
-        strftime(buffer, sizeof(buffer), "%Y-%m-%d", lt);
-        fprintf(
-            f,
-            "/*\n"
-            " * Copyright (C) 1999-%s, International Business Machines\n"
-            " * Corporation and others.  All Rights Reserved.\n"
-            " *\n"
-            " * file name: %s\n"
-            " *\n"
-            " * machine-generated on: %s\n"
-            " */\n\n",
-            year,
-            filename,
-            buffer);
+        if(generator==NULL) {
+            strftime(buffer, sizeof(buffer), "%Y-%m-%d", lt);
+            fprintf(f, header, year, filename, buffer);
+        } else {
+            fprintf(f, header, year, filename, generator);
+        }
     } else {
         fprintf(
             stderr,
@@ -77,6 +70,33 @@ usrc_create(const char *path, const char *filename) {
     return f;
 }
 
+U_CAPI FILE * U_EXPORT2
+usrc_create(const char *path, const char *filename, const char *generator) {
+    static const char *header=
+        "/*\n"
+        " * Copyright (C) 1999-%s, International Business Machines\n"
+        " * Corporation and others.  All Rights Reserved.\n"
+        " *\n"
+        " * file name: %s\n"
+        " *\n"
+        " * machine-generated by: %s\n"
+        " */\n\n";
+    return usrc_createWithHeader(path, filename, generator, header);
+}
+
+U_CAPI FILE * U_EXPORT2
+usrc_createTextData(const char *path, const char *filename, const char *generator) {
+    static const char *header=
+        "# Copyright (C) 1999-%s, International Business Machines\n"
+        "# Corporation and others.  All Rights Reserved.\n"
+        "#\n"
+        "# file name: %s\n"
+        "#\n"
+        "# machine-generated by: %s\n"
+        "#\n\n";
+    return usrc_createWithHeader(path, filename, generator, header);
+}
+
 U_CAPI void U_EXPORT2
 usrc_writeArray(FILE *f,
                 const char *prefix,
@@ -139,51 +159,103 @@ usrc_writeArray(FILE *f,
 }
 
 U_CAPI void U_EXPORT2
-usrc_writeUTrieArrays(FILE *f,
-                      const char *indexPrefix, const char *dataPrefix,
-                      const UTrie *pTrie,
-                      const char *postfix) {
+usrc_writeUTrie2Arrays(FILE *f,
+                       const char *indexPrefix, const char *data32Prefix,
+                       const UTrie2 *pTrie,
+                       const char *postfix) {
     if(pTrie->data32==NULL) {
         /* 16-bit trie */
         usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength+pTrie->dataLength, postfix);
     } else {
         /* 32-bit trie */
         usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength, postfix);
-        usrc_writeArray(f, dataPrefix, pTrie->data32, 32, pTrie->dataLength, postfix);
+        usrc_writeArray(f, data32Prefix, pTrie->data32, 32, pTrie->dataLength, postfix);
     }
 }
 
 U_CAPI void U_EXPORT2
-usrc_writeUTrieStruct(FILE *f,
-                      const char *prefix,
-                      const UTrie *pTrie,
-                      const char *indexName, const char *dataName,
-                      const char *getFoldingOffsetName,
-                      const char *postfix) {
+usrc_writeUTrie2Struct(FILE *f,
+                       const char *prefix,
+                       const UTrie2 *pTrie,
+                       const char *indexName, const char *data32Name,
+                       const char *postfix) {
     if(prefix!=NULL) {
         fputs(prefix, f);
     }
-    if(dataName==NULL) {
-        dataName="NULL";
-    }
-    if(getFoldingOffsetName==NULL) {
-        getFoldingOffsetName="utrie_defaultGetFoldingOffset";
+    if(pTrie->data32==NULL) {
+        /* 16-bit trie */
+        fprintf(
+            f,
+            "    %s,\n"         /* index */
+            "    %s+%ld,\n"     /* data16 */
+            "    NULL,\n",      /* data32 */
+            indexName,
+            indexName, 
+            (long)pTrie->indexLength);
+    } else {
+        /* 32-bit trie */
+        fprintf(
+            f,
+            "    %s,\n"         /* index */
+            "    NULL,\n"       /* data16 */
+            "    %s,\n",        /* data32 */
+            indexName,
+            data32Name);
     }
     fprintf(
         f,
-        "    %s,\n"
-        "    %s,\n"
-        "    %s,\n"
-        "    %ld,\n"
-        "    %ld,\n"
-        "    %lu,\n"
-        "    %s\n",
-        indexName,
-        dataName,
-        getFoldingOffsetName,
+        "    %ld,\n"            /* indexLength */
+        "    %ld,\n"            /* dataLength */
+        "    0x%hx,\n"          /* index2NullOffset */
+        "    0x%hx,\n"          /* dataNullOffset */
+        "    0x%lx,\n"          /* initialValue */
+        "    0x%lx,\n"          /* errorValue */
+        "    0x%lx,\n"          /* highStart */
+        "    0x%lx,\n"          /* highValueIndex */
+        "    NULL, 0, FALSE, FALSE, 0, NULL\n",
         (long)pTrie->indexLength, (long)pTrie->dataLength,
-        (unsigned long)pTrie->initialValue,
-        pTrie->isLatin1Linear ? "TRUE" : "FALSE");
+        (short)pTrie->index2NullOffset, (short)pTrie->dataNullOffset,
+        (long)pTrie->initialValue, (long)pTrie->errorValue,
+        (long)pTrie->highStart, (long)pTrie->highValueIndex);
+    if(postfix!=NULL) {
+        fputs(postfix, f);
+    }
+}
+
+U_CAPI void U_EXPORT2
+usrc_writeArrayOfMostlyInvChars(FILE *f,
+                                const char *prefix,
+                                const char *p, int32_t length,
+                                const char *postfix) {
+    int32_t i, col;
+    int prev2, prev, c;
+
+    if(prefix!=NULL) {
+        fprintf(f, prefix, (long)length);
+    }
+    prev2=prev=-1;
+    for(i=col=0; i<length; ++i, ++col) {
+        c=(uint8_t)p[i];
+        if(i>0) {
+            /* Break long lines. Try to break at interesting places, to minimize revision diffs. */
+            if( 
+                /* Very long line. */
+                col>=32 ||
+                /* Long line, break after terminating NUL. */
+                (col>=24 && prev2>=0x20 && prev==0) ||
+                /* Medium-long line, break before non-NUL, non-character byte. */
+                (col>=16 && (prev==0 || prev>=0x20) && 0<c && c<0x20)
+            ) {
+                fputs(",\n", f);
+                col=0;
+            } else {
+                fputc(',', f);
+            }
+        }
+        fprintf(f, c<0x20 ? "%u" : "'%c'", c);
+        prev2=prev;
+        prev=c;
+    }
     if(postfix!=NULL) {
         fputs(postfix, f);
     }