2
*******************************************************************************
4
* Copyright (C) 1998-2001, International Business Machines
5
* Corporation and others. All Rights Reserved.
7
*******************************************************************************
11
* Modification History:
13
* Date Name Description
14
* 05/25/99 stephen Creation.
15
* 5/10/01 Ram removed ustdio dependency
16
*******************************************************************************
22
static void processFile(const char *filename, const char* cp, const char *inputDir, const char *outputDir, UErrorCode *status);
23
static char *make_res_filename(const char *filename, const char *outputDir, UErrorCode *status);
26
#define RES_SUFFIX ".res"
27
#define COL_SUFFIX ".col"
29
static char theCurrentFileName[4096];
30
const char *gCurrentFileName = theCurrentFileName;
52
UOPTION_HELP_QUESTION_MARK,
64
static UBool verbose = FALSE;
65
static UBool write_java = FALSE;
66
static const char* outputEnc ="";
71
UErrorCode status = U_ZERO_ERROR;
72
const char *arg = NULL;
73
const char *outputDir = NULL; /* NULL = no output directory, use current */
74
const char *inputDir = NULL;
75
const char *encoding = "";
78
U_MAIN_INIT_ARGS(argc, argv);
80
argc = u_parseArgs(argc, argv, (int32_t)(sizeof(options)/sizeof(options[0])), options);
82
/* error handling, printing usage message */
84
fprintf(stderr, "%s: error in command line argument \"%s\"\n", argv[0], argv[-argc]);
89
if(options[VERSION].doesOccur) {
91
"%s version %s (ICU version %s).\n"
93
argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING);
97
if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) {
99
* Broken into chucks because the C89 standard says the minimum
100
* required supported string length is 509 bytes.
103
"Usage: %s [OPTIONS] [FILES]\n"
104
"\tReads the list of resource bundle source files and creates\n"
105
"\tbinary version of reosurce bundles (.res files)\n",
109
"\t-h or -? or --help this usage text\n"
110
"\t-q or --quiet do not display warnings\n"
111
"\t-v or --verbose prints out extra information about processing the files\n"
112
"\t-V or --version prints out version number and exits\n"
113
"\t-c or --copyright include copyright notice\n");
115
"\t-e or --encoding encoding of source files, leave empty for system default encoding\n"
116
"\t NOTE: ICU must be completely built to use this option\n"
117
"\t-d of --destdir destination directory, followed by the path, defaults to %s\n"
118
"\t-s or --sourcedir source directory for files followed by path, defaults to %s\n"
119
"\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
120
"\t followed by path, defaults to %s\n",
121
u_getDataDirectory(), u_getDataDirectory(), u_getDataDirectory());
123
"\t-j or --write-java write a Java ListResourceBundle for ICU4J, followed by optional encoding\n"
124
"\t defaults to ASCII and \\uXXXX format.\n");
125
return argc < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
128
if(options[VERBOSE].doesOccur) {
132
if(options[QUIET].doesOccur) {
133
setShowWarning(FALSE);
136
if(options[COPYRIGHT].doesOccur){
137
setIncludeCopyright(TRUE);
140
if(options[SOURCEDIR].doesOccur) {
141
inputDir = options[SOURCEDIR].value;
144
if(options[DESTDIR].doesOccur) {
145
outputDir = options[DESTDIR].value;
148
if(options[ENCODING].doesOccur) {
149
encoding = options[ENCODING].value;
152
if(options[ICUDATADIR].doesOccur) {
153
u_setDataDirectory(options[ICUDATADIR].value);
155
if(options[WRITE_JAVA].doesOccur) {
157
outputEnc = options[WRITE_JAVA].value;
162
/* generate the binary files */
163
for(i = 1; i < argc; ++i) {
164
status = U_ZERO_ERROR;
165
arg = getLongPathname(argv[i]);
168
uprv_strcpy(theCurrentFileName, inputDir);
169
uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING);
171
*theCurrentFileName = 0;
173
uprv_strcat(theCurrentFileName, arg);
176
printf("processing file \"%s\"\n", theCurrentFileName);
178
processFile(arg, encoding, inputDir, outputDir, &status);
186
processFile(const char *filename, const char *cp, const char *inputDir, const char *outputDir, UErrorCode *status) {
187
FileStream *in = NULL;
188
struct SRBRoot *data = NULL;
189
UCHARBUF *ucbuf = NULL;
191
char *openFileName = NULL;
192
char *inputDirBuf = NULL;
194
char outputFileName[256];
196
if (U_FAILURE(*status)) {
203
/* Open the input file for reading */
204
if (!uprv_strcmp(filename, "-")) {
205
in = T_FileStream_stdin();
206
} else if(inputDir == NULL) {
207
const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR);
208
if (filenameBegin != NULL) {
210
* When a filename ../../../data/root.txt is specified,
211
* we presume that the input directory is ../../../data
212
* This is very important when the resource file includes
213
* another file, like UCARules.txt or thaidict.brk.
215
int32_t filenameSize = filenameBegin - filename + 1;
216
inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize);
217
inputDirBuf[filenameSize - 1] = 0;
218
inputDir = inputDirBuf;
220
in = T_FileStream_open(filename, "rb");
222
int32_t dirlen = (int32_t)uprv_strlen(inputDir);
223
int32_t filelen = (int32_t)uprv_strlen(filename);
224
if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) {
225
openFileName = (char *) uprv_malloc(dirlen + filelen + 2);
226
openFileName[0] = '\0';
228
* append the input dir to openFileName if the first char in
229
* filename is not file seperation char and the last char input directory is not '.'.
230
* This is to support :
231
* genrb -s. /home/icu/data
233
* The user cannot mix notations like
234
* genrb -s. /icu/data --- the absolute path specified. -s redundant
236
* genrb -s. icu/data --- start from CWD and look in icu/data dir
238
if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){
239
uprv_strcpy(openFileName, inputDir);
240
openFileName[dirlen] = U_FILE_SEP_CHAR;
242
openFileName[dirlen + 1] = '\0';
243
uprv_strcat(openFileName, filename);
245
openFileName = (char *) uprv_malloc(dirlen + filelen + 1);
246
uprv_strcpy(openFileName, inputDir);
247
uprv_strcat(openFileName, filename);
249
in = T_FileStream_open(openFileName, "rb");
253
*status = U_FILE_ACCESS_ERROR;
254
fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName);
257
/* auto detect popular encodings */
258
if (*cp=='\0' && ucbuf_autodetect(in, &cp) && verbose) {
259
printf("autodetected encoding %s\n", cp);
263
ucbuf = ucbuf_open(in, cp, 0, status);
265
if (ucbuf == NULL || U_FAILURE(*status)) {
269
/* Parse the data into an SRBRoot */
270
data = parse(ucbuf, inputDir, status);
272
if (data == NULL || U_FAILURE(*status)) {
276
/* Determine the target rb filename */
277
rbname = make_res_filename(filename, outputDir, status);
278
if(U_FAILURE(*status)) {
281
if(write_java== FALSE){
282
/* Write the data to the file */
283
bundle_write(data, outputDir, outputFileName, sizeof(outputFileName), status);
285
bundle_write_java(data,outputDir,outputEnc, outputFileName, sizeof(outputFileName), status);
287
if (U_FAILURE(*status)) {
288
fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName,u_errorName(*status));
290
bundle_close(data, status);
294
if (inputDirBuf != NULL) {
295
uprv_free(inputDirBuf);
298
if (openFileName != NULL) {
299
uprv_free(openFileName);
307
if (in != T_FileStream_stdin()) {
308
T_FileStream_close(in);
316
/* Generate the target .res file name from the input file name */
318
make_res_filename(const char *filename,
319
const char *outputDir,
320
UErrorCode *status) {
325
if (U_FAILURE(*status)) {
330
basename = dirname = resName = 0;
332
/* determine basename, and compiled file names */
333
basename = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1));
335
*status = U_MEMORY_ALLOCATION_ERROR;
339
get_basename(basename, filename);
341
dirname = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1));
343
*status = U_MEMORY_ALLOCATION_ERROR;
347
get_dirname(dirname, filename);
349
if (outputDir == NULL) {
350
/* output in same dir as .txt */
351
resName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(dirname)
352
+ uprv_strlen(basename)
353
+ uprv_strlen(RES_SUFFIX) + 1));
355
*status = U_MEMORY_ALLOCATION_ERROR;
359
uprv_strcpy(resName, dirname);
360
uprv_strcat(resName, basename);
362
int32_t dirlen = (int32_t)uprv_strlen(outputDir);
363
int32_t basenamelen = (int32_t)uprv_strlen(basename);
365
resName = (char*) uprv_malloc(sizeof(char) * (dirlen + basenamelen + 2));
367
if (resName == NULL) {
368
*status = U_MEMORY_ALLOCATION_ERROR;
372
uprv_strcpy(resName, outputDir);
374
if(outputDir[dirlen] != U_FILE_SEP_CHAR) {
375
resName[dirlen] = U_FILE_SEP_CHAR;
376
resName[dirlen + 1] = '\0';
379
uprv_strcat(resName, basename);
391
* indent-tabs-mode: nil