Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (C) 2002-2009, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File genbrk.c |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | //-------------------------------------------------------------------- |
michael@0 | 11 | // |
michael@0 | 12 | // Tool for generating RuleBasedBreakIterator data files (.brk files). |
michael@0 | 13 | // .brk files contain the precompiled rules for standard types |
michael@0 | 14 | // of iterators - word, line, sentence, etc. |
michael@0 | 15 | // |
michael@0 | 16 | // Usage: genbrk [options] -r rule-file.txt -o output-file.brk |
michael@0 | 17 | // |
michael@0 | 18 | // options: -v verbose |
michael@0 | 19 | // -? or -h help |
michael@0 | 20 | // |
michael@0 | 21 | // The input rule file is a plain text file containing break rules |
michael@0 | 22 | // in the input format accepted by RuleBasedBreakIterators. The |
michael@0 | 23 | // file can be encoded as utf-8, or utf-16 (either endian), or |
michael@0 | 24 | // in the default code page (platform dependent.). utf encoded |
michael@0 | 25 | // files must include a BOM. |
michael@0 | 26 | // |
michael@0 | 27 | //-------------------------------------------------------------------- |
michael@0 | 28 | |
michael@0 | 29 | #include "unicode/utypes.h" |
michael@0 | 30 | #include "unicode/ucnv.h" |
michael@0 | 31 | #include "unicode/unistr.h" |
michael@0 | 32 | #include "unicode/rbbi.h" |
michael@0 | 33 | #include "unicode/uclean.h" |
michael@0 | 34 | #include "unicode/udata.h" |
michael@0 | 35 | #include "unicode/putil.h" |
michael@0 | 36 | |
michael@0 | 37 | #include "uoptions.h" |
michael@0 | 38 | #include "unewdata.h" |
michael@0 | 39 | #include "ucmndata.h" |
michael@0 | 40 | #include "rbbidata.h" |
michael@0 | 41 | #include "cmemory.h" |
michael@0 | 42 | |
michael@0 | 43 | #include <stdio.h> |
michael@0 | 44 | #include <stdlib.h> |
michael@0 | 45 | #include <string.h> |
michael@0 | 46 | |
michael@0 | 47 | U_NAMESPACE_USE |
michael@0 | 48 | |
michael@0 | 49 | static char *progName; |
michael@0 | 50 | static UOption options[]={ |
michael@0 | 51 | UOPTION_HELP_H, /* 0 */ |
michael@0 | 52 | UOPTION_HELP_QUESTION_MARK, /* 1 */ |
michael@0 | 53 | UOPTION_VERBOSE, /* 2 */ |
michael@0 | 54 | { "rules", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0 }, /* 3 */ |
michael@0 | 55 | { "out", NULL, NULL, NULL, 'o', UOPT_REQUIRES_ARG, 0 }, /* 4 */ |
michael@0 | 56 | UOPTION_ICUDATADIR, /* 5 */ |
michael@0 | 57 | UOPTION_DESTDIR, /* 6 */ |
michael@0 | 58 | UOPTION_COPYRIGHT, /* 7 */ |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | void usageAndDie(int retCode) { |
michael@0 | 62 | printf("Usage: %s [-v] [-options] -r rule-file -o output-file\n", progName); |
michael@0 | 63 | printf("\tRead in break iteration rules text and write out the binary data\n" |
michael@0 | 64 | "options:\n" |
michael@0 | 65 | "\t-h or -? or --help this usage text\n" |
michael@0 | 66 | "\t-V or --version show a version message\n" |
michael@0 | 67 | "\t-c or --copyright include a copyright notice\n" |
michael@0 | 68 | "\t-v or --verbose turn on verbose output\n" |
michael@0 | 69 | "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" |
michael@0 | 70 | "\t followed by path, defaults to %s\n" |
michael@0 | 71 | "\t-d or --destdir destination directory, followed by the path\n", |
michael@0 | 72 | u_getDataDirectory()); |
michael@0 | 73 | exit (retCode); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | |
michael@0 | 77 | #if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO |
michael@0 | 78 | |
michael@0 | 79 | /* dummy UDataInfo cf. udata.h */ |
michael@0 | 80 | static UDataInfo dummyDataInfo = { |
michael@0 | 81 | sizeof(UDataInfo), |
michael@0 | 82 | 0, |
michael@0 | 83 | |
michael@0 | 84 | U_IS_BIG_ENDIAN, |
michael@0 | 85 | U_CHARSET_FAMILY, |
michael@0 | 86 | U_SIZEOF_UCHAR, |
michael@0 | 87 | 0, |
michael@0 | 88 | |
michael@0 | 89 | { 0, 0, 0, 0 }, /* dummy dataFormat */ |
michael@0 | 90 | { 0, 0, 0, 0 }, /* dummy formatVersion */ |
michael@0 | 91 | { 0, 0, 0, 0 } /* dummy dataVersion */ |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | #else |
michael@0 | 95 | |
michael@0 | 96 | // |
michael@0 | 97 | // Set up the ICU data header, defined in ucmndata.h |
michael@0 | 98 | // |
michael@0 | 99 | DataHeader dh ={ |
michael@0 | 100 | {sizeof(DataHeader), // Struct MappedData |
michael@0 | 101 | 0xda, |
michael@0 | 102 | 0x27}, |
michael@0 | 103 | |
michael@0 | 104 | { // struct UDataInfo |
michael@0 | 105 | sizeof(UDataInfo), // size |
michael@0 | 106 | 0, // reserved |
michael@0 | 107 | U_IS_BIG_ENDIAN, |
michael@0 | 108 | U_CHARSET_FAMILY, |
michael@0 | 109 | U_SIZEOF_UCHAR, |
michael@0 | 110 | 0, // reserved |
michael@0 | 111 | |
michael@0 | 112 | { 0x42, 0x72, 0x6b, 0x20 }, // dataFormat="Brk " |
michael@0 | 113 | { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values |
michael@0 | 114 | // from the RBBI rule builder. The values declared |
michael@0 | 115 | // here should never appear in any real RBBI data. |
michael@0 | 116 | { 4, 1, 0, 0 } // dataVersion (Unicode version) |
michael@0 | 117 | }}; |
michael@0 | 118 | |
michael@0 | 119 | #endif |
michael@0 | 120 | |
michael@0 | 121 | //---------------------------------------------------------------------------- |
michael@0 | 122 | // |
michael@0 | 123 | // main for genbrk |
michael@0 | 124 | // |
michael@0 | 125 | //---------------------------------------------------------------------------- |
michael@0 | 126 | int main(int argc, char **argv) { |
michael@0 | 127 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 128 | const char *ruleFileName; |
michael@0 | 129 | const char *outFileName; |
michael@0 | 130 | const char *outDir = NULL; |
michael@0 | 131 | const char *copyright = NULL; |
michael@0 | 132 | |
michael@0 | 133 | // |
michael@0 | 134 | // Pick up and check the command line arguments, |
michael@0 | 135 | // using the standard ICU tool utils option handling. |
michael@0 | 136 | // |
michael@0 | 137 | U_MAIN_INIT_ARGS(argc, argv); |
michael@0 | 138 | progName = argv[0]; |
michael@0 | 139 | argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); |
michael@0 | 140 | if(argc<0) { |
michael@0 | 141 | // Unrecognized option |
michael@0 | 142 | fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]); |
michael@0 | 143 | usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | if(options[0].doesOccur || options[1].doesOccur) { |
michael@0 | 147 | // -? or -h for help. |
michael@0 | 148 | usageAndDie(0); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | if (!(options[3].doesOccur && options[4].doesOccur)) { |
michael@0 | 152 | fprintf(stderr, "rule file and output file must both be specified.\n"); |
michael@0 | 153 | usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); |
michael@0 | 154 | } |
michael@0 | 155 | ruleFileName = options[3].value; |
michael@0 | 156 | outFileName = options[4].value; |
michael@0 | 157 | |
michael@0 | 158 | if (options[5].doesOccur) { |
michael@0 | 159 | u_setDataDirectory(options[5].value); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | status = U_ZERO_ERROR; |
michael@0 | 163 | |
michael@0 | 164 | /* Combine the directory with the file name */ |
michael@0 | 165 | if(options[6].doesOccur) { |
michael@0 | 166 | outDir = options[6].value; |
michael@0 | 167 | } |
michael@0 | 168 | if (options[7].doesOccur) { |
michael@0 | 169 | copyright = U_COPYRIGHT_STRING; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | #if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO |
michael@0 | 173 | |
michael@0 | 174 | UNewDataMemory *pData; |
michael@0 | 175 | char msg[1024]; |
michael@0 | 176 | |
michael@0 | 177 | /* write message with just the name */ |
michael@0 | 178 | sprintf(msg, "genbrk writes dummy %s because of UCONFIG_NO_BREAK_ITERATION and/or UCONFIG_NO_FILE_IO, see uconfig.h", outFileName); |
michael@0 | 179 | fprintf(stderr, "%s\n", msg); |
michael@0 | 180 | |
michael@0 | 181 | /* write the dummy data file */ |
michael@0 | 182 | pData = udata_create(outDir, NULL, outFileName, &dummyDataInfo, NULL, &status); |
michael@0 | 183 | udata_writeBlock(pData, msg, strlen(msg)); |
michael@0 | 184 | udata_finish(pData, &status); |
michael@0 | 185 | return (int)status; |
michael@0 | 186 | |
michael@0 | 187 | #else |
michael@0 | 188 | /* Initialize ICU */ |
michael@0 | 189 | u_init(&status); |
michael@0 | 190 | if (U_FAILURE(status)) { |
michael@0 | 191 | fprintf(stderr, "%s: can not initialize ICU. status = %s\n", |
michael@0 | 192 | argv[0], u_errorName(status)); |
michael@0 | 193 | exit(1); |
michael@0 | 194 | } |
michael@0 | 195 | status = U_ZERO_ERROR; |
michael@0 | 196 | |
michael@0 | 197 | // |
michael@0 | 198 | // Read in the rule source file |
michael@0 | 199 | // |
michael@0 | 200 | long result; |
michael@0 | 201 | long ruleFileSize; |
michael@0 | 202 | FILE *file; |
michael@0 | 203 | char *ruleBufferC; |
michael@0 | 204 | |
michael@0 | 205 | file = fopen(ruleFileName, "rb"); |
michael@0 | 206 | if( file == 0 ) { |
michael@0 | 207 | fprintf(stderr, "Could not open file \"%s\"\n", ruleFileName); |
michael@0 | 208 | exit(-1); |
michael@0 | 209 | } |
michael@0 | 210 | fseek(file, 0, SEEK_END); |
michael@0 | 211 | ruleFileSize = ftell(file); |
michael@0 | 212 | fseek(file, 0, SEEK_SET); |
michael@0 | 213 | ruleBufferC = new char[ruleFileSize+10]; |
michael@0 | 214 | |
michael@0 | 215 | result = (long)fread(ruleBufferC, 1, ruleFileSize, file); |
michael@0 | 216 | if (result != ruleFileSize) { |
michael@0 | 217 | fprintf(stderr, "Error reading file \"%s\"\n", ruleFileName); |
michael@0 | 218 | exit (-1); |
michael@0 | 219 | } |
michael@0 | 220 | ruleBufferC[ruleFileSize]=0; |
michael@0 | 221 | fclose(file); |
michael@0 | 222 | |
michael@0 | 223 | // |
michael@0 | 224 | // Look for a Unicode Signature (BOM) on the rule file |
michael@0 | 225 | // |
michael@0 | 226 | int32_t signatureLength; |
michael@0 | 227 | const char * ruleSourceC = ruleBufferC; |
michael@0 | 228 | const char* encoding = ucnv_detectUnicodeSignature( |
michael@0 | 229 | ruleSourceC, ruleFileSize, &signatureLength, &status); |
michael@0 | 230 | if (U_FAILURE(status)) { |
michael@0 | 231 | exit(status); |
michael@0 | 232 | } |
michael@0 | 233 | if(encoding!=NULL ){ |
michael@0 | 234 | ruleSourceC += signatureLength; |
michael@0 | 235 | ruleFileSize -= signatureLength; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | // |
michael@0 | 239 | // Open a converter to take the rule file to UTF-16 |
michael@0 | 240 | // |
michael@0 | 241 | UConverter* conv; |
michael@0 | 242 | conv = ucnv_open(encoding, &status); |
michael@0 | 243 | if (U_FAILURE(status)) { |
michael@0 | 244 | fprintf(stderr, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status)); |
michael@0 | 245 | exit(status); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | // |
michael@0 | 249 | // Convert the rules to UChar. |
michael@0 | 250 | // Preflight first to determine required buffer size. |
michael@0 | 251 | // |
michael@0 | 252 | uint32_t destCap = ucnv_toUChars(conv, |
michael@0 | 253 | NULL, // dest, |
michael@0 | 254 | 0, // destCapacity, |
michael@0 | 255 | ruleSourceC, |
michael@0 | 256 | ruleFileSize, |
michael@0 | 257 | &status); |
michael@0 | 258 | if (status != U_BUFFER_OVERFLOW_ERROR) { |
michael@0 | 259 | fprintf(stderr, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status)); |
michael@0 | 260 | exit(status); |
michael@0 | 261 | }; |
michael@0 | 262 | |
michael@0 | 263 | status = U_ZERO_ERROR; |
michael@0 | 264 | UChar *ruleSourceU = new UChar[destCap+1]; |
michael@0 | 265 | ucnv_toUChars(conv, |
michael@0 | 266 | ruleSourceU, // dest, |
michael@0 | 267 | destCap+1, |
michael@0 | 268 | ruleSourceC, |
michael@0 | 269 | ruleFileSize, |
michael@0 | 270 | &status); |
michael@0 | 271 | if (U_FAILURE(status)) { |
michael@0 | 272 | fprintf(stderr, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status)); |
michael@0 | 273 | exit(status); |
michael@0 | 274 | }; |
michael@0 | 275 | ucnv_close(conv); |
michael@0 | 276 | |
michael@0 | 277 | |
michael@0 | 278 | // |
michael@0 | 279 | // Put the source rules into a UnicodeString |
michael@0 | 280 | // |
michael@0 | 281 | UnicodeString ruleSourceS(FALSE, ruleSourceU, destCap); |
michael@0 | 282 | |
michael@0 | 283 | // |
michael@0 | 284 | // Create the break iterator from the rules |
michael@0 | 285 | // This will compile the rules. |
michael@0 | 286 | // |
michael@0 | 287 | UParseError parseError; |
michael@0 | 288 | parseError.line = 0; |
michael@0 | 289 | parseError.offset = 0; |
michael@0 | 290 | RuleBasedBreakIterator *bi = new RuleBasedBreakIterator(ruleSourceS, parseError, status); |
michael@0 | 291 | if (U_FAILURE(status)) { |
michael@0 | 292 | fprintf(stderr, "createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n", |
michael@0 | 293 | u_errorName(status), (int)parseError.line, (int)parseError.offset); |
michael@0 | 294 | exit(status); |
michael@0 | 295 | }; |
michael@0 | 296 | |
michael@0 | 297 | |
michael@0 | 298 | // |
michael@0 | 299 | // Get the compiled rule data from the break iterator. |
michael@0 | 300 | // |
michael@0 | 301 | uint32_t outDataSize; |
michael@0 | 302 | const uint8_t *outData; |
michael@0 | 303 | outData = bi->getBinaryRules(outDataSize); |
michael@0 | 304 | |
michael@0 | 305 | // Copy the data format version numbers from the RBBI data header into the UDataMemory header. |
michael@0 | 306 | uprv_memcpy(dh.info.formatVersion, ((RBBIDataHeader *)outData)->fFormatVersion, sizeof(dh.info.formatVersion)); |
michael@0 | 307 | |
michael@0 | 308 | // |
michael@0 | 309 | // Create the output file |
michael@0 | 310 | // |
michael@0 | 311 | size_t bytesWritten; |
michael@0 | 312 | UNewDataMemory *pData; |
michael@0 | 313 | pData = udata_create(outDir, NULL, outFileName, &(dh.info), copyright, &status); |
michael@0 | 314 | if(U_FAILURE(status)) { |
michael@0 | 315 | fprintf(stderr, "genbrk: Could not open output file \"%s\", \"%s\"\n", |
michael@0 | 316 | outFileName, u_errorName(status)); |
michael@0 | 317 | exit(status); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | |
michael@0 | 321 | // Write the data itself. |
michael@0 | 322 | udata_writeBlock(pData, outData, outDataSize); |
michael@0 | 323 | // finish up |
michael@0 | 324 | bytesWritten = udata_finish(pData, &status); |
michael@0 | 325 | if(U_FAILURE(status)) { |
michael@0 | 326 | fprintf(stderr, "genbrk: error %d writing the output file\n", status); |
michael@0 | 327 | exit(status); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | if (bytesWritten != outDataSize) { |
michael@0 | 331 | fprintf(stderr, "Error writing to output file \"%s\"\n", outFileName); |
michael@0 | 332 | exit(-1); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | delete bi; |
michael@0 | 336 | delete[] ruleSourceU; |
michael@0 | 337 | delete[] ruleBufferC; |
michael@0 | 338 | u_cleanup(); |
michael@0 | 339 | |
michael@0 | 340 | |
michael@0 | 341 | printf("genbrk: tool completed successfully.\n"); |
michael@0 | 342 | return 0; |
michael@0 | 343 | |
michael@0 | 344 | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |
michael@0 | 345 | } |
michael@0 | 346 |