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