1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
2001-03-21 23:22:16 +00:00
|
|
|
* Copyright (C) 1998-2001, International Business Machines
|
1999-12-13 22:28:37 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File genrb.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
2000-11-28 19:53:40 +00:00
|
|
|
* 05/25/99 stephen Creation.
|
2001-05-10 21:43:01 +00:00
|
|
|
* 5/10/01 Ram removed ustdio dependency
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/utypes.h"
|
2001-01-03 00:18:57 +00:00
|
|
|
#include "unicode/putil.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "filestrm.h"
|
2001-05-10 21:43:01 +00:00
|
|
|
|
1999-12-15 21:42:15 +00:00
|
|
|
|
2001-05-10 21:43:01 +00:00
|
|
|
#include "ucbuf.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "util.h"
|
2000-05-15 18:39:17 +00:00
|
|
|
#include "reslist.h"
|
1999-12-15 21:42:15 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
|
1999-12-14 01:23:07 +00:00
|
|
|
#include "toolutil.h"
|
2000-05-15 18:39:17 +00:00
|
|
|
#include "uoptions.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/ucol.h"
|
|
|
|
#include "unicode/uloc.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* Protos */
|
2001-07-11 20:31:32 +00:00
|
|
|
static void processFile(const char *filename, const char* cp, const char *inputDir, const char *outputDir, UErrorCode *status);
|
|
|
|
static char *make_res_filename(const char *filename, const char *outputDir, UErrorCode *status);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* File suffixes */
|
|
|
|
#define RES_SUFFIX ".res"
|
|
|
|
#define COL_SUFFIX ".col"
|
|
|
|
|
|
|
|
/* The version of genrb */
|
2001-07-11 20:31:32 +00:00
|
|
|
#define GENRB_VERSION "3.0"
|
2000-05-15 18:39:17 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
const char *gCurrentFileName;
|
2000-05-15 18:39:17 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
enum
|
|
|
|
{
|
2000-05-15 18:39:17 +00:00
|
|
|
HELP1,
|
|
|
|
HELP2,
|
2001-10-10 18:32:06 +00:00
|
|
|
/* VERBOSE, */
|
|
|
|
QUIET,
|
2000-05-15 18:39:17 +00:00
|
|
|
VERSION,
|
|
|
|
SOURCEDIR,
|
|
|
|
DESTDIR,
|
2001-05-16 19:13:53 +00:00
|
|
|
ENCODING,
|
2001-11-03 02:54:08 +00:00
|
|
|
ICUDATADIR,
|
|
|
|
COPYRIGHT
|
2000-05-15 18:39:17 +00:00
|
|
|
};
|
|
|
|
|
2000-05-24 21:54:28 +00:00
|
|
|
UOption options[]={
|
2001-07-11 20:31:32 +00:00
|
|
|
UOPTION_HELP_H,
|
|
|
|
UOPTION_HELP_QUESTION_MARK,
|
2001-10-10 18:32:06 +00:00
|
|
|
/* UOPTION_VERBOSE, */
|
|
|
|
UOPTION_QUIET,
|
2001-07-11 20:31:32 +00:00
|
|
|
UOPTION_VERSION,
|
|
|
|
UOPTION_SOURCEDIR,
|
|
|
|
UOPTION_DESTDIR,
|
|
|
|
UOPTION_ENCODING,
|
2001-11-03 02:54:08 +00:00
|
|
|
UOPTION_ICUDATADIR,
|
|
|
|
UOPTION_COPYRIGHT
|
2001-07-11 20:31:32 +00:00
|
|
|
};
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-02-16 22:38:38 +00:00
|
|
|
|
2000-08-28 21:36:13 +00:00
|
|
|
#ifdef XP_MAC_CONSOLE
|
|
|
|
#include <console.h>
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc,
|
2001-07-11 20:31:32 +00:00
|
|
|
char* argv[]) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const char *arg = NULL;
|
|
|
|
const char *outputDir = NULL; /* NULL = no output directory, use current */
|
|
|
|
const char *inputDir = NULL;
|
|
|
|
const char *encoding = "";
|
2001-10-10 18:32:06 +00:00
|
|
|
/* UBool verbose; */
|
2001-07-11 20:31:32 +00:00
|
|
|
int i;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-08-28 21:36:13 +00:00
|
|
|
#ifdef XP_MAC_CONSOLE
|
2001-07-11 20:31:32 +00:00
|
|
|
|
|
|
|
argc = ccommand((char***)&argv);
|
2000-08-28 21:36:13 +00:00
|
|
|
#endif
|
|
|
|
|
2001-09-25 19:28:25 +00:00
|
|
|
argc = u_parseArgs(argc, argv, (int32_t)(sizeof(options)/sizeof(options[0])), options);
|
2001-05-16 19:13:53 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
/* error handling, printing usage message */
|
|
|
|
if(argc<0) {
|
|
|
|
fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]);
|
|
|
|
} else if(argc<2) {
|
|
|
|
argc = -1;
|
2000-05-15 18:39:17 +00:00
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
|
|
|
|
if(options[VERSION].doesOccur) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s version %s (ICU version %s).\n"
|
|
|
|
"%s\n",
|
|
|
|
argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING);
|
|
|
|
return U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %s [OPTIONS] [FILES]\n"
|
|
|
|
"\treads the list of resource bundle source files and creates\n"
|
|
|
|
"\tbinary version of reosurce bundles (.res files)\n"
|
2001-10-10 18:32:06 +00:00
|
|
|
"Options:\n"
|
|
|
|
"\t-h, -? or --help this usage text\n"
|
|
|
|
"\t-V or --version prints out version number and exits\n"
|
|
|
|
"\t-d of --destdir destination directory, followed by the path, defaults to %s\n"
|
|
|
|
/* "\t-v or --verbose be verbose\n"*/
|
|
|
|
"\t-q or --quiet do not display warnings\n"
|
|
|
|
"\t-e or --encoding encoding of source files, leave empty for system default encoding\n"
|
|
|
|
"\t NOTE: ICU must be completely built to use this option\n"
|
|
|
|
"\t-s or --sourcedir source directory for files followed by path, defaults to %s\n"
|
|
|
|
"\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
|
|
|
|
"\t followed by path, defaults to %s\n",
|
2001-11-03 02:54:08 +00:00
|
|
|
"\t-c or --copyright include copyright notice \n",
|
2001-07-11 20:31:32 +00:00
|
|
|
argv[0], u_getDataDirectory(), u_getDataDirectory(),u_getDataDirectory());
|
|
|
|
return argc < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
|
2001-10-10 18:32:06 +00:00
|
|
|
/* if(options[VERBOSE].doesOccur) {
|
2001-07-11 20:31:32 +00:00
|
|
|
verbose = TRUE;
|
2001-10-10 18:32:06 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
if(options[QUIET].doesOccur) {
|
|
|
|
setShowWarning(FALSE);
|
2001-07-11 20:31:32 +00:00
|
|
|
}
|
2001-11-03 02:54:08 +00:00
|
|
|
|
|
|
|
if(options[COPYRIGHT].doesOccur){
|
|
|
|
setIncludeCopyright(TRUE);
|
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
|
|
|
|
if(options[SOURCEDIR].doesOccur) {
|
|
|
|
inputDir = options[SOURCEDIR].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[DESTDIR].doesOccur) {
|
|
|
|
outputDir = options[DESTDIR].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[ENCODING].doesOccur) {
|
|
|
|
encoding = options[ENCODING].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[ICUDATADIR].doesOccur) {
|
|
|
|
u_setDataDirectory(options[ICUDATADIR].value);
|
|
|
|
}
|
|
|
|
|
|
|
|
initParser();
|
|
|
|
|
|
|
|
/* generate the binary files */
|
|
|
|
for(i = 1; i < argc; ++i) {
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
arg = getLongPathname(argv[i]);
|
|
|
|
|
|
|
|
printf("genrb: processing file \"%s\"\n", arg);
|
|
|
|
|
|
|
|
processFile(arg, encoding, inputDir, outputDir, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a file */
|
|
|
|
static void
|
2001-07-11 20:31:32 +00:00
|
|
|
processFile(const char *filename, const char *cp, const char *inputDir, const char *outputDir, UErrorCode *status) {
|
|
|
|
FileStream *in = NULL;
|
|
|
|
struct SRBRoot *data = NULL;
|
|
|
|
UCHARBUF *ucbuf = NULL;
|
|
|
|
char *rbname = NULL;
|
|
|
|
char *openFileName = NULL;
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
/* Setup */
|
|
|
|
in = 0;
|
|
|
|
|
|
|
|
/* Open the input file for reading */
|
|
|
|
if(inputDir == NULL) {
|
|
|
|
in = T_FileStream_open(filename, "rb");
|
|
|
|
} else {
|
2001-09-25 19:28:25 +00:00
|
|
|
int32_t dirlen = (int32_t)uprv_strlen(inputDir);
|
|
|
|
int32_t filelen = (int32_t)uprv_strlen(filename);
|
2001-07-11 20:31:32 +00:00
|
|
|
if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) {
|
|
|
|
openFileName = (char *) uprv_malloc(dirlen + filelen + 2);
|
2001-11-05 23:16:09 +00:00
|
|
|
/*
|
|
|
|
* append the input dir to openFileName if the first char in
|
|
|
|
* filename is not file seperation char and the last char input directory is not '.'.
|
|
|
|
* This is to support :
|
|
|
|
* genrb -s. /home/icu/data
|
|
|
|
* genrb -s. icu/data
|
|
|
|
* The user cannot mix notations like
|
|
|
|
* genrb -s. /icu/data --- the absolute path specified. -s redundant
|
|
|
|
* user should use
|
|
|
|
* genrb -s. icu/data --- start from CWD and look in icu/data dir
|
|
|
|
*/
|
|
|
|
if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){
|
|
|
|
uprv_strcpy(openFileName, inputDir);
|
|
|
|
openFileName[dirlen] = U_FILE_SEP_CHAR;
|
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
openFileName[dirlen + 1] = '\0';
|
|
|
|
uprv_strcat(openFileName, filename);
|
|
|
|
} else {
|
|
|
|
openFileName = (char *) uprv_malloc(dirlen + filelen + 1);
|
|
|
|
uprv_strcpy(openFileName, inputDir);
|
|
|
|
uprv_strcat(openFileName, filename);
|
|
|
|
}
|
|
|
|
in = T_FileStream_open(openFileName, "rb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(in == 0) {
|
|
|
|
*status = U_FILE_ACCESS_ERROR;
|
|
|
|
fprintf(stderr, "Couldn't open file %s", openFileName == NULL ? filename : openFileName);
|
|
|
|
goto finish;
|
|
|
|
} else {
|
|
|
|
/* auto detect popular encodings */
|
2001-11-01 19:43:21 +00:00
|
|
|
if (*cp=='\0' &&ucbuf_autodetect(in, &cp)) {
|
2001-07-11 20:31:32 +00:00
|
|
|
printf("Autodetected encoding %s\n", cp);
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-11-03 02:54:08 +00:00
|
|
|
ucbuf = ucbuf_open(in, cp,getShowWarning(), status);
|
2000-08-21 22:17:55 +00:00
|
|
|
|
2001-09-28 23:25:24 +00:00
|
|
|
if (ucbuf == NULL || U_FAILURE(*status)) {
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
/* Parse the data into an SRBRoot */
|
|
|
|
gCurrentFileName = filename;
|
|
|
|
data = parse(ucbuf, inputDir, status);
|
|
|
|
|
|
|
|
if (data == NULL || U_FAILURE(*status)) {
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the target rb filename */
|
|
|
|
rbname = make_res_filename(filename, outputDir, status);
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the data to the file */
|
|
|
|
bundle_write(data, outputDir, status);
|
|
|
|
bundle_close(data, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-08-21 22:17:55 +00:00
|
|
|
finish:
|
2001-11-01 19:43:21 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
if (openFileName != NULL) {
|
|
|
|
uprv_free(openFileName);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
if(ucbuf) {
|
|
|
|
ucbuf_close(ucbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
T_FileStream_close(in);
|
|
|
|
|
|
|
|
if (rbname) {
|
|
|
|
uprv_free(rbname);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-12-13 03:41:08 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
/* Generate the target .res file name from the input file name */
|
|
|
|
static char*
|
|
|
|
make_res_filename(const char *filename,
|
|
|
|
const char *outputDir,
|
|
|
|
UErrorCode *status) {
|
1999-12-13 03:41:08 +00:00
|
|
|
char *basename;
|
2001-07-11 20:31:32 +00:00
|
|
|
char *dirname;
|
|
|
|
char *resName;
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
1999-12-13 03:41:08 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
/* setup */
|
|
|
|
basename = dirname = resName = 0;
|
|
|
|
|
|
|
|
/* determine basename, and compiled file names */
|
1999-12-28 23:57:50 +00:00
|
|
|
basename = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1));
|
1999-12-13 03:41:08 +00:00
|
|
|
if(basename == 0) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2001-07-11 20:31:32 +00:00
|
|
|
goto finish;
|
1999-12-13 03:41:08 +00:00
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
|
1999-12-13 03:41:08 +00:00
|
|
|
get_basename(basename, filename);
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2001-07-11 20:31:32 +00:00
|
|
|
dirname = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1));
|
|
|
|
if(dirname == 0) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto finish;
|
1999-12-13 03:41:08 +00:00
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
|
|
|
|
get_dirname(dirname, filename);
|
|
|
|
|
|
|
|
if (outputDir == NULL) {
|
|
|
|
/* output in same dir as .txt */
|
|
|
|
resName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(dirname)
|
|
|
|
+ uprv_strlen(basename)
|
|
|
|
+ uprv_strlen(RES_SUFFIX) + 1));
|
|
|
|
if(resName == 0) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
uprv_strcpy(resName, dirname);
|
|
|
|
uprv_strcat(resName, basename);
|
|
|
|
} else {
|
2001-09-25 19:28:25 +00:00
|
|
|
int32_t dirlen = (int32_t)uprv_strlen(outputDir);
|
|
|
|
int32_t basenamelen = (int32_t)uprv_strlen(basename);
|
2001-07-11 20:31:32 +00:00
|
|
|
|
|
|
|
resName = (char*) uprv_malloc(sizeof(char) * (dirlen + basenamelen + 2));
|
|
|
|
|
|
|
|
if (resName == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
uprv_strcpy(resName, outputDir);
|
|
|
|
|
|
|
|
if(outputDir[dirlen] != U_FILE_SEP_CHAR) {
|
|
|
|
resName[dirlen] = U_FILE_SEP_CHAR;
|
|
|
|
resName[dirlen + 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
uprv_strcat(resName, basename);
|
1999-12-13 03:41:08 +00:00
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
|
|
|
|
finish:
|
|
|
|
uprv_free(basename);
|
|
|
|
uprv_free(dirname);
|
|
|
|
|
|
|
|
return resName;
|
1999-12-15 21:42:15 +00:00
|
|
|
}
|
2001-07-11 20:31:32 +00:00
|
|
|
|
2000-03-04 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*/
|