12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others. 22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html 32e5b6d6dSopenharmony_ci/* 42e5b6d6dSopenharmony_ci******************************************************************************* 52e5b6d6dSopenharmony_ci* 62e5b6d6dSopenharmony_ci* Copyright (C) 1999-2016, International Business Machines 72e5b6d6dSopenharmony_ci* Corporation and others. All Rights Reserved. 82e5b6d6dSopenharmony_ci* 92e5b6d6dSopenharmony_ci******************************************************************************* 102e5b6d6dSopenharmony_ci* file name: derb.cpp 112e5b6d6dSopenharmony_ci* encoding: UTF-8 122e5b6d6dSopenharmony_ci* tab size: 8 (not used) 132e5b6d6dSopenharmony_ci* indentation:4 142e5b6d6dSopenharmony_ci* 152e5b6d6dSopenharmony_ci* created on: 2000sep6 162e5b6d6dSopenharmony_ci* created by: Vladimir Weinstein as an ICU workshop example 172e5b6d6dSopenharmony_ci* maintained by: Yves Arrouye <yves@realnames.com> 182e5b6d6dSopenharmony_ci*/ 192e5b6d6dSopenharmony_ci 202e5b6d6dSopenharmony_ci#include "unicode/stringpiece.h" 212e5b6d6dSopenharmony_ci#include "unicode/ucnv.h" 222e5b6d6dSopenharmony_ci#include "unicode/unistr.h" 232e5b6d6dSopenharmony_ci#include "unicode/ustring.h" 242e5b6d6dSopenharmony_ci#include "unicode/putil.h" 252e5b6d6dSopenharmony_ci#include "unicode/ustdio.h" 262e5b6d6dSopenharmony_ci 272e5b6d6dSopenharmony_ci#include "charstr.h" 282e5b6d6dSopenharmony_ci#include "uresimp.h" 292e5b6d6dSopenharmony_ci#include "cmemory.h" 302e5b6d6dSopenharmony_ci#include "cstring.h" 312e5b6d6dSopenharmony_ci#include "uoptions.h" 322e5b6d6dSopenharmony_ci#include "toolutil.h" 332e5b6d6dSopenharmony_ci#include "ustrfmt.h" 342e5b6d6dSopenharmony_ci 352e5b6d6dSopenharmony_ci#if !UCONFIG_NO_FORMATTING 362e5b6d6dSopenharmony_ci 372e5b6d6dSopenharmony_ci#define DERB_VERSION "1.1" 382e5b6d6dSopenharmony_ci 392e5b6d6dSopenharmony_ci#define DERB_DEFAULT_TRUNC 80 402e5b6d6dSopenharmony_ci 412e5b6d6dSopenharmony_cistatic const int32_t indentsize = 4; 422e5b6d6dSopenharmony_cistatic int32_t truncsize = DERB_DEFAULT_TRUNC; 432e5b6d6dSopenharmony_cistatic UBool opt_truncate = false; 442e5b6d6dSopenharmony_ci 452e5b6d6dSopenharmony_cistatic const char *getEncodingName(const char *encoding); 462e5b6d6dSopenharmony_cistatic void reportError(const char *pname, UErrorCode *status, const char *when); 472e5b6d6dSopenharmony_cistatic UChar *quotedString(const UChar *string); 482e5b6d6dSopenharmony_cistatic void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status); 492e5b6d6dSopenharmony_cistatic void printString(UFILE *out, const UChar *str, int32_t len); 502e5b6d6dSopenharmony_cistatic void printCString(UFILE *out, const char *str, int32_t len); 512e5b6d6dSopenharmony_cistatic void printIndent(UFILE *out, int32_t indent); 522e5b6d6dSopenharmony_cistatic void printHex(UFILE *out, uint8_t what); 532e5b6d6dSopenharmony_ci 542e5b6d6dSopenharmony_cistatic UOption options[]={ 552e5b6d6dSopenharmony_ci UOPTION_HELP_H, 562e5b6d6dSopenharmony_ci UOPTION_HELP_QUESTION_MARK, 572e5b6d6dSopenharmony_ci/* 2 */ UOPTION_ENCODING, 582e5b6d6dSopenharmony_ci/* 3 */ { "to-stdout", NULL, NULL, NULL, 'c', UOPT_NO_ARG, 0 } , 592e5b6d6dSopenharmony_ci/* 4 */ { "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 }, 602e5b6d6dSopenharmony_ci/* 5 */ UOPTION_VERBOSE, 612e5b6d6dSopenharmony_ci/* 6 */ UOPTION_DESTDIR, 622e5b6d6dSopenharmony_ci/* 7 */ UOPTION_SOURCEDIR, 632e5b6d6dSopenharmony_ci/* 8 */ { "bom", NULL, NULL, NULL, 0, UOPT_NO_ARG, 0 }, 642e5b6d6dSopenharmony_ci/* 9 */ UOPTION_ICUDATADIR, 652e5b6d6dSopenharmony_ci/* 10 */ UOPTION_VERSION, 662e5b6d6dSopenharmony_ci/* 11 */ { "suppressAliases", NULL, NULL, NULL, 'A', UOPT_NO_ARG, 0 }, 672e5b6d6dSopenharmony_ci}; 682e5b6d6dSopenharmony_ci 692e5b6d6dSopenharmony_cistatic UBool verbose = false; 702e5b6d6dSopenharmony_cistatic UBool suppressAliases = false; 712e5b6d6dSopenharmony_cistatic UFILE *ustderr = NULL; 722e5b6d6dSopenharmony_ci 732e5b6d6dSopenharmony_ciextern int 742e5b6d6dSopenharmony_cimain(int argc, char* argv[]) { 752e5b6d6dSopenharmony_ci const char *encoding = NULL; 762e5b6d6dSopenharmony_ci const char *outputDir = NULL; /* NULL = no output directory, use current */ 772e5b6d6dSopenharmony_ci const char *inputDir = "."; 782e5b6d6dSopenharmony_ci int tostdout = 0; 792e5b6d6dSopenharmony_ci int prbom = 0; 802e5b6d6dSopenharmony_ci 812e5b6d6dSopenharmony_ci const char *pname; 822e5b6d6dSopenharmony_ci 832e5b6d6dSopenharmony_ci UResourceBundle *bundle = NULL; 842e5b6d6dSopenharmony_ci int32_t i = 0; 852e5b6d6dSopenharmony_ci 862e5b6d6dSopenharmony_ci const char* arg; 872e5b6d6dSopenharmony_ci 882e5b6d6dSopenharmony_ci /* Get the name of tool. */ 892e5b6d6dSopenharmony_ci pname = uprv_strrchr(*argv, U_FILE_SEP_CHAR); 902e5b6d6dSopenharmony_ci#if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR 912e5b6d6dSopenharmony_ci if (!pname) { 922e5b6d6dSopenharmony_ci pname = uprv_strrchr(*argv, U_FILE_ALT_SEP_CHAR); 932e5b6d6dSopenharmony_ci } 942e5b6d6dSopenharmony_ci#endif 952e5b6d6dSopenharmony_ci if (!pname) { 962e5b6d6dSopenharmony_ci pname = *argv; 972e5b6d6dSopenharmony_ci } else { 982e5b6d6dSopenharmony_ci ++pname; 992e5b6d6dSopenharmony_ci } 1002e5b6d6dSopenharmony_ci 1012e5b6d6dSopenharmony_ci /* error handling, printing usage message */ 1022e5b6d6dSopenharmony_ci argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options); 1032e5b6d6dSopenharmony_ci 1042e5b6d6dSopenharmony_ci /* error handling, printing usage message */ 1052e5b6d6dSopenharmony_ci if(argc<0) { 1062e5b6d6dSopenharmony_ci fprintf(stderr, 1072e5b6d6dSopenharmony_ci "%s: error in command line argument \"%s\"\n", pname, 1082e5b6d6dSopenharmony_ci argv[-argc]); 1092e5b6d6dSopenharmony_ci } 1102e5b6d6dSopenharmony_ci if(argc<0 || options[0].doesOccur || options[1].doesOccur) { 1112e5b6d6dSopenharmony_ci fprintf(argc < 0 ? stderr : stdout, 1122e5b6d6dSopenharmony_ci "%csage: %s [ -h, -?, --help ] [ -V, --version ]\n" 1132e5b6d6dSopenharmony_ci " [ -v, --verbose ] [ -e, --encoding encoding ] [ --bom ]\n" 1142e5b6d6dSopenharmony_ci " [ -t, --truncate [ size ] ]\n" 1152e5b6d6dSopenharmony_ci " [ -s, --sourcedir source ] [ -d, --destdir destination ]\n" 1162e5b6d6dSopenharmony_ci " [ -i, --icudatadir directory ] [ -c, --to-stdout ]\n" 1172e5b6d6dSopenharmony_ci " [ -A, --suppressAliases]\n" 1182e5b6d6dSopenharmony_ci " bundle ...\n", argc < 0 ? 'u' : 'U', 1192e5b6d6dSopenharmony_ci pname); 1202e5b6d6dSopenharmony_ci return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; 1212e5b6d6dSopenharmony_ci } 1222e5b6d6dSopenharmony_ci 1232e5b6d6dSopenharmony_ci if(options[10].doesOccur) { 1242e5b6d6dSopenharmony_ci fprintf(stderr, 1252e5b6d6dSopenharmony_ci "%s version %s (ICU version %s).\n" 1262e5b6d6dSopenharmony_ci "%s\n", 1272e5b6d6dSopenharmony_ci pname, DERB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); 1282e5b6d6dSopenharmony_ci return U_ZERO_ERROR; 1292e5b6d6dSopenharmony_ci } 1302e5b6d6dSopenharmony_ci if(options[2].doesOccur) { 1312e5b6d6dSopenharmony_ci encoding = options[2].value; 1322e5b6d6dSopenharmony_ci } 1332e5b6d6dSopenharmony_ci 1342e5b6d6dSopenharmony_ci if (options[3].doesOccur) { 1352e5b6d6dSopenharmony_ci if(options[2].doesOccur) { 1362e5b6d6dSopenharmony_ci fprintf(stderr, "%s: Error: don't specify an encoding (-e) when writing to stdout (-c).\n", pname); 1372e5b6d6dSopenharmony_ci return 3; 1382e5b6d6dSopenharmony_ci } 1392e5b6d6dSopenharmony_ci tostdout = 1; 1402e5b6d6dSopenharmony_ci } 1412e5b6d6dSopenharmony_ci 1422e5b6d6dSopenharmony_ci if(options[4].doesOccur) { 1432e5b6d6dSopenharmony_ci opt_truncate = true; 1442e5b6d6dSopenharmony_ci if(options[4].value != NULL) { 1452e5b6d6dSopenharmony_ci truncsize = atoi(options[4].value); /* user defined printable size */ 1462e5b6d6dSopenharmony_ci } else { 1472e5b6d6dSopenharmony_ci truncsize = DERB_DEFAULT_TRUNC; /* we'll use default omitting size */ 1482e5b6d6dSopenharmony_ci } 1492e5b6d6dSopenharmony_ci } else { 1502e5b6d6dSopenharmony_ci opt_truncate = false; 1512e5b6d6dSopenharmony_ci } 1522e5b6d6dSopenharmony_ci 1532e5b6d6dSopenharmony_ci if(options[5].doesOccur) { 1542e5b6d6dSopenharmony_ci verbose = true; 1552e5b6d6dSopenharmony_ci } 1562e5b6d6dSopenharmony_ci 1572e5b6d6dSopenharmony_ci if (options[6].doesOccur) { 1582e5b6d6dSopenharmony_ci outputDir = options[6].value; 1592e5b6d6dSopenharmony_ci } 1602e5b6d6dSopenharmony_ci 1612e5b6d6dSopenharmony_ci if(options[7].doesOccur) { 1622e5b6d6dSopenharmony_ci inputDir = options[7].value; /* we'll use users resources */ 1632e5b6d6dSopenharmony_ci } 1642e5b6d6dSopenharmony_ci 1652e5b6d6dSopenharmony_ci if (options[8].doesOccur) { 1662e5b6d6dSopenharmony_ci prbom = 1; 1672e5b6d6dSopenharmony_ci } 1682e5b6d6dSopenharmony_ci 1692e5b6d6dSopenharmony_ci if (options[9].doesOccur) { 1702e5b6d6dSopenharmony_ci u_setDataDirectory(options[9].value); 1712e5b6d6dSopenharmony_ci } 1722e5b6d6dSopenharmony_ci 1732e5b6d6dSopenharmony_ci if (options[11].doesOccur) { 1742e5b6d6dSopenharmony_ci suppressAliases = true; 1752e5b6d6dSopenharmony_ci } 1762e5b6d6dSopenharmony_ci 1772e5b6d6dSopenharmony_ci fflush(stderr); // use ustderr now. 1782e5b6d6dSopenharmony_ci ustderr = u_finit(stderr, NULL, NULL); 1792e5b6d6dSopenharmony_ci 1802e5b6d6dSopenharmony_ci for (i = 1; i < argc; ++i) { 1812e5b6d6dSopenharmony_ci static const UChar sp[] = { 0x0020 }; /* " " */ 1822e5b6d6dSopenharmony_ci 1832e5b6d6dSopenharmony_ci arg = getLongPathname(argv[i]); 1842e5b6d6dSopenharmony_ci 1852e5b6d6dSopenharmony_ci if (verbose) { 1862e5b6d6dSopenharmony_ci u_fprintf(ustderr, "processing bundle \"%s\"\n", argv[i]); 1872e5b6d6dSopenharmony_ci } 1882e5b6d6dSopenharmony_ci 1892e5b6d6dSopenharmony_ci icu::CharString locale; 1902e5b6d6dSopenharmony_ci UErrorCode status = U_ZERO_ERROR; 1912e5b6d6dSopenharmony_ci { 1922e5b6d6dSopenharmony_ci const char *p = findBasename(arg); 1932e5b6d6dSopenharmony_ci const char *q = uprv_strrchr(p, '.'); 1942e5b6d6dSopenharmony_ci if (q == NULL) { 1952e5b6d6dSopenharmony_ci locale.append(p, status); 1962e5b6d6dSopenharmony_ci } else { 1972e5b6d6dSopenharmony_ci locale.append(p, (int32_t)(q - p), status); 1982e5b6d6dSopenharmony_ci } 1992e5b6d6dSopenharmony_ci } 2002e5b6d6dSopenharmony_ci if (U_FAILURE(status)) { 2012e5b6d6dSopenharmony_ci return status; 2022e5b6d6dSopenharmony_ci } 2032e5b6d6dSopenharmony_ci 2042e5b6d6dSopenharmony_ci icu::CharString infile; 2052e5b6d6dSopenharmony_ci const char *thename = NULL; 2062e5b6d6dSopenharmony_ci UBool fromICUData = !uprv_strcmp(inputDir, "-"); 2072e5b6d6dSopenharmony_ci if (!fromICUData) { 2082e5b6d6dSopenharmony_ci UBool absfilename = *arg == U_FILE_SEP_CHAR; 2092e5b6d6dSopenharmony_ci#if U_PLATFORM_HAS_WIN32_API 2102e5b6d6dSopenharmony_ci if (!absfilename) { 2112e5b6d6dSopenharmony_ci absfilename = (uprv_strlen(arg) > 2 && isalpha(arg[0]) 2122e5b6d6dSopenharmony_ci && arg[1] == ':' && arg[2] == U_FILE_SEP_CHAR); 2132e5b6d6dSopenharmony_ci } 2142e5b6d6dSopenharmony_ci#endif 2152e5b6d6dSopenharmony_ci if (absfilename) { 2162e5b6d6dSopenharmony_ci thename = arg; 2172e5b6d6dSopenharmony_ci } else { 2182e5b6d6dSopenharmony_ci const char *q = uprv_strrchr(arg, U_FILE_SEP_CHAR); 2192e5b6d6dSopenharmony_ci#if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR 2202e5b6d6dSopenharmony_ci if (q == NULL) { 2212e5b6d6dSopenharmony_ci q = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); 2222e5b6d6dSopenharmony_ci } 2232e5b6d6dSopenharmony_ci#endif 2242e5b6d6dSopenharmony_ci infile.append(inputDir, status); 2252e5b6d6dSopenharmony_ci if(q != NULL) { 2262e5b6d6dSopenharmony_ci infile.appendPathPart(icu::StringPiece(arg, (int32_t)(q - arg)), status); 2272e5b6d6dSopenharmony_ci } 2282e5b6d6dSopenharmony_ci if (U_FAILURE(status)) { 2292e5b6d6dSopenharmony_ci return status; 2302e5b6d6dSopenharmony_ci } 2312e5b6d6dSopenharmony_ci thename = infile.data(); 2322e5b6d6dSopenharmony_ci } 2332e5b6d6dSopenharmony_ci } 2342e5b6d6dSopenharmony_ci if (thename) { 2352e5b6d6dSopenharmony_ci bundle = ures_openDirect(thename, locale.data(), &status); 2362e5b6d6dSopenharmony_ci } else { 2372e5b6d6dSopenharmony_ci bundle = ures_open(fromICUData ? 0 : inputDir, locale.data(), &status); 2382e5b6d6dSopenharmony_ci } 2392e5b6d6dSopenharmony_ci if (U_SUCCESS(status)) { 2402e5b6d6dSopenharmony_ci UFILE *out = NULL; 2412e5b6d6dSopenharmony_ci 2422e5b6d6dSopenharmony_ci const char *filename = 0; 2432e5b6d6dSopenharmony_ci const char *ext = 0; 2442e5b6d6dSopenharmony_ci 2452e5b6d6dSopenharmony_ci if (locale.isEmpty() || !tostdout) { 2462e5b6d6dSopenharmony_ci filename = findBasename(arg); 2472e5b6d6dSopenharmony_ci ext = uprv_strrchr(filename, '.'); 2482e5b6d6dSopenharmony_ci if (!ext) { 2492e5b6d6dSopenharmony_ci ext = uprv_strchr(filename, 0); 2502e5b6d6dSopenharmony_ci } 2512e5b6d6dSopenharmony_ci } 2522e5b6d6dSopenharmony_ci 2532e5b6d6dSopenharmony_ci if (tostdout) { 2542e5b6d6dSopenharmony_ci out = u_get_stdout(); 2552e5b6d6dSopenharmony_ci } else { 2562e5b6d6dSopenharmony_ci icu::CharString thefile; 2572e5b6d6dSopenharmony_ci if (outputDir) { 2582e5b6d6dSopenharmony_ci thefile.append(outputDir, status); 2592e5b6d6dSopenharmony_ci } 2602e5b6d6dSopenharmony_ci thefile.appendPathPart(filename, status); 2612e5b6d6dSopenharmony_ci if (*ext) { 2622e5b6d6dSopenharmony_ci thefile.truncate(thefile.length() - (int32_t)uprv_strlen(ext)); 2632e5b6d6dSopenharmony_ci } 2642e5b6d6dSopenharmony_ci thefile.append(".txt", status); 2652e5b6d6dSopenharmony_ci if (U_FAILURE(status)) { 2662e5b6d6dSopenharmony_ci return status; 2672e5b6d6dSopenharmony_ci } 2682e5b6d6dSopenharmony_ci 2692e5b6d6dSopenharmony_ci out = u_fopen(thefile.data(), "w", NULL, encoding); 2702e5b6d6dSopenharmony_ci if (!out) { 2712e5b6d6dSopenharmony_ci u_fprintf(ustderr, "%s: couldn't create %s\n", pname, thefile.data()); 2722e5b6d6dSopenharmony_ci u_fclose(ustderr); 2732e5b6d6dSopenharmony_ci return 4; 2742e5b6d6dSopenharmony_ci } 2752e5b6d6dSopenharmony_ci } 2762e5b6d6dSopenharmony_ci 2772e5b6d6dSopenharmony_ci // now, set the callback. 2782e5b6d6dSopenharmony_ci ucnv_setFromUCallBack(u_fgetConverter(out), UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, 0, 0, &status); 2792e5b6d6dSopenharmony_ci if (U_FAILURE(status)) { 2802e5b6d6dSopenharmony_ci u_fprintf(ustderr, "%s: couldn't configure converter for encoding\n", pname); 2812e5b6d6dSopenharmony_ci u_fclose(ustderr); 2822e5b6d6dSopenharmony_ci if(!tostdout) { 2832e5b6d6dSopenharmony_ci u_fclose(out); 2842e5b6d6dSopenharmony_ci } 2852e5b6d6dSopenharmony_ci return 3; 2862e5b6d6dSopenharmony_ci } 2872e5b6d6dSopenharmony_ci 2882e5b6d6dSopenharmony_ci if (prbom) { /* XXX: Should be done only for UTFs */ 2892e5b6d6dSopenharmony_ci u_fputc(0xFEFF, out); 2902e5b6d6dSopenharmony_ci } 2912e5b6d6dSopenharmony_ci u_fprintf(out, "// -*- Coding: %s; -*-\n//\n", encoding ? encoding : getEncodingName(ucnv_getDefaultName())); 2922e5b6d6dSopenharmony_ci u_fprintf(out, "// This file was dumped by derb(8) from "); 2932e5b6d6dSopenharmony_ci if (thename) { 2942e5b6d6dSopenharmony_ci u_fprintf(out, "%s", thename); 2952e5b6d6dSopenharmony_ci } else if (fromICUData) { 2962e5b6d6dSopenharmony_ci u_fprintf(out, "the ICU internal %s locale", locale.data()); 2972e5b6d6dSopenharmony_ci } 2982e5b6d6dSopenharmony_ci 2992e5b6d6dSopenharmony_ci u_fprintf(out, "\n// derb(8) by Vladimir Weinstein and Yves Arrouye\n\n"); 3002e5b6d6dSopenharmony_ci 3012e5b6d6dSopenharmony_ci if (!locale.isEmpty()) { 3022e5b6d6dSopenharmony_ci u_fprintf(out, "%s", locale.data()); 3032e5b6d6dSopenharmony_ci } else { 3042e5b6d6dSopenharmony_ci u_fprintf(out, "%.*s%.*S", (int32_t)(ext - filename), filename, UPRV_LENGTHOF(sp), sp); 3052e5b6d6dSopenharmony_ci } 3062e5b6d6dSopenharmony_ci printOutBundle(out, bundle, 0, pname, &status); 3072e5b6d6dSopenharmony_ci 3082e5b6d6dSopenharmony_ci if (!tostdout) { 3092e5b6d6dSopenharmony_ci u_fclose(out); 3102e5b6d6dSopenharmony_ci } 3112e5b6d6dSopenharmony_ci } 3122e5b6d6dSopenharmony_ci else { 3132e5b6d6dSopenharmony_ci reportError(pname, &status, "opening resource file"); 3142e5b6d6dSopenharmony_ci } 3152e5b6d6dSopenharmony_ci 3162e5b6d6dSopenharmony_ci ures_close(bundle); 3172e5b6d6dSopenharmony_ci } 3182e5b6d6dSopenharmony_ci 3192e5b6d6dSopenharmony_ci return 0; 3202e5b6d6dSopenharmony_ci} 3212e5b6d6dSopenharmony_ci 3222e5b6d6dSopenharmony_cistatic UChar *quotedString(const UChar *string) { 3232e5b6d6dSopenharmony_ci int len = u_strlen(string); 3242e5b6d6dSopenharmony_ci int alen = len; 3252e5b6d6dSopenharmony_ci const UChar *sp; 3262e5b6d6dSopenharmony_ci UChar *newstr, *np; 3272e5b6d6dSopenharmony_ci 3282e5b6d6dSopenharmony_ci for (sp = string; *sp; ++sp) { 3292e5b6d6dSopenharmony_ci switch (*sp) { 3302e5b6d6dSopenharmony_ci case '\n': 3312e5b6d6dSopenharmony_ci case 0x0022: 3322e5b6d6dSopenharmony_ci ++alen; 3332e5b6d6dSopenharmony_ci break; 3342e5b6d6dSopenharmony_ci } 3352e5b6d6dSopenharmony_ci } 3362e5b6d6dSopenharmony_ci 3372e5b6d6dSopenharmony_ci newstr = (UChar *) uprv_malloc((1 + alen) * U_SIZEOF_UCHAR); 3382e5b6d6dSopenharmony_ci for (sp = string, np = newstr; *sp; ++sp) { 3392e5b6d6dSopenharmony_ci switch (*sp) { 3402e5b6d6dSopenharmony_ci case '\n': 3412e5b6d6dSopenharmony_ci *np++ = 0x005C; 3422e5b6d6dSopenharmony_ci *np++ = 0x006E; 3432e5b6d6dSopenharmony_ci break; 3442e5b6d6dSopenharmony_ci 3452e5b6d6dSopenharmony_ci case 0x0022: 3462e5b6d6dSopenharmony_ci *np++ = 0x005C; 3472e5b6d6dSopenharmony_ci U_FALLTHROUGH; 3482e5b6d6dSopenharmony_ci default: 3492e5b6d6dSopenharmony_ci *np++ = *sp; 3502e5b6d6dSopenharmony_ci break; 3512e5b6d6dSopenharmony_ci } 3522e5b6d6dSopenharmony_ci } 3532e5b6d6dSopenharmony_ci *np = 0; 3542e5b6d6dSopenharmony_ci 3552e5b6d6dSopenharmony_ci return newstr; 3562e5b6d6dSopenharmony_ci} 3572e5b6d6dSopenharmony_ci 3582e5b6d6dSopenharmony_ci 3592e5b6d6dSopenharmony_cistatic void printString(UFILE *out, const UChar *str, int32_t len) { 3602e5b6d6dSopenharmony_ci u_file_write(str, len, out); 3612e5b6d6dSopenharmony_ci} 3622e5b6d6dSopenharmony_ci 3632e5b6d6dSopenharmony_cistatic void printCString(UFILE *out, const char *str, int32_t len) { 3642e5b6d6dSopenharmony_ci if(len==-1) { 3652e5b6d6dSopenharmony_ci u_fprintf(out, "%s", str); 3662e5b6d6dSopenharmony_ci } else { 3672e5b6d6dSopenharmony_ci u_fprintf(out, "%.*s", len, str); 3682e5b6d6dSopenharmony_ci } 3692e5b6d6dSopenharmony_ci} 3702e5b6d6dSopenharmony_ci 3712e5b6d6dSopenharmony_cistatic void printIndent(UFILE *out, int32_t indent) { 3722e5b6d6dSopenharmony_ci icu::UnicodeString inchar(indent, 0x20, indent); 3732e5b6d6dSopenharmony_ci printString(out, inchar.getBuffer(), indent); 3742e5b6d6dSopenharmony_ci} 3752e5b6d6dSopenharmony_ci 3762e5b6d6dSopenharmony_cistatic void printHex(UFILE *out, uint8_t what) { 3772e5b6d6dSopenharmony_ci static const char map[] = "0123456789ABCDEF"; 3782e5b6d6dSopenharmony_ci UChar hex[2]; 3792e5b6d6dSopenharmony_ci 3802e5b6d6dSopenharmony_ci hex[0] = map[what >> 4]; 3812e5b6d6dSopenharmony_ci hex[1] = map[what & 0xf]; 3822e5b6d6dSopenharmony_ci 3832e5b6d6dSopenharmony_ci printString(out, hex, 2); 3842e5b6d6dSopenharmony_ci} 3852e5b6d6dSopenharmony_ci 3862e5b6d6dSopenharmony_cistatic void printOutAlias(UFILE *out, UResourceBundle *parent, Resource r, const char *key, int32_t indent, const char *pname, UErrorCode *status) { 3872e5b6d6dSopenharmony_ci static const UChar cr[] = { 0xA }; // LF 3882e5b6d6dSopenharmony_ci int32_t len = 0; 3892e5b6d6dSopenharmony_ci const UChar* thestr = res_getAlias(&(parent->getResData()), r, &len); 3902e5b6d6dSopenharmony_ci UChar *string = quotedString(thestr); 3912e5b6d6dSopenharmony_ci if(opt_truncate && len > truncsize) { 3922e5b6d6dSopenharmony_ci char msg[128]; 3932e5b6d6dSopenharmony_ci printIndent(out, indent); 3942e5b6d6dSopenharmony_ci sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", 3952e5b6d6dSopenharmony_ci (long)len, (long)truncsize/2); 3962e5b6d6dSopenharmony_ci printCString(out, msg, -1); 3972e5b6d6dSopenharmony_ci len = truncsize; 3982e5b6d6dSopenharmony_ci } 3992e5b6d6dSopenharmony_ci if(U_SUCCESS(*status)) { 4002e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x003A, 0x0061, 0x006C, 0x0069, 0x0061, 0x0073, 0x0020, 0x007B, 0x0020, 0x0022 }; /* ":alias { \"" */ 4012e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D, 0x0020 }; /* "\" } " */ 4022e5b6d6dSopenharmony_ci printIndent(out, indent); 4032e5b6d6dSopenharmony_ci if(key != NULL) { 4042e5b6d6dSopenharmony_ci printCString(out, key, -1); 4052e5b6d6dSopenharmony_ci } 4062e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 4072e5b6d6dSopenharmony_ci printString(out, string, len); 4082e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 4092e5b6d6dSopenharmony_ci if(verbose) { 4102e5b6d6dSopenharmony_ci printCString(out, " // ALIAS", -1); 4112e5b6d6dSopenharmony_ci } 4122e5b6d6dSopenharmony_ci printString(out, cr, UPRV_LENGTHOF(cr)); 4132e5b6d6dSopenharmony_ci } else { 4142e5b6d6dSopenharmony_ci reportError(pname, status, "getting binary value"); 4152e5b6d6dSopenharmony_ci } 4162e5b6d6dSopenharmony_ci uprv_free(string); 4172e5b6d6dSopenharmony_ci} 4182e5b6d6dSopenharmony_ci 4192e5b6d6dSopenharmony_cistatic void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status) 4202e5b6d6dSopenharmony_ci{ 4212e5b6d6dSopenharmony_ci static const UChar cr[] = { 0xA }; // LF 4222e5b6d6dSopenharmony_ci 4232e5b6d6dSopenharmony_ci/* int32_t noOfElements = ures_getSize(resource);*/ 4242e5b6d6dSopenharmony_ci int32_t i = 0; 4252e5b6d6dSopenharmony_ci const char *key = ures_getKey(resource); 4262e5b6d6dSopenharmony_ci 4272e5b6d6dSopenharmony_ci switch(ures_getType(resource)) { 4282e5b6d6dSopenharmony_ci case URES_STRING : 4292e5b6d6dSopenharmony_ci { 4302e5b6d6dSopenharmony_ci int32_t len=0; 4312e5b6d6dSopenharmony_ci const UChar* thestr = ures_getString(resource, &len, status); 4322e5b6d6dSopenharmony_ci UChar *string = quotedString(thestr); 4332e5b6d6dSopenharmony_ci 4342e5b6d6dSopenharmony_ci /* TODO: String truncation */ 4352e5b6d6dSopenharmony_ci if(opt_truncate && len > truncsize) { 4362e5b6d6dSopenharmony_ci char msg[128]; 4372e5b6d6dSopenharmony_ci printIndent(out, indent); 4382e5b6d6dSopenharmony_ci sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", 4392e5b6d6dSopenharmony_ci (long)len, (long)(truncsize/2)); 4402e5b6d6dSopenharmony_ci printCString(out, msg, -1); 4412e5b6d6dSopenharmony_ci len = truncsize/2; 4422e5b6d6dSopenharmony_ci } 4432e5b6d6dSopenharmony_ci printIndent(out, indent); 4442e5b6d6dSopenharmony_ci if(key != NULL) { 4452e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x0020, 0x007B, 0x0020, 0x0022 }; /* " { \"" */ 4462e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D }; /* "\" }" */ 4472e5b6d6dSopenharmony_ci printCString(out, key, (int32_t)uprv_strlen(key)); 4482e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 4492e5b6d6dSopenharmony_ci printString(out, string, len); 4502e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 4512e5b6d6dSopenharmony_ci } else { 4522e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x0022 }; /* "\"" */ 4532e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x0022, 0x002C }; /* "\"," */ 4542e5b6d6dSopenharmony_ci 4552e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 4562e5b6d6dSopenharmony_ci printString(out, string, (int32_t)(u_strlen(string))); 4572e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 4582e5b6d6dSopenharmony_ci } 4592e5b6d6dSopenharmony_ci 4602e5b6d6dSopenharmony_ci if(verbose) { 4612e5b6d6dSopenharmony_ci printCString(out, "// STRING", -1); 4622e5b6d6dSopenharmony_ci } 4632e5b6d6dSopenharmony_ci printString(out, cr, UPRV_LENGTHOF(cr)); 4642e5b6d6dSopenharmony_ci 4652e5b6d6dSopenharmony_ci uprv_free(string); 4662e5b6d6dSopenharmony_ci } 4672e5b6d6dSopenharmony_ci break; 4682e5b6d6dSopenharmony_ci 4692e5b6d6dSopenharmony_ci case URES_INT : 4702e5b6d6dSopenharmony_ci { 4712e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0020, 0x007B, 0x0020 }; /* ":int { " */ 4722e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x0020, 0x007D }; /* " }" */ 4732e5b6d6dSopenharmony_ci UChar num[20]; 4742e5b6d6dSopenharmony_ci 4752e5b6d6dSopenharmony_ci printIndent(out, indent); 4762e5b6d6dSopenharmony_ci if(key != NULL) { 4772e5b6d6dSopenharmony_ci printCString(out, key, -1); 4782e5b6d6dSopenharmony_ci } 4792e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 4802e5b6d6dSopenharmony_ci uprv_itou(num, 20, ures_getInt(resource, status), 10, 0); 4812e5b6d6dSopenharmony_ci printString(out, num, u_strlen(num)); 4822e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 4832e5b6d6dSopenharmony_ci 4842e5b6d6dSopenharmony_ci if(verbose) { 4852e5b6d6dSopenharmony_ci printCString(out, "// INT", -1); 4862e5b6d6dSopenharmony_ci } 4872e5b6d6dSopenharmony_ci printString(out, cr, UPRV_LENGTHOF(cr)); 4882e5b6d6dSopenharmony_ci break; 4892e5b6d6dSopenharmony_ci } 4902e5b6d6dSopenharmony_ci case URES_BINARY : 4912e5b6d6dSopenharmony_ci { 4922e5b6d6dSopenharmony_ci int32_t len = 0; 4932e5b6d6dSopenharmony_ci const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status); 4942e5b6d6dSopenharmony_ci if(opt_truncate && len > truncsize) { 4952e5b6d6dSopenharmony_ci char msg[128]; 4962e5b6d6dSopenharmony_ci printIndent(out, indent); 4972e5b6d6dSopenharmony_ci sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", 4982e5b6d6dSopenharmony_ci (long)len, (long)(truncsize/2)); 4992e5b6d6dSopenharmony_ci printCString(out, msg, -1); 5002e5b6d6dSopenharmony_ci len = truncsize; 5012e5b6d6dSopenharmony_ci } 5022e5b6d6dSopenharmony_ci if(U_SUCCESS(*status)) { 5032e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x003A, 0x0062, 0x0069, 0x006E, 0x0061, 0x0072, 0x0079, 0x0020, 0x007B, 0x0020 }; /* ":binary { " */ 5042e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ 5052e5b6d6dSopenharmony_ci printIndent(out, indent); 5062e5b6d6dSopenharmony_ci if(key != NULL) { 5072e5b6d6dSopenharmony_ci printCString(out, key, -1); 5082e5b6d6dSopenharmony_ci } 5092e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 5102e5b6d6dSopenharmony_ci for(i = 0; i<len; i++) { 5112e5b6d6dSopenharmony_ci printHex(out, *data++); 5122e5b6d6dSopenharmony_ci } 5132e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 5142e5b6d6dSopenharmony_ci if(verbose) { 5152e5b6d6dSopenharmony_ci printCString(out, " // BINARY", -1); 5162e5b6d6dSopenharmony_ci } 5172e5b6d6dSopenharmony_ci printString(out, cr, UPRV_LENGTHOF(cr)); 5182e5b6d6dSopenharmony_ci } else { 5192e5b6d6dSopenharmony_ci reportError(pname, status, "getting binary value"); 5202e5b6d6dSopenharmony_ci } 5212e5b6d6dSopenharmony_ci } 5222e5b6d6dSopenharmony_ci break; 5232e5b6d6dSopenharmony_ci case URES_INT_VECTOR : 5242e5b6d6dSopenharmony_ci { 5252e5b6d6dSopenharmony_ci int32_t len = 0; 5262e5b6d6dSopenharmony_ci const int32_t *data = ures_getIntVector(resource, &len, status); 5272e5b6d6dSopenharmony_ci if(U_SUCCESS(*status)) { 5282e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0076, 0x0065, 0x0063, 0x0074, 0x006F, 0x0072, 0x0020, 0x007B, 0x0020 }; /* ":intvector { " */ 5292e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ 5302e5b6d6dSopenharmony_ci UChar num[20]; 5312e5b6d6dSopenharmony_ci 5322e5b6d6dSopenharmony_ci printIndent(out, indent); 5332e5b6d6dSopenharmony_ci if(key != NULL) { 5342e5b6d6dSopenharmony_ci printCString(out, key, -1); 5352e5b6d6dSopenharmony_ci } 5362e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 5372e5b6d6dSopenharmony_ci for(i = 0; i < len - 1; i++) { 5382e5b6d6dSopenharmony_ci int32_t numLen = uprv_itou(num, 20, data[i], 10, 0); 5392e5b6d6dSopenharmony_ci num[numLen++] = 0x002C; /* ',' */ 5402e5b6d6dSopenharmony_ci num[numLen++] = 0x0020; /* ' ' */ 5412e5b6d6dSopenharmony_ci num[numLen] = 0; 5422e5b6d6dSopenharmony_ci printString(out, num, u_strlen(num)); 5432e5b6d6dSopenharmony_ci } 5442e5b6d6dSopenharmony_ci if(len > 0) { 5452e5b6d6dSopenharmony_ci uprv_itou(num, 20, data[len - 1], 10, 0); 5462e5b6d6dSopenharmony_ci printString(out, num, u_strlen(num)); 5472e5b6d6dSopenharmony_ci } 5482e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 5492e5b6d6dSopenharmony_ci if(verbose) { 5502e5b6d6dSopenharmony_ci printCString(out, "// INTVECTOR", -1); 5512e5b6d6dSopenharmony_ci } 5522e5b6d6dSopenharmony_ci printString(out, cr, UPRV_LENGTHOF(cr)); 5532e5b6d6dSopenharmony_ci } else { 5542e5b6d6dSopenharmony_ci reportError(pname, status, "getting int vector"); 5552e5b6d6dSopenharmony_ci } 5562e5b6d6dSopenharmony_ci } 5572e5b6d6dSopenharmony_ci break; 5582e5b6d6dSopenharmony_ci case URES_TABLE : 5592e5b6d6dSopenharmony_ci case URES_ARRAY : 5602e5b6d6dSopenharmony_ci { 5612e5b6d6dSopenharmony_ci static const UChar openStr[] = { 0x007B }; /* "{" */ 5622e5b6d6dSopenharmony_ci static const UChar closeStr[] = { 0x007D, '\n' }; /* "}\n" */ 5632e5b6d6dSopenharmony_ci 5642e5b6d6dSopenharmony_ci UResourceBundle *t = NULL; 5652e5b6d6dSopenharmony_ci ures_resetIterator(resource); 5662e5b6d6dSopenharmony_ci printIndent(out, indent); 5672e5b6d6dSopenharmony_ci if(key != NULL) { 5682e5b6d6dSopenharmony_ci printCString(out, key, -1); 5692e5b6d6dSopenharmony_ci } 5702e5b6d6dSopenharmony_ci printString(out, openStr, UPRV_LENGTHOF(openStr)); 5712e5b6d6dSopenharmony_ci if(verbose) { 5722e5b6d6dSopenharmony_ci if(ures_getType(resource) == URES_TABLE) { 5732e5b6d6dSopenharmony_ci printCString(out, "// TABLE", -1); 5742e5b6d6dSopenharmony_ci } else { 5752e5b6d6dSopenharmony_ci printCString(out, "// ARRAY", -1); 5762e5b6d6dSopenharmony_ci } 5772e5b6d6dSopenharmony_ci } 5782e5b6d6dSopenharmony_ci printString(out, cr, UPRV_LENGTHOF(cr)); 5792e5b6d6dSopenharmony_ci 5802e5b6d6dSopenharmony_ci if(suppressAliases == false) { 5812e5b6d6dSopenharmony_ci while(U_SUCCESS(*status) && ures_hasNext(resource)) { 5822e5b6d6dSopenharmony_ci t = ures_getNextResource(resource, t, status); 5832e5b6d6dSopenharmony_ci if(U_SUCCESS(*status)) { 5842e5b6d6dSopenharmony_ci printOutBundle(out, t, indent+indentsize, pname, status); 5852e5b6d6dSopenharmony_ci } else { 5862e5b6d6dSopenharmony_ci reportError(pname, status, "While processing table"); 5872e5b6d6dSopenharmony_ci *status = U_ZERO_ERROR; 5882e5b6d6dSopenharmony_ci } 5892e5b6d6dSopenharmony_ci } 5902e5b6d6dSopenharmony_ci } else { /* we have to use low level access to do this */ 5912e5b6d6dSopenharmony_ci Resource r; 5922e5b6d6dSopenharmony_ci int32_t resSize = ures_getSize(resource); 5932e5b6d6dSopenharmony_ci UBool isTable = (UBool)(ures_getType(resource) == URES_TABLE); 5942e5b6d6dSopenharmony_ci for(i = 0; i < resSize; i++) { 5952e5b6d6dSopenharmony_ci /* need to know if it's an alias */ 5962e5b6d6dSopenharmony_ci if(isTable) { 5972e5b6d6dSopenharmony_ci r = res_getTableItemByIndex(&resource->getResData(), resource->fRes, i, &key); 5982e5b6d6dSopenharmony_ci } else { 5992e5b6d6dSopenharmony_ci r = res_getArrayItem(&resource->getResData(), resource->fRes, i); 6002e5b6d6dSopenharmony_ci } 6012e5b6d6dSopenharmony_ci if(U_SUCCESS(*status)) { 6022e5b6d6dSopenharmony_ci if(res_getPublicType(r) == URES_ALIAS) { 6032e5b6d6dSopenharmony_ci printOutAlias(out, resource, r, key, indent+indentsize, pname, status); 6042e5b6d6dSopenharmony_ci } else { 6052e5b6d6dSopenharmony_ci t = ures_getByIndex(resource, i, t, status); 6062e5b6d6dSopenharmony_ci printOutBundle(out, t, indent+indentsize, pname, status); 6072e5b6d6dSopenharmony_ci } 6082e5b6d6dSopenharmony_ci } else { 6092e5b6d6dSopenharmony_ci reportError(pname, status, "While processing table"); 6102e5b6d6dSopenharmony_ci *status = U_ZERO_ERROR; 6112e5b6d6dSopenharmony_ci } 6122e5b6d6dSopenharmony_ci } 6132e5b6d6dSopenharmony_ci } 6142e5b6d6dSopenharmony_ci 6152e5b6d6dSopenharmony_ci printIndent(out, indent); 6162e5b6d6dSopenharmony_ci printString(out, closeStr, UPRV_LENGTHOF(closeStr)); 6172e5b6d6dSopenharmony_ci ures_close(t); 6182e5b6d6dSopenharmony_ci } 6192e5b6d6dSopenharmony_ci break; 6202e5b6d6dSopenharmony_ci default: 6212e5b6d6dSopenharmony_ci break; 6222e5b6d6dSopenharmony_ci } 6232e5b6d6dSopenharmony_ci 6242e5b6d6dSopenharmony_ci} 6252e5b6d6dSopenharmony_ci 6262e5b6d6dSopenharmony_cistatic const char *getEncodingName(const char *encoding) { 6272e5b6d6dSopenharmony_ci UErrorCode err; 6282e5b6d6dSopenharmony_ci const char *enc; 6292e5b6d6dSopenharmony_ci 6302e5b6d6dSopenharmony_ci err = U_ZERO_ERROR; 6312e5b6d6dSopenharmony_ci if (!(enc = ucnv_getStandardName(encoding, "MIME", &err))) { 6322e5b6d6dSopenharmony_ci err = U_ZERO_ERROR; 6332e5b6d6dSopenharmony_ci if (!(enc = ucnv_getStandardName(encoding, "IANA", &err))) { 6342e5b6d6dSopenharmony_ci // do nothing 6352e5b6d6dSopenharmony_ci } 6362e5b6d6dSopenharmony_ci } 6372e5b6d6dSopenharmony_ci 6382e5b6d6dSopenharmony_ci return enc; 6392e5b6d6dSopenharmony_ci} 6402e5b6d6dSopenharmony_ci 6412e5b6d6dSopenharmony_cistatic void reportError(const char *pname, UErrorCode *status, const char *when) { 6422e5b6d6dSopenharmony_ci u_fprintf(ustderr, "%s: error %d while %s: %s\n", pname, *status, when, u_errorName(*status)); 6432e5b6d6dSopenharmony_ci} 6442e5b6d6dSopenharmony_ci 6452e5b6d6dSopenharmony_ci#else 6462e5b6d6dSopenharmony_ciextern int 6472e5b6d6dSopenharmony_cimain(int argc, char* argv[]) { 6482e5b6d6dSopenharmony_ci /* Changing stdio.h ustdio.h requires that formatting not be disabled. */ 6492e5b6d6dSopenharmony_ci return 3; 6502e5b6d6dSopenharmony_ci} 6512e5b6d6dSopenharmony_ci#endif /* !UCONFIG_NO_FORMATTING */ 6522e5b6d6dSopenharmony_ci 6532e5b6d6dSopenharmony_ci/* 6542e5b6d6dSopenharmony_ci * Local Variables: 6552e5b6d6dSopenharmony_ci * indent-tabs-mode: nil 6562e5b6d6dSopenharmony_ci * End: 6572e5b6d6dSopenharmony_ci */ 658