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