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 | * |
michael@0 | 4 | * Copyright (C) 1999-2013, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: derb.c |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2000sep6 |
michael@0 | 14 | * created by: Vladimir Weinstein as an ICU workshop example |
michael@0 | 15 | * maintained by: Yves Arrouye <yves@realnames.com> |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #include "unicode/ucnv.h" |
michael@0 | 19 | #include "unicode/ustring.h" |
michael@0 | 20 | #include "unicode/putil.h" |
michael@0 | 21 | #include "unicode/ustdio.h" |
michael@0 | 22 | |
michael@0 | 23 | #include "uresimp.h" |
michael@0 | 24 | #include "cmemory.h" |
michael@0 | 25 | #include "cstring.h" |
michael@0 | 26 | #include "uoptions.h" |
michael@0 | 27 | #include "toolutil.h" |
michael@0 | 28 | #include "ustrfmt.h" |
michael@0 | 29 | |
michael@0 | 30 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 31 | |
michael@0 | 32 | #define DERB_VERSION "1.1" |
michael@0 | 33 | |
michael@0 | 34 | #define DERB_DEFAULT_TRUNC 80 |
michael@0 | 35 | |
michael@0 | 36 | static const int32_t indentsize = 4; |
michael@0 | 37 | static int32_t truncsize = DERB_DEFAULT_TRUNC; |
michael@0 | 38 | static UBool opt_truncate = FALSE; |
michael@0 | 39 | |
michael@0 | 40 | static const char *getEncodingName(const char *encoding); |
michael@0 | 41 | static void reportError(const char *pname, UErrorCode *status, const char *when); |
michael@0 | 42 | static UChar *quotedString(const UChar *string); |
michael@0 | 43 | static void printOutBundle(UFILE *out, UConverter *converter, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status); |
michael@0 | 44 | static void printString(UFILE *out, UConverter *converter, const UChar *str, int32_t len); |
michael@0 | 45 | static void printCString(UFILE *out, UConverter *converter, const char *str, int32_t len); |
michael@0 | 46 | static void printIndent(UFILE *out, UConverter *converter, int32_t indent); |
michael@0 | 47 | static void printHex(UFILE *out, UConverter *converter, uint8_t what); |
michael@0 | 48 | |
michael@0 | 49 | static UOption options[]={ |
michael@0 | 50 | UOPTION_HELP_H, |
michael@0 | 51 | UOPTION_HELP_QUESTION_MARK, |
michael@0 | 52 | /* 2 */ UOPTION_ENCODING, |
michael@0 | 53 | /* 3 */ { "to-stdout", NULL, NULL, NULL, 'c', UOPT_NO_ARG, 0 } , |
michael@0 | 54 | /* 4 */ { "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 }, |
michael@0 | 55 | /* 5 */ UOPTION_VERBOSE, |
michael@0 | 56 | /* 6 */ UOPTION_DESTDIR, |
michael@0 | 57 | /* 7 */ UOPTION_SOURCEDIR, |
michael@0 | 58 | /* 8 */ { "bom", NULL, NULL, NULL, 0, UOPT_NO_ARG, 0 }, |
michael@0 | 59 | /* 9 */ UOPTION_ICUDATADIR, |
michael@0 | 60 | /* 10 */ UOPTION_VERSION, |
michael@0 | 61 | /* 11 */ { "suppressAliases", NULL, NULL, NULL, 'A', UOPT_NO_ARG, 0 }, |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | static UBool verbose = FALSE; |
michael@0 | 65 | static UBool suppressAliases = FALSE; |
michael@0 | 66 | static UFILE *ustderr = NULL; |
michael@0 | 67 | |
michael@0 | 68 | extern int |
michael@0 | 69 | main(int argc, char* argv[]) { |
michael@0 | 70 | const char *encoding = NULL; |
michael@0 | 71 | const char *outputDir = NULL; /* NULL = no output directory, use current */ |
michael@0 | 72 | const char *inputDir = "."; |
michael@0 | 73 | int tostdout = 0; |
michael@0 | 74 | int prbom = 0; |
michael@0 | 75 | |
michael@0 | 76 | const char *pname; |
michael@0 | 77 | |
michael@0 | 78 | UResourceBundle *bundle = NULL; |
michael@0 | 79 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 80 | int32_t i = 0; |
michael@0 | 81 | |
michael@0 | 82 | UConverter *converter = NULL; // not used |
michael@0 | 83 | |
michael@0 | 84 | const char* arg; |
michael@0 | 85 | |
michael@0 | 86 | /* Get the name of tool. */ |
michael@0 | 87 | pname = uprv_strrchr(*argv, U_FILE_SEP_CHAR); |
michael@0 | 88 | #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR |
michael@0 | 89 | if (!pname) { |
michael@0 | 90 | pname = uprv_strrchr(*argv, U_FILE_ALT_SEP_CHAR); |
michael@0 | 91 | } |
michael@0 | 92 | #endif |
michael@0 | 93 | if (!pname) { |
michael@0 | 94 | pname = *argv; |
michael@0 | 95 | } else { |
michael@0 | 96 | ++pname; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /* error handling, printing usage message */ |
michael@0 | 100 | argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); |
michael@0 | 101 | |
michael@0 | 102 | /* error handling, printing usage message */ |
michael@0 | 103 | if(argc<0) { |
michael@0 | 104 | fprintf(stderr, |
michael@0 | 105 | "%s: error in command line argument \"%s\"\n", pname, |
michael@0 | 106 | argv[-argc]); |
michael@0 | 107 | } |
michael@0 | 108 | if(argc<0 || options[0].doesOccur || options[1].doesOccur) { |
michael@0 | 109 | fprintf(argc < 0 ? stderr : stdout, |
michael@0 | 110 | "%csage: %s [ -h, -?, --help ] [ -V, --version ]\n" |
michael@0 | 111 | " [ -v, --verbose ] [ -e, --encoding encoding ] [ --bom ]\n" |
michael@0 | 112 | " [ -t, --truncate [ size ] ]\n" |
michael@0 | 113 | " [ -s, --sourcedir source ] [ -d, --destdir destination ]\n" |
michael@0 | 114 | " [ -i, --icudatadir directory ] [ -c, --to-stdout ]\n" |
michael@0 | 115 | " [ -A, --suppressAliases]\n" |
michael@0 | 116 | " bundle ...\n", argc < 0 ? 'u' : 'U', |
michael@0 | 117 | pname); |
michael@0 | 118 | return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | if(options[10].doesOccur) { |
michael@0 | 122 | fprintf(stderr, |
michael@0 | 123 | "%s version %s (ICU version %s).\n" |
michael@0 | 124 | "%s\n", |
michael@0 | 125 | pname, DERB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); |
michael@0 | 126 | return U_ZERO_ERROR; |
michael@0 | 127 | } |
michael@0 | 128 | if(options[2].doesOccur) { |
michael@0 | 129 | encoding = options[2].value; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | if (options[3].doesOccur) { |
michael@0 | 133 | if(options[2].doesOccur) { |
michael@0 | 134 | fprintf(stderr, "%s: Error: don't specify an encoding (-e) when writing to stdout (-c).\n", pname); |
michael@0 | 135 | return 3; |
michael@0 | 136 | } |
michael@0 | 137 | tostdout = 1; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if(options[4].doesOccur) { |
michael@0 | 141 | opt_truncate = TRUE; |
michael@0 | 142 | if(options[4].value != NULL) { |
michael@0 | 143 | truncsize = atoi(options[4].value); /* user defined printable size */ |
michael@0 | 144 | } else { |
michael@0 | 145 | truncsize = DERB_DEFAULT_TRUNC; /* we'll use default omitting size */ |
michael@0 | 146 | } |
michael@0 | 147 | } else { |
michael@0 | 148 | opt_truncate = FALSE; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | if(options[5].doesOccur) { |
michael@0 | 152 | verbose = TRUE; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | if (options[6].doesOccur) { |
michael@0 | 156 | outputDir = options[6].value; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | if(options[7].doesOccur) { |
michael@0 | 160 | inputDir = options[7].value; /* we'll use users resources */ |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | if (options[8].doesOccur) { |
michael@0 | 164 | prbom = 1; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | if (options[9].doesOccur) { |
michael@0 | 168 | u_setDataDirectory(options[9].value); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | if (options[11].doesOccur) { |
michael@0 | 172 | suppressAliases = TRUE; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | fflush(stderr); // use ustderr now. |
michael@0 | 176 | ustderr = u_finit(stderr, NULL, NULL); |
michael@0 | 177 | |
michael@0 | 178 | for (i = 1; i < argc; ++i) { |
michael@0 | 179 | static const UChar sp[] = { 0x0020 }; /* " " */ |
michael@0 | 180 | char infile[4096]; /* XXX Sloppy. */ |
michael@0 | 181 | char locale[64]; |
michael@0 | 182 | const char *thename = 0, *p, *q; |
michael@0 | 183 | UBool fromICUData = FALSE; |
michael@0 | 184 | |
michael@0 | 185 | arg = getLongPathname(argv[i]); |
michael@0 | 186 | |
michael@0 | 187 | if (verbose) { |
michael@0 | 188 | u_fprintf(ustderr, "processing bundle \"%s\"\n", argv[i]); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | p = uprv_strrchr(arg, U_FILE_SEP_CHAR); |
michael@0 | 192 | #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR |
michael@0 | 193 | if (p == NULL) { |
michael@0 | 194 | p = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); |
michael@0 | 195 | } |
michael@0 | 196 | #endif |
michael@0 | 197 | if (!p) { |
michael@0 | 198 | p = arg; |
michael@0 | 199 | } else { |
michael@0 | 200 | p++; |
michael@0 | 201 | } |
michael@0 | 202 | q = uprv_strrchr(p, '.'); |
michael@0 | 203 | if (!q) { |
michael@0 | 204 | for (q = p; *q; ++q) |
michael@0 | 205 | ; |
michael@0 | 206 | } |
michael@0 | 207 | uprv_strncpy(locale, p, q - p); |
michael@0 | 208 | locale[q - p] = 0; |
michael@0 | 209 | |
michael@0 | 210 | if (!(fromICUData = !uprv_strcmp(inputDir, "-"))) { |
michael@0 | 211 | UBool absfilename = *arg == U_FILE_SEP_CHAR; |
michael@0 | 212 | #if U_PLATFORM_HAS_WIN32_API |
michael@0 | 213 | if (!absfilename) { |
michael@0 | 214 | absfilename = (uprv_strlen(arg) > 2 && isalpha(arg[0]) |
michael@0 | 215 | && arg[1] == ':' && arg[2] == U_FILE_SEP_CHAR); |
michael@0 | 216 | } |
michael@0 | 217 | #endif |
michael@0 | 218 | if (absfilename) { |
michael@0 | 219 | thename = arg; |
michael@0 | 220 | } else { |
michael@0 | 221 | q = uprv_strrchr(arg, U_FILE_SEP_CHAR); |
michael@0 | 222 | #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR |
michael@0 | 223 | if (q == NULL) { |
michael@0 | 224 | q = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); |
michael@0 | 225 | } |
michael@0 | 226 | #endif |
michael@0 | 227 | uprv_strcpy(infile, inputDir); |
michael@0 | 228 | if(q != NULL) { |
michael@0 | 229 | uprv_strcat(infile, U_FILE_SEP_STRING); |
michael@0 | 230 | strncat(infile, arg, q-arg); |
michael@0 | 231 | } |
michael@0 | 232 | thename = infile; |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | status = U_ZERO_ERROR; |
michael@0 | 236 | if (thename) { |
michael@0 | 237 | bundle = ures_openDirect(thename, locale, &status); |
michael@0 | 238 | } else { |
michael@0 | 239 | bundle = ures_open(fromICUData ? 0 : inputDir, locale, &status); |
michael@0 | 240 | } |
michael@0 | 241 | if (status == U_ZERO_ERROR) { |
michael@0 | 242 | UFILE *out = NULL; |
michael@0 | 243 | |
michael@0 | 244 | const char *filename = 0; |
michael@0 | 245 | const char *ext = 0; |
michael@0 | 246 | |
michael@0 | 247 | if (!locale[0] || !tostdout) { |
michael@0 | 248 | filename = uprv_strrchr(arg, U_FILE_SEP_CHAR); |
michael@0 | 249 | |
michael@0 | 250 | #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR |
michael@0 | 251 | if (!filename) { |
michael@0 | 252 | filename = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); |
michael@0 | 253 | } |
michael@0 | 254 | #endif |
michael@0 | 255 | if (!filename) { |
michael@0 | 256 | filename = arg; |
michael@0 | 257 | } else { |
michael@0 | 258 | ++filename; |
michael@0 | 259 | } |
michael@0 | 260 | ext = uprv_strrchr(arg, '.'); |
michael@0 | 261 | if (!ext) { |
michael@0 | 262 | ext = filename + uprv_strlen(filename); |
michael@0 | 263 | } |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | if (tostdout) { |
michael@0 | 267 | out = u_get_stdout(); |
michael@0 | 268 | } else { |
michael@0 | 269 | char thefile[4096], *tp; |
michael@0 | 270 | int32_t len; |
michael@0 | 271 | |
michael@0 | 272 | if (outputDir) { |
michael@0 | 273 | uprv_strcpy(thefile, outputDir); |
michael@0 | 274 | uprv_strcat(thefile, U_FILE_SEP_STRING); |
michael@0 | 275 | } else { |
michael@0 | 276 | *thefile = 0; |
michael@0 | 277 | } |
michael@0 | 278 | uprv_strcat(thefile, filename); |
michael@0 | 279 | tp = thefile + uprv_strlen(thefile); |
michael@0 | 280 | len = (int32_t)uprv_strlen(ext); |
michael@0 | 281 | if (len) { |
michael@0 | 282 | tp -= len - 1; |
michael@0 | 283 | } else { |
michael@0 | 284 | *tp++ = '.'; |
michael@0 | 285 | } |
michael@0 | 286 | uprv_strcpy(tp, "txt"); |
michael@0 | 287 | |
michael@0 | 288 | out = u_fopen(thefile, "w", NULL, encoding); |
michael@0 | 289 | if (!out) { |
michael@0 | 290 | u_fprintf(ustderr, "%s: couldn't create %s\n", pname, thefile); |
michael@0 | 291 | u_fclose(ustderr); |
michael@0 | 292 | return 4; |
michael@0 | 293 | } |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | // now, set the callback. |
michael@0 | 297 | ucnv_setFromUCallBack(u_fgetConverter(out), UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, 0, 0, &status); |
michael@0 | 298 | if (U_FAILURE(status)) { |
michael@0 | 299 | u_fprintf(ustderr, "%s: couldn't configure converter for encoding\n", pname); |
michael@0 | 300 | u_fclose(ustderr); |
michael@0 | 301 | if(!tostdout) { |
michael@0 | 302 | u_fclose(out); |
michael@0 | 303 | } |
michael@0 | 304 | return 3; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | if (prbom) { /* XXX: Should be done only for UTFs */ |
michael@0 | 308 | u_fputc(0xFEFF, out); |
michael@0 | 309 | } |
michael@0 | 310 | u_fprintf(out, "// -*- Coding: %s; -*-\n//\n", encoding ? encoding : getEncodingName(ucnv_getDefaultName())); |
michael@0 | 311 | u_fprintf(out, "// This file was dumped by derb(8) from "); |
michael@0 | 312 | if (thename) { |
michael@0 | 313 | u_fprintf(out, "%s", thename); |
michael@0 | 314 | } else if (fromICUData) { |
michael@0 | 315 | u_fprintf(out, "the ICU internal %s locale", locale); |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | u_fprintf(out, "\n// derb(8) by Vladimir Weinstein and Yves Arrouye\n\n"); |
michael@0 | 319 | |
michael@0 | 320 | if (locale[0]) { |
michael@0 | 321 | u_fprintf(out, "%s", locale); |
michael@0 | 322 | } else { |
michael@0 | 323 | u_fprintf(out, "%.*s%.*S", (int32_t)(ext - filename), filename, (int32_t)(sizeof(sp)/sizeof(*sp)), sp); |
michael@0 | 324 | } |
michael@0 | 325 | printOutBundle(out, converter, bundle, 0, pname, &status); |
michael@0 | 326 | |
michael@0 | 327 | if (!tostdout) { |
michael@0 | 328 | u_fclose(out); |
michael@0 | 329 | } |
michael@0 | 330 | } |
michael@0 | 331 | else { |
michael@0 | 332 | reportError(pname, &status, "opening resource file"); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | ures_close(bundle); |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | ucnv_close(converter); |
michael@0 | 339 | |
michael@0 | 340 | return 0; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | static UChar *quotedString(const UChar *string) { |
michael@0 | 344 | int len = u_strlen(string); |
michael@0 | 345 | int alen = len; |
michael@0 | 346 | const UChar *sp; |
michael@0 | 347 | UChar *newstr, *np; |
michael@0 | 348 | |
michael@0 | 349 | for (sp = string; *sp; ++sp) { |
michael@0 | 350 | switch (*sp) { |
michael@0 | 351 | case '\n': |
michael@0 | 352 | case 0x0022: |
michael@0 | 353 | ++alen; |
michael@0 | 354 | break; |
michael@0 | 355 | } |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | newstr = (UChar *) uprv_malloc((1 + alen) * sizeof(*newstr)); |
michael@0 | 359 | for (sp = string, np = newstr; *sp; ++sp) { |
michael@0 | 360 | switch (*sp) { |
michael@0 | 361 | case '\n': |
michael@0 | 362 | *np++ = 0x005C; |
michael@0 | 363 | *np++ = 0x006E; |
michael@0 | 364 | break; |
michael@0 | 365 | |
michael@0 | 366 | case 0x0022: |
michael@0 | 367 | *np++ = 0x005C; |
michael@0 | 368 | |
michael@0 | 369 | default: |
michael@0 | 370 | *np++ = *sp; |
michael@0 | 371 | break; |
michael@0 | 372 | } |
michael@0 | 373 | } |
michael@0 | 374 | *np = 0; |
michael@0 | 375 | |
michael@0 | 376 | return newstr; |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | |
michael@0 | 380 | static void printString(UFILE *out, UConverter *converter, const UChar *str, int32_t len) { |
michael@0 | 381 | u_file_write(str, len, out); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | static void printCString(UFILE *out, UConverter *converter, const char *str, int32_t len) { |
michael@0 | 385 | if(len==-1) { |
michael@0 | 386 | u_fprintf(out, "%s", str); |
michael@0 | 387 | } else { |
michael@0 | 388 | u_fprintf(out, "%.*s", len, str); |
michael@0 | 389 | } |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | static void printIndent(UFILE *out, UConverter *converter, int32_t indent) { |
michael@0 | 393 | UChar inchar[256]; |
michael@0 | 394 | int32_t i = 0; |
michael@0 | 395 | for(i = 0; i<indent; i++) { |
michael@0 | 396 | inchar[i] = 0x0020; |
michael@0 | 397 | } |
michael@0 | 398 | inchar[indent] = 0; |
michael@0 | 399 | |
michael@0 | 400 | printString(out, converter, inchar, indent); |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | static void printHex(UFILE *out, UConverter *converter, uint8_t what) { |
michael@0 | 404 | static const char map[] = "0123456789ABCDEF"; |
michael@0 | 405 | UChar hex[2]; |
michael@0 | 406 | |
michael@0 | 407 | hex[0] = map[what >> 4]; |
michael@0 | 408 | hex[1] = map[what & 0xf]; |
michael@0 | 409 | |
michael@0 | 410 | printString(out, converter, hex, (int32_t)(sizeof(hex)/sizeof(*hex))); |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | static void printOutAlias(UFILE *out, UConverter *converter, UResourceBundle *parent, Resource r, const char *key, int32_t indent, const char *pname, UErrorCode *status) { |
michael@0 | 414 | static const UChar cr[] = { '\n' }; |
michael@0 | 415 | int32_t len = 0; |
michael@0 | 416 | const UChar* thestr = res_getAlias(&(parent->fResData), r, &len); |
michael@0 | 417 | UChar *string = quotedString(thestr); |
michael@0 | 418 | if(opt_truncate && len > truncsize) { |
michael@0 | 419 | char msg[128]; |
michael@0 | 420 | printIndent(out, converter, indent); |
michael@0 | 421 | sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", |
michael@0 | 422 | (long)len, (long)truncsize/2); |
michael@0 | 423 | printCString(out, converter, msg, -1); |
michael@0 | 424 | len = truncsize; |
michael@0 | 425 | } |
michael@0 | 426 | if(U_SUCCESS(*status)) { |
michael@0 | 427 | static const UChar openStr[] = { 0x003A, 0x0061, 0x006C, 0x0069, 0x0061, 0x0073, 0x0020, 0x007B, 0x0020, 0x0022 }; /* ":alias { \"" */ |
michael@0 | 428 | static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D, 0x0020 }; /* "\" } " */ |
michael@0 | 429 | printIndent(out, converter, indent); |
michael@0 | 430 | if(key != NULL) { |
michael@0 | 431 | printCString(out, converter, key, -1); |
michael@0 | 432 | } |
michael@0 | 433 | printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); |
michael@0 | 434 | printString(out, converter, string, len); |
michael@0 | 435 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 436 | if(verbose) { |
michael@0 | 437 | printCString(out, converter, " // ALIAS", -1); |
michael@0 | 438 | } |
michael@0 | 439 | printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); |
michael@0 | 440 | } else { |
michael@0 | 441 | reportError(pname, status, "getting binary value"); |
michael@0 | 442 | } |
michael@0 | 443 | uprv_free(string); |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | static void printOutBundle(UFILE *out, UConverter *converter, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status) |
michael@0 | 447 | { |
michael@0 | 448 | static const UChar cr[] = { '\n' }; |
michael@0 | 449 | |
michael@0 | 450 | /* int32_t noOfElements = ures_getSize(resource);*/ |
michael@0 | 451 | int32_t i = 0; |
michael@0 | 452 | const char *key = ures_getKey(resource); |
michael@0 | 453 | |
michael@0 | 454 | switch(ures_getType(resource)) { |
michael@0 | 455 | case URES_STRING : |
michael@0 | 456 | { |
michael@0 | 457 | int32_t len=0; |
michael@0 | 458 | const UChar* thestr = ures_getString(resource, &len, status); |
michael@0 | 459 | UChar *string = quotedString(thestr); |
michael@0 | 460 | |
michael@0 | 461 | /* TODO: String truncation */ |
michael@0 | 462 | if(opt_truncate && len > truncsize) { |
michael@0 | 463 | char msg[128]; |
michael@0 | 464 | printIndent(out, converter, indent); |
michael@0 | 465 | sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", |
michael@0 | 466 | (long)len, (long)(truncsize/2)); |
michael@0 | 467 | printCString(out, converter, msg, -1); |
michael@0 | 468 | len = truncsize/2; |
michael@0 | 469 | } |
michael@0 | 470 | printIndent(out, converter, indent); |
michael@0 | 471 | if(key != NULL) { |
michael@0 | 472 | static const UChar openStr[] = { 0x0020, 0x007B, 0x0020, 0x0022 }; /* " { \"" */ |
michael@0 | 473 | static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D }; /* "\" }" */ |
michael@0 | 474 | printCString(out, converter, key, (int32_t)uprv_strlen(key)); |
michael@0 | 475 | printString(out, converter, openStr, (int32_t)(sizeof(openStr)/sizeof(*openStr))); |
michael@0 | 476 | printString(out, converter, string, len); |
michael@0 | 477 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 478 | } else { |
michael@0 | 479 | static const UChar openStr[] = { 0x0022 }; /* "\"" */ |
michael@0 | 480 | static const UChar closeStr[] = { 0x0022, 0x002C }; /* "\"," */ |
michael@0 | 481 | |
michael@0 | 482 | printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); |
michael@0 | 483 | printString(out, converter, string, (int32_t)(u_strlen(string))); |
michael@0 | 484 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | if(verbose) { |
michael@0 | 488 | printCString(out, converter, "// STRING", -1); |
michael@0 | 489 | } |
michael@0 | 490 | printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); |
michael@0 | 491 | |
michael@0 | 492 | uprv_free(string); |
michael@0 | 493 | } |
michael@0 | 494 | break; |
michael@0 | 495 | |
michael@0 | 496 | case URES_INT : |
michael@0 | 497 | { |
michael@0 | 498 | static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0020, 0x007B, 0x0020 }; /* ":int { " */ |
michael@0 | 499 | static const UChar closeStr[] = { 0x0020, 0x007D }; /* " }" */ |
michael@0 | 500 | UChar num[20]; |
michael@0 | 501 | |
michael@0 | 502 | printIndent(out, converter, indent); |
michael@0 | 503 | if(key != NULL) { |
michael@0 | 504 | printCString(out, converter, key, -1); |
michael@0 | 505 | } |
michael@0 | 506 | printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); |
michael@0 | 507 | uprv_itou(num, 20, ures_getInt(resource, status), 10, 0); |
michael@0 | 508 | printString(out, converter, num, u_strlen(num)); |
michael@0 | 509 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 510 | |
michael@0 | 511 | if(verbose) { |
michael@0 | 512 | printCString(out, converter, "// INT", -1); |
michael@0 | 513 | } |
michael@0 | 514 | printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); |
michael@0 | 515 | break; |
michael@0 | 516 | } |
michael@0 | 517 | case URES_BINARY : |
michael@0 | 518 | { |
michael@0 | 519 | int32_t len = 0; |
michael@0 | 520 | const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status); |
michael@0 | 521 | if(opt_truncate && len > truncsize) { |
michael@0 | 522 | char msg[128]; |
michael@0 | 523 | printIndent(out, converter, indent); |
michael@0 | 524 | sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", |
michael@0 | 525 | (long)len, (long)(truncsize/2)); |
michael@0 | 526 | printCString(out, converter, msg, -1); |
michael@0 | 527 | len = truncsize; |
michael@0 | 528 | } |
michael@0 | 529 | if(U_SUCCESS(*status)) { |
michael@0 | 530 | static const UChar openStr[] = { 0x003A, 0x0062, 0x0069, 0x006E, 0x0061, 0x0072, 0x0079, 0x0020, 0x007B, 0x0020 }; /* ":binary { " */ |
michael@0 | 531 | static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ |
michael@0 | 532 | printIndent(out, converter, indent); |
michael@0 | 533 | if(key != NULL) { |
michael@0 | 534 | printCString(out, converter, key, -1); |
michael@0 | 535 | } |
michael@0 | 536 | printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); |
michael@0 | 537 | for(i = 0; i<len; i++) { |
michael@0 | 538 | printHex(out, converter, *data++); |
michael@0 | 539 | } |
michael@0 | 540 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 541 | if(verbose) { |
michael@0 | 542 | printCString(out, converter, " // BINARY", -1); |
michael@0 | 543 | } |
michael@0 | 544 | printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); |
michael@0 | 545 | } else { |
michael@0 | 546 | reportError(pname, status, "getting binary value"); |
michael@0 | 547 | } |
michael@0 | 548 | } |
michael@0 | 549 | break; |
michael@0 | 550 | case URES_INT_VECTOR : |
michael@0 | 551 | { |
michael@0 | 552 | int32_t len = 0; |
michael@0 | 553 | const int32_t *data = ures_getIntVector(resource, &len, status); |
michael@0 | 554 | if(U_SUCCESS(*status)) { |
michael@0 | 555 | static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0076, 0x0065, 0x0063, 0x0074, 0x006F, 0x0072, 0x0020, 0x007B, 0x0020 }; /* ":intvector { " */ |
michael@0 | 556 | static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ |
michael@0 | 557 | UChar num[20]; |
michael@0 | 558 | |
michael@0 | 559 | printIndent(out, converter, indent); |
michael@0 | 560 | if(key != NULL) { |
michael@0 | 561 | printCString(out, converter, key, -1); |
michael@0 | 562 | } |
michael@0 | 563 | printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); |
michael@0 | 564 | for(i = 0; i < len - 1; i++) { |
michael@0 | 565 | int32_t numLen = uprv_itou(num, 20, data[i], 10, 0); |
michael@0 | 566 | num[numLen++] = 0x002C; /* ',' */ |
michael@0 | 567 | num[numLen++] = 0x0020; /* ' ' */ |
michael@0 | 568 | num[numLen] = 0; |
michael@0 | 569 | printString(out, converter, num, u_strlen(num)); |
michael@0 | 570 | } |
michael@0 | 571 | if(len > 0) { |
michael@0 | 572 | uprv_itou(num, 20, data[len - 1], 10, 0); |
michael@0 | 573 | printString(out, converter, num, u_strlen(num)); |
michael@0 | 574 | } |
michael@0 | 575 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 576 | if(verbose) { |
michael@0 | 577 | printCString(out, converter, "// INTVECTOR", -1); |
michael@0 | 578 | } |
michael@0 | 579 | printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); |
michael@0 | 580 | } else { |
michael@0 | 581 | reportError(pname, status, "getting int vector"); |
michael@0 | 582 | } |
michael@0 | 583 | } |
michael@0 | 584 | break; |
michael@0 | 585 | case URES_TABLE : |
michael@0 | 586 | case URES_ARRAY : |
michael@0 | 587 | { |
michael@0 | 588 | static const UChar openStr[] = { 0x007B }; /* "{" */ |
michael@0 | 589 | static const UChar closeStr[] = { 0x007D, '\n' }; /* "}\n" */ |
michael@0 | 590 | |
michael@0 | 591 | UResourceBundle *t = NULL; |
michael@0 | 592 | ures_resetIterator(resource); |
michael@0 | 593 | printIndent(out, converter, indent); |
michael@0 | 594 | if(key != NULL) { |
michael@0 | 595 | printCString(out, converter, key, -1); |
michael@0 | 596 | } |
michael@0 | 597 | printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); |
michael@0 | 598 | if(verbose) { |
michael@0 | 599 | if(ures_getType(resource) == URES_TABLE) { |
michael@0 | 600 | printCString(out, converter, "// TABLE", -1); |
michael@0 | 601 | } else { |
michael@0 | 602 | printCString(out, converter, "// ARRAY", -1); |
michael@0 | 603 | } |
michael@0 | 604 | } |
michael@0 | 605 | printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); |
michael@0 | 606 | |
michael@0 | 607 | if(suppressAliases == FALSE) { |
michael@0 | 608 | while(U_SUCCESS(*status) && ures_hasNext(resource)) { |
michael@0 | 609 | t = ures_getNextResource(resource, t, status); |
michael@0 | 610 | if(U_SUCCESS(*status)) { |
michael@0 | 611 | printOutBundle(out, converter, t, indent+indentsize, pname, status); |
michael@0 | 612 | } else { |
michael@0 | 613 | reportError(pname, status, "While processing table"); |
michael@0 | 614 | *status = U_ZERO_ERROR; |
michael@0 | 615 | } |
michael@0 | 616 | } |
michael@0 | 617 | } else { /* we have to use low level access to do this */ |
michael@0 | 618 | Resource r; |
michael@0 | 619 | int32_t resSize = ures_getSize(resource); |
michael@0 | 620 | UBool isTable = (UBool)(ures_getType(resource) == URES_TABLE); |
michael@0 | 621 | for(i = 0; i < resSize; i++) { |
michael@0 | 622 | /* need to know if it's an alias */ |
michael@0 | 623 | if(isTable) { |
michael@0 | 624 | r = res_getTableItemByIndex(&resource->fResData, resource->fRes, i, &key); |
michael@0 | 625 | } else { |
michael@0 | 626 | r = res_getArrayItem(&resource->fResData, resource->fRes, i); |
michael@0 | 627 | } |
michael@0 | 628 | if(U_SUCCESS(*status)) { |
michael@0 | 629 | if(res_getPublicType(r) == URES_ALIAS) { |
michael@0 | 630 | printOutAlias(out, converter, resource, r, key, indent+indentsize, pname, status); |
michael@0 | 631 | } else { |
michael@0 | 632 | t = ures_getByIndex(resource, i, t, status); |
michael@0 | 633 | printOutBundle(out, converter, t, indent+indentsize, pname, status); |
michael@0 | 634 | } |
michael@0 | 635 | } else { |
michael@0 | 636 | reportError(pname, status, "While processing table"); |
michael@0 | 637 | *status = U_ZERO_ERROR; |
michael@0 | 638 | } |
michael@0 | 639 | } |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | printIndent(out, converter, indent); |
michael@0 | 643 | printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); |
michael@0 | 644 | ures_close(t); |
michael@0 | 645 | } |
michael@0 | 646 | break; |
michael@0 | 647 | default: |
michael@0 | 648 | break; |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | } |
michael@0 | 652 | |
michael@0 | 653 | static const char *getEncodingName(const char *encoding) { |
michael@0 | 654 | UErrorCode err; |
michael@0 | 655 | const char *enc; |
michael@0 | 656 | |
michael@0 | 657 | err = U_ZERO_ERROR; |
michael@0 | 658 | if (!(enc = ucnv_getStandardName(encoding, "MIME", &err))) { |
michael@0 | 659 | err = U_ZERO_ERROR; |
michael@0 | 660 | if (!(enc = ucnv_getStandardName(encoding, "IANA", &err))) { |
michael@0 | 661 | ; |
michael@0 | 662 | } |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | return enc; |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | static void reportError(const char *pname, UErrorCode *status, const char *when) { |
michael@0 | 669 | u_fprintf(ustderr, "%s: error %d while %s: %s\n", pname, *status, when, u_errorName(*status)); |
michael@0 | 670 | } |
michael@0 | 671 | |
michael@0 | 672 | #else |
michael@0 | 673 | extern int |
michael@0 | 674 | main(int argc, char* argv[]) { |
michael@0 | 675 | /* Changing stdio.h ustdio.h requires that formatting not be disabled. */ |
michael@0 | 676 | return 3; |
michael@0 | 677 | } |
michael@0 | 678 | #endif /* !UCONFIG_NO_FORMATTING */ |
michael@0 | 679 | |
michael@0 | 680 | /* |
michael@0 | 681 | * Local Variables: |
michael@0 | 682 | * indent-tabs-mode: nil |
michael@0 | 683 | * End: |
michael@0 | 684 | */ |