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