1736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov/*
2736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*******************************************************************************
3736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*
4736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   Copyright (C) 1999-2007, International Business Machines
5736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   Corporation and others.  All Rights Reserved.
6736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*
7736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*******************************************************************************
8736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   file name:  uresb.c
9736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   encoding:   US-ASCII
10736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   tab size:   8 (not used)
11736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   indentation:4
12736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*
13736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   created on: 2000sep6
14736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*   created by: Vladimir Weinstein
15736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov*/
16736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
17736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov/******************************************************************************
18736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov * This program prints out resource bundles - example for
19736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov * ICU workshop
20736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov * TODO: make a complete i18n layout for this program.
21736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov ******************************************************************************/
22736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
23736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include "unicode/putil.h"
24736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include "unicode/ures.h"
25736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include "unicode/ustdio.h"
26f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov#include "unicode/uloc.h"
27f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov#include "unicode/ustring.h"
28736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include "uoptions.h"
29736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include "toolutil.h"
30736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
31f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov#include <string.h>
32f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov#include <stdlib.h>
33f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov#ifdef WIN32
34736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include <direct.h>
35736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#else
36736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#include <unistd.h>
37736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#endif
38736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
39736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#define URESB_DEFAULTTRUNC 40
40736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
41736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic char *currdir = NULL;
42736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov/*--locale sr_YU and --encoding cp855
43736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov * are interesting on Win32
44736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov */
45736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
46736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic const char *locale = NULL;
47736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic const char *encoding = NULL;
48736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic const char *resPath = NULL;
49736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic const int32_t indentsize = 4;
50736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic UFILE *outerr = NULL;
51736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic int32_t truncsize = URESB_DEFAULTTRUNC;
52736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic UBool trunc = FALSE;
53736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
54736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovconst UChar baderror[] = { 0x0042, 0x0041, 0x0044, 0x0000 };
55736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
56736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovconst UChar *getErrorName(UErrorCode errorNumber);
57736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid reportError(UErrorCode *status);
58736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic UChar *quotedString(const UChar *string);
59736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status);
60736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid printIndent(UFILE *out, int32_t indent);
61736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid printHex(UFILE *out, const int8_t *what);
62736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
63736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic UOption options[]={
64736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UOPTION_HELP_H,
65736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UOPTION_HELP_QUESTION_MARK,
66736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    { "locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0 },
67736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UOPTION_ENCODING,
68736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    { "path", NULL, NULL, NULL, 'p', UOPT_OPTIONAL_ARG, 0 },
69736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    { "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 },
70736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UOPTION_VERBOSE
71736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov};
72736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
73736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic UBool VERBOSE = FALSE;
74736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
75736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovextern int
76736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovmain(int argc, char* argv[]) {
77736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
78736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UResourceBundle *bundle = NULL;
79736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UErrorCode status = U_ZERO_ERROR;
80736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UFILE *out = NULL;
81736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    int32_t i = 0;
82736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    const char* arg;
83736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    char resPathBuffer[1024];
84736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#ifdef WIN32
85736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    currdir = _getcwd(NULL, 0);
86736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#else
8712a024ca681d877fe16b7e087356f7aff175a218Svetoslav Ganov    currdir = getcwd(NULL, 0);
88736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov#endif
89736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
90f804420d6e37748b75478406e989c69303756980Svetoslav Ganov    argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
91f804420d6e37748b75478406e989c69303756980Svetoslav Ganov
92f804420d6e37748b75478406e989c69303756980Svetoslav Ganov    /* error handling, printing usage message */
93f804420d6e37748b75478406e989c69303756980Svetoslav Ganov    if(argc<0) {
94736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        fprintf(stderr,
95736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            "error in command line argument \"%s\"\n",
96736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            argv[-argc]);
97736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
98736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    if(argc<2 || options[0].doesOccur || options[1].doesOccur) {
99736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        fprintf(stderr,
100736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            "usage: %s [-options] locale(s)\n",
101736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            argv[0]);
102736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
103736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
104736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
105736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    if(options[2].doesOccur) {
106736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        locale = options[2].value;
107736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    } else {
108736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        locale = 0;
109736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
110736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
111736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    if(options[3].doesOccur) {
112736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        encoding = options[3].value;
113736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    } else {
114736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        encoding = NULL;
115736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
116736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
117736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    if(options[4].doesOccur) {
118736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        if(options[4].value != NULL) {
119736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            resPath = options[4].value; /* we'll use users resources */
120736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        } else {
121736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            resPath = NULL; /* we'll use ICU system resources for dumping */
122736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
123736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    } else {
124736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        strcpy(resPathBuffer, currdir);
125736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        /*strcat(resPathBuffer, U_FILE_SEP_STRING);
126736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        strcat(resPathBuffer, "uresb");*/
127736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        resPath = resPathBuffer; /* we'll just dump uresb samples resources */
128736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
129736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
130736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    if(options[5].doesOccur) {
131736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        trunc = TRUE;
132f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov        if(options[5].value != NULL) {
133f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            truncsize = atoi(options[5].value); /* user defined printable size */
134f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov        } else {
135736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            truncsize = URESB_DEFAULTTRUNC; /* we'll use default omitting size */
136736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
137736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    } else {
138736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        trunc = FALSE;
139736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
140736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
141736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    if(options[6].doesOccur) {
142736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        VERBOSE = TRUE;
143736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
144736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
145736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    outerr = u_finit(stderr, locale, encoding);
146736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    out = u_finit(stdout, locale, encoding);
147736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
148f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov    for(i = 1; i < argc; ++i) {
149736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        status = U_ZERO_ERROR;
150736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        arg = getLongPathname(argv[i]);
151736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
152736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        u_fprintf(out, "uresb: processing file \"%s\" in path \"%s\"\n", arg, resPath);
153736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        bundle = ures_open(resPath, arg, &status);
154736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        if(U_SUCCESS(status)) {
155736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            u_fprintf(out, "%s\n", arg);
156736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            printOutBundle(out, bundle, 0, &status);
157736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        } else {
158736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            reportError(&status);
159736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
160736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
161736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        ures_close(bundle);
162736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
163736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
16451cccf0845b36539d42503495f0689d487712b3aSvetoslav Ganov
165736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
166736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    u_fclose(out);
167736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    u_fclose(outerr);
168736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    return 0;
169736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov}
170736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
171736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid printIndent(UFILE *out, int32_t indent) {
172736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    char inchar[256];
173736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    int32_t i = 0;
174736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    for(i = 0; i<indent; i++) {
175736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        inchar[i] = ' ';
176736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
177736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    inchar[indent] = '\0';
178736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    u_fprintf(out, "%s", inchar);
179736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov}
180736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
181736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid printHex(UFILE *out, const int8_t *what) {
182736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov  u_fprintf(out, "%02X", (uint8_t)*what);
183736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov}
184736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
185736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovstatic UChar *quotedString(const UChar *string) {
186736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    int len = u_strlen(string);
187736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    int alen = len;
188736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    const UChar *sp;
189736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UChar *newstr, *np;
190736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
191736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    for (sp = string; *sp; ++sp) {
192736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        switch (*sp) {
193736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            case '\n':
194736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            case 0x0022:
195736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                ++alen;
196736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                break;
197f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov        }
198736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
199736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
200736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    newstr = (UChar *) malloc((1 + alen) * sizeof(*newstr));
201736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    for (sp = string, np = newstr; *sp; ++sp) {
202736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        switch (*sp) {
203736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            case '\n':
204736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                *np++ = 0x005C;
205f804420d6e37748b75478406e989c69303756980Svetoslav Ganov                *np++ = 0x006E;
206406970b06c8472cbd44ecc278d643a12589c6b38Svetoslav Ganov                break;
207f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov
208736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            case 0x0022:
20991feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                *np++ = 0x005C;
210736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
211f804420d6e37748b75478406e989c69303756980Svetoslav Ganov            default:
212f804420d6e37748b75478406e989c69303756980Svetoslav Ganov                *np++ = *sp;
213f804420d6e37748b75478406e989c69303756980Svetoslav Ganov                break;
214f804420d6e37748b75478406e989c69303756980Svetoslav Ganov        }
215f804420d6e37748b75478406e989c69303756980Svetoslav Ganov    }
216f804420d6e37748b75478406e989c69303756980Svetoslav Ganov    *np = 0;
217f804420d6e37748b75478406e989c69303756980Svetoslav Ganov
218f804420d6e37748b75478406e989c69303756980Svetoslav Ganov    return newstr;
219f804420d6e37748b75478406e989c69303756980Svetoslav Ganov}
220f804420d6e37748b75478406e989c69303756980Svetoslav Ganov
221736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status) {
222736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    int32_t i = 0;
223736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    const char *key = ures_getKey(resource);
224736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
225736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    switch(ures_getType(resource)) {
226736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    case URES_STRING :
227736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        {
228736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            int32_t len=0;
229f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            const UChar*thestr = ures_getString(resource, &len, status);
230f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            UChar *string = quotedString(thestr);
231f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov
232f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            /* TODO: String truncation */
233f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            /*
234f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            if(trunc && len > truncsize) {
235f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                printIndent(out, indent);
236f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                u_fprintf(out, "// WARNING: this string, size %d is truncated to %d\n", len, truncsize/2);
237f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                len = truncsize/2;
238f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            }
239f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            */
240f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            printIndent(out, indent);
241f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            if(key != NULL) {
242736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                u_fprintf(out, "%s { \"%S\" } ", key, string);
243736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            } else {
244736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                u_fprintf(out, "\"%S\",", string);
245736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            }
246736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            if(VERBOSE) {
247736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                u_fprintf(out, " // STRING");
248736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            }
249736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            u_fprintf(out, "\n");
25091feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov            free(string);
25191feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov        }
25291feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov        break;
253736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    case URES_INT :
254736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        printIndent(out, indent);
255736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        if(key != NULL) {
256736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            u_fprintf(out, "%s", key);
257736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
258736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        u_fprintf(out, ":int { %li } ", ures_getInt(resource, status));
259736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
260736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        if(VERBOSE) {
261736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            u_fprintf(out, " // INT");
262736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
263736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        u_fprintf(out, "\n");
264736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        break;
265736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    case URES_BINARY :
266736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        {
267736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            int32_t len = 0;
268736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status);
269736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            if(trunc && len > truncsize) {
270736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                printIndent(out, indent);
271736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                u_fprintf(out, "// WARNING: this resource, size %li is truncated to %li\n", len, truncsize/2);
272f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                len = truncsize/2;
27391feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov            }
27491feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov            if(U_SUCCESS(*status)) {
27591feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                printIndent(out, indent);
27691feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                if(key != NULL) {
27791feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                    u_fprintf(out, "%s", key);
27891feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                }
27991feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                u_fprintf(out, ":binary { ");
28091feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                for(i = 0; i<len; i++) {
281736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                    printHex(out, data++);
282736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                }
283736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                u_fprintf(out, " }");
284736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                if(VERBOSE) {
28591feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov                    u_fprintf(out, " // BINARY");
286736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                }
287736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                u_fprintf(out, "\n");
288736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
289f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            } else {
290f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                reportError(status);
291f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov            }
292736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
293f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov        break;
294736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    case URES_INT_VECTOR :
295736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov      {
296736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov          int32_t len = 0;
297736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov          const int32_t *data = ures_getIntVector(resource, &len, status);
298736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov          if(U_SUCCESS(*status)) {
299736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              printIndent(out, indent);
300736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              if(key != NULL) {
301f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                  u_fprintf(out, "%s", key);
30247e02711d78ecac9112aa7f66e5664cdc46fb3d1Svetoslav Ganov              }
303736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              u_fprintf(out, ":intvector { ");
304736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              for(i = 0; i<len-1; i++) {
305736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                  u_fprintf(out, "%d, ", data[i]);
306736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              }
307f804420d6e37748b75478406e989c69303756980Svetoslav Ganov              if(len > 0) {
308f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov                  u_fprintf(out, "%d ", data[len-1]);
309736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              }
310736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              u_fprintf(out, "}");
311f804420d6e37748b75478406e989c69303756980Svetoslav Ganov              if(VERBOSE) {
312736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                  u_fprintf(out, " // INTVECTOR");
313736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              }
314736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              u_fprintf(out, "\n");
315736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
31600f7b3f76515d1c6fbe5cf9fee9d3760787c03cdSvetoslav Ganov          } else {
317736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov              reportError(status);
318736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov          }
319736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov      }
320736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov      break;
32100f7b3f76515d1c6fbe5cf9fee9d3760787c03cdSvetoslav Ganov    case URES_TABLE :
32291feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov    case URES_ARRAY :
32391feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov        {
32491feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov            UResourceBundle *t = NULL;
325736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            ures_resetIterator(resource);
326736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            printIndent(out, indent);
327736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            if(key != NULL) {
328f804420d6e37748b75478406e989c69303756980Svetoslav Ganov                u_fprintf(out, "%s ", key);
329736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            }
330736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            u_fprintf(out, "{");
331736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            if(VERBOSE) {
332736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                if(ures_getType(resource) == URES_TABLE) {
33300f7b3f76515d1c6fbe5cf9fee9d3760787c03cdSvetoslav Ganov                    u_fprintf(out, " // TABLE");
334736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                } else {
335736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                    u_fprintf(out, " // ARRAY");
336736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                }
337736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            }
338f804420d6e37748b75478406e989c69303756980Svetoslav Ganov            u_fprintf(out, "\n");
339f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov
340736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            while(ures_hasNext(resource)) {
341736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                t = ures_getNextResource(resource, t, status);
342f804420d6e37748b75478406e989c69303756980Svetoslav Ganov                printOutBundle(out, t, indent+indentsize, status);
343736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            }
344736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
345736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            printIndent(out, indent);
346736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            u_fprintf(out, "}\n");
347736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            ures_close(t);
348736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        }
349736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        break;
350736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    default:
35100f7b3f76515d1c6fbe5cf9fee9d3760787c03cdSvetoslav Ganov        break;
352736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
353736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
354736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov}
355736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
356736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovvoid reportError(UErrorCode *status) {
357736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    u_fprintf(outerr, "Error %d(%s) : %U happened!\n", *status, u_errorName(*status), getErrorName(*status));
35891feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov}
35991feae3c5994bd4768cea3507c62c65746adcfa6Svetoslav Ganov
360736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
361736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovconst UChar *getErrorName(UErrorCode errorNumber) {
362736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UErrorCode status = U_ZERO_ERROR;
363736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    int32_t len = 0;
364736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
365736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    UResourceBundle *error = ures_open(currdir, locale, &status);
366736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
367f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov    UResourceBundle *errorcodes = ures_getByKey(error, "errorcodes", NULL, &status);
368f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov
369736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    const UChar *result = ures_getStringByIndex(errorcodes, errorNumber, &len, &status);
370f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov
371736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    ures_close(errorcodes);
372736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    ures_close(error);
37312a024ca681d877fe16b7e087356f7aff175a218Svetoslav Ganov
37412a024ca681d877fe16b7e087356f7aff175a218Svetoslav Ganov    if(U_SUCCESS(status)) {
375f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov        return result;
376f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov    } else {
377f5a07905a3e025f95472a3f8d9935263e49ad6d3Svetoslav Ganov        return baderror;
378736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
379736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
380736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov}
381736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov