1 /******************************************************************************
2 * Copyright (C) 2008-2012, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *******************************************************************************
6 #include "unicode/utypes.h"
7 #include "unicode/putil.h"
20 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
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
<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 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 Package
*addListPkg
= NULL
;
154 UBool pkgDelete
= FALSE
;
159 fprintf(stderr
, "icupkg: not enough memory\n");
160 return U_MEMORY_ALLOCATION_ERROR
;
163 addListPkg
= readList(sourcePath
, addList
, TRUE
, NULL
);
164 if(addListPkg
!= NULL
) {
165 pkg
->addItems(*addListPkg
);
167 return U_ILLEGAL_ARGUMENT_ERROR
;
173 pkg
->writePackage(outFilename
, outType
, outComment
);