michael@0: /****************************************************************************** michael@0: * Copyright (C) 2008-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/putil.h" michael@0: #include "cstring.h" michael@0: #include "toolutil.h" michael@0: #include "uoptions.h" michael@0: #include "uparse.h" michael@0: #include "package.h" michael@0: #include "pkg_icu.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: michael@0: #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) michael@0: michael@0: // read a file list -------------------------------------------------------- *** michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: static const struct { michael@0: const char *suffix; michael@0: int32_t length; michael@0: } listFileSuffixes[]={ michael@0: { ".txt", 4 }, michael@0: { ".lst", 4 }, michael@0: { ".tmp", 4 } michael@0: }; michael@0: michael@0: /* check for multiple text file suffixes to see if this list name is a text file name */ michael@0: static UBool michael@0: isListTextFile(const char *listname) { michael@0: const char *listNameEnd=strchr(listname, 0); michael@0: const char *suffix; michael@0: int32_t i, length; michael@0: for(i=0; ilength && 0==memcmp(listNameEnd-length, suffix, length)) { michael@0: return TRUE; michael@0: } michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: /* michael@0: * Read a file list. michael@0: * If the listname ends with ".txt", then read the list file michael@0: * (in the system/ invariant charset). michael@0: * If the listname ends with ".dat", then read the ICU .dat package file. michael@0: * Otherwise, read the file itself as a single-item list. michael@0: */ michael@0: U_CAPI Package * U_EXPORT2 michael@0: readList(const char *filesPath, const char *listname, UBool readContents, Package *listPkgIn) { michael@0: Package *listPkg = listPkgIn; michael@0: FILE *file; michael@0: const char *listNameEnd; michael@0: michael@0: if(listname==NULL || listname[0]==0) { michael@0: fprintf(stderr, "missing list file\n"); michael@0: return NULL; michael@0: } michael@0: michael@0: if (listPkg == NULL) { michael@0: listPkg=new Package(); michael@0: if(listPkg==NULL) { michael@0: fprintf(stderr, "icupkg: not enough memory\n"); michael@0: exit(U_MEMORY_ALLOCATION_ERROR); michael@0: } michael@0: } michael@0: michael@0: listNameEnd=strchr(listname, 0); michael@0: if(isListTextFile(listname)) { michael@0: // read the list file michael@0: char line[1024]; michael@0: char *end; michael@0: const char *start; michael@0: michael@0: file=fopen(listname, "r"); michael@0: if(file==NULL) { michael@0: fprintf(stderr, "icupkg: unable to open list file \"%s\"\n", listname); michael@0: delete listPkg; michael@0: exit(U_FILE_ACCESS_ERROR); michael@0: } michael@0: michael@0: while(fgets(line, sizeof(line), file)) { michael@0: // remove comments michael@0: end=strchr(line, '#'); michael@0: if(end!=NULL) { michael@0: *end=0; michael@0: } else { michael@0: // remove trailing CR LF michael@0: end=strchr(line, 0); michael@0: while(lineaddFile(filesPath, start); michael@0: } else { michael@0: listPkg->addItem(start); michael@0: } michael@0: michael@0: // find the start of the next item or exit the loop michael@0: if(end==NULL || *(start=u_skipWhitespace(end+1))==0) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: fclose(file); michael@0: } else if((listNameEnd-listname)>4 && 0==memcmp(listNameEnd-4, ".dat", 4)) { michael@0: // read the ICU .dat package michael@0: // Accept a .dat file whose name differs from the ToC prefixes. michael@0: listPkg->setAutoPrefix(); michael@0: listPkg->readPackage(listname); michael@0: } else { michael@0: // list the single file itself michael@0: if(readContents) { michael@0: listPkg->addFile(filesPath, listname); michael@0: } else { michael@0: listPkg->addItem(listname); michael@0: } michael@0: } michael@0: michael@0: return listPkg; michael@0: } michael@0: michael@0: U_CAPI int U_EXPORT2 michael@0: writePackageDatFile(const char *outFilename, const char *outComment, const char *sourcePath, const char *addList, Package *pkg, char outType) { michael@0: Package *addListPkg = NULL; michael@0: UBool pkgDelete = FALSE; michael@0: michael@0: if (pkg == NULL) { michael@0: pkg = new Package; michael@0: if(pkg == NULL) { michael@0: fprintf(stderr, "icupkg: not enough memory\n"); michael@0: return U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: michael@0: addListPkg = readList(sourcePath, addList, TRUE, NULL); michael@0: if(addListPkg != NULL) { michael@0: pkg->addItems(*addListPkg); michael@0: } else { michael@0: return U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: michael@0: pkgDelete = TRUE; michael@0: } michael@0: michael@0: pkg->writePackage(outFilename, outType, outComment); michael@0: michael@0: if (pkgDelete) { michael@0: delete pkg; michael@0: delete addListPkg; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: