1 /******************************************************************************
2 * Copyright (C) 2008-2015, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *******************************************************************************
6 #include "unicode/utypes.h"
7 #include "unicode/localpointer.h"
8 #include "unicode/putil.h"
20 // read a file list -------------------------------------------------------- ***
27 } listFileSuffixes
[]={
33 /* check for multiple text file suffixes to see if this list name is a text file name */
35 isListTextFile(const char *listname
) {
36 const char *listNameEnd
=strchr(listname
, 0);
39 for(i
=0; i
<UPRV_LENGTHOF(listFileSuffixes
); ++i
) {
40 suffix
=listFileSuffixes
[i
].suffix
;
41 length
=listFileSuffixes
[i
].length
;
42 if((listNameEnd
-listname
)>length
&& 0==memcmp(listNameEnd
-length
, suffix
, length
)) {
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.
56 U_CAPI Package
* U_EXPORT2
57 readList(const char *filesPath
, const char *listname
, UBool readContents
, Package
*listPkgIn
) {
58 Package
*listPkg
= listPkgIn
;
60 const char *listNameEnd
;
62 if(listname
==NULL
|| listname
[0]==0) {
63 fprintf(stderr
, "missing list file\n");
67 if (listPkg
== NULL
) {
68 listPkg
=new Package();
70 fprintf(stderr
, "icupkg: not enough memory\n");
71 exit(U_MEMORY_ALLOCATION_ERROR
);
75 listNameEnd
=strchr(listname
, 0);
76 if(isListTextFile(listname
)) {
82 file
=fopen(listname
, "r");
84 fprintf(stderr
, "icupkg: unable to open list file \"%s\"\n", listname
);
86 exit(U_FILE_ACCESS_ERROR
);
89 while(fgets(line
, sizeof(line
), file
)) {
91 end
=strchr(line
, '#');
95 // remove trailing CR LF
97 while(line
<end
&& (*(end
-1)=='\r' || *(end
-1)=='\n')) {
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
)) {
110 // take whitespace-separated items from the line
112 // find whitespace after the item or the end of the line
113 for(end
=(char *)start
; *end
!=0 && *end
!=' ' && *end
!='\t'; ++end
) {}
115 // this item is the last one on the line
118 // the item is terminated by whitespace, terminate it with NUL
122 listPkg
->addFile(filesPath
, start
);
124 listPkg
->addItem(start
);
127 // find the start of the next item or exit the loop
128 if(end
==NULL
|| *(start
=u_skipWhitespace(end
+1))==0) {
134 } else if((listNameEnd
-listname
)>4 && 0==memcmp(listNameEnd
-4, ".dat", 4)) {
135 // read the ICU .dat package
136 // Accept a .dat file whose name differs from the ToC prefixes.
137 listPkg
->setAutoPrefix();
138 listPkg
->readPackage(listname
);
140 // list the single file itself
142 listPkg
->addFile(filesPath
, listname
);
144 listPkg
->addItem(listname
);
152 writePackageDatFile(const char *outFilename
, const char *outComment
, const char *sourcePath
, const char *addList
, Package
*pkg
, char outType
) {
153 LocalPointer
<Package
> ownedPkg
;
154 LocalPointer
<Package
> addListPkg
;
157 ownedPkg
.adoptInstead(new Package
);
158 if(ownedPkg
.isNull()) {
159 fprintf(stderr
, "icupkg: not enough memory\n");
160 return U_MEMORY_ALLOCATION_ERROR
;
162 pkg
= ownedPkg
.getAlias();
164 addListPkg
.adoptInstead(readList(sourcePath
, addList
, TRUE
, NULL
));
165 if(addListPkg
.isValid()) {
166 pkg
->addItems(*addListPkg
);
168 return U_ILLEGAL_ARGUMENT_ERROR
;
172 pkg
->writePackage(outFilename
, outType
, outComment
);