]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/icupkg/icupkg.cpp
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / tools / icupkg / icupkg.cpp
index 93c488ddda71f1e46cd8f55f6c2c24f9fb4fcf4a..eb198fdedc8f83da0f652a2c1620cbe472813e02 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2005-2006, International Business Machines
+*   Copyright (C) 2005-2010, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 #include "toolutil.h"
 #include "uoptions.h"
 #include "uparse.h"
+#include "filestrm.h"
 #include "package.h"
+#include "pkg_icu.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+U_NAMESPACE_USE
+
 // TODO: add --matchmode=regex for using the ICU regex engine for item name pattern matching?
 
 // general definitions ----------------------------------------------------- ***
 
 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
 
-// read a file list -------------------------------------------------------- ***
-
-static const char *reservedChars="\"%&'()*+,-./:;<=>?_";
-
-static const struct {
-    const char *suffix;
-    int32_t length;
-} listFileSuffixes[]={
-    { ".txt", 4 },
-    { ".lst", 4 },
-    { ".tmp", 4 }
-};
-
-/* check for multiple text file suffixes to see if this list name is a text file name */
-static UBool
-isListTextFile(const char *listname) {
-    const char *listNameEnd=strchr(listname, 0);
-    const char *suffix;
-    int32_t i, length;
-    for(i=0; i<LENGTHOF(listFileSuffixes); ++i) {
-        suffix=listFileSuffixes[i].suffix;
-        length=listFileSuffixes[i].length;
-        if((listNameEnd-listname)>length && 0==memcmp(listNameEnd-length, suffix, length)) {
-            return TRUE;
-        }
-    }
-    return FALSE;
-}
-
-/*
- * Read a file list.
- * If the listname ends with ".txt", then read the list file
- * (in the system/ invariant charset).
- * If the listname ends with ".dat", then read the ICU .dat package file.
- * Otherwise, read the file itself as a single-item list.
- */
-static Package *
-readList(const char *filesPath, const char *listname, UBool readContents) {
-    Package *listPkg;
-    FILE *file;
-    const char *listNameEnd;
-
-    if(listname==NULL || listname[0]==0) {
-        fprintf(stderr, "missing list file\n");
-        return NULL;
-    }
-
-    listPkg=new Package();
-    if(listPkg==NULL) {
-        fprintf(stderr, "icupkg: not enough memory\n");
-        exit(U_MEMORY_ALLOCATION_ERROR);
-    }
-
-    listNameEnd=strchr(listname, 0);
-    if(isListTextFile(listname)) {
-        // read the list file
-        char line[1024];
-        char *end;
-        const char *start;
-
-        file=fopen(listname, "r");
-        if(file==NULL) {
-            fprintf(stderr, "icupkg: unable to open list file \"%s\"\n", listname);
-            delete listPkg;
-            exit(U_FILE_ACCESS_ERROR);
-        }
-
-        while(fgets(line, sizeof(line), file)) {
-            // remove comments
-            end=strchr(line, '#');
-            if(end!=NULL) {
-                *end=0;
-            } else {
-                // remove trailing CR LF
-                end=strchr(line, 0);
-                while(line<end && (*(end-1)=='\r' || *(end-1)=='\n')) {
-                    *--end=0;
-                }
-            }
-
-            // check first non-whitespace character and
-            // skip empty lines and
-            // skip lines starting with reserved characters
-            start=u_skipWhitespace(line);
-            if(*start==0 || NULL!=strchr(reservedChars, *start)) {
-                continue;
-            }
-
-            // take whitespace-separated items from the line
-            for(;;) {
-                // find whitespace after the item or the end of the line
-                for(end=(char *)start; *end!=0 && *end!=' ' && *end!='\t'; ++end) {}
-                if(*end==0) {
-                    // this item is the last one on the line
-                    end=NULL;
-                } else {
-                    // the item is terminated by whitespace, terminate it with NUL
-                    *end=0;
-                }
-                if(readContents) {
-                    listPkg->addFile(filesPath, start);
-                } else {
-                    listPkg->addItem(start);
-                }
-
-                // find the start of the next item or exit the loop
-                if(end==NULL || *(start=u_skipWhitespace(end+1))==0) {
-                    break;
-                }
-            }
-        }
-        fclose(file);
-    } else if((listNameEnd-listname)>4 && 0==memcmp(listNameEnd-4, ".dat", 4)) {
-        // read the ICU .dat package
-        listPkg->readPackage(listname);
-    } else {
-        // list the single file itself
-        if(readContents) {
-            listPkg->addFile(filesPath, listname);
-        } else {
-            listPkg->addItem(listname);
-        }
-    }
-
-    return listPkg;
-}
-
 // main() ------------------------------------------------------------------ ***
 
 static void
