@ -52,7 +52,6 @@ Japanese, it doesn't *claim* to have Japanese.
# include "string.h"
# include "charstr.h" // ICU internal header
# include <unicode/ustdio.h>
# include <unicode/ures.h>
# include <unicode/udata.h>
# include <unicode/putil.h>
@ -69,36 +68,36 @@ CharString packageName;
const char * locale = RES_INDEX ; // locale referring to our index
void usage ( ) {
u_ printf( " Usage: %s [options] \n " , PROG ) ;
u_ printf(
printf ( " Usage: %s [options] \n " , PROG ) ;
printf (
" This program lists and optionally regenerates the locale "
" manifests \n "
" in ICU 'res_index.res' files. \n " ) ;
u_ printf(
printf (
" -i ICUDATA Set ICUDATA dir to ICUDATA. \n "
" NOTE: this must be the first option given. \n " ) ;
u_ printf( " -h This Help \n " ) ;
u_ printf( " -v Verbose Mode on \n " ) ;
u_ printf( " -l List locales to stdout \n " ) ;
u_ printf(
printf ( " -h This Help \n " ) ;
printf ( " -v Verbose Mode on \n " ) ;
printf ( " -l List locales to stdout \n " ) ;
printf (
" if Verbose mode, then missing (unopenable) "
" locales \n "
" will be listed preceded by a '#'. \n " ) ;
u_ printf(
printf (
" -b res_index.txt Write 'corrected' bundle "
" to res_index.txt \n "
" missing bundles will be "
" OMITTED \n " ) ;
u_ printf(
printf (
" -T TREE Choose tree TREE \n "
" (TREE should be one of: \n "
" ROOT, brkitr, coll, curr, lang, rbnf, region, zone) \n " ) ;
// see ureslocs.h and elsewhere
u_ printf(
printf (
" -N NAME Choose name NAME \n "
" (default: '%s') \n " ,
U_ICUDATA_NAME ) ;
u_ printf(
printf (
" \n NOTE: for best results, this tool ought to be "
" linked against \n "
" stubdata. i.e. '%s -l' SHOULD return an error with "
@ -108,7 +107,7 @@ void usage() {
# define ASSERT_SUCCESS(status, what) \
if ( U_FAILURE ( * status ) ) { \
u_ printf( " %s:%d: %s: ERROR: %s %s \n " , \
printf ( " %s:%d: %s: ERROR: %s %s \n " , \
__FILE__ , \
__LINE__ , \
PROG , \
@ -130,7 +129,7 @@ void calculatePackageName(UErrorCode* status) {
}
}
if ( VERBOSE ) {
u_ printf( " packageName: %s \n " , packageName . data ( ) ) ;
printf ( " packageName: %s \n " , packageName . data ( ) ) ;
}
}
@ -146,7 +145,7 @@ void calculatePackageName(UErrorCode* status) {
int localeExists ( const char * loc , UBool * exists ) {
UErrorCode status = U_ZERO_ERROR ;
if ( VERBOSE > 1 ) {
u_ printf( " Trying to open %s:%s \n " , packageName . data ( ) , loc ) ;
printf ( " Trying to open %s:%s \n " , packageName . data ( ) , loc ) ;
}
LocalUResourceBundlePointer aResource (
ures_openDirect ( packageName . data ( ) , loc , & status ) ) ;
@ -154,13 +153,13 @@ int localeExists(const char* loc, UBool* exists) {
if ( U_SUCCESS ( status ) ) {
* exists = true ;
if ( VERBOSE > 1 ) {
u_ printf( " %s:%s existed! \n " , packageName . data ( ) , loc ) ;
printf ( " %s:%s existed! \n " , packageName . data ( ) , loc ) ;
}
return 0 ;
} else if ( status = = U_MISSING_RESOURCE_ERROR ) {
* exists = false ;
if ( VERBOSE > 1 ) {
u_ printf( " %s:%s did NOT exist (%s)! \n " ,
printf ( " %s:%s did NOT exist (%s)! \n " ,
packageName . data ( ) ,
loc ,
u_errorName ( status ) ) ;
@ -168,7 +167,7 @@ int localeExists(const char* loc, UBool* exists) {
return 0 ; // "good" failure
} else {
// some other failure..
u_ printf( " %s:%d: %s: ERROR %s opening %s: %s for test. \n " ,
printf ( " %s:%d: %s: ERROR %s opening %s for test. \n " ,
__FILE__ ,
__LINE__ ,
u_errorName ( status ) ,
@ -178,9 +177,9 @@ int localeExists(const char* loc, UBool* exists) {
}
}
void printIndent ( const LocalU FILEPointer * bf , int indent ) {
void printIndent ( FILE * bf , int indent ) {
for ( int i = 0 ; i < indent + 1 ; i + + ) {
u_ fprintf( bf - > getAlias ( ) , " " ) ;
fprintf ( bf , " " ) ;
}
}
@ -191,7 +190,7 @@ void printIndent(const LocalUFILEPointer* bf, int indent) {
*/
int dumpAllButInstalledLocales ( int lev ,
LocalUResourceBundlePointer * bund ,
LocalU FILEPointer * bf ,
FILE * bf ,
UErrorCode * status ) {
ures_resetIterator ( bund - > getAlias ( ) ) ;
LocalUResourceBundlePointer t ;
@ -200,30 +199,30 @@ int dumpAllButInstalledLocales(int lev,
ASSERT_SUCCESS ( status , " while processing table " ) ;
const char * key = ures_getKey ( t . getAlias ( ) ) ;
if ( VERBOSE > 1 ) {
u_ printf( " dump@%d: got key %s \n " , lev , key ) ;
printf ( " dump@%d: got key %s \n " , lev , key ) ;
}
if ( lev = = 0 & & ! strcmp ( key , INSTALLEDLOCALES ) ) {
if ( VERBOSE > 1 ) {
u_ printf( " dump: skipping '%s' as it must be evaluated. \n " , key ) ;
printf ( " dump: skipping '%s' as it must be evaluated. \n " , key ) ;
}
} else {
printIndent ( bf , lev ) ;
u_ fprintf( bf - > getAlias ( ) , " %s " , key ) ;
fprintf ( bf , " %s " , key ) ;
switch ( ures_getType ( t . getAlias ( ) ) ) {
case URES_STRING : {
int32_t len = 0 ;
const UChar * s = ures_getString ( t . getAlias ( ) , & len , status ) ;
ASSERT_SUCCESS ( status , " getting string " ) ;
u_ fprintf( bf - > getAlias ( ) , " :string { \" " ) ;
u_ file_ write( s , len , bf - > getAlias ( ) ) ;
u_ fprintf( bf - > getAlias ( ) , " \" } " ) ;
fprintf ( bf , " :string { \" " ) ;
fwrite ( s , len , 1 , bf ) ;
fprintf ( bf , " \" } " ) ;
} break ;
default : {
u_ printf( " ERROR: unhandled type in dumpAllButInstalledLocales(). \n " ) ;
printf ( " ERROR: unhandled type in dumpAllButInstalledLocales(). \n " ) ;
return 1 ;
} break ;
}
u_ fprintf( bf - > getAlias ( ) , " \n " ) ;
fprintf ( bf , " \n " ) ;
}
}
return 0 ;
@ -232,20 +231,19 @@ int dumpAllButInstalledLocales(int lev,
int list ( const char * toBundle ) {
UErrorCode status = U_ZERO_ERROR ;
LocalUFILEPointer bf ;
FILE * bf = NULL ;
if ( toBundle ! = NULL ) {
if ( VERBOSE ) {
u_ printf( " writing to bundle %s \n " , toBundle ) ;
printf ( " writing to bundle %s \n " , toBundle ) ;
}
// we write UTF-8 with BOM only. No exceptions.
bf . adoptInstead ( u_fopen ( toBundle , " w " , " en_US_POSIX " , " UTF-8 " ) ) ;
if ( bf . isNull ( ) ) {
u_printf ( " ERROR: Could not open '%s' for writing. \n " , toBundle ) ;
bf = fopen ( toBundle , " wb " ) ;
if ( bf = = NULL ) {
printf ( " ERROR: Could not open '%s' for writing. \n " , toBundle ) ;
return 1 ;
}
u_fputc ( 0xFEFF , bf . getAlias ( ) ) ; // write BOM
u_ fprintf( bf . getAlias ( ) , " // -*- Coding: utf-8; -*- \n // \n " ) ;
fprintf ( bf , " \xEF \xBB \xBF " ) ; // write UTF-8 BOM
fprintf ( bf , " // -*- Coding: utf-8; -*- \n // \n " ) ;
}
// first, calculate the bundle name.
@ -253,7 +251,7 @@ int list(const char* toBundle) {
ASSERT_SUCCESS ( & status , " calculating package name " ) ;
if ( VERBOSE ) {
u_ printf( " \" locale \" : %s \n " , locale ) ;
printf ( " \" locale \" : %s \n " , locale ) ;
}
LocalUResourceBundlePointer bund (
@ -265,29 +263,30 @@ int list(const char* toBundle) {
int32_t count = ures_getSize ( installedLocales . getAlias ( ) ) ;
if ( VERBOSE ) {
u_ printf( " Locales: %d \n " , count ) ;
printf ( " Locales: %d \n " , count ) ;
}
if ( bf . isValid ( ) ) {
if ( bf ! = NULL ) {
// write the HEADER
u_ fprintf( bf . getAlias ( ) ,
fprintf ( bf ,
" // Warning this file is automatically generated \n "
" // Updated by %s based on %s:%s.txt \n " ,
PROG ,
packageName . data ( ) ,
locale ) ;
u_ fprintf( bf . getAlias ( ) ,
fprintf ( bf ,
" %s:table(nofallback) { \n "
" // First, everything besides InstalledLocales: \n " ,
locale ) ;
if ( dumpAllButInstalledLocales ( 0 , & bund , & bf , & status ) ) {
u_printf ( " Error dumping prolog for %s \n " , toBundle ) ;
if ( dumpAllButInstalledLocales ( 0 , & bund , bf , & status ) ) {
printf ( " Error dumping prolog for %s \n " , toBundle ) ;
fclose ( bf ) ;
return 1 ;
}
// in case an error was missed
ASSERT_SUCCESS ( & status , " while writing prolog " ) ;
u_ fprintf( bf . getAlias ( ) ,
fprintf ( bf ,
" %s:table { // %d locales in input %s.res \n " ,
INSTALLEDLOCALES ,
count ,
@ -305,35 +304,38 @@ int list(const char* toBundle) {
const char * key = ures_getKey ( subkey . getAlias ( ) ) ;
if ( VERBOSE > 1 ) {
u_ printf( " @%d: %s \n " , i , key ) ;
printf ( " @%d: %s \n " , i , key ) ;
}
// now, see if the locale is installed..
UBool exists ;
if ( localeExists ( key , & exists ) ) {
if ( bf ! = NULL ) fclose ( bf ) ;
return 1 ; // get out.
}
if ( exists ) {
validCount + + ;
u_ printf( " %s \n " , key ) ;
if ( bf . isValid ( ) ) {
u_ fprintf( bf . getAlias ( ) , " %s { \" \" } \n " , key ) ;
printf ( " %s \n " , key ) ;
if ( bf ! = NULL ) {
fprintf ( bf , " %s { \" \" } \n " , key ) ;
}
} else {
if ( bf . isValid ( ) ) {
u_ fprintf( bf . getAlias ( ) , " // %s { \" \" } \n " , key ) ;
if ( bf ! = NULL ) {
fprintf ( bf , " // %s { \" \" } \n " , key ) ;
}
if ( VERBOSE ) {
u_ printf( " #%s \n " , key ) ; // verbosity one - '' vs '#'
printf ( " #%s \n " , key ) ; // verbosity one - '' vs '#'
}
}
}
if ( bf . isValid ( ) ) {
u_ fprintf( bf . getAlias ( ) , " } // %d/%d valid \n " , validCount , count ) ;
if ( bf ! = NULL ) {
fprintf ( bf , " } // %d/%d valid \n " , validCount , count ) ;
// write the HEADER
u_fprintf ( bf . getAlias ( ) , " } \n " ) ;
fprintf ( bf , " } \n " ) ;
fclose ( bf ) ;
}
return 0 ;
}
@ -346,24 +348,24 @@ int main(int argc, const char* argv[]) {
VERBOSE + + ;
} else if ( ! strcmp ( arg , " -i " ) & & ( argsLeft > = 1 ) ) {
if ( i ! = 1 ) {
u_ printf( " ERROR: -i must be the first argument given. \n " ) ;
printf ( " ERROR: -i must be the first argument given. \n " ) ;
usage ( ) ;
return 1 ;
}
const char * dir = argv [ + + i ] ;
u_setDataDirectory ( dir ) ;
if ( VERBOSE ) {
u_ printf( " ICUDATA is now %s \n " , dir ) ;
printf ( " ICUDATA is now %s \n " , dir ) ;
}
} else if ( ! strcmp ( arg , " -T " ) & & ( argsLeft > = 1 ) ) {
TREE = argv [ + + i ] ;
if ( VERBOSE ) {
u_ printf( " TREE is now %s \n " , TREE ) ;
printf ( " TREE is now %s \n " , TREE ) ;
}
} else if ( ! strcmp ( arg , " -N " ) & & ( argsLeft > = 1 ) ) {
NAME = argv [ + + i ] ;
if ( VERBOSE ) {
u_ printf( " NAME is now %s \n " , NAME ) ;
printf ( " NAME is now %s \n " , NAME ) ;
}
} else if ( ! strcmp ( arg , " -? " ) | | ! strcmp ( arg , " -h " ) ) {
usage ( ) ;
@ -377,7 +379,7 @@ int main(int argc, const char* argv[]) {
return 1 ;
}
} else {
u_ printf( " Unknown or malformed option: %s \n " , arg ) ;
printf ( " Unknown or malformed option: %s \n " , arg ) ;
usage ( ) ;
return 1 ;
}