michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1998-2014, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * michael@0: * File genrb.c michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 05/25/99 stephen Creation. michael@0: * 5/10/01 Ram removed ustdio dependency michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "genrb.h" michael@0: #include "unicode/uclean.h" michael@0: michael@0: #include "ucmndata.h" /* TODO: for reading the pool bundle */ michael@0: michael@0: /* Protos */ michael@0: void processFile(const char *filename, const char* cp, const char *inputDir, const char *outputDir, michael@0: const char *packageName, UBool omitBinaryCollation, UErrorCode *status); michael@0: static char *make_res_filename(const char *filename, const char *outputDir, michael@0: const char *packageName, UErrorCode *status); michael@0: michael@0: /* File suffixes */ michael@0: #define RES_SUFFIX ".res" michael@0: #define COL_SUFFIX ".col" michael@0: michael@0: static char theCurrentFileName[2048]; michael@0: const char *gCurrentFileName = theCurrentFileName; michael@0: #ifdef XP_MAC_CONSOLE michael@0: #include michael@0: #endif michael@0: michael@0: enum michael@0: { michael@0: HELP1, michael@0: HELP2, michael@0: VERBOSE, michael@0: QUIET, michael@0: VERSION, michael@0: SOURCEDIR, michael@0: DESTDIR, michael@0: ENCODING, michael@0: ICUDATADIR, michael@0: WRITE_JAVA, michael@0: COPYRIGHT, michael@0: JAVA_PACKAGE, michael@0: BUNDLE_NAME, michael@0: WRITE_XLIFF, michael@0: STRICT, michael@0: NO_BINARY_COLLATION, michael@0: LANGUAGE, michael@0: NO_COLLATION_RULES, michael@0: FORMAT_VERSION, michael@0: WRITE_POOL_BUNDLE, michael@0: USE_POOL_BUNDLE, michael@0: INCLUDE_UNIHAN_COLL michael@0: }; michael@0: michael@0: UOption options[]={ michael@0: UOPTION_HELP_H, michael@0: UOPTION_HELP_QUESTION_MARK, michael@0: UOPTION_VERBOSE, michael@0: UOPTION_QUIET, michael@0: UOPTION_VERSION, michael@0: UOPTION_SOURCEDIR, michael@0: UOPTION_DESTDIR, michael@0: UOPTION_ENCODING, michael@0: UOPTION_ICUDATADIR, michael@0: UOPTION_WRITE_JAVA, michael@0: UOPTION_COPYRIGHT, michael@0: UOPTION_DEF("java-package", '\x01', UOPT_REQUIRES_ARG), michael@0: UOPTION_BUNDLE_NAME, michael@0: UOPTION_DEF("write-xliff", 'x', UOPT_OPTIONAL_ARG), michael@0: UOPTION_DEF("strict", 'k', UOPT_NO_ARG), /* 14 */ michael@0: UOPTION_DEF("noBinaryCollation", 'C', UOPT_NO_ARG),/* 15 */ michael@0: UOPTION_DEF("language", 'l', UOPT_REQUIRES_ARG), /* 16 */ michael@0: UOPTION_DEF("omitCollationRules", 'R', UOPT_NO_ARG),/* 17 */ michael@0: UOPTION_DEF("formatVersion", '\x01', UOPT_REQUIRES_ARG),/* 18 */ michael@0: UOPTION_DEF("writePoolBundle", '\x01', UOPT_NO_ARG),/* 19 */ michael@0: UOPTION_DEF("usePoolBundle", '\x01', UOPT_OPTIONAL_ARG),/* 20 */ michael@0: UOPTION_DEF("includeUnihanColl", '\x01', UOPT_NO_ARG),/* 21 */ /* temporary, don't display in usage info */ michael@0: }; michael@0: michael@0: static UBool write_java = FALSE; michael@0: static UBool write_xliff = FALSE; michael@0: static const char* outputEnc =""; michael@0: static struct SRBRoot *newPoolBundle = NULL; michael@0: UBool gIncludeUnihanColl = FALSE; michael@0: michael@0: /* TODO: separate header file for ResFile? */ michael@0: typedef struct ResFile { michael@0: uint8_t *fBytes; michael@0: const int32_t *fIndexes; michael@0: const char *fKeys; michael@0: int32_t fKeysLength; michael@0: int32_t fKeysCount; michael@0: int32_t fChecksum; michael@0: } ResFile; michael@0: michael@0: static ResFile poolBundle = { NULL }; michael@0: michael@0: /*added by Jing*/ michael@0: static const char* language = NULL; michael@0: static const char* xliffOutputFileName = NULL; michael@0: int michael@0: main(int argc, michael@0: char* argv[]) michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: const char *arg = NULL; michael@0: const char *outputDir = NULL; /* NULL = no output directory, use current */ michael@0: const char *inputDir = NULL; michael@0: const char *encoding = ""; michael@0: int i; michael@0: UBool illegalArg = FALSE; michael@0: michael@0: U_MAIN_INIT_ARGS(argc, argv); michael@0: michael@0: options[JAVA_PACKAGE].value = "com.ibm.icu.impl.data"; michael@0: options[BUNDLE_NAME].value = "LocaleElements"; michael@0: argc = u_parseArgs(argc, argv, (int32_t)(sizeof(options)/sizeof(options[0])), options); michael@0: michael@0: /* error handling, printing usage message */ michael@0: if(argc<0) { michael@0: fprintf(stderr, "%s: error in command line argument \"%s\"\n", argv[0], argv[-argc]); michael@0: } else if(argc<2) { michael@0: argc = -1; michael@0: } michael@0: if(options[WRITE_POOL_BUNDLE].doesOccur && options[USE_POOL_BUNDLE].doesOccur) { michael@0: fprintf(stderr, "%s: cannot combine --writePoolBundle and --usePoolBundle\n", argv[0]); michael@0: argc = -1; michael@0: } michael@0: if(options[FORMAT_VERSION].doesOccur) { michael@0: const char *s = options[FORMAT_VERSION].value; michael@0: if(uprv_strlen(s) != 1 || (s[0] != '1' && s[0] != '2')) { michael@0: fprintf(stderr, "%s: unsupported --formatVersion %s\n", argv[0], s); michael@0: argc = -1; michael@0: } else if(s[0] == '1' && michael@0: (options[WRITE_POOL_BUNDLE].doesOccur || options[USE_POOL_BUNDLE].doesOccur) michael@0: ) { michael@0: fprintf(stderr, "%s: cannot combine --formatVersion 1 with --writePoolBundle or --usePoolBundle\n", argv[0]); michael@0: argc = -1; michael@0: } else { michael@0: setFormatVersion(s[0] - '0'); michael@0: } michael@0: } michael@0: michael@0: if(options[VERSION].doesOccur) { michael@0: fprintf(stderr, michael@0: "%s version %s (ICU version %s).\n" michael@0: "%s\n", michael@0: argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); michael@0: return U_ZERO_ERROR; michael@0: } michael@0: michael@0: if(argc<0) { michael@0: illegalArg = TRUE; michael@0: } else if((options[JAVA_PACKAGE].doesOccur || options[BUNDLE_NAME].doesOccur) && michael@0: !options[WRITE_JAVA].doesOccur) { michael@0: fprintf(stderr, michael@0: "%s error: command line argument --java-package or --bundle-name " michael@0: "without --write-java\n", michael@0: argv[0]); michael@0: illegalArg = TRUE; michael@0: } michael@0: michael@0: if(illegalArg || options[HELP1].doesOccur || options[HELP2].doesOccur) { michael@0: /* michael@0: * Broken into chunks because the C89 standard says the minimum michael@0: * required supported string length is 509 bytes. michael@0: */ michael@0: fprintf(stderr, michael@0: "Usage: %s [OPTIONS] [FILES]\n" michael@0: "\tReads the list of resource bundle source files and creates\n" michael@0: "\tbinary version of resource bundles (.res files)\n", michael@0: argv[0]); michael@0: fprintf(stderr, michael@0: "Options:\n" michael@0: "\t-h or -? or --help this usage text\n" michael@0: "\t-q or --quiet do not display warnings\n" michael@0: "\t-v or --verbose print extra information when processing files\n" michael@0: "\t-V or --version prints out version number and exits\n" michael@0: "\t-c or --copyright include copyright notice\n"); michael@0: fprintf(stderr, michael@0: "\t-e or --encoding encoding of source files\n" michael@0: "\t-d of --destdir destination directory, followed by the path, defaults to %s\n" michael@0: "\t-s or --sourcedir source directory for files followed by path, defaults to %s\n" michael@0: "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" michael@0: "\t followed by path, defaults to %s\n", michael@0: u_getDataDirectory(), u_getDataDirectory(), u_getDataDirectory()); michael@0: fprintf(stderr, michael@0: "\t-j or --write-java write a Java ListResourceBundle for ICU4J, followed by optional encoding\n" michael@0: "\t defaults to ASCII and \\uXXXX format.\n" michael@0: "\t --java-package For --write-java: package name for writing the ListResourceBundle,\n" michael@0: "\t defaults to com.ibm.icu.impl.data\n"); michael@0: fprintf(stderr, michael@0: "\t-b or --bundle-name For --write-java: root resource bundle name for writing the ListResourceBundle,\n" michael@0: "\t defaults to LocaleElements\n" michael@0: "\t-x or --write-xliff write an XLIFF file for the resource bundle. Followed by\n" michael@0: "\t an optional output file name.\n" michael@0: "\t-k or --strict use pedantic parsing of syntax\n" michael@0: /*added by Jing*/ michael@0: "\t-l or --language for XLIFF: language code compliant with BCP 47.\n"); michael@0: fprintf(stderr, michael@0: "\t-C or --noBinaryCollation do not generate binary collation image;\n" michael@0: "\t makes .res file smaller but collator instantiation much slower;\n" michael@0: "\t maintains ability to get tailoring rules\n" michael@0: "\t-R or --omitCollationRules do not include collation (tailoring) rules;\n" michael@0: "\t makes .res file smaller and maintains collator instantiation speed\n" michael@0: "\t but tailoring rules will not be available (they are rarely used)\n"); michael@0: fprintf(stderr, michael@0: "\t --formatVersion write a .res file compatible with the requested formatVersion (single digit);\n" michael@0: "\t for example, --formatVersion 1\n"); michael@0: fprintf(stderr, michael@0: "\t --writePoolBundle write a pool.res file with all of the keys of all input bundles\n" michael@0: "\t --usePoolBundle [path-to-pool.res] point to keys from the pool.res keys pool bundle if they are available there;\n" michael@0: "\t makes .res files smaller but dependent on the pool bundle\n" michael@0: "\t (--writePoolBundle and --usePoolBundle cannot be combined)\n"); michael@0: michael@0: return illegalArg ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; michael@0: } michael@0: michael@0: if(options[VERBOSE].doesOccur) { michael@0: setVerbose(TRUE); michael@0: } michael@0: michael@0: if(options[QUIET].doesOccur) { michael@0: setShowWarning(FALSE); michael@0: } michael@0: if(options[STRICT].doesOccur) { michael@0: setStrict(TRUE); michael@0: } michael@0: if(options[COPYRIGHT].doesOccur){ michael@0: setIncludeCopyright(TRUE); michael@0: } michael@0: michael@0: if(options[SOURCEDIR].doesOccur) { michael@0: inputDir = options[SOURCEDIR].value; michael@0: } michael@0: michael@0: if(options[DESTDIR].doesOccur) { michael@0: outputDir = options[DESTDIR].value; michael@0: } michael@0: michael@0: if(options[ENCODING].doesOccur) { michael@0: encoding = options[ENCODING].value; michael@0: } michael@0: michael@0: if(options[ICUDATADIR].doesOccur) { michael@0: u_setDataDirectory(options[ICUDATADIR].value); michael@0: } michael@0: /* Initialize ICU */ michael@0: u_init(&status); michael@0: if (U_FAILURE(status) && status != U_FILE_ACCESS_ERROR) { michael@0: /* Note: u_init() will try to open ICU property data. michael@0: * failures here are expected when building ICU from scratch. michael@0: * ignore them. michael@0: */ michael@0: fprintf(stderr, "%s: can not initialize ICU. status = %s\n", michael@0: argv[0], u_errorName(status)); michael@0: exit(1); michael@0: } michael@0: status = U_ZERO_ERROR; michael@0: if(options[WRITE_JAVA].doesOccur) { michael@0: write_java = TRUE; michael@0: outputEnc = options[WRITE_JAVA].value; michael@0: } michael@0: michael@0: if(options[WRITE_XLIFF].doesOccur) { michael@0: write_xliff = TRUE; michael@0: if(options[WRITE_XLIFF].value != NULL){ michael@0: xliffOutputFileName = options[WRITE_XLIFF].value; michael@0: } michael@0: } michael@0: michael@0: initParser(); michael@0: michael@0: /*added by Jing*/ michael@0: if(options[LANGUAGE].doesOccur) { michael@0: language = options[LANGUAGE].value; michael@0: } michael@0: michael@0: if(options[WRITE_POOL_BUNDLE].doesOccur) { michael@0: newPoolBundle = bundle_open(NULL, TRUE, &status); michael@0: if(U_FAILURE(status)) { michael@0: fprintf(stderr, "unable to create an empty bundle for the pool keys: %s\n", u_errorName(status)); michael@0: return status; michael@0: } else { michael@0: const char *poolResName = "pool.res"; michael@0: char *nameWithoutSuffix = uprv_malloc(uprv_strlen(poolResName) + 1); michael@0: if (nameWithoutSuffix == NULL) { michael@0: fprintf(stderr, "out of memory error\n"); michael@0: return U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: uprv_strcpy(nameWithoutSuffix, poolResName); michael@0: *uprv_strrchr(nameWithoutSuffix, '.') = 0; michael@0: newPoolBundle->fLocale = nameWithoutSuffix; michael@0: } michael@0: } michael@0: michael@0: if(options[USE_POOL_BUNDLE].doesOccur) { michael@0: const char *poolResName = "pool.res"; michael@0: FileStream *poolFile; michael@0: int32_t poolFileSize; michael@0: int32_t indexLength; michael@0: /* michael@0: * TODO: Consolidate inputDir/filename handling from main() and processFile() michael@0: * into a common function, and use it here as well. michael@0: * Try to create toolutil functions for dealing with dir/filenames and michael@0: * loading ICU data files without udata_open(). michael@0: * Share code with icupkg? michael@0: * Also, make_res_filename() seems to be unused. Review and remove. michael@0: */ michael@0: if (options[USE_POOL_BUNDLE].value!=NULL) { michael@0: uprv_strcpy(theCurrentFileName, options[USE_POOL_BUNDLE].value); michael@0: uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING); michael@0: } else if (inputDir) { michael@0: uprv_strcpy(theCurrentFileName, inputDir); michael@0: uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING); michael@0: } else { michael@0: *theCurrentFileName = 0; michael@0: } michael@0: uprv_strcat(theCurrentFileName, poolResName); michael@0: poolFile = T_FileStream_open(theCurrentFileName, "rb"); michael@0: if (poolFile == NULL) { michael@0: fprintf(stderr, "unable to open pool bundle file %s\n", theCurrentFileName); michael@0: return 1; michael@0: } michael@0: poolFileSize = T_FileStream_size(poolFile); michael@0: if (poolFileSize < 32) { michael@0: fprintf(stderr, "the pool bundle file %s is too small\n", theCurrentFileName); michael@0: return 1; michael@0: } michael@0: poolBundle.fBytes = (uint8_t *)uprv_malloc((poolFileSize + 15) & ~15); michael@0: if (poolFileSize > 0 && poolBundle.fBytes == NULL) { michael@0: fprintf(stderr, "unable to allocate memory for the pool bundle file %s\n", theCurrentFileName); michael@0: return U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: UDataSwapper *ds; michael@0: const DataHeader *header; michael@0: int32_t bytesRead = T_FileStream_read(poolFile, poolBundle.fBytes, poolFileSize); michael@0: int32_t keysBottom; michael@0: if (bytesRead != poolFileSize) { michael@0: fprintf(stderr, "unable to read the pool bundle file %s\n", theCurrentFileName); michael@0: return 1; michael@0: } michael@0: /* michael@0: * Swap the pool bundle so that a single checked-in file can be used. michael@0: * The swapper functions also test that the data looks like michael@0: * a well-formed .res file. michael@0: */ michael@0: ds = udata_openSwapperForInputData(poolBundle.fBytes, bytesRead, michael@0: U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &status); michael@0: if (U_FAILURE(status)) { michael@0: fprintf(stderr, "udata_openSwapperForInputData(pool bundle %s) failed: %s\n", michael@0: theCurrentFileName, u_errorName(status)); michael@0: return status; michael@0: } michael@0: ures_swap(ds, poolBundle.fBytes, bytesRead, poolBundle.fBytes, &status); michael@0: udata_closeSwapper(ds); michael@0: if (U_FAILURE(status)) { michael@0: fprintf(stderr, "ures_swap(pool bundle %s) failed: %s\n", michael@0: theCurrentFileName, u_errorName(status)); michael@0: return status; michael@0: } michael@0: header = (const DataHeader *)poolBundle.fBytes; michael@0: if (header->info.formatVersion[0]!=2) { michael@0: fprintf(stderr, "invalid format of pool bundle file %s\n", theCurrentFileName); michael@0: return U_INVALID_FORMAT_ERROR; michael@0: } michael@0: poolBundle.fKeys = (const char *)header + header->dataHeader.headerSize; michael@0: poolBundle.fIndexes = (const int32_t *)poolBundle.fKeys + 1; michael@0: indexLength = poolBundle.fIndexes[URES_INDEX_LENGTH] & 0xff; michael@0: if (indexLength <= URES_INDEX_POOL_CHECKSUM) { michael@0: fprintf(stderr, "insufficient indexes[] in pool bundle file %s\n", theCurrentFileName); michael@0: return U_INVALID_FORMAT_ERROR; michael@0: } michael@0: keysBottom = (1 + indexLength) * 4; michael@0: poolBundle.fKeys += keysBottom; michael@0: poolBundle.fKeysLength = (poolBundle.fIndexes[URES_INDEX_KEYS_TOP] * 4) - keysBottom; michael@0: poolBundle.fChecksum = poolBundle.fIndexes[URES_INDEX_POOL_CHECKSUM]; michael@0: } michael@0: for (i = 0; i < poolBundle.fKeysLength; ++i) { michael@0: if (poolBundle.fKeys[i] == 0) { michael@0: ++poolBundle.fKeysCount; michael@0: } michael@0: } michael@0: T_FileStream_close(poolFile); michael@0: setUsePoolBundle(TRUE); michael@0: } michael@0: michael@0: if(options[INCLUDE_UNIHAN_COLL].doesOccur) { michael@0: gIncludeUnihanColl = TRUE; michael@0: } michael@0: michael@0: if((argc-1)!=1) { michael@0: printf("genrb number of files: %d\n", argc - 1); michael@0: } michael@0: /* generate the binary files */ michael@0: for(i = 1; i < argc; ++i) { michael@0: status = U_ZERO_ERROR; michael@0: arg = getLongPathname(argv[i]); michael@0: michael@0: if (inputDir) { michael@0: uprv_strcpy(theCurrentFileName, inputDir); michael@0: uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING); michael@0: } else { michael@0: *theCurrentFileName = 0; michael@0: } michael@0: uprv_strcat(theCurrentFileName, arg); michael@0: michael@0: if (isVerbose()) { michael@0: printf("Processing file \"%s\"\n", theCurrentFileName); michael@0: } michael@0: processFile(arg, encoding, inputDir, outputDir, NULL, michael@0: options[NO_BINARY_COLLATION].doesOccur, michael@0: &status); michael@0: } michael@0: michael@0: uprv_free(poolBundle.fBytes); michael@0: michael@0: if(options[WRITE_POOL_BUNDLE].doesOccur) { michael@0: char outputFileName[256]; michael@0: bundle_write(newPoolBundle, outputDir, NULL, outputFileName, sizeof(outputFileName), &status); michael@0: bundle_close(newPoolBundle, &status); michael@0: if(U_FAILURE(status)) { michael@0: fprintf(stderr, "unable to write the pool bundle: %s\n", u_errorName(status)); michael@0: } michael@0: } michael@0: michael@0: u_cleanup(); michael@0: michael@0: /* Dont return warnings as a failure */ michael@0: if (U_SUCCESS(status)) { michael@0: return 0; michael@0: } michael@0: michael@0: return status; michael@0: } michael@0: michael@0: /* Process a file */ michael@0: void michael@0: processFile( michael@0: const char *filename, const char *cp, const char *inputDir, const char *outputDir, const char *packageName, michael@0: UBool omitBinaryCollation, UErrorCode *status) { michael@0: /*FileStream *in = NULL;*/ michael@0: struct SRBRoot *data = NULL; michael@0: UCHARBUF *ucbuf = NULL; michael@0: char *rbname = NULL; michael@0: char *openFileName = NULL; michael@0: char *inputDirBuf = NULL; michael@0: michael@0: char outputFileName[256]; michael@0: michael@0: int32_t dirlen = 0; michael@0: int32_t filelen = 0; michael@0: michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if(filename==NULL){ michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: }else{ michael@0: filelen = (int32_t)uprv_strlen(filename); michael@0: } michael@0: michael@0: if(inputDir == NULL) { michael@0: const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR); michael@0: openFileName = (char *) uprv_malloc(dirlen + filelen + 2); michael@0: openFileName[0] = '\0'; michael@0: if (filenameBegin != NULL) { michael@0: /* michael@0: * When a filename ../../../data/root.txt is specified, michael@0: * we presume that the input directory is ../../../data michael@0: * This is very important when the resource file includes michael@0: * another file, like UCARules.txt or thaidict.brk. michael@0: */ michael@0: int32_t filenameSize = (int32_t)(filenameBegin - filename + 1); michael@0: inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize); michael@0: michael@0: /* test for NULL */ michael@0: if(inputDirBuf == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: inputDirBuf[filenameSize - 1] = 0; michael@0: inputDir = inputDirBuf; michael@0: dirlen = (int32_t)uprv_strlen(inputDir); michael@0: } michael@0: }else{ michael@0: dirlen = (int32_t)uprv_strlen(inputDir); michael@0: michael@0: if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { michael@0: openFileName = (char *) uprv_malloc(dirlen + filelen + 2); michael@0: michael@0: /* test for NULL */ michael@0: if(openFileName == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: openFileName[0] = '\0'; michael@0: /* michael@0: * append the input dir to openFileName if the first char in michael@0: * filename is not file seperation char and the last char input directory is not '.'. michael@0: * This is to support : michael@0: * genrb -s. /home/icu/data michael@0: * genrb -s. icu/data michael@0: * The user cannot mix notations like michael@0: * genrb -s. /icu/data --- the absolute path specified. -s redundant michael@0: * user should use michael@0: * genrb -s. icu/data --- start from CWD and look in icu/data dir michael@0: */ michael@0: if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){ michael@0: uprv_strcpy(openFileName, inputDir); michael@0: openFileName[dirlen] = U_FILE_SEP_CHAR; michael@0: } michael@0: openFileName[dirlen + 1] = '\0'; michael@0: } else { michael@0: openFileName = (char *) uprv_malloc(dirlen + filelen + 1); michael@0: michael@0: /* test for NULL */ michael@0: if(openFileName == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: uprv_strcpy(openFileName, inputDir); michael@0: michael@0: } michael@0: } michael@0: michael@0: uprv_strcat(openFileName, filename); michael@0: michael@0: ucbuf = ucbuf_open(openFileName, &cp,getShowWarning(),TRUE, status); michael@0: if(*status == U_FILE_ACCESS_ERROR) { michael@0: michael@0: fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName); michael@0: goto finish; michael@0: } michael@0: if (ucbuf == NULL || U_FAILURE(*status)) { michael@0: fprintf(stderr, "An error occured processing file %s. Error: %s\n", openFileName == NULL ? filename : openFileName,u_errorName(*status)); michael@0: goto finish; michael@0: } michael@0: /* auto detected popular encodings? */ michael@0: if (cp!=NULL && isVerbose()) { michael@0: printf("autodetected encoding %s\n", cp); michael@0: } michael@0: /* Parse the data into an SRBRoot */ michael@0: data = parse(ucbuf, inputDir, outputDir, michael@0: !omitBinaryCollation, options[NO_COLLATION_RULES].doesOccur, status); michael@0: michael@0: if (data == NULL || U_FAILURE(*status)) { michael@0: fprintf(stderr, "couldn't parse the file %s. Error:%s\n", filename,u_errorName(*status)); michael@0: goto finish; michael@0: } michael@0: if(options[WRITE_POOL_BUNDLE].doesOccur) { michael@0: int32_t newKeysLength; michael@0: const char *newKeys, *newKeysLimit; michael@0: bundle_compactKeys(data, status); michael@0: newKeys = bundle_getKeyBytes(data, &newKeysLength); michael@0: bundle_addKeyBytes(newPoolBundle, newKeys, newKeysLength, status); michael@0: if(U_FAILURE(*status)) { michael@0: fprintf(stderr, "bundle_compactKeys(%s) or bundle_getKeyBytes() failed: %s\n", michael@0: filename, u_errorName(*status)); michael@0: goto finish; michael@0: } michael@0: /* count the number of just-added key strings */ michael@0: for(newKeysLimit = newKeys + newKeysLength; newKeys < newKeysLimit; ++newKeys) { michael@0: if(*newKeys == 0) { michael@0: ++newPoolBundle->fKeysCount; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(options[USE_POOL_BUNDLE].doesOccur) { michael@0: data->fPoolBundleKeys = poolBundle.fKeys; michael@0: data->fPoolBundleKeysLength = poolBundle.fKeysLength; michael@0: data->fPoolBundleKeysCount = poolBundle.fKeysCount; michael@0: data->fPoolChecksum = poolBundle.fChecksum; michael@0: } michael@0: michael@0: /* Determine the target rb filename */ michael@0: rbname = make_res_filename(filename, outputDir, packageName, status); michael@0: if(U_FAILURE(*status)) { michael@0: fprintf(stderr, "couldn't make the res fileName for bundle %s. Error:%s\n", filename,u_errorName(*status)); michael@0: goto finish; michael@0: } michael@0: if(write_java== TRUE){ michael@0: bundle_write_java(data,outputDir,outputEnc, outputFileName, sizeof(outputFileName), michael@0: options[JAVA_PACKAGE].value, options[BUNDLE_NAME].value, status); michael@0: }else if(write_xliff ==TRUE){ michael@0: bundle_write_xml(data,outputDir,outputEnc, filename, outputFileName, sizeof(outputFileName),language, xliffOutputFileName,status); michael@0: }else{ michael@0: /* Write the data to the file */ michael@0: bundle_write(data, outputDir, packageName, outputFileName, sizeof(outputFileName), status); michael@0: } michael@0: if (U_FAILURE(*status)) { michael@0: fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName,u_errorName(*status)); michael@0: } michael@0: bundle_close(data, status); michael@0: michael@0: finish: michael@0: michael@0: if (inputDirBuf != NULL) { michael@0: uprv_free(inputDirBuf); michael@0: } michael@0: michael@0: if (openFileName != NULL) { michael@0: uprv_free(openFileName); michael@0: } michael@0: michael@0: if(ucbuf) { michael@0: ucbuf_close(ucbuf); michael@0: } michael@0: michael@0: if (rbname) { michael@0: uprv_free(rbname); michael@0: } michael@0: } michael@0: michael@0: /* Generate the target .res file name from the input file name */ michael@0: static char* michael@0: make_res_filename(const char *filename, michael@0: const char *outputDir, michael@0: const char *packageName, michael@0: UErrorCode *status) { michael@0: char *basename; michael@0: char *dirname; michael@0: char *resName; michael@0: michael@0: int32_t pkgLen = 0; /* length of package prefix */ michael@0: michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(packageName != NULL) michael@0: { michael@0: pkgLen = (int32_t)(1 + uprv_strlen(packageName)); michael@0: } michael@0: michael@0: /* setup */ michael@0: basename = dirname = resName = 0; michael@0: michael@0: /* determine basename, and compiled file names */ michael@0: basename = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1)); michael@0: if(basename == 0) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: get_basename(basename, filename); michael@0: michael@0: dirname = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1)); michael@0: if(dirname == 0) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: get_dirname(dirname, filename); michael@0: michael@0: if (outputDir == NULL) { michael@0: /* output in same dir as .txt */ michael@0: resName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(dirname) michael@0: + pkgLen michael@0: + uprv_strlen(basename) michael@0: + uprv_strlen(RES_SUFFIX) + 8)); michael@0: if(resName == 0) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: uprv_strcpy(resName, dirname); michael@0: michael@0: if(packageName != NULL) michael@0: { michael@0: uprv_strcat(resName, packageName); michael@0: uprv_strcat(resName, "_"); michael@0: } michael@0: michael@0: uprv_strcat(resName, basename); michael@0: michael@0: } else { michael@0: int32_t dirlen = (int32_t)uprv_strlen(outputDir); michael@0: int32_t basenamelen = (int32_t)uprv_strlen(basename); michael@0: michael@0: resName = (char*) uprv_malloc(sizeof(char) * (dirlen + pkgLen + basenamelen + 8)); michael@0: michael@0: if (resName == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finish; michael@0: } michael@0: michael@0: uprv_strcpy(resName, outputDir); michael@0: michael@0: if(outputDir[dirlen] != U_FILE_SEP_CHAR) { michael@0: resName[dirlen] = U_FILE_SEP_CHAR; michael@0: resName[dirlen + 1] = '\0'; michael@0: } michael@0: michael@0: if(packageName != NULL) michael@0: { michael@0: uprv_strcat(resName, packageName); michael@0: uprv_strcat(resName, "_"); michael@0: } michael@0: michael@0: uprv_strcat(resName, basename); michael@0: } michael@0: michael@0: finish: michael@0: uprv_free(basename); michael@0: uprv_free(dirname); michael@0: michael@0: return resName; michael@0: } michael@0: michael@0: /* michael@0: * Local Variables: michael@0: * indent-tabs-mode: nil michael@0: * End: michael@0: */