1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /******************************************************************************
4 * Copyright (C) 2008-2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *******************************************************************************
8 #include "unicode/utypes.h"
9 #include "unicode/localpointer.h"
10 #include "unicode/putil.h"
22 // read a file list -------------------------------------------------------- ***
29 } listFileSuffixes
[]={
35 /* check for multiple text file suffixes to see if this list name is a text file name */
37 isListTextFile(const char *listname
) {
38 const char *listNameEnd
=strchr(listname
, 0);
41 for(i
=0; i
<UPRV_LENGTHOF(listFileSuffixes
); ++i
) {
42 suffix
=listFileSuffixes
[i
].suffix
;
43 length
=listFileSuffixes
[i
].length
;
44 if((listNameEnd
-listname
)>length
&& 0==memcmp(listNameEnd
-length
, suffix
, length
)) {
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.
58 U_CAPI Package
* U_EXPORT2
59 readList(const char *filesPath
, const char *listname
, UBool readContents
, Package
*listPkgIn
) {
60 Package
*listPkg
= listPkgIn
;
62 const char *listNameEnd
;
64 if(listname
==NULL
|| listname
[0]==0) {
65 fprintf(stderr
, "missing list file\n");
69 if (listPkg
== NULL
) {
70 listPkg
=new Package();
72 fprintf(stderr
, "icupkg: not enough memory\n");
73 exit(U_MEMORY_ALLOCATION_ERROR
);
77 listNameEnd
=strchr(listname
, 0);
78 if(isListTextFile(listname
)) {
84 file
=fopen(listname
, "r");
86 fprintf(stderr
, "icupkg: unable to open list file \"%s\"\n", listname
);
88 exit(U_FILE_ACCESS_ERROR
);
91 while(fgets(line
, sizeof(line
), file
)) {
93 end
=strchr(line
, '#');
97 // remove trailing CR LF
99 while(line
<end
&& (*(end
-1)=='\r' || *(end
-1)=='\n')) {
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
)) {
112 // take whitespace-separated items from the line
114 // find whitespace after the item or the end of the line
115 for(end
=(char *)start
; *end
!=0 && *end
!=' ' && *end
!='\t'; ++end
) {}
117 // this item is the last one on the line
120 // the item is terminated by whitespace, terminate it with NUL
124 listPkg
->addFile(filesPath
, start
);
126 listPkg
->addItem(start
);
129 // find the start of the next item or exit the loop
130 if(end
==NULL
|| *(start
=u_skipWhitespace(end
+1))==0) {
136 } else if((listNameEnd
-listname
)>4 && 0==memcmp(listNameEnd
-4, ".dat", 4)) {
137 // read the ICU .dat package
138 // Accept a .dat file whose name differs from the ToC prefixes.
139 listPkg
->setAutoPrefix();
140 listPkg
->readPackage(listname
);
142 // list the single file itself
144 listPkg
->addFile(filesPath
, listname
);
146 listPkg
->addItem(listname
);
154 writePackageDatFile(const char *outFilename
, const char *outComment
, const char *sourcePath
, const char *addList
, Package
*pkg
, char outType
) {
155 LocalPointer
<Package
> ownedPkg
;
156 LocalPointer
<Package
> addListPkg
;
159 ownedPkg
.adoptInstead(new Package
);
160 if(ownedPkg
.isNull()) {
161 fprintf(stderr
, "icupkg: not enough memory\n");
162 return U_MEMORY_ALLOCATION_ERROR
;
164 pkg
= ownedPkg
.getAlias();
166 addListPkg
.adoptInstead(readList(sourcePath
, addList
, TRUE
, NULL
));
167 if(addListPkg
.isValid()) {
168 pkg
->addItems(*addListPkg
);
170 return U_ILLEGAL_ARGUMENT_ERROR
;
174 pkg
->writePackage(outFilename
, outType
, outComment
);