1 /******************************************************************************
2 * Copyright (C) 2008-2014, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *******************************************************************************
6 #include "unicode/utypes.h"
7 #include "unicode/putil.h"
19 // read a file list -------------------------------------------------------- ***
26 } listFileSuffixes
[]={
32 /* check for multiple text file suffixes to see if this list name is a text file name */
34 isListTextFile(const char *listname
) {
35 const char *listNameEnd
=strchr(listname
, 0);
38 for(i
=0; i
<UPRV_LENGTHOF(listFileSuffixes
); ++i
) {
39 suffix
=listFileSuffixes
[i
].suffix
;
40 length
=listFileSuffixes
[i
].length
;
41 if((listNameEnd
-listname
)>length
&& 0==memcmp(listNameEnd
-length
, suffix
, length
)) {
50 * If the listname ends with ".txt", then read the list file
51 * (in the system/ invariant charset).
52 * If the listname ends with ".dat", then read the ICU .dat package file.
53 * Otherwise, read the file itself as a single-item list.
55 U_CAPI Package
* U_EXPORT2
56 readList(const char *filesPath
, const char *listname
, UBool readContents
, Package
*listPkgIn
) {
57 Package
*listPkg
= listPkgIn
;
59 const char *listNameEnd
;
61 if(listname
==NULL
|| listname
[0]==0) {
62 fprintf(stderr
, "missing list file\n");
66 if (listPkg
== NULL
) {
67 listPkg
=new Package();
69 fprintf(stderr
, "icupkg: not enough memory\n");
70 exit(U_MEMORY_ALLOCATION_ERROR
);
74 listNameEnd
=strchr(listname
, 0);
75 if(isListTextFile(listname
)) {
81 file
=fopen(listname
, "r");
83 fprintf(stderr
, "icupkg: unable to open list file \"%s\"\n", listname
);
85 exit(U_FILE_ACCESS_ERROR
);
88 while(fgets(line
, sizeof(line
), file
)) {
90 end
=strchr(line
, '#');
94 // remove trailing CR LF
96 while(line
<end
&& (*(end
-1)=='\r' || *(end
-1)=='\n')) {
101 // check first non-whitespace character and
102 // skip empty lines and
103 // skip lines starting with reserved characters
104 start
=u_skipWhitespace(line
);
105 if(*start
==0 || NULL
!=strchr(U_PKG_RESERVED_CHARS
, *start
)) {
109 // take whitespace-separated items from the line
111 // find whitespace after the item or the end of the line
112 for(end
=(char *)start
; *end
!=0 && *end
!=' ' && *end
!='\t'; ++end
) {}
114 // this item is the last one on the line
117 // the item is terminated by whitespace, terminate it with NUL
121 listPkg
->addFile(filesPath
, start
);
123 listPkg
->addItem(start
);
126 // find the start of the next item or exit the loop
127 if(end
==NULL
|| *(start
=u_skipWhitespace(end
+1))==0) {
133 } else if((listNameEnd
-listname
)>4 && 0==memcmp(listNameEnd
-4, ".dat", 4)) {
134 // read the ICU .dat package
135 // Accept a .dat file whose name differs from the ToC prefixes.
136 listPkg
->setAutoPrefix();
137 listPkg
->readPackage(listname
);
139 // list the single file itself
141 listPkg
->addFile(filesPath
, listname
);
143 listPkg
->addItem(listname
);
151 writePackageDatFile(const char *outFilename
, const char *outComment
, const char *sourcePath
, const char *addList
, Package
*pkg
, char outType
) {
152 Package
*addListPkg
= NULL
;
153 UBool pkgDelete
= FALSE
;
158 fprintf(stderr
, "icupkg: not enough memory\n");
159 return U_MEMORY_ALLOCATION_ERROR
;
162 addListPkg
= readList(sourcePath
, addList
, TRUE
, NULL
);
163 if(addListPkg
!= NULL
) {
164 pkg
->addItems(*addListPkg
);
166 return U_ILLEGAL_ARGUMENT_ERROR
;
172 pkg
->writePackage(outFilename
, outType
, outComment
);