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