2000-09-06 22:32:37 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2000, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: uresb.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2000sep6
|
|
|
|
* created by: Vladimir Weinstein
|
|
|
|
*/
|
|
|
|
|
|
|
|
/******************************************************************************
|
2000-09-11 05:18:43 +00:00
|
|
|
* This program prints out resource bundles - example for
|
2000-09-06 22:32:37 +00:00
|
|
|
* ICU workshop
|
2000-09-11 05:18:43 +00:00
|
|
|
* TODO: make a complete i18n layout for this program.
|
2000-09-06 22:32:37 +00:00
|
|
|
******************************************************************************/
|
2000-09-11 05:18:43 +00:00
|
|
|
|
2000-09-06 22:32:37 +00:00
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "unicode/ustdio.h"
|
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "uoptions.h"
|
2000-12-05 20:42:15 +00:00
|
|
|
#include "toolutil.h"
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
#include <stdlib.h>
|
2000-09-06 22:32:37 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
#define URESB_DEFAULTTRUNC 40
|
|
|
|
|
2000-09-06 22:32:37 +00:00
|
|
|
static char *currdir = NULL;
|
2000-09-11 05:18:43 +00:00
|
|
|
/*--locale sr_YU and --encoding cp855
|
|
|
|
* are interesting on Win32
|
|
|
|
*/
|
|
|
|
|
2000-09-06 22:32:37 +00:00
|
|
|
static const char *locale = NULL;
|
2000-09-11 05:18:43 +00:00
|
|
|
static const char *encoding = NULL;
|
|
|
|
static const char *resPath = NULL;
|
|
|
|
static const int32_t indentsize = 4;
|
|
|
|
static UFILE *outerr = NULL;
|
|
|
|
static int32_t truncsize = URESB_DEFAULTTRUNC;
|
|
|
|
static UBool trunc = FALSE;
|
2000-09-06 22:32:37 +00:00
|
|
|
|
|
|
|
const UChar baderror[] = { 0x0042, 0x0041, 0x0044, 0x0000 };
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
const UChar *getErrorName(UErrorCode errorNumber);
|
|
|
|
void reportError(UErrorCode *status);
|
2002-03-22 03:35:12 +00:00
|
|
|
static UChar *quotedString(const UChar *string);
|
2000-09-11 05:18:43 +00:00
|
|
|
void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status);
|
|
|
|
void printIndent(UFILE *out, int32_t indent);
|
2001-08-01 17:09:30 +00:00
|
|
|
void printHex(UFILE *out, const uint8_t *what);
|
2000-09-06 22:32:37 +00:00
|
|
|
|
|
|
|
static UOption options[]={
|
|
|
|
UOPTION_HELP_H,
|
|
|
|
UOPTION_HELP_QUESTION_MARK,
|
2000-09-11 05:18:43 +00:00
|
|
|
{ "locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0 },
|
|
|
|
UOPTION_ENCODING,
|
|
|
|
{ "path", NULL, NULL, NULL, 'p', UOPT_OPTIONAL_ARG, 0 },
|
2001-07-11 20:29:18 +00:00
|
|
|
{ "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 },
|
|
|
|
UOPTION_VERBOSE
|
2000-09-06 22:32:37 +00:00
|
|
|
};
|
|
|
|
|
2001-07-11 20:29:18 +00:00
|
|
|
static UBool VERBOSE = FALSE;
|
2000-09-06 22:32:37 +00:00
|
|
|
|
|
|
|
extern int
|
2000-09-21 21:49:32 +00:00
|
|
|
main(int argc, char* argv[]) {
|
2000-09-06 22:32:37 +00:00
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
UResourceBundle *bundle = NULL;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UFILE *out = NULL;
|
2000-09-06 22:32:37 +00:00
|
|
|
int32_t i = 0;
|
2000-12-05 20:42:15 +00:00
|
|
|
const char* arg;
|
2000-09-06 22:32:37 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
currdir = _getcwd(NULL, 0);
|
|
|
|
#else
|
|
|
|
currdir = getcwd(NULL, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
|
|
|
|
|
|
|
|
/* error handling, printing usage message */
|
|
|
|
if(argc<0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error in command line argument \"%s\"\n",
|
|
|
|
argv[-argc]);
|
|
|
|
}
|
|
|
|
if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [-options]\n",
|
|
|
|
argv[0]);
|
|
|
|
return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[2].doesOccur) {
|
|
|
|
locale = options[2].value;
|
|
|
|
} else {
|
2002-03-22 03:35:12 +00:00
|
|
|
locale = 0;
|
2000-09-06 22:32:37 +00:00
|
|
|
}
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
if(options[3].doesOccur) {
|
|
|
|
encoding = options[3].value;
|
|
|
|
} else {
|
|
|
|
encoding = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[4].doesOccur) {
|
|
|
|
if(options[4].value != NULL) {
|
|
|
|
resPath = options[4].value; /* we'll use users resources */
|
|
|
|
} else {
|
|
|
|
resPath = NULL; /* we'll use ICU system resources for dumping */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resPath = currdir; /* we'll just dump uresb samples resources */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[5].doesOccur) {
|
|
|
|
trunc = TRUE;
|
|
|
|
if(options[5].value != NULL) {
|
|
|
|
truncsize = atoi(options[5].value); /* user defined printable size */
|
|
|
|
} else {
|
|
|
|
truncsize = URESB_DEFAULTTRUNC; /* we'll use default omitting size */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trunc = FALSE;
|
|
|
|
}
|
|
|
|
|
2001-07-11 20:29:18 +00:00
|
|
|
if(options[6].doesOccur) {
|
|
|
|
VERBOSE = TRUE;
|
|
|
|
}
|
|
|
|
|
2000-12-05 20:42:15 +00:00
|
|
|
outerr = u_finit(stderr, locale, encoding);
|
2000-09-11 05:18:43 +00:00
|
|
|
out = u_finit(stdout, locale, encoding);
|
2000-09-06 22:32:37 +00:00
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
/*
|
2000-09-06 22:32:37 +00:00
|
|
|
for(i = 0; i<20; i++) {
|
2000-09-11 05:18:43 +00:00
|
|
|
reportError(&i);
|
2000-09-06 22:32:37 +00:00
|
|
|
}
|
2000-09-11 05:18:43 +00:00
|
|
|
*/
|
|
|
|
|
2000-12-05 20:42:15 +00:00
|
|
|
for(i = 1; i < argc; ++i) {
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
arg = getLongPathname(argv[i]);
|
|
|
|
|
|
|
|
printf("uresb: processing file \"%s\"\n", arg);
|
|
|
|
bundle = ures_open(resPath, arg, &status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
u_fprintf(out, "%s\n", arg);
|
|
|
|
printOutBundle(out, bundle, 0, &status);
|
|
|
|
} else {
|
|
|
|
reportError(&status);
|
|
|
|
}
|
|
|
|
|
|
|
|
ures_close(bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-06 22:32:37 +00:00
|
|
|
|
|
|
|
u_fclose(out);
|
2000-09-11 05:18:43 +00:00
|
|
|
u_fclose(outerr);
|
2000-09-06 22:32:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
void printIndent(UFILE *out, int32_t indent) {
|
|
|
|
char inchar[256];
|
|
|
|
int32_t i = 0;
|
|
|
|
for(i = 0; i<indent; i++) {
|
|
|
|
inchar[i] = ' ';
|
|
|
|
}
|
|
|
|
inchar[indent] = '\0';
|
|
|
|
u_fprintf(out, "%s", inchar);
|
|
|
|
}
|
|
|
|
|
2001-08-01 17:09:30 +00:00
|
|
|
void printHex(UFILE *out, const uint8_t *what) {
|
|
|
|
u_fprintf(out, "%02X", *what);
|
2000-09-11 05:18:43 +00:00
|
|
|
}
|
|
|
|
|
2002-03-22 03:35:12 +00:00
|
|
|
static UChar *quotedString(const UChar *string) {
|
|
|
|
int len = u_strlen(string);
|
|
|
|
int alen = len;
|
|
|
|
const UChar *sp;
|
|
|
|
UChar *newstr, *np;
|
|
|
|
|
|
|
|
for (sp = string; *sp; ++sp) {
|
|
|
|
switch (*sp) {
|
|
|
|
case '\n':
|
|
|
|
case 0x0022:
|
|
|
|
++alen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newstr = (UChar *) malloc((1 + alen) * sizeof(*newstr));
|
|
|
|
for (sp = string, np = newstr; *sp; ++sp) {
|
|
|
|
switch (*sp) {
|
|
|
|
case '\n':
|
|
|
|
*np++ = 0x005C;
|
|
|
|
*np++ = 0x006E;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0022:
|
|
|
|
*np++ = 0x005C;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*np++ = *sp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*np = 0;
|
|
|
|
|
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status) {
|
|
|
|
int32_t noOfElements = ures_getSize(resource);
|
|
|
|
int32_t i = 0;
|
|
|
|
const char *key = ures_getKey(resource);
|
|
|
|
|
|
|
|
switch(ures_getType(resource)) {
|
|
|
|
case RES_STRING :
|
|
|
|
{
|
|
|
|
int32_t len=0;
|
2002-03-22 03:35:12 +00:00
|
|
|
const UChar*thestr = ures_getString(resource, &len, status);
|
|
|
|
UChar *string = quotedString(thestr);
|
|
|
|
|
2000-09-11 05:18:43 +00:00
|
|
|
/* TODO: String truncation */
|
|
|
|
/*
|
|
|
|
if(trunc && len > truncsize) {
|
|
|
|
printIndent(out, indent);
|
|
|
|
u_fprintf(out, "// WARNING: this string, size %d is truncated to %d\n", len, truncsize/2);
|
|
|
|
len = truncsize/2;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
printIndent(out, indent);
|
|
|
|
if(key != NULL) {
|
2001-07-11 20:29:18 +00:00
|
|
|
u_fprintf(out, "%s { \"%U\" } ", key, string);
|
2000-09-11 05:18:43 +00:00
|
|
|
} else {
|
2001-07-11 20:29:18 +00:00
|
|
|
u_fprintf(out, "\"%U\",", string);
|
2000-09-11 05:18:43 +00:00
|
|
|
}
|
2001-07-11 20:29:18 +00:00
|
|
|
if(VERBOSE) {
|
|
|
|
u_fprintf(out, " // STRING");
|
|
|
|
}
|
|
|
|
u_fprintf(out, "\n");
|
2002-03-22 03:35:12 +00:00
|
|
|
free(string);
|
2000-09-11 05:18:43 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RES_INT :
|
|
|
|
printIndent(out, indent);
|
|
|
|
if(key != NULL) {
|
2001-07-11 20:29:18 +00:00
|
|
|
u_fprintf(out, "%s", key);
|
|
|
|
}
|
|
|
|
u_fprintf(out, ":int { %li } ", ures_getInt(resource, status));
|
|
|
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
u_fprintf(out, " // INT");
|
|
|
|
}
|
|
|
|
u_fprintf(out, "\n");
|
2000-09-11 05:18:43 +00:00
|
|
|
break;
|
|
|
|
case RES_BINARY :
|
|
|
|
{
|
|
|
|
int32_t len = 0;
|
|
|
|
const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status);
|
|
|
|
if(trunc && len > truncsize) {
|
|
|
|
printIndent(out, indent);
|
|
|
|
u_fprintf(out, "// WARNING: this resource, size %li is truncated to %li\n", len, truncsize/2);
|
|
|
|
len = truncsize/2;
|
|
|
|
}
|
2001-08-01 17:09:30 +00:00
|
|
|
if(U_SUCCESS(*status)) {
|
2001-07-11 20:29:18 +00:00
|
|
|
printIndent(out, indent);
|
2000-09-11 05:18:43 +00:00
|
|
|
if(key != NULL) {
|
2001-07-11 20:29:18 +00:00
|
|
|
u_fprintf(out, "%s", key);
|
|
|
|
}
|
|
|
|
u_fprintf(out, ":binary { ");
|
|
|
|
for(i = 0; i<len; i++) {
|
|
|
|
printHex(out, data++);
|
2000-09-11 05:18:43 +00:00
|
|
|
}
|
2001-07-11 20:29:18 +00:00
|
|
|
u_fprintf(out, " }");
|
|
|
|
if(VERBOSE) {
|
|
|
|
u_fprintf(out, " // BINARY");
|
|
|
|
}
|
|
|
|
u_fprintf(out, "\n");
|
2000-09-11 05:18:43 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
reportError(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2001-08-01 17:09:30 +00:00
|
|
|
case RES_INT_VECTOR :
|
|
|
|
{
|
|
|
|
int32_t len = 0;
|
|
|
|
const int32_t *data = ures_getIntVector(resource, &len, status);
|
|
|
|
if(U_SUCCESS(*status)) {
|
|
|
|
printIndent(out, indent);
|
|
|
|
if(key != NULL) {
|
|
|
|
u_fprintf(out, "%s", key);
|
|
|
|
}
|
|
|
|
u_fprintf(out, ":intvector { ");
|
|
|
|
for(i = 0; i<len-1; i++) {
|
|
|
|
u_fprintf(out, "%d, ", data[i]);
|
|
|
|
}
|
|
|
|
if(len > 0) {
|
|
|
|
u_fprintf(out, "%d ", data[len-1]);
|
|
|
|
}
|
|
|
|
u_fprintf(out, "}");
|
|
|
|
if(VERBOSE) {
|
|
|
|
u_fprintf(out, " // INTVECTOR");
|
|
|
|
}
|
|
|
|
u_fprintf(out, "\n");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
reportError(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-09-11 05:18:43 +00:00
|
|
|
case RES_TABLE :
|
|
|
|
case RES_ARRAY :
|
|
|
|
{
|
|
|
|
UResourceBundle *t = NULL;
|
|
|
|
ures_resetIterator(resource);
|
|
|
|
printIndent(out, indent);
|
|
|
|
if(key != NULL) {
|
|
|
|
u_fprintf(out, "%s ", key);
|
|
|
|
}
|
2001-07-11 20:29:18 +00:00
|
|
|
u_fprintf(out, "{");
|
|
|
|
if(VERBOSE) {
|
|
|
|
if(ures_getType(resource) == RES_TABLE) {
|
|
|
|
u_fprintf(out, " // TABLE");
|
|
|
|
} else {
|
|
|
|
u_fprintf(out, " // ARRAY");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u_fprintf(out, "\n");
|
2000-09-11 05:18:43 +00:00
|
|
|
|
|
|
|
while(ures_hasNext(resource)) {
|
|
|
|
t = ures_getNextResource(resource, t, status);
|
|
|
|
printOutBundle(out, t, indent+indentsize, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
printIndent(out, indent);
|
|
|
|
u_fprintf(out, "}\n");
|
|
|
|
ures_close(t);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportError(UErrorCode *status) {
|
|
|
|
u_fprintf(outerr, "Error %d : %U happened!\n", *status, getErrorName(*status));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const UChar *getErrorName(UErrorCode errorNumber) {
|
2000-09-06 22:32:37 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t len = 0;
|
|
|
|
|
|
|
|
UResourceBundle *error = ures_open(currdir, locale, &status);
|
|
|
|
|
|
|
|
UResourceBundle *errorcodes = ures_getByKey(error, "errorcodes", NULL, &status);
|
|
|
|
|
|
|
|
const UChar *result = ures_getStringByIndex(errorcodes, errorNumber, &len, &status);
|
|
|
|
|
|
|
|
ures_close(errorcodes);
|
|
|
|
ures_close(error);
|
|
|
|
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return baderror;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:59:51 +00:00
|
|
|
}
|