X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..f59164e3d128c7675a4d3934206346a3384e53a5:/icuSources/tools/genbrk/genbrk.cpp diff --git a/icuSources/tools/genbrk/genbrk.cpp b/icuSources/tools/genbrk/genbrk.cpp index d9942f03..290b2e7d 100644 --- a/icuSources/tools/genbrk/genbrk.cpp +++ b/icuSources/tools/genbrk/genbrk.cpp @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 2002-2003, International Business Machines +* Copyright (C) 2002-2016, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * @@ -26,23 +26,25 @@ // //-------------------------------------------------------------------- -#include #include "unicode/utypes.h" #include "unicode/ucnv.h" #include "unicode/unistr.h" #include "unicode/rbbi.h" #include "unicode/uclean.h" #include "unicode/udata.h" +#include "unicode/putil.h" #include "uoptions.h" #include "unewdata.h" #include "ucmndata.h" +#include "rbbidata.h" +#include "cmemory.h" #include #include #include -#define DATA_TYPE "brk" +U_NAMESPACE_USE static char *progName; static UOption options[]={ @@ -52,16 +54,29 @@ static UOption options[]={ { "rules", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0 }, /* 3 */ { "out", NULL, NULL, NULL, 'o', UOPT_REQUIRES_ARG, 0 }, /* 4 */ UOPTION_ICUDATADIR, /* 5 */ - UOPTION_DESTDIR /* 6 */ + UOPTION_DESTDIR, /* 6 */ + UOPTION_COPYRIGHT, /* 7 */ + UOPTION_QUIET, /* 8 */ }; void usageAndDie(int retCode) { - printf("Usage: %s [-v] -r rule-file -o output-file\n", progName); + printf("Usage: %s [-v] [-options] -r rule-file -o output-file\n", progName); + printf("\tRead in break iteration rules text and write out the binary data\n" + "options:\n" + "\t-h or -? or --help this usage text\n" + "\t-V or --version show a version message\n" + "\t-c or --copyright include a copyright notice\n" + "\t-v or --verbose turn on verbose output\n" + "\t-q or --quiet do not display warnings and progress\n" + "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" + "\t followed by path, defaults to %s\n" + "\t-d or --destdir destination directory, followed by the path\n", + u_getDataDirectory()); exit (retCode); } -#if UCONFIG_NO_BREAK_ITERATION +#if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO /* dummy UDataInfo cf. udata.h */ static UDataInfo dummyDataInfo = { @@ -97,8 +112,10 @@ DataHeader dh ={ 0, // reserved { 0x42, 0x72, 0x6b, 0x20 }, // dataFormat="Brk " - { 2, 1, 0, 0 }, // formatVersion - { 3, 1, 0, 0 } // dataVersion (Unicode version) + { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values + // from the RBBI rule builder. The values declared + // here should never appear in any real RBBI data. + { 4, 1, 0, 0 } // dataVersion (Unicode version) }}; #endif @@ -113,8 +130,7 @@ int main(int argc, char **argv) { const char *ruleFileName; const char *outFileName; const char *outDir = NULL; - char *outFullFileName; - int32_t outFullFileNameLen; + const char *copyright = NULL; // // Pick up and check the command line arguments, @@ -122,7 +138,7 @@ int main(int argc, char **argv) { // U_MAIN_INIT_ARGS(argc, argv); progName = argv[0]; - argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); + argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options); if(argc<0) { // Unrecognized option fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]); @@ -140,67 +156,50 @@ int main(int argc, char **argv) { } ruleFileName = options[3].value; outFileName = options[4].value; - outFullFileNameLen = strlen(outFileName); if (options[5].doesOccur) { u_setDataDirectory(options[5].value); } + status = U_ZERO_ERROR; + /* Combine the directory with the file name */ if(options[6].doesOccur) { outDir = options[6].value; - outFullFileNameLen += strlen(outDir); } - outFullFileName = (char*)malloc(outFullFileNameLen + 2); - outFullFileName[0] = 0; - if (outDir) { - strcpy(outFullFileName, outDir); - strcat(outFullFileName, U_FILE_SEP_STRING); + if (options[7].doesOccur) { + copyright = U_COPYRIGHT_STRING; } - strcat(outFullFileName, outFileName); -#if UCONFIG_NO_BREAK_ITERATION +#if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO UNewDataMemory *pData; - char msg[2048], folder[2048], name[32]; - char *basename; - int length; - - /* split the outFileName into folder + name + type */ - strcpy(folder, outFileName); - basename = strrchr(folder, U_FILE_SEP_CHAR); - if(basename == NULL) { - basename = folder; - } else { - ++basename; - } - - /* copy the data name and remove it from the folder */ - strcpy(name, basename); - *basename = 0; + char msg[1024]; /* write message with just the name */ - sprintf(msg, "genbrk writes dummy %s because of UCONFIG_NO_BREAK_ITERATION, see uconfig.h", name); + sprintf(msg, "genbrk writes dummy %s because of UCONFIG_NO_BREAK_ITERATION and/or UCONFIG_NO_FILE_IO, see uconfig.h", outFileName); fprintf(stderr, "%s\n", msg); - /* remove the type suffix (hardcode to DATA_TYPE) */ - length = strlen(name); - if(length > 4 && name[length - 4] == '.') { - name[length - 4] = 0; - } - /* write the dummy data file */ - pData = udata_create(folder, DATA_TYPE, name, &dummyDataInfo, NULL, &status); + pData = udata_create(outDir, NULL, outFileName, &dummyDataInfo, NULL, &status); udata_writeBlock(pData, msg, strlen(msg)); udata_finish(pData, &status); return (int)status; #else + /* Initialize ICU */ + u_init(&status); + if (U_FAILURE(status)) { + fprintf(stderr, "%s: can not initialize ICU. status = %s\n", + argv[0], u_errorName(status)); + exit(1); + } + status = U_ZERO_ERROR; // // Read in the rule source file // - int result; + long result; long ruleFileSize; FILE *file; char *ruleBufferC; @@ -215,7 +214,7 @@ int main(int argc, char **argv) { fseek(file, 0, SEEK_SET); ruleBufferC = new char[ruleFileSize+10]; - result = fread(ruleBufferC, 1, ruleFileSize, file); + result = (long)fread(ruleBufferC, 1, ruleFileSize, file); if (result != ruleFileSize) { fprintf(stderr, "Error reading file \"%s\"\n", ruleFileName); exit (-1); @@ -288,12 +287,12 @@ int main(int argc, char **argv) { // This will compile the rules. // UParseError parseError; - parseError.line = 0; - parseError.offset = 0; + parseError.line = 0; + parseError.offset = 0; RuleBasedBreakIterator *bi = new RuleBasedBreakIterator(ruleSourceS, parseError, status); if (U_FAILURE(status)) { fprintf(stderr, "createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n", - u_errorName(status), parseError.line, parseError.offset); + u_errorName(status), (int)parseError.line, (int)parseError.offset); exit(status); }; @@ -305,38 +304,47 @@ int main(int argc, char **argv) { const uint8_t *outData; outData = bi->getBinaryRules(outDataSize); + // Copy the data format version numbers from the RBBI data header into the UDataMemory header. + uprv_memcpy(dh.info.formatVersion, ((RBBIDataHeader *)outData)->fFormatVersion, sizeof(dh.info.formatVersion)); // // Create the output file // size_t bytesWritten; - file = fopen(outFullFileName, "wb"); - if (file == 0) { - fprintf(stderr, "Could not open output file \"%s\"\n", outFullFileName); - exit(-1); + UNewDataMemory *pData; + pData = udata_create(outDir, NULL, outFileName, &(dh.info), copyright, &status); + if(U_FAILURE(status)) { + fprintf(stderr, "genbrk: Could not open output file \"%s\", \"%s\"\n", + outFileName, u_errorName(status)); + exit(status); } - bytesWritten = fwrite(&dh, 1, sizeof(DataHeader), file); - // // Write the data itself. - // - bytesWritten = fwrite(outData, 1, outDataSize, file); + udata_writeBlock(pData, outData, outDataSize); + // finish up + bytesWritten = udata_finish(pData, &status); + if(U_FAILURE(status)) { + fprintf(stderr, "genbrk: error %d writing the output file\n", status); + exit(status); + } + if (bytesWritten != outDataSize) { - fprintf(stderr, "Error writing to output file \"%s\"\n", outFullFileName); + fprintf(stderr, "Error writing to output file \"%s\"\n", outFileName); exit(-1); } - fclose(file); delete bi; delete[] ruleSourceU; delete[] ruleBufferC; - free(outFullFileName); u_cleanup(); - printf("genbrk: tool completed successfully.\n"); + if(!options[8].doesOccur) { + printf("genbrk: tool completed successfully.\n"); + } return 0; #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ } +