1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/tools/toolutil/pkg_icu.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +/****************************************************************************** 1.5 + * Copyright (C) 2008-2013, International Business Machines 1.6 + * Corporation and others. All Rights Reserved. 1.7 + ******************************************************************************* 1.8 + */ 1.9 +#include "unicode/utypes.h" 1.10 +#include "unicode/putil.h" 1.11 +#include "cstring.h" 1.12 +#include "toolutil.h" 1.13 +#include "uoptions.h" 1.14 +#include "uparse.h" 1.15 +#include "package.h" 1.16 +#include "pkg_icu.h" 1.17 + 1.18 +#include <stdio.h> 1.19 +#include <stdlib.h> 1.20 +#include <string.h> 1.21 + 1.22 + 1.23 +#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) 1.24 + 1.25 +// read a file list -------------------------------------------------------- *** 1.26 + 1.27 +U_NAMESPACE_USE 1.28 + 1.29 +static const struct { 1.30 + const char *suffix; 1.31 + int32_t length; 1.32 +} listFileSuffixes[]={ 1.33 + { ".txt", 4 }, 1.34 + { ".lst", 4 }, 1.35 + { ".tmp", 4 } 1.36 +}; 1.37 + 1.38 +/* check for multiple text file suffixes to see if this list name is a text file name */ 1.39 +static UBool 1.40 +isListTextFile(const char *listname) { 1.41 + const char *listNameEnd=strchr(listname, 0); 1.42 + const char *suffix; 1.43 + int32_t i, length; 1.44 + for(i=0; i<LENGTHOF(listFileSuffixes); ++i) { 1.45 + suffix=listFileSuffixes[i].suffix; 1.46 + length=listFileSuffixes[i].length; 1.47 + if((listNameEnd-listname)>length && 0==memcmp(listNameEnd-length, suffix, length)) { 1.48 + return TRUE; 1.49 + } 1.50 + } 1.51 + return FALSE; 1.52 +} 1.53 + 1.54 +/* 1.55 + * Read a file list. 1.56 + * If the listname ends with ".txt", then read the list file 1.57 + * (in the system/ invariant charset). 1.58 + * If the listname ends with ".dat", then read the ICU .dat package file. 1.59 + * Otherwise, read the file itself as a single-item list. 1.60 + */ 1.61 +U_CAPI Package * U_EXPORT2 1.62 +readList(const char *filesPath, const char *listname, UBool readContents, Package *listPkgIn) { 1.63 + Package *listPkg = listPkgIn; 1.64 + FILE *file; 1.65 + const char *listNameEnd; 1.66 + 1.67 + if(listname==NULL || listname[0]==0) { 1.68 + fprintf(stderr, "missing list file\n"); 1.69 + return NULL; 1.70 + } 1.71 + 1.72 + if (listPkg == NULL) { 1.73 + listPkg=new Package(); 1.74 + if(listPkg==NULL) { 1.75 + fprintf(stderr, "icupkg: not enough memory\n"); 1.76 + exit(U_MEMORY_ALLOCATION_ERROR); 1.77 + } 1.78 + } 1.79 + 1.80 + listNameEnd=strchr(listname, 0); 1.81 + if(isListTextFile(listname)) { 1.82 + // read the list file 1.83 + char line[1024]; 1.84 + char *end; 1.85 + const char *start; 1.86 + 1.87 + file=fopen(listname, "r"); 1.88 + if(file==NULL) { 1.89 + fprintf(stderr, "icupkg: unable to open list file \"%s\"\n", listname); 1.90 + delete listPkg; 1.91 + exit(U_FILE_ACCESS_ERROR); 1.92 + } 1.93 + 1.94 + while(fgets(line, sizeof(line), file)) { 1.95 + // remove comments 1.96 + end=strchr(line, '#'); 1.97 + if(end!=NULL) { 1.98 + *end=0; 1.99 + } else { 1.100 + // remove trailing CR LF 1.101 + end=strchr(line, 0); 1.102 + while(line<end && (*(end-1)=='\r' || *(end-1)=='\n')) { 1.103 + *--end=0; 1.104 + } 1.105 + } 1.106 + 1.107 + // check first non-whitespace character and 1.108 + // skip empty lines and 1.109 + // skip lines starting with reserved characters 1.110 + start=u_skipWhitespace(line); 1.111 + if(*start==0 || NULL!=strchr(U_PKG_RESERVED_CHARS, *start)) { 1.112 + continue; 1.113 + } 1.114 + 1.115 + // take whitespace-separated items from the line 1.116 + for(;;) { 1.117 + // find whitespace after the item or the end of the line 1.118 + for(end=(char *)start; *end!=0 && *end!=' ' && *end!='\t'; ++end) {} 1.119 + if(*end==0) { 1.120 + // this item is the last one on the line 1.121 + end=NULL; 1.122 + } else { 1.123 + // the item is terminated by whitespace, terminate it with NUL 1.124 + *end=0; 1.125 + } 1.126 + if(readContents) { 1.127 + listPkg->addFile(filesPath, start); 1.128 + } else { 1.129 + listPkg->addItem(start); 1.130 + } 1.131 + 1.132 + // find the start of the next item or exit the loop 1.133 + if(end==NULL || *(start=u_skipWhitespace(end+1))==0) { 1.134 + break; 1.135 + } 1.136 + } 1.137 + } 1.138 + fclose(file); 1.139 + } else if((listNameEnd-listname)>4 && 0==memcmp(listNameEnd-4, ".dat", 4)) { 1.140 + // read the ICU .dat package 1.141 + // Accept a .dat file whose name differs from the ToC prefixes. 1.142 + listPkg->setAutoPrefix(); 1.143 + listPkg->readPackage(listname); 1.144 + } else { 1.145 + // list the single file itself 1.146 + if(readContents) { 1.147 + listPkg->addFile(filesPath, listname); 1.148 + } else { 1.149 + listPkg->addItem(listname); 1.150 + } 1.151 + } 1.152 + 1.153 + return listPkg; 1.154 +} 1.155 + 1.156 +U_CAPI int U_EXPORT2 1.157 +writePackageDatFile(const char *outFilename, const char *outComment, const char *sourcePath, const char *addList, Package *pkg, char outType) { 1.158 + Package *addListPkg = NULL; 1.159 + UBool pkgDelete = FALSE; 1.160 + 1.161 + if (pkg == NULL) { 1.162 + pkg = new Package; 1.163 + if(pkg == NULL) { 1.164 + fprintf(stderr, "icupkg: not enough memory\n"); 1.165 + return U_MEMORY_ALLOCATION_ERROR; 1.166 + } 1.167 + 1.168 + addListPkg = readList(sourcePath, addList, TRUE, NULL); 1.169 + if(addListPkg != NULL) { 1.170 + pkg->addItems(*addListPkg); 1.171 + } else { 1.172 + return U_ILLEGAL_ARGUMENT_ERROR; 1.173 + } 1.174 + 1.175 + pkgDelete = TRUE; 1.176 + } 1.177 + 1.178 + pkg->writePackage(outFilename, outType, outComment); 1.179 + 1.180 + if (pkgDelete) { 1.181 + delete pkg; 1.182 + delete addListPkg; 1.183 + } 1.184 + 1.185 + return 0; 1.186 +} 1.187 +