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