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