]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/uresb/uresb.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / samples / uresb / uresb.c
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3*
4* Copyright (C) 1999-2003, International Business Machines
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8* file name: uresb.c
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2000sep6
14* created by: Vladimir Weinstein
15*/
16
17/******************************************************************************
18 * This program prints out resource bundles - example for
19 * ICU workshop
20 * TODO: make a complete i18n layout for this program.
21 ******************************************************************************/
22
23#include "unicode/ures.h"
24#include "unicode/ustdio.h"
25#include "unicode/uloc.h"
26#include "unicode/ustring.h"
27#include "uoptions.h"
28#include "toolutil.h"
29
30#include <string.h>
31#include <stdlib.h>
32#ifdef WIN32
33#include <direct.h>
34#else
35#include <unistd.h>
36#endif
37
38#define URESB_DEFAULTTRUNC 40
39
40static char *currdir = NULL;
41/*--locale sr_YU and --encoding cp855
42 * are interesting on Win32
43 */
44
45static const char *locale = NULL;
46static const char *encoding = NULL;
47static const char *resPath = NULL;
48static const int32_t indentsize = 4;
49static UFILE *outerr = NULL;
50static int32_t truncsize = URESB_DEFAULTTRUNC;
51static UBool trunc = FALSE;
52
53const UChar baderror[] = { 0x0042, 0x0041, 0x0044, 0x0000 };
54
55const UChar *getErrorName(UErrorCode errorNumber);
56void reportError(UErrorCode *status);
57static UChar *quotedString(const UChar *string);
58void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status);
59void printIndent(UFILE *out, int32_t indent);
60void printHex(UFILE *out, const uint8_t *what);
61
62static UOption options[]={
63 UOPTION_HELP_H,
64 UOPTION_HELP_QUESTION_MARK,
65 { "locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0 },
66 UOPTION_ENCODING,
67 { "path", NULL, NULL, NULL, 'p', UOPT_OPTIONAL_ARG, 0 },
68 { "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 },
69 UOPTION_VERBOSE
70};
71
72static UBool VERBOSE = FALSE;
73
74extern int
75main(int argc, char* argv[]) {
76
77 UResourceBundle *bundle = NULL;
78 UErrorCode status = U_ZERO_ERROR;
79 UFILE *out = NULL;
80 int32_t i = 0;
81 const char* arg;
82 char resPathBuffer[1024];
83#ifdef WIN32
84 currdir = _getcwd(NULL, 0);
85#else
86 currdir = getcwd(NULL, 0);
87#endif
88
89 argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
90
91 /* error handling, printing usage message */
92 if(argc<0) {
93 fprintf(stderr,
94 "error in command line argument \"%s\"\n",
95 argv[-argc]);
96 }
97 if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
98 fprintf(stderr,
99 "usage: %s [-options]\n",
100 argv[0]);
101 return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
102 }
103
104 if(options[2].doesOccur) {
105 locale = options[2].value;
106 } else {
107 locale = 0;
108 }
109
110 if(options[3].doesOccur) {
111 encoding = options[3].value;
112 } else {
113 encoding = NULL;
114 }
115
116 if(options[4].doesOccur) {
117 if(options[4].value != NULL) {
118 resPath = options[4].value; /* we'll use users resources */
119 } else {
120 resPath = NULL; /* we'll use ICU system resources for dumping */
121 }
122 } else {
123 strcpy(resPathBuffer, currdir);
124 strcat(resPathBuffer, U_FILE_SEP_STRING);
125 strcat(resPathBuffer, "uresb");
126 resPath = resPathBuffer; /* we'll just dump uresb samples resources */
127 }
128
129 if(options[5].doesOccur) {
130 trunc = TRUE;
131 if(options[5].value != NULL) {
132 truncsize = atoi(options[5].value); /* user defined printable size */
133 } else {
134 truncsize = URESB_DEFAULTTRUNC; /* we'll use default omitting size */
135 }
136 } else {
137 trunc = FALSE;
138 }
139
140 if(options[6].doesOccur) {
141 VERBOSE = TRUE;
142 }
143
144 outerr = u_finit(stderr, locale, encoding);
145 out = u_finit(stdout, locale, encoding);
146
147 for(i = 1; i < argc; ++i) {
148 status = U_ZERO_ERROR;
149 arg = getLongPathname(argv[i]);
150
151 printf("uresb: processing file \"%s\" in path \"%s\"\n", arg, resPath);
152 bundle = ures_open(resPath, arg, &status);
153 if(U_SUCCESS(status)) {
154 u_fprintf(out, "%s\n", arg);
155 printOutBundle(out, bundle, 0, &status);
156 } else {
157 reportError(&status);
158 }
159
160 ures_close(bundle);
161 }
162
163
164
165 u_fclose(out);
166 u_fclose(outerr);
167 return 0;
168}
169
170void printIndent(UFILE *out, int32_t indent) {
171 char inchar[256];
172 int32_t i = 0;
173 for(i = 0; i<indent; i++) {
174 inchar[i] = ' ';
175 }
176 inchar[indent] = '\0';
177 u_fprintf(out, "%s", inchar);
178}
179
180void printHex(UFILE *out, const uint8_t *what) {
181 u_fprintf(out, "%02X", *what);
182}
183
184static UChar *quotedString(const UChar *string) {
185 int len = u_strlen(string);
186 int alen = len;
187 const UChar *sp;
188 UChar *newstr, *np;
189
190 for (sp = string; *sp; ++sp) {
191 switch (*sp) {
192 case '\n':
193 case 0x0022:
194 ++alen;
195 break;
196 }
197 }
198
199 newstr = (UChar *) malloc((1 + alen) * sizeof(*newstr));
200 for (sp = string, np = newstr; *sp; ++sp) {
201 switch (*sp) {
202 case '\n':
203 *np++ = 0x005C;
204 *np++ = 0x006E;
205 break;
206
207 case 0x0022:
208 *np++ = 0x005C;
209
210 default:
211 *np++ = *sp;
212 break;
213 }
214 }
215 *np = 0;
216
217 return newstr;
218}
219
220void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status) {
221 int32_t noOfElements = ures_getSize(resource);
222 int32_t i = 0;
223 const char *key = ures_getKey(resource);
224
225 switch(ures_getType(resource)) {
226 case URES_STRING :
227 {
228 int32_t len=0;
229 const UChar*thestr = ures_getString(resource, &len, status);
230 UChar *string = quotedString(thestr);
231
232 /* TODO: String truncation */
233 /*
234 if(trunc && len > truncsize) {
235 printIndent(out, indent);
236 u_fprintf(out, "// WARNING: this string, size %d is truncated to %d\n", len, truncsize/2);
237 len = truncsize/2;
238 }
239 */
240 printIndent(out, indent);
241 if(key != NULL) {
242 u_fprintf(out, "%s { \"%U\" } ", key, string);
243 } else {
244 u_fprintf(out, "\"%U\",", string);
245 }
246 if(VERBOSE) {
247 u_fprintf(out, " // STRING");
248 }
249 u_fprintf(out, "\n");
250 free(string);
251 }
252 break;
253 case URES_INT :
254 printIndent(out, indent);
255 if(key != NULL) {
256 u_fprintf(out, "%s", key);
257 }
258 u_fprintf(out, ":int { %li } ", ures_getInt(resource, status));
259
260 if(VERBOSE) {
261 u_fprintf(out, " // INT");
262 }
263 u_fprintf(out, "\n");
264 break;
265 case URES_BINARY :
266 {
267 int32_t len = 0;
268 const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status);
269 if(trunc && len > truncsize) {
270 printIndent(out, indent);
271 u_fprintf(out, "// WARNING: this resource, size %li is truncated to %li\n", len, truncsize/2);
272 len = truncsize/2;
273 }
274 if(U_SUCCESS(*status)) {
275 printIndent(out, indent);
276 if(key != NULL) {
277 u_fprintf(out, "%s", key);
278 }
279 u_fprintf(out, ":binary { ");
280 for(i = 0; i<len; i++) {
281 printHex(out, data++);
282 }
283 u_fprintf(out, " }");
284 if(VERBOSE) {
285 u_fprintf(out, " // BINARY");
286 }
287 u_fprintf(out, "\n");
288
289 } else {
290 reportError(status);
291 }
292 }
293 break;
294 case URES_INT_VECTOR :
295 {
296 int32_t len = 0;
297 const int32_t *data = ures_getIntVector(resource, &len, status);
298 if(U_SUCCESS(*status)) {
299 printIndent(out, indent);
300 if(key != NULL) {
301 u_fprintf(out, "%s", key);
302 }
303 u_fprintf(out, ":intvector { ");
304 for(i = 0; i<len-1; i++) {
305 u_fprintf(out, "%d, ", data[i]);
306 }
307 if(len > 0) {
308 u_fprintf(out, "%d ", data[len-1]);
309 }
310 u_fprintf(out, "}");
311 if(VERBOSE) {
312 u_fprintf(out, " // INTVECTOR");
313 }
314 u_fprintf(out, "\n");
315
316 } else {
317 reportError(status);
318 }
319 }
320 break;
321 case URES_TABLE :
322 case URES_ARRAY :
323 {
324 UResourceBundle *t = NULL;
325 ures_resetIterator(resource);
326 printIndent(out, indent);
327 if(key != NULL) {
328 u_fprintf(out, "%s ", key);
329 }
330 u_fprintf(out, "{");
331 if(VERBOSE) {
332 if(ures_getType(resource) == URES_TABLE) {
333 u_fprintf(out, " // TABLE");
334 } else {
335 u_fprintf(out, " // ARRAY");
336 }
337 }
338 u_fprintf(out, "\n");
339
340 while(ures_hasNext(resource)) {
341 t = ures_getNextResource(resource, t, status);
342 printOutBundle(out, t, indent+indentsize, status);
343 }
344
345 printIndent(out, indent);
346 u_fprintf(out, "}\n");
347 ures_close(t);
348 }
349 break;
350 default:
351 break;
352 }
353
354}
355
356void reportError(UErrorCode *status) {
357 u_fprintf(outerr, "Error %d : %U happened!\n", *status, getErrorName(*status));
358}
359
360
361const UChar *getErrorName(UErrorCode errorNumber) {
362 UErrorCode status = U_ZERO_ERROR;
363 int32_t len = 0;
364
365 UResourceBundle *error = ures_open(currdir, locale, &status);
366
367 UResourceBundle *errorcodes = ures_getByKey(error, "errorcodes", NULL, &status);
368
369 const UChar *result = ures_getStringByIndex(errorcodes, errorNumber, &len, &status);
370
371 ures_close(errorcodes);
372 ures_close(error);
373
374 if(U_SUCCESS(status)) {
375 return result;
376 } else {
377 return baderror;
378 }
379
380}