@@ -180,7 +57,7 @@ printUsage(const char *pname, UBool isHelp) {
 
     fprintf(where,
             "%csage: %s [-h|-?|--help ] [-tl|-tb|-te] [-c] [-C comment]\n"
-            "\t[-a list] [-r list] [-x list] [-l]\n"
+            "\t[-a list] [-r list] [-x list] [-l [-o outputListFileName]]\n"
             "\t[-s path] [-d path] [-w] [-m mode]\n"
             "\tinfilename [outfilename]\n",
             isHelp ? 'U' : 'u', pname);
@@ -253,7 +130,7 @@ printUsage(const char *pname, UBool isHelp) {
             "\tComments begin with # and are ignored. Empty lines are ignored.\n"
             "\tLines where the first non-whitespace character is one of %s\n"
             "\tare also ignored, to reserve for future syntax.\n",
-            reservedChars);
+            U_PKG_RESERVED_CHARS);
         fprintf(where,
             "\tItems for removal or extraction may contain a single '*' wildcard\n"
             "\tcharacter. The '*' matches zero or more characters.\n"
@@ -273,7 +150,7 @@ printUsage(const char *pname, UBool isHelp) {
             "\t-s path or --sourcedir path  directory for the --add items\n"
             "\t-d path or --destdir path    directory for the --extract items\n"
             "\n"
-            "\t-l or --list                 list the package items to stdout\n"
+            "\t-l or --list                 list the package items to stdout or to output list file\n"
             "\t                             (after modifying the package)\n");
     }
 }
@@ -297,7 +174,9 @@ static UOption options[]={
     UOPTION_DEF("remove", 'r', UOPT_REQUIRES_ARG),
     UOPTION_DEF("extract", 'x', UOPT_REQUIRES_ARG),
 
-    UOPTION_DEF("list", 'l', UOPT_NO_ARG)
+    UOPTION_DEF("list", 'l', UOPT_NO_ARG),
+    
+    UOPTION_DEF("outlist", 'o', UOPT_REQUIRES_ARG)
 };
 
 enum {
@@ -320,6 +199,8 @@ enum {
     OPT_EXTRACT_LIST,
 
     OPT_LIST_ITEMS,
+    
+    OPT_LIST_FILE,
 
     OPT_COUNT
 };
@@ -331,14 +212,20 @@ isPackageName(const char *filename) {
     len=(int32_t)strlen(filename)-4; /* -4: subtract the length of ".dat" */
     return (UBool)(len>0 && 0==strcmp(filename+len, ".dat"));
 }
+/*
+This line is required by MinGW because it incorrectly globs the arguments.
+So when \* is used, it turns into a list of files instead of a literal "*"
+*/
+int _CRT_glob = 0;
 
 extern int
 main(int argc, char *argv[]) {
     const char *pname, *sourcePath, *destPath, *inFilename, *outFilename, *outComment;
     char outType;
     UBool isHelp, isModified, isPackage;
+    int result = 0;
 
-    Package *pkg, *listPkg;
+    Package *pkg, *listPkg, *addListPkg;
 
     U_MAIN_INIT_ARGS(argc, argv);
 
@@ -428,7 +315,7 @@ main(int argc, char *argv[]) {
          * If we swap a single file, just assume that we are modifying it.
          * The Package class does not give us access to the item and its type.
          */
-        isModified=(UBool)(!isPackage || outType!=pkg->getInType());
+        isModified|=(UBool)(!isPackage || outType!=pkg->getInType());
     } else if(isPackage) {
         outType=pkg->getInType(); // default to input type
     } else /* !isPackage: swap single file */ {
@@ -460,7 +347,7 @@ main(int argc, char *argv[]) {
         }
 
         delete pkg;
-        return 0;
+        return result;
     }
 
     /* Work with a package. */
@@ -500,11 +387,12 @@ main(int argc, char *argv[]) {
      * use a separate Package so that its memory and items stay around
      * as long as the main Package
      */
+    addListPkg=NULL;
     if(options[OPT_ADD_LIST].doesOccur) {
-        listPkg=readList(sourcePath, options[OPT_ADD_LIST].value, TRUE);
-        if(listPkg!=NULL) {
-            pkg->addItems(*listPkg);
-            delete listPkg;
+        addListPkg=readList(sourcePath, options[OPT_ADD_LIST].value, TRUE);
+        if(addListPkg!=NULL) {
+            pkg->addItems(*addListPkg);
+            // delete addListPkg; deferred until after writePackage()
             isModified=TRUE;
         } else {
             printUsage(pname, FALSE);
@@ -526,7 +414,24 @@ main(int argc, char *argv[]) {
 
     /* list items */
     if(options[OPT_LIST_ITEMS].doesOccur) {
-        pkg->listItems(stdout);
+        int32_t i;
+        if (options[OPT_LIST_FILE].doesOccur) {
+            FileStream *out;
+            out = T_FileStream_open(options[OPT_LIST_FILE].value, "w");
+            if (out != NULL) {
+                for(i=0; i<pkg->getItemCount(); ++i) {
+                    T_FileStream_writeLine(out, pkg->getItem(i)->name);
+                    T_FileStream_writeLine(out, "\n");
+                }
+                T_FileStream_close(out);
+            } else {
+                return U_ILLEGAL_ARGUMENT_ERROR;
+            }
+        } else {
+            for(i=0; i<pkg->getItemCount(); ++i) {
+                fprintf(stdout, "%s\n", pkg->getItem(i)->name);
+            }
+        }
     }
 
     /* check dependencies between items */
@@ -562,11 +467,12 @@ main(int argc, char *argv[]) {
             }
             outFilename=outFilenameBuffer;
         }
-        pkg->writePackage(outFilename, outType, outComment);
+        result = writePackageDatFile(outFilename, outComment, NULL, NULL, pkg, outType);
     }
 
+    delete addListPkg;
     delete pkg;
-    return 0;
+    return result;
 }
 
 /*