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