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