]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/genrb/genrb.cpp
ICU-57163.0.1.tar.gz
[apple/icu.git] / icuSources / tools / genrb / genrb.cpp
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1998-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 *
9 * File genrb.cpp
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 05/25/99 stephen Creation.
15 * 5/10/01 Ram removed ustdio dependency
16 *******************************************************************************
17 */
18
19 #include <assert.h>
20 #include "genrb.h"
21 #include "unicode/localpointer.h"
22 #include "unicode/uclean.h"
23 #include "unicode/utf16.h"
24 #include "charstr.h"
25 #include "cmemory.h"
26 #include "reslist.h"
27 #include "ucmndata.h" /* TODO: for reading the pool bundle */
28
29 U_NAMESPACE_USE
30
31 /* Protos */
32 void processFile(const char *filename, const char* cp, const char *inputDir, const char *outputDir,
33 const char *packageName,
34 SRBRoot *newPoolBundle, UBool omitBinaryCollation, UErrorCode &status);
35 static char *make_res_filename(const char *filename, const char *outputDir,
36 const char *packageName, UErrorCode &status);
37
38 /* File suffixes */
39 #define RES_SUFFIX ".res"
40 #define COL_SUFFIX ".col"
41
42 const char *gCurrentFileName = NULL;
43 #ifdef XP_MAC_CONSOLE
44 #include <console.h>
45 #endif
46
47 void ResFile::close() {
48 delete[] fBytes;
49 fBytes = NULL;
50 delete fStrings;
51 fStrings = NULL;
52 }
53
54 enum
55 {
56 HELP1,
57 HELP2,
58 VERBOSE,
59 QUIET,
60 VERSION,
61 SOURCEDIR,
62 DESTDIR,
63 ENCODING,
64 ICUDATADIR,
65 WRITE_JAVA,
66 COPYRIGHT,
67 JAVA_PACKAGE,
68 BUNDLE_NAME,
69 WRITE_XLIFF,
70 STRICT,
71 NO_BINARY_COLLATION,
72 LANGUAGE,
73 NO_COLLATION_RULES,
74 FORMAT_VERSION,
75 WRITE_POOL_BUNDLE,
76 USE_POOL_BUNDLE,
77 INCLUDE_UNIHAN_COLL
78 };
79
80 UOption options[]={
81 UOPTION_HELP_H,
82 UOPTION_HELP_QUESTION_MARK,
83 UOPTION_VERBOSE,
84 UOPTION_QUIET,
85 UOPTION_VERSION,
86 UOPTION_SOURCEDIR,
87 UOPTION_DESTDIR,
88 UOPTION_ENCODING,
89 UOPTION_ICUDATADIR,
90 UOPTION_WRITE_JAVA,
91 UOPTION_COPYRIGHT,
92 UOPTION_DEF("java-package", '\x01', UOPT_REQUIRES_ARG),
93 UOPTION_BUNDLE_NAME,
94 UOPTION_DEF("write-xliff", 'x', UOPT_OPTIONAL_ARG),
95 UOPTION_DEF("strict", 'k', UOPT_NO_ARG), /* 14 */
96 UOPTION_DEF("noBinaryCollation", 'C', UOPT_NO_ARG),/* 15 */
97 UOPTION_DEF("language", 'l', UOPT_REQUIRES_ARG), /* 16 */
98 UOPTION_DEF("omitCollationRules", 'R', UOPT_NO_ARG),/* 17 */
99 UOPTION_DEF("formatVersion", '\x01', UOPT_REQUIRES_ARG),/* 18 */
100 UOPTION_DEF("writePoolBundle", '\x01', UOPT_NO_ARG),/* 19 */
101 UOPTION_DEF("usePoolBundle", '\x01', UOPT_OPTIONAL_ARG),/* 20 */
102 UOPTION_DEF("includeUnihanColl", '\x01', UOPT_NO_ARG),/* 21 */ /* temporary, don't display in usage info */
103 };
104
105 static UBool write_java = FALSE;
106 static UBool write_xliff = FALSE;
107 static const char* outputEnc ="";
108
109 static ResFile poolBundle;
110
111 /*added by Jing*/
112 static const char* language = NULL;
113 static const char* xliffOutputFileName = NULL;
114 int
115 main(int argc,
116 char* argv[])
117 {
118 UErrorCode status = U_ZERO_ERROR;
119 const char *arg = NULL;
120 const char *outputDir = NULL; /* NULL = no output directory, use current */
121 const char *inputDir = NULL;
122 const char *encoding = "";
123 int i;
124 UBool illegalArg = FALSE;
125
126 U_MAIN_INIT_ARGS(argc, argv);
127
128 options[JAVA_PACKAGE].value = "com.ibm.icu.impl.data";
129 options[BUNDLE_NAME].value = "LocaleElements";
130 argc = u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
131
132 /* error handling, printing usage message */
133 if(argc<0) {
134 fprintf(stderr, "%s: error in command line argument \"%s\"\n", argv[0], argv[-argc]);
135 illegalArg = TRUE;
136 } else if(argc<2) {
137 illegalArg = TRUE;
138 }
139 if(options[WRITE_POOL_BUNDLE].doesOccur && options[USE_POOL_BUNDLE].doesOccur) {
140 fprintf(stderr, "%s: cannot combine --writePoolBundle and --usePoolBundle\n", argv[0]);
141 illegalArg = TRUE;
142 }
143 if(options[FORMAT_VERSION].doesOccur) {
144 const char *s = options[FORMAT_VERSION].value;
145 if(uprv_strlen(s) != 1 || (s[0] < '1' && '3' < s[0])) {
146 fprintf(stderr, "%s: unsupported --formatVersion %s\n", argv[0], s);
147 illegalArg = TRUE;
148 } else if(s[0] == '1' &&
149 (options[WRITE_POOL_BUNDLE].doesOccur || options[USE_POOL_BUNDLE].doesOccur)
150 ) {
151 fprintf(stderr, "%s: cannot combine --formatVersion 1 with --writePoolBundle or --usePoolBundle\n", argv[0]);
152 illegalArg = TRUE;
153 } else {
154 setFormatVersion(s[0] - '0');
155 }
156 }
157
158 if((options[JAVA_PACKAGE].doesOccur || options[BUNDLE_NAME].doesOccur) &&
159 !options[WRITE_JAVA].doesOccur) {
160 fprintf(stderr,
161 "%s error: command line argument --java-package or --bundle-name "
162 "without --write-java\n",
163 argv[0]);
164 illegalArg = TRUE;
165 }
166
167 if(options[VERSION].doesOccur) {
168 fprintf(stderr,
169 "%s version %s (ICU version %s).\n"
170 "%s\n",
171 argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING);
172 if(!illegalArg) {
173 return U_ZERO_ERROR;
174 }
175 }
176
177 if(illegalArg || options[HELP1].doesOccur || options[HELP2].doesOccur) {
178 /*
179 * Broken into chunks because the C89 standard says the minimum
180 * required supported string length is 509 bytes.
181 */
182 fprintf(stderr,
183 "Usage: %s [OPTIONS] [FILES]\n"
184 "\tReads the list of resource bundle source files and creates\n"
185 "\tbinary version of resource bundles (.res files)\n",
186 argv[0]);
187 fprintf(stderr,
188 "Options:\n"
189 "\t-h or -? or --help this usage text\n"
190 "\t-q or --quiet do not display warnings\n"
191 "\t-v or --verbose print extra information when processing files\n"
192 "\t-V or --version prints out version number and exits\n"
193 "\t-c or --copyright include copyright notice\n");
194 fprintf(stderr,
195 "\t-e or --encoding encoding of source files\n"
196 "\t-d of --destdir destination directory, followed by the path, defaults to %s\n"
197 "\t-s or --sourcedir source directory for files followed by path, defaults to %s\n"
198 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
199 "\t followed by path, defaults to %s\n",
200 u_getDataDirectory(), u_getDataDirectory(), u_getDataDirectory());
201 fprintf(stderr,
202 "\t-j or --write-java write a Java ListResourceBundle for ICU4J, followed by optional encoding\n"
203 "\t defaults to ASCII and \\uXXXX format.\n"
204 "\t --java-package For --write-java: package name for writing the ListResourceBundle,\n"
205 "\t defaults to com.ibm.icu.impl.data\n");
206 fprintf(stderr,
207 "\t-b or --bundle-name For --write-java: root resource bundle name for writing the ListResourceBundle,\n"
208 "\t defaults to LocaleElements\n"
209 "\t-x or --write-xliff write an XLIFF file for the resource bundle. Followed by\n"
210 "\t an optional output file name.\n"
211 "\t-k or --strict use pedantic parsing of syntax\n"
212 /*added by Jing*/
213 "\t-l or --language for XLIFF: language code compliant with BCP 47.\n");
214 fprintf(stderr,
215 "\t-C or --noBinaryCollation do not generate binary collation image;\n"
216 "\t makes .res file smaller but collator instantiation much slower;\n"
217 "\t maintains ability to get tailoring rules\n"
218 "\t-R or --omitCollationRules do not include collation (tailoring) rules;\n"
219 "\t makes .res file smaller and maintains collator instantiation speed\n"
220 "\t but tailoring rules will not be available (they are rarely used)\n");
221 fprintf(stderr,
222 "\t --formatVersion write a .res file compatible with the requested formatVersion (single digit);\n"
223 "\t for example, --formatVersion 1\n");
224 fprintf(stderr,
225 "\t --writePoolBundle write a pool.res file with all of the keys of all input bundles\n"
226 "\t --usePoolBundle [path-to-pool.res] point to keys from the pool.res keys pool bundle if they are available there;\n"
227 "\t makes .res files smaller but dependent on the pool bundle\n"
228 "\t (--writePoolBundle and --usePoolBundle cannot be combined)\n");
229
230 return illegalArg ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
231 }
232
233 if(options[VERBOSE].doesOccur) {
234 setVerbose(TRUE);
235 }
236
237 if(options[QUIET].doesOccur) {
238 setShowWarning(FALSE);
239 }
240 if(options[STRICT].doesOccur) {
241 setStrict(TRUE);
242 }
243 if(options[COPYRIGHT].doesOccur){
244 setIncludeCopyright(TRUE);
245 }
246
247 if(options[SOURCEDIR].doesOccur) {
248 inputDir = options[SOURCEDIR].value;
249 }
250
251 if(options[DESTDIR].doesOccur) {
252 outputDir = options[DESTDIR].value;
253 }
254
255 if(options[ENCODING].doesOccur) {
256 encoding = options[ENCODING].value;
257 }
258
259 if(options[ICUDATADIR].doesOccur) {
260 u_setDataDirectory(options[ICUDATADIR].value);
261 }
262 /* Initialize ICU */
263 u_init(&status);
264 if (U_FAILURE(status) && status != U_FILE_ACCESS_ERROR) {
265 /* Note: u_init() will try to open ICU property data.
266 * failures here are expected when building ICU from scratch.
267 * ignore them.
268 */
269 fprintf(stderr, "%s: can not initialize ICU. status = %s\n",
270 argv[0], u_errorName(status));
271 exit(1);
272 }
273 status = U_ZERO_ERROR;
274 if(options[WRITE_JAVA].doesOccur) {
275 write_java = TRUE;
276 outputEnc = options[WRITE_JAVA].value;
277 }
278
279 if(options[WRITE_XLIFF].doesOccur) {
280 write_xliff = TRUE;
281 if(options[WRITE_XLIFF].value != NULL){
282 xliffOutputFileName = options[WRITE_XLIFF].value;
283 }
284 }
285
286 initParser();
287
288 /*added by Jing*/
289 if(options[LANGUAGE].doesOccur) {
290 language = options[LANGUAGE].value;
291 }
292
293 LocalPointer<SRBRoot> newPoolBundle;
294 if(options[WRITE_POOL_BUNDLE].doesOccur) {
295 newPoolBundle.adoptInsteadAndCheckErrorCode(new SRBRoot(NULL, TRUE, status), status);
296 if(U_FAILURE(status)) {
297 fprintf(stderr, "unable to create an empty bundle for the pool keys: %s\n", u_errorName(status));
298 return status;
299 } else {
300 const char *poolResName = "pool.res";
301 char *nameWithoutSuffix = static_cast<char *>(uprv_malloc(uprv_strlen(poolResName) + 1));
302 if (nameWithoutSuffix == NULL) {
303 fprintf(stderr, "out of memory error\n");
304 return U_MEMORY_ALLOCATION_ERROR;
305 }
306 uprv_strcpy(nameWithoutSuffix, poolResName);
307 *uprv_strrchr(nameWithoutSuffix, '.') = 0;
308 newPoolBundle->fLocale = nameWithoutSuffix;
309 }
310 }
311
312 if(options[USE_POOL_BUNDLE].doesOccur) {
313 const char *poolResName = "pool.res";
314 FileStream *poolFile;
315 int32_t poolFileSize;
316 int32_t indexLength;
317 /*
318 * TODO: Consolidate inputDir/filename handling from main() and processFile()
319 * into a common function, and use it here as well.
320 * Try to create toolutil functions for dealing with dir/filenames and
321 * loading ICU data files without udata_open().
322 * Share code with icupkg?
323 * Also, make_res_filename() seems to be unused. Review and remove.
324 */
325 CharString poolFileName;
326 if (options[USE_POOL_BUNDLE].value!=NULL) {
327 poolFileName.append(options[USE_POOL_BUNDLE].value, status);
328 } else if (inputDir) {
329 poolFileName.append(inputDir, status);
330 }
331 poolFileName.appendPathPart(poolResName, status);
332 if (U_FAILURE(status)) {
333 return status;
334 }
335 poolFile = T_FileStream_open(poolFileName.data(), "rb");
336 if (poolFile == NULL) {
337 fprintf(stderr, "unable to open pool bundle file %s\n", poolFileName.data());
338 return 1;
339 }
340 poolFileSize = T_FileStream_size(poolFile);
341 if (poolFileSize < 32) {
342 fprintf(stderr, "the pool bundle file %s is too small\n", poolFileName.data());
343 return 1;
344 }
345 poolBundle.fBytes = new uint8_t[(poolFileSize + 15) & ~15];
346 if (poolFileSize > 0 && poolBundle.fBytes == NULL) {
347 fprintf(stderr, "unable to allocate memory for the pool bundle file %s\n", poolFileName.data());
348 return U_MEMORY_ALLOCATION_ERROR;
349 }
350
351 UDataSwapper *ds;
352 const DataHeader *header;
353 int32_t bytesRead = T_FileStream_read(poolFile, poolBundle.fBytes, poolFileSize);
354 if (bytesRead != poolFileSize) {
355 fprintf(stderr, "unable to read the pool bundle file %s\n", poolFileName.data());
356 return 1;
357 }
358 /*
359 * Swap the pool bundle so that a single checked-in file can be used.
360 * The swapper functions also test that the data looks like
361 * a well-formed .res file.
362 */
363 ds = udata_openSwapperForInputData(poolBundle.fBytes, bytesRead,
364 U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &status);
365 if (U_FAILURE(status)) {
366 fprintf(stderr, "udata_openSwapperForInputData(pool bundle %s) failed: %s\n",
367 poolFileName.data(), u_errorName(status));
368 return status;
369 }
370 ures_swap(ds, poolBundle.fBytes, bytesRead, poolBundle.fBytes, &status);
371 udata_closeSwapper(ds);
372 if (U_FAILURE(status)) {
373 fprintf(stderr, "ures_swap(pool bundle %s) failed: %s\n",
374 poolFileName.data(), u_errorName(status));
375 return status;
376 }
377 header = (const DataHeader *)poolBundle.fBytes;
378 if (header->info.formatVersion[0] < 2) {
379 fprintf(stderr, "invalid format of pool bundle file %s\n", poolFileName.data());
380 return U_INVALID_FORMAT_ERROR;
381 }
382 const int32_t *pRoot = (const int32_t *)(
383 (const char *)header + header->dataHeader.headerSize);
384 poolBundle.fIndexes = pRoot + 1;
385 indexLength = poolBundle.fIndexes[URES_INDEX_LENGTH] & 0xff;
386 if (indexLength <= URES_INDEX_POOL_CHECKSUM) {
387 fprintf(stderr, "insufficient indexes[] in pool bundle file %s\n", poolFileName.data());
388 return U_INVALID_FORMAT_ERROR;
389 }
390 int32_t keysBottom = 1 + indexLength;
391 int32_t keysTop = poolBundle.fIndexes[URES_INDEX_KEYS_TOP];
392 poolBundle.fKeys = (const char *)(pRoot + keysBottom);
393 poolBundle.fKeysLength = (keysTop - keysBottom) * 4;
394 poolBundle.fChecksum = poolBundle.fIndexes[URES_INDEX_POOL_CHECKSUM];
395
396 for (i = 0; i < poolBundle.fKeysLength; ++i) {
397 if (poolBundle.fKeys[i] == 0) {
398 ++poolBundle.fKeysCount;
399 }
400 }
401
402 // 16BitUnits[] begins with strings-v2.
403 // The strings-v2 may optionally be terminated by what looks like
404 // an explicit string length that exceeds the number of remaining 16-bit units.
405 int32_t stringUnitsLength = (poolBundle.fIndexes[URES_INDEX_16BIT_TOP] - keysTop) * 2;
406 if (stringUnitsLength >= 2 && getFormatVersion() >= 3) {
407 poolBundle.fStrings = new PseudoListResource(NULL, status);
408 if (poolBundle.fStrings == NULL) {
409 fprintf(stderr, "unable to allocate memory for the pool bundle strings %s\n",
410 poolFileName.data());
411 return U_MEMORY_ALLOCATION_ERROR;
412 }
413 // The PseudoListResource constructor call did not allocate further memory.
414 assert(U_SUCCESS(status));
415 const UChar *p = (const UChar *)(pRoot + keysTop);
416 int32_t remaining = stringUnitsLength;
417 do {
418 int32_t first = *p;
419 int8_t numCharsForLength;
420 int32_t length;
421 if (!U16_IS_TRAIL(first)) {
422 // NUL-terminated
423 numCharsForLength = 0;
424 for (length = 0;
425 length < remaining && p[length] != 0;
426 ++length) {}
427 } else if (first < 0xdfef) {
428 numCharsForLength = 1;
429 length = first & 0x3ff;
430 } else if (first < 0xdfff && remaining >= 2) {
431 numCharsForLength = 2;
432 length = ((first - 0xdfef) << 16) | p[1];
433 } else if (first == 0xdfff && remaining >= 3) {
434 numCharsForLength = 3;
435 length = ((int32_t)p[1] << 16) | p[2];
436 } else {
437 break; // overrun
438 }
439 // Check for overrun before changing remaining,
440 // so that it is always accurate after the loop body.
441 if ((numCharsForLength + length) >= remaining ||
442 p[numCharsForLength + length] != 0) {
443 break; // overrun or explicitly terminated
444 }
445 int32_t poolStringIndex = stringUnitsLength - remaining;
446 // Maximum pool string index when suffix-sharing the last character.
447 int32_t maxStringIndex = poolStringIndex + numCharsForLength + length - 1;
448 if (maxStringIndex >= RES_MAX_OFFSET) {
449 // pool string index overrun
450 break;
451 }
452 p += numCharsForLength;
453 remaining -= numCharsForLength;
454 if (length != 0) {
455 StringResource *sr =
456 new StringResource(poolStringIndex, numCharsForLength,
457 p, length, status);
458 if (sr == NULL) {
459 fprintf(stderr, "unable to allocate memory for a pool bundle string %s\n",
460 poolFileName.data());
461 return U_MEMORY_ALLOCATION_ERROR;
462 }
463 poolBundle.fStrings->add(sr);
464 poolBundle.fStringIndexLimit = maxStringIndex + 1;
465 // The StringResource constructor did not allocate further memory.
466 assert(U_SUCCESS(status));
467 }
468 p += length + 1;
469 remaining -= length + 1;
470 } while (remaining > 0);
471 if (poolBundle.fStrings->fCount == 0) {
472 delete poolBundle.fStrings;
473 poolBundle.fStrings = NULL;
474 }
475 }
476
477 T_FileStream_close(poolFile);
478 setUsePoolBundle(TRUE);
479 if (isVerbose() && poolBundle.fStrings != NULL) {
480 printf("number of shared strings: %d\n", (int)poolBundle.fStrings->fCount);
481 int32_t length = poolBundle.fStringIndexLimit + 1; // incl. last NUL
482 printf("16-bit units for strings: %6d = %6d bytes\n",
483 (int)length, (int)length * 2);
484 }
485 }
486
487 if(!options[FORMAT_VERSION].doesOccur && getFormatVersion() == 3 &&
488 poolBundle.fStrings == NULL &&
489 !options[WRITE_POOL_BUNDLE].doesOccur) {
490 // If we just default to formatVersion 3
491 // but there are no pool bundle strings to share
492 // and we do not write a pool bundle,
493 // then write formatVersion 2 which is just as good.
494 setFormatVersion(2);
495 }
496
497 if(options[INCLUDE_UNIHAN_COLL].doesOccur) {
498 puts("genrb option --includeUnihanColl ignored: \n"
499 "CLDR 26/ICU 54 unihan data is small, except\n"
500 "the ucadata-unihan.icu version of the collation root data\n"
501 "is about 300kB larger than the ucadata-implicithan.icu version.");
502 }
503
504 if((argc-1)!=1) {
505 printf("genrb number of files: %d\n", argc - 1);
506 }
507 /* generate the binary files */
508 for(i = 1; i < argc; ++i) {
509 status = U_ZERO_ERROR;
510 arg = getLongPathname(argv[i]);
511
512 CharString theCurrentFileName;
513 if (inputDir) {
514 theCurrentFileName.append(inputDir, status);
515 }
516 theCurrentFileName.appendPathPart(arg, status);
517 if (U_FAILURE(status)) {
518 break;
519 }
520
521 gCurrentFileName = theCurrentFileName.data();
522 if (isVerbose()) {
523 printf("Processing file \"%s\"\n", theCurrentFileName.data());
524 }
525 processFile(arg, encoding, inputDir, outputDir, NULL,
526 newPoolBundle.getAlias(),
527 options[NO_BINARY_COLLATION].doesOccur, status);
528 }
529
530 poolBundle.close();
531
532 if(U_SUCCESS(status) && options[WRITE_POOL_BUNDLE].doesOccur) {
533 char outputFileName[256];
534 newPoolBundle->write(outputDir, NULL, outputFileName, sizeof(outputFileName), status);
535 if(U_FAILURE(status)) {
536 fprintf(stderr, "unable to write the pool bundle: %s\n", u_errorName(status));
537 }
538 }
539
540 u_cleanup();
541
542 /* Dont return warnings as a failure */
543 if (U_SUCCESS(status)) {
544 return 0;
545 }
546
547 return status;
548 }
549
550 /* Process a file */
551 void
552 processFile(const char *filename, const char *cp,
553 const char *inputDir, const char *outputDir, const char *packageName,
554 SRBRoot *newPoolBundle,
555 UBool omitBinaryCollation, UErrorCode &status) {
556 LocalPointer<SRBRoot> data;
557 UCHARBUF *ucbuf = NULL;
558 char *rbname = NULL;
559 char *openFileName = NULL;
560 char *inputDirBuf = NULL;
561
562 char outputFileName[256];
563
564 int32_t dirlen = 0;
565 int32_t filelen = 0;
566
567 if (U_FAILURE(status)) {
568 return;
569 }
570 if(filename==NULL){
571 status=U_ILLEGAL_ARGUMENT_ERROR;
572 return;
573 }else{
574 filelen = (int32_t)uprv_strlen(filename);
575 }
576
577 if(inputDir == NULL) {
578 const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR);
579 openFileName = (char *) uprv_malloc(dirlen + filelen + 2);
580 openFileName[0] = '\0';
581 if (filenameBegin != NULL) {
582 /*
583 * When a filename ../../../data/root.txt is specified,
584 * we presume that the input directory is ../../../data
585 * This is very important when the resource file includes
586 * another file, like UCARules.txt or thaidict.brk.
587 */
588 int32_t filenameSize = (int32_t)(filenameBegin - filename + 1);
589 inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize);
590
591 /* test for NULL */
592 if(inputDirBuf == NULL) {
593 status = U_MEMORY_ALLOCATION_ERROR;
594 goto finish;
595 }
596
597 inputDirBuf[filenameSize - 1] = 0;
598 inputDir = inputDirBuf;
599 dirlen = (int32_t)uprv_strlen(inputDir);
600 }
601 }else{
602 dirlen = (int32_t)uprv_strlen(inputDir);
603
604 if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) {
605 openFileName = (char *) uprv_malloc(dirlen + filelen + 2);
606
607 /* test for NULL */
608 if(openFileName == NULL) {
609 status = U_MEMORY_ALLOCATION_ERROR;
610 goto finish;
611 }
612
613 openFileName[0] = '\0';
614 /*
615 * append the input dir to openFileName if the first char in
616 * filename is not file seperation char and the last char input directory is not '.'.
617 * This is to support :
618 * genrb -s. /home/icu/data
619 * genrb -s. icu/data
620 * The user cannot mix notations like
621 * genrb -s. /icu/data --- the absolute path specified. -s redundant
622 * user should use
623 * genrb -s. icu/data --- start from CWD and look in icu/data dir
624 */
625 if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){
626 uprv_strcpy(openFileName, inputDir);
627 openFileName[dirlen] = U_FILE_SEP_CHAR;
628 }
629 openFileName[dirlen + 1] = '\0';
630 } else {
631 openFileName = (char *) uprv_malloc(dirlen + filelen + 1);
632
633 /* test for NULL */
634 if(openFileName == NULL) {
635 status = U_MEMORY_ALLOCATION_ERROR;
636 goto finish;
637 }
638
639 uprv_strcpy(openFileName, inputDir);
640
641 }
642 }
643
644 uprv_strcat(openFileName, filename);
645
646 ucbuf = ucbuf_open(openFileName, &cp,getShowWarning(),TRUE, &status);
647 if(status == U_FILE_ACCESS_ERROR) {
648
649 fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName);
650 goto finish;
651 }
652 if (ucbuf == NULL || U_FAILURE(status)) {
653 fprintf(stderr, "An error occured processing file %s. Error: %s\n",
654 openFileName == NULL ? filename : openFileName, u_errorName(status));
655 goto finish;
656 }
657 /* auto detected popular encodings? */
658 if (cp!=NULL && isVerbose()) {
659 printf("autodetected encoding %s\n", cp);
660 }
661 /* Parse the data into an SRBRoot */
662 data.adoptInstead(parse(ucbuf, inputDir, outputDir, filename,
663 !omitBinaryCollation, options[NO_COLLATION_RULES].doesOccur, &status));
664
665 if (data.isNull() || U_FAILURE(status)) {
666 fprintf(stderr, "couldn't parse the file %s. Error:%s\n", filename, u_errorName(status));
667 goto finish;
668 }
669 if(options[WRITE_POOL_BUNDLE].doesOccur) {
670 data->fWritePoolBundle = newPoolBundle;
671 data->compactKeys(status);
672 int32_t newKeysLength;
673 const char *newKeys = data->getKeyBytes(&newKeysLength);
674 newPoolBundle->addKeyBytes(newKeys, newKeysLength, status);
675 if(U_FAILURE(status)) {
676 fprintf(stderr, "bundle_compactKeys(%s) or bundle_getKeyBytes() failed: %s\n",
677 filename, u_errorName(status));
678 goto finish;
679 }
680 /* count the number of just-added key strings */
681 for(const char *newKeysLimit = newKeys + newKeysLength; newKeys < newKeysLimit; ++newKeys) {
682 if(*newKeys == 0) {
683 ++newPoolBundle->fKeysCount;
684 }
685 }
686 }
687
688 if(options[USE_POOL_BUNDLE].doesOccur) {
689 data->fUsePoolBundle = &poolBundle;
690 }
691
692 /* Determine the target rb filename */
693 rbname = make_res_filename(filename, outputDir, packageName, status);
694 if(U_FAILURE(status)) {
695 fprintf(stderr, "couldn't make the res fileName for bundle %s. Error:%s\n",
696 filename, u_errorName(status));
697 goto finish;
698 }
699 if(write_java== TRUE){
700 bundle_write_java(data.getAlias(), outputDir, outputEnc,
701 outputFileName, sizeof(outputFileName),
702 options[JAVA_PACKAGE].value, options[BUNDLE_NAME].value, &status);
703 }else if(write_xliff ==TRUE){
704 bundle_write_xml(data.getAlias(), outputDir, outputEnc,
705 filename, outputFileName, sizeof(outputFileName),
706 language, xliffOutputFileName, &status);
707 }else{
708 /* Write the data to the file */
709 data->write(outputDir, packageName, outputFileName, sizeof(outputFileName), status);
710 }
711 if (U_FAILURE(status)) {
712 fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName, u_errorName(status));
713 }
714
715 finish:
716
717 if (inputDirBuf != NULL) {
718 uprv_free(inputDirBuf);
719 }
720
721 if (openFileName != NULL) {
722 uprv_free(openFileName);
723 }
724
725 if(ucbuf) {
726 ucbuf_close(ucbuf);
727 }
728
729 if (rbname) {
730 uprv_free(rbname);
731 }
732 }
733
734 /* Generate the target .res file name from the input file name */
735 static char*
736 make_res_filename(const char *filename,
737 const char *outputDir,
738 const char *packageName,
739 UErrorCode &status) {
740 char *basename;
741 char *dirname;
742 char *resName;
743
744 int32_t pkgLen = 0; /* length of package prefix */
745
746
747 if (U_FAILURE(status)) {
748 return 0;
749 }
750
751 if(packageName != NULL)
752 {
753 pkgLen = (int32_t)(1 + uprv_strlen(packageName));
754 }
755
756 /* setup */
757 basename = dirname = resName = 0;
758
759 /* determine basename, and compiled file names */
760 basename = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1));
761 if(basename == 0) {
762 status = U_MEMORY_ALLOCATION_ERROR;
763 goto finish;
764 }
765
766 get_basename(basename, filename);
767
768 dirname = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1));
769 if(dirname == 0) {
770 status = U_MEMORY_ALLOCATION_ERROR;
771 goto finish;
772 }
773
774 get_dirname(dirname, filename);
775
776 if (outputDir == NULL) {
777 /* output in same dir as .txt */
778 resName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(dirname)
779 + pkgLen
780 + uprv_strlen(basename)
781 + uprv_strlen(RES_SUFFIX) + 8));
782 if(resName == 0) {
783 status = U_MEMORY_ALLOCATION_ERROR;
784 goto finish;
785 }
786
787 uprv_strcpy(resName, dirname);
788
789 if(packageName != NULL)
790 {
791 uprv_strcat(resName, packageName);
792 uprv_strcat(resName, "_");
793 }
794
795 uprv_strcat(resName, basename);
796
797 } else {
798 int32_t dirlen = (int32_t)uprv_strlen(outputDir);
799 int32_t basenamelen = (int32_t)uprv_strlen(basename);
800
801 resName = (char*) uprv_malloc(sizeof(char) * (dirlen + pkgLen + basenamelen + 8));
802
803 if (resName == NULL) {
804 status = U_MEMORY_ALLOCATION_ERROR;
805 goto finish;
806 }
807
808 uprv_strcpy(resName, outputDir);
809
810 if(outputDir[dirlen] != U_FILE_SEP_CHAR) {
811 resName[dirlen] = U_FILE_SEP_CHAR;
812 resName[dirlen + 1] = '\0';
813 }
814
815 if(packageName != NULL)
816 {
817 uprv_strcat(resName, packageName);
818 uprv_strcat(resName, "_");
819 }
820
821 uprv_strcat(resName, basename);
822 }
823
824 finish:
825 uprv_free(basename);
826 uprv_free(dirname);
827
828 return resName;
829 }
830
831 /*
832 * Local Variables:
833 * indent-tabs-mode: nil
834 * End:
835 */