2 *******************************************************************************
4 * Copyright (C) 2005-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: icupkg.cpp
10 * tab size: 8 (not used)
13 * created on: 2005jul29
14 * created by: Markus W. Scherer
16 * This tool operates on ICU data (.dat package) files.
17 * It takes one as input, or creates an empty one, and can remove, add, and
18 * extract data pieces according to command-line options.
19 * At the same time, it swaps each piece to a consistent set of platform
20 * properties as desired.
21 * Useful as an install-time tool for shipping only one flavor of ICU data
22 * and preparing data files for the target platform.
23 * Also for customizing ICU data (pruning, augmenting, replacing) and for
25 * Subsumes functionality and implementation code from
26 * gencmn, decmn, and icuswap tools.
27 * Will not work with data DLLs (shared libraries).
30 #include "unicode/utypes.h"
31 #include "unicode/putil.h"
46 // TODO: add --matchmode=regex for using the ICU regex engine for item name pattern matching?
48 // general definitions ----------------------------------------------------- ***
50 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
52 // main() ------------------------------------------------------------------ ***
55 printUsage(const char *pname
, UBool isHelp
) {
56 FILE *where
=isHelp
? stdout
: stderr
;
59 "%csage: %s [-h|-?|--help ] [-tl|-tb|-te] [-c] [-C comment]\n"
60 "\t[-a list] [-r list] [-x list] [-l [-o outputListFileName]]\n"
61 "\t[-s path] [-d path] [-w] [-m mode]\n"
62 "\tinfilename [outfilename]\n",
63 isHelp
? 'U' : 'u', pname
);
67 "Read the input ICU .dat package file, modify it according to the options,\n"
68 "swap it to the desired platform properties (charset & endianness),\n"
69 "and optionally write the resulting ICU .dat package to the output file.\n"
70 "Items are removed, then added, then extracted and listed.\n"
71 "An ICU .dat package is written if items are removed or added,\n"
72 "or if the input and output filenames differ,\n"
73 "or if the --writepkg (-w) option is set.\n");
76 "If the input filename is \"new\" then an empty package is created.\n"
77 "If the output filename is missing, then it is automatically generated\n"
78 "from the input filename: If the input filename ends with an l, b, or e\n"
79 "matching its platform properties, then the output filename will\n"
80 "contain the letter from the -t (--type) option.\n");
83 "This tool can also be used to just swap a single ICU data file, replacing the\n"
84 "former icuswap tool. For this mode, provide the infilename (and optional\n"
85 "outfilename) for a non-package ICU data file.\n"
86 "Allowed options include -t, -w, -s and -d.\n"
87 "The filenames can be absolute, or relative to the source/dest dir paths.\n"
88 "Other options are not allowed in this mode.\n");
92 "\t(Only the last occurrence of an option is used.)\n"
94 "\t-h or -? or --help print this message and exit\n");
97 "\t-tl or --type l output for little-endian/ASCII charset family\n"
98 "\t-tb or --type b output for big-endian/ASCII charset family\n"
99 "\t-te or --type e output for big-endian/EBCDIC charset family\n"
100 "\t The output type defaults to the input type.\n"
102 "\t-c or --copyright include the ICU copyright notice\n"
103 "\t-C comment or --comment comment include a comment string\n");
106 "\t-a list or --add list add items to the package\n"
107 "\t-r list or --remove list remove items from the package\n"
108 "\t-x list or --extract list extract items from the package\n"
109 "\tThe list can be a single item's filename,\n"
110 "\tor a .txt filename with a list of item filenames,\n"
111 "\tor an ICU .dat package filename.\n");
114 "\t-w or --writepkg write the output package even if no items are removed\n"
115 "\t or added (e.g., for only swapping the data)\n");
118 "\t-m mode or --matchmode mode set the matching mode for item names with\n"
120 "\t noslash: the '*' wildcard does not match the '/' tree separator\n");
122 * Usage text columns, starting after the initial TAB.
124 * 901234567890123456789012345678901234567890123456789012345678901234567890
128 "\tList file syntax: Items are listed on one or more lines and separated\n"
129 "\tby whitespace (space+tab).\n"
130 "\tComments begin with # and are ignored. Empty lines are ignored.\n"
131 "\tLines where the first non-whitespace character is one of %s\n"
132 "\tare also ignored, to reserve for future syntax.\n",
133 U_PKG_RESERVED_CHARS
);
135 "\tItems for removal or extraction may contain a single '*' wildcard\n"
136 "\tcharacter. The '*' matches zero or more characters.\n"
137 "\tIf --matchmode noslash (-m noslash) is set, then the '*'\n"
138 "\tdoes not match '/'.\n");
141 "\tItems must be listed relative to the package, and the --sourcedir or\n"
142 "\tthe --destdir path will be prepended.\n"
143 "\tThe paths are only prepended to item filenames while adding or\n"
144 "\textracting items, not to ICU .dat package or list filenames.\n"
146 "\tPaths may contain '/' instead of the platform's\n"
147 "\tfile separator character, and are converted as appropriate.\n");
150 "\t-s path or --sourcedir path directory for the --add items\n"
151 "\t-d path or --destdir path directory for the --extract items\n"
153 "\t-l or --list list the package items to stdout or to output list file\n"
154 "\t (after modifying the package)\n");
158 static UOption options
[]={
160 UOPTION_HELP_QUESTION_MARK
,
161 UOPTION_DEF("type", 't', UOPT_REQUIRES_ARG
),
164 UOPTION_DEF("comment", 'C', UOPT_REQUIRES_ARG
),
169 UOPTION_DEF("writepkg", 'w', UOPT_NO_ARG
),
171 UOPTION_DEF("matchmode", 'm', UOPT_REQUIRES_ARG
),
173 UOPTION_DEF("add", 'a', UOPT_REQUIRES_ARG
),
174 UOPTION_DEF("remove", 'r', UOPT_REQUIRES_ARG
),
175 UOPTION_DEF("extract", 'x', UOPT_REQUIRES_ARG
),
177 UOPTION_DEF("list", 'l', UOPT_NO_ARG
),
179 UOPTION_DEF("outlist", 'o', UOPT_REQUIRES_ARG
)
184 OPT_HELP_QUESTION_MARK
,
209 isPackageName(const char *filename
) {
212 len
=(int32_t)strlen(filename
)-4; /* -4: subtract the length of ".dat" */
213 return (UBool
)(len
>0 && 0==strcmp(filename
+len
, ".dat"));
216 This line is required by MinGW because it incorrectly globs the arguments.
217 So when \* is used, it turns into a list of files instead of a literal "*"
222 main(int argc
, char *argv
[]) {
223 const char *pname
, *sourcePath
, *destPath
, *inFilename
, *outFilename
, *outComment
;
225 UBool isHelp
, isModified
, isPackage
;
228 Package
*pkg
, *listPkg
, *addListPkg
;
230 U_MAIN_INIT_ARGS(argc
, argv
);
232 /* get the program basename */
233 pname
=findBasename(argv
[0]);
235 argc
=u_parseArgs(argc
, argv
, LENGTHOF(options
), options
);
236 isHelp
=options
[OPT_HELP_H
].doesOccur
|| options
[OPT_HELP_QUESTION_MARK
].doesOccur
;
238 printUsage(pname
, TRUE
);
241 if(argc
<2 || 3<argc
) {
242 printUsage(pname
, FALSE
);
243 return U_ILLEGAL_ARGUMENT_ERROR
;
248 fprintf(stderr
, "icupkg: not enough memory\n");
249 return U_MEMORY_ALLOCATION_ERROR
;
253 if(options
[OPT_SOURCEDIR
].doesOccur
) {
254 sourcePath
=options
[OPT_SOURCEDIR
].value
;
256 // work relative to the current working directory
259 if(options
[OPT_DESTDIR
].doesOccur
) {
260 destPath
=options
[OPT_DESTDIR
].value
;
262 // work relative to the current working directory
266 if(0==strcmp(argv
[1], "new")) {
271 if(isPackageName(inFilename
)) {
272 pkg
->readPackage(inFilename
);
275 /* swap a single file (icuswap replacement) rather than work on a package */
276 pkg
->addFile(sourcePath
, inFilename
);
283 if(0!=strcmp(argv
[1], argv
[2])) {
286 } else if(isPackage
) {
288 } else /* !isPackage */ {
289 outFilename
=inFilename
;
290 isModified
=(UBool
)(sourcePath
!=destPath
);
293 /* parse the output type option */
294 if(options
[OPT_OUT_TYPE
].doesOccur
) {
295 const char *type
=options
[OPT_OUT_TYPE
].value
;
296 if(type
[0]==0 || type
[1]!=0) {
297 /* the type must be exactly one letter */
298 printUsage(pname
, FALSE
);
299 return U_ILLEGAL_ARGUMENT_ERROR
;
308 printUsage(pname
, FALSE
);
309 return U_ILLEGAL_ARGUMENT_ERROR
;
313 * Set the isModified flag if the output type differs from the
314 * input package type.
315 * If we swap a single file, just assume that we are modifying it.
316 * The Package class does not give us access to the item and its type.
318 isModified
|=(UBool
)(!isPackage
|| outType
!=pkg
->getInType());
319 } else if(isPackage
) {
320 outType
=pkg
->getInType(); // default to input type
321 } else /* !isPackage: swap single file */ {
322 outType
=0; /* tells extractItem() to not swap */
325 if(options
[OPT_WRITEPKG
].doesOccur
) {
331 * icuswap tool replacement: Only swap a single file.
332 * Check that irrelevant options are not set.
334 if( options
[OPT_COMMENT
].doesOccur
||
335 options
[OPT_COPYRIGHT
].doesOccur
||
336 options
[OPT_MATCHMODE
].doesOccur
||
337 options
[OPT_REMOVE_LIST
].doesOccur
||
338 options
[OPT_ADD_LIST
].doesOccur
||
339 options
[OPT_EXTRACT_LIST
].doesOccur
||
340 options
[OPT_LIST_ITEMS
].doesOccur
342 printUsage(pname
, FALSE
);
343 return U_ILLEGAL_ARGUMENT_ERROR
;
346 pkg
->extractItem(destPath
, outFilename
, 0, outType
);
353 /* Work with a package. */
355 if(options
[OPT_COMMENT
].doesOccur
) {
356 outComment
=options
[OPT_COMMENT
].value
;
357 } else if(options
[OPT_COPYRIGHT
].doesOccur
) {
358 outComment
=U_COPYRIGHT_STRING
;
363 if(options
[OPT_MATCHMODE
].doesOccur
) {
364 if(0==strcmp(options
[OPT_MATCHMODE
].value
, "noslash")) {
365 pkg
->setMatchMode(Package::MATCH_NOSLASH
);
367 printUsage(pname
, FALSE
);
368 return U_ILLEGAL_ARGUMENT_ERROR
;
373 if(options
[OPT_REMOVE_LIST
].doesOccur
) {
374 listPkg
=new Package();
376 fprintf(stderr
, "icupkg: not enough memory\n");
377 exit(U_MEMORY_ALLOCATION_ERROR
);
379 if(readList(NULL
, options
[OPT_REMOVE_LIST
].value
, FALSE
, listPkg
)) {
380 pkg
->removeItems(*listPkg
);
384 printUsage(pname
, FALSE
);
385 return U_ILLEGAL_ARGUMENT_ERROR
;
391 * use a separate Package so that its memory and items stay around
392 * as long as the main Package
395 if(options
[OPT_ADD_LIST
].doesOccur
) {
396 addListPkg
=new Package();
397 if(addListPkg
==NULL
) {
398 fprintf(stderr
, "icupkg: not enough memory\n");
399 exit(U_MEMORY_ALLOCATION_ERROR
);
401 if(readList(sourcePath
, options
[OPT_ADD_LIST
].value
, TRUE
, addListPkg
)) {
402 pkg
->addItems(*addListPkg
);
403 // delete addListPkg; deferred until after writePackage()
406 printUsage(pname
, FALSE
);
407 return U_ILLEGAL_ARGUMENT_ERROR
;
412 if(options
[OPT_EXTRACT_LIST
].doesOccur
) {
413 listPkg
=new Package();
415 fprintf(stderr
, "icupkg: not enough memory\n");
416 exit(U_MEMORY_ALLOCATION_ERROR
);
418 if(readList(NULL
, options
[OPT_EXTRACT_LIST
].value
, FALSE
, listPkg
)) {
419 pkg
->extractItems(destPath
, *listPkg
, outType
);
422 printUsage(pname
, FALSE
);
423 return U_ILLEGAL_ARGUMENT_ERROR
;
428 if(options
[OPT_LIST_ITEMS
].doesOccur
) {
430 if (options
[OPT_LIST_FILE
].doesOccur
) {
432 out
= T_FileStream_open(options
[OPT_LIST_FILE
].value
, "w");
434 for(i
=0; i
<pkg
->getItemCount(); ++i
) {
435 T_FileStream_writeLine(out
, pkg
->getItem(i
)->name
);
436 T_FileStream_writeLine(out
, "\n");
438 T_FileStream_close(out
);
440 return U_ILLEGAL_ARGUMENT_ERROR
;
443 for(i
=0; i
<pkg
->getItemCount(); ++i
) {
444 fprintf(stdout
, "%s\n", pkg
->getItem(i
)->name
);
449 /* check dependencies between items */
450 if(!pkg
->checkDependencies()) {
451 /* some dependencies are not fulfilled */
452 return U_MISSING_RESOURCE_ERROR
;
455 /* write the output .dat package if there are any modifications */
457 char outFilenameBuffer
[1024]; // for auto-generated output filename, if necessary
459 if(outFilename
==NULL
|| outFilename
[0]==0) {
460 if(inFilename
==NULL
|| inFilename
[0]==0) {
461 fprintf(stderr
, "icupkg: unable to auto-generate an output filename if there is no input filename\n");
462 exit(U_ILLEGAL_ARGUMENT_ERROR
);
466 * auto-generate a filename:
467 * copy the inFilename,
468 * and if the last basename character matches the input file's type,
469 * then replace it with the output file's type
471 char suffix
[6]="?.dat";
474 suffix
[0]=pkg
->getInType();
475 strcpy(outFilenameBuffer
, inFilename
);
476 s
=strchr(outFilenameBuffer
, 0);
477 if((s
-outFilenameBuffer
)>5 && 0==memcmp(s
-5, suffix
, 5)) {
480 outFilename
=outFilenameBuffer
;
482 result
= writePackageDatFile(outFilename
, outComment
, NULL
, NULL
, pkg
, outType
);
491 * Hey, Emacs, please set the following:
494 * indent-tabs-mode: nil