Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /****************************************************************************** |
michael@0 | 2 | * Copyright (C) 2008-2013, International Business Machines |
michael@0 | 3 | * Corporation and others. All Rights Reserved. |
michael@0 | 4 | ******************************************************************************* |
michael@0 | 5 | */ |
michael@0 | 6 | #include "unicode/utypes.h" |
michael@0 | 7 | #include "unicode/putil.h" |
michael@0 | 8 | #include "cstring.h" |
michael@0 | 9 | #include "toolutil.h" |
michael@0 | 10 | #include "uoptions.h" |
michael@0 | 11 | #include "uparse.h" |
michael@0 | 12 | #include "package.h" |
michael@0 | 13 | #include "pkg_icu.h" |
michael@0 | 14 | |
michael@0 | 15 | #include <stdio.h> |
michael@0 | 16 | #include <stdlib.h> |
michael@0 | 17 | #include <string.h> |
michael@0 | 18 | |
michael@0 | 19 | |
michael@0 | 20 | #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) |
michael@0 | 21 | |
michael@0 | 22 | // read a file list -------------------------------------------------------- *** |
michael@0 | 23 | |
michael@0 | 24 | U_NAMESPACE_USE |
michael@0 | 25 | |
michael@0 | 26 | static const struct { |
michael@0 | 27 | const char *suffix; |
michael@0 | 28 | int32_t length; |
michael@0 | 29 | } listFileSuffixes[]={ |
michael@0 | 30 | { ".txt", 4 }, |
michael@0 | 31 | { ".lst", 4 }, |
michael@0 | 32 | { ".tmp", 4 } |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | /* check for multiple text file suffixes to see if this list name is a text file name */ |
michael@0 | 36 | static UBool |
michael@0 | 37 | isListTextFile(const char *listname) { |
michael@0 | 38 | const char *listNameEnd=strchr(listname, 0); |
michael@0 | 39 | const char *suffix; |
michael@0 | 40 | int32_t i, length; |
michael@0 | 41 | for(i=0; i<LENGTHOF(listFileSuffixes); ++i) { |
michael@0 | 42 | suffix=listFileSuffixes[i].suffix; |
michael@0 | 43 | length=listFileSuffixes[i].length; |
michael@0 | 44 | if((listNameEnd-listname)>length && 0==memcmp(listNameEnd-length, suffix, length)) { |
michael@0 | 45 | return TRUE; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | return FALSE; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | /* |
michael@0 | 52 | * Read a file list. |
michael@0 | 53 | * If the listname ends with ".txt", then read the list file |
michael@0 | 54 | * (in the system/ invariant charset). |
michael@0 | 55 | * If the listname ends with ".dat", then read the ICU .dat package file. |
michael@0 | 56 | * Otherwise, read the file itself as a single-item list. |
michael@0 | 57 | */ |
michael@0 | 58 | U_CAPI Package * U_EXPORT2 |
michael@0 | 59 | readList(const char *filesPath, const char *listname, UBool readContents, Package *listPkgIn) { |
michael@0 | 60 | Package *listPkg = listPkgIn; |
michael@0 | 61 | FILE *file; |
michael@0 | 62 | const char *listNameEnd; |
michael@0 | 63 | |
michael@0 | 64 | if(listname==NULL || listname[0]==0) { |
michael@0 | 65 | fprintf(stderr, "missing list file\n"); |
michael@0 | 66 | return NULL; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | if (listPkg == NULL) { |
michael@0 | 70 | listPkg=new Package(); |
michael@0 | 71 | if(listPkg==NULL) { |
michael@0 | 72 | fprintf(stderr, "icupkg: not enough memory\n"); |
michael@0 | 73 | exit(U_MEMORY_ALLOCATION_ERROR); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | listNameEnd=strchr(listname, 0); |
michael@0 | 78 | if(isListTextFile(listname)) { |
michael@0 | 79 | // read the list file |
michael@0 | 80 | char line[1024]; |
michael@0 | 81 | char *end; |
michael@0 | 82 | const char *start; |
michael@0 | 83 | |
michael@0 | 84 | file=fopen(listname, "r"); |
michael@0 | 85 | if(file==NULL) { |
michael@0 | 86 | fprintf(stderr, "icupkg: unable to open list file \"%s\"\n", listname); |
michael@0 | 87 | delete listPkg; |
michael@0 | 88 | exit(U_FILE_ACCESS_ERROR); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | while(fgets(line, sizeof(line), file)) { |
michael@0 | 92 | // remove comments |
michael@0 | 93 | end=strchr(line, '#'); |
michael@0 | 94 | if(end!=NULL) { |
michael@0 | 95 | *end=0; |
michael@0 | 96 | } else { |
michael@0 | 97 | // remove trailing CR LF |
michael@0 | 98 | end=strchr(line, 0); |
michael@0 | 99 | while(line<end && (*(end-1)=='\r' || *(end-1)=='\n')) { |
michael@0 | 100 | *--end=0; |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | // check first non-whitespace character and |
michael@0 | 105 | // skip empty lines and |
michael@0 | 106 | // skip lines starting with reserved characters |
michael@0 | 107 | start=u_skipWhitespace(line); |
michael@0 | 108 | if(*start==0 || NULL!=strchr(U_PKG_RESERVED_CHARS, *start)) { |
michael@0 | 109 | continue; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // take whitespace-separated items from the line |
michael@0 | 113 | for(;;) { |
michael@0 | 114 | // find whitespace after the item or the end of the line |
michael@0 | 115 | for(end=(char *)start; *end!=0 && *end!=' ' && *end!='\t'; ++end) {} |
michael@0 | 116 | if(*end==0) { |
michael@0 | 117 | // this item is the last one on the line |
michael@0 | 118 | end=NULL; |
michael@0 | 119 | } else { |
michael@0 | 120 | // the item is terminated by whitespace, terminate it with NUL |
michael@0 | 121 | *end=0; |
michael@0 | 122 | } |
michael@0 | 123 | if(readContents) { |
michael@0 | 124 | listPkg->addFile(filesPath, start); |
michael@0 | 125 | } else { |
michael@0 | 126 | listPkg->addItem(start); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | // find the start of the next item or exit the loop |
michael@0 | 130 | if(end==NULL || *(start=u_skipWhitespace(end+1))==0) { |
michael@0 | 131 | break; |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | } |
michael@0 | 135 | fclose(file); |
michael@0 | 136 | } else if((listNameEnd-listname)>4 && 0==memcmp(listNameEnd-4, ".dat", 4)) { |
michael@0 | 137 | // read the ICU .dat package |
michael@0 | 138 | // Accept a .dat file whose name differs from the ToC prefixes. |
michael@0 | 139 | listPkg->setAutoPrefix(); |
michael@0 | 140 | listPkg->readPackage(listname); |
michael@0 | 141 | } else { |
michael@0 | 142 | // list the single file itself |
michael@0 | 143 | if(readContents) { |
michael@0 | 144 | listPkg->addFile(filesPath, listname); |
michael@0 | 145 | } else { |
michael@0 | 146 | listPkg->addItem(listname); |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | return listPkg; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | U_CAPI int U_EXPORT2 |
michael@0 | 154 | writePackageDatFile(const char *outFilename, const char *outComment, const char *sourcePath, const char *addList, Package *pkg, char outType) { |
michael@0 | 155 | Package *addListPkg = NULL; |
michael@0 | 156 | UBool pkgDelete = FALSE; |
michael@0 | 157 | |
michael@0 | 158 | if (pkg == NULL) { |
michael@0 | 159 | pkg = new Package; |
michael@0 | 160 | if(pkg == NULL) { |
michael@0 | 161 | fprintf(stderr, "icupkg: not enough memory\n"); |
michael@0 | 162 | return U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | addListPkg = readList(sourcePath, addList, TRUE, NULL); |
michael@0 | 166 | if(addListPkg != NULL) { |
michael@0 | 167 | pkg->addItems(*addListPkg); |
michael@0 | 168 | } else { |
michael@0 | 169 | return U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | pkgDelete = TRUE; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | pkg->writePackage(outFilename, outType, outComment); |
michael@0 | 176 | |
michael@0 | 177 | if (pkgDelete) { |
michael@0 | 178 | delete pkg; |
michael@0 | 179 | delete addListPkg; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | return 0; |
michael@0 | 183 | } |
michael@0 | 184 |