2002-06-25 17:23:07 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2004-05-17 06:33:22 +00:00
|
|
|
* Copyright (C) 2002-2004, International Business Machines
|
2002-06-25 17:23:07 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
*
|
|
|
|
* File genbrk.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Tool for generating RuleBasedBreakIterator data files (.brk files).
|
|
|
|
// .brk files contain the precompiled rules for standard types
|
|
|
|
// of iterators - word, line, sentence, etc.
|
|
|
|
//
|
|
|
|
// Usage: genbrk [options] -r rule-file.txt -o output-file.brk
|
|
|
|
//
|
|
|
|
// options: -v verbose
|
|
|
|
// -? or -h help
|
|
|
|
//
|
|
|
|
// The input rule file is a plain text file containing break rules
|
|
|
|
// in the input format accepted by RuleBasedBreakIterators. The
|
|
|
|
// file can be encoded as utf-8, or utf-16 (either endian), or
|
|
|
|
// in the default code page (platform dependent.). utf encoded
|
|
|
|
// files must include a BOM.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#include "unicode/utypes.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/rbbi.h"
|
|
|
|
#include "unicode/uclean.h"
|
|
|
|
#include "unicode/udata.h"
|
2004-11-10 03:47:56 +00:00
|
|
|
#include "unicode/putil.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
#include "uoptions.h"
|
2002-10-01 17:44:04 +00:00
|
|
|
#include "unewdata.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "ucmndata.h"
|
|
|
|
|
2002-07-16 02:34:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-10-01 17:44:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DATA_TYPE "brk"
|
2002-07-16 02:34:19 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
static char *progName;
|
|
|
|
static UOption options[]={
|
2002-08-09 20:55:35 +00:00
|
|
|
UOPTION_HELP_H, /* 0 */
|
|
|
|
UOPTION_HELP_QUESTION_MARK, /* 1 */
|
|
|
|
UOPTION_VERBOSE, /* 2 */
|
|
|
|
{ "rules", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0 }, /* 3 */
|
|
|
|
{ "out", NULL, NULL, NULL, 'o', UOPT_REQUIRES_ARG, 0 }, /* 4 */
|
2003-05-05 16:52:15 +00:00
|
|
|
UOPTION_ICUDATADIR, /* 5 */
|
2003-09-03 21:12:50 +00:00
|
|
|
UOPTION_DESTDIR, /* 6 */
|
|
|
|
UOPTION_COPYRIGHT, /* 7 */
|
2002-06-25 17:23:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void usageAndDie(int retCode) {
|
2003-12-02 02:31:40 +00:00
|
|
|
printf("Usage: %s [-v] [-options] -r rule-file -o output-file\n", progName);
|
2003-09-03 21:12:50 +00:00
|
|
|
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-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());
|
2002-06-25 17:23:07 +00:00
|
|
|
exit (retCode);
|
|
|
|
}
|
|
|
|
|
2002-10-01 17:44:04 +00:00
|
|
|
|
|
|
|
#if UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
|
|
|
/* dummy UDataInfo cf. udata.h */
|
|
|
|
static UDataInfo dummyDataInfo = {
|
|
|
|
sizeof(UDataInfo),
|
|
|
|
0,
|
|
|
|
|
|
|
|
U_IS_BIG_ENDIAN,
|
|
|
|
U_CHARSET_FAMILY,
|
|
|
|
U_SIZEOF_UCHAR,
|
|
|
|
0,
|
|
|
|
|
|
|
|
{ 0, 0, 0, 0 }, /* dummy dataFormat */
|
|
|
|
{ 0, 0, 0, 0 }, /* dummy formatVersion */
|
|
|
|
{ 0, 0, 0, 0 } /* dummy dataVersion */
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set up the ICU data header, defined in ucmndata.h
|
|
|
|
//
|
|
|
|
DataHeader dh ={
|
|
|
|
{sizeof(DataHeader), // Struct MappedData
|
|
|
|
0xda,
|
|
|
|
0x27},
|
|
|
|
|
|
|
|
{ // struct UDataInfo
|
|
|
|
sizeof(UDataInfo), // size
|
|
|
|
0, // reserved
|
|
|
|
U_IS_BIG_ENDIAN,
|
|
|
|
U_CHARSET_FAMILY,
|
|
|
|
U_SIZEOF_UCHAR,
|
|
|
|
0, // reserved
|
|
|
|
|
|
|
|
{ 0x42, 0x72, 0x6b, 0x20 }, // dataFormat="Brk "
|
2003-11-07 22:49:38 +00:00
|
|
|
{ 3, 0, 0, 0 }, // formatVersion
|
|
|
|
{ 4, 0, 0, 0 } // dataVersion (Unicode version)
|
2002-10-01 17:44:04 +00:00
|
|
|
}};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// main for genbrk
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const char *ruleFileName;
|
|
|
|
const char *outFileName;
|
2003-05-05 16:52:15 +00:00
|
|
|
const char *outDir = NULL;
|
2003-09-03 21:12:50 +00:00
|
|
|
const char *copyright = NULL;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Pick up and check the command line arguments,
|
|
|
|
// using the standard ICU tool utils option handling.
|
|
|
|
//
|
|
|
|
U_MAIN_INIT_ARGS(argc, argv);
|
2002-07-16 02:34:19 +00:00
|
|
|
progName = argv[0];
|
2002-06-25 17:23:07 +00:00
|
|
|
argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
|
|
|
|
if(argc<0) {
|
|
|
|
// Unrecognized option
|
|
|
|
fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]);
|
|
|
|
usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options[0].doesOccur || options[1].doesOccur) {
|
|
|
|
// -? or -h for help.
|
|
|
|
usageAndDie(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(options[3].doesOccur && options[4].doesOccur)) {
|
|
|
|
fprintf(stderr, "rule file and output file must both be specified.\n");
|
|
|
|
usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
}
|
|
|
|
ruleFileName = options[3].value;
|
|
|
|
outFileName = options[4].value;
|
|
|
|
|
2002-08-09 20:55:35 +00:00
|
|
|
if (options[5].doesOccur) {
|
|
|
|
u_setDataDirectory(options[5].value);
|
|
|
|
}
|
|
|
|
|
2003-08-15 01:26:22 +00:00
|
|
|
/* 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;
|
|
|
|
|
2003-05-05 16:52:15 +00:00
|
|
|
/* Combine the directory with the file name */
|
|
|
|
if(options[6].doesOccur) {
|
|
|
|
outDir = options[6].value;
|
|
|
|
}
|
2003-09-03 21:12:50 +00:00
|
|
|
if (options[7].doesOccur) {
|
|
|
|
copyright = U_COPYRIGHT_STRING;
|
2003-05-05 16:52:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 17:44:04 +00:00
|
|
|
#if UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* write message with just the name */
|
|
|
|
sprintf(msg, "genbrk writes dummy %s because of UCONFIG_NO_BREAK_ITERATION, see uconfig.h", name);
|
|
|
|
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);
|
|
|
|
udata_writeBlock(pData, msg, strlen(msg));
|
|
|
|
udata_finish(pData, &status);
|
|
|
|
return (int)status;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// Read in the rule source file
|
|
|
|
//
|
2003-12-11 05:00:40 +00:00
|
|
|
long result;
|
2002-06-25 17:23:07 +00:00
|
|
|
long ruleFileSize;
|
|
|
|
FILE *file;
|
|
|
|
char *ruleBufferC;
|
|
|
|
|
|
|
|
file = fopen(ruleFileName, "rb");
|
|
|
|
if( file == 0 ) {
|
|
|
|
fprintf(stderr, "Could not open file \"%s\"\n", ruleFileName);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
ruleFileSize = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
ruleBufferC = new char[ruleFileSize+10];
|
|
|
|
|
2003-12-11 05:00:40 +00:00
|
|
|
result = (long)fread(ruleBufferC, 1, ruleFileSize, file);
|
2002-06-25 17:23:07 +00:00
|
|
|
if (result != ruleFileSize) {
|
|
|
|
fprintf(stderr, "Error reading file \"%s\"\n", ruleFileName);
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
ruleBufferC[ruleFileSize]=0;
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Look for a Unicode Signature (BOM) on the rule file
|
|
|
|
//
|
|
|
|
int32_t signatureLength;
|
|
|
|
const char * ruleSourceC = ruleBufferC;
|
|
|
|
const char* encoding = ucnv_detectUnicodeSignature(
|
|
|
|
ruleSourceC, ruleFileSize, &signatureLength, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
if(encoding!=NULL ){
|
|
|
|
ruleSourceC += signatureLength;
|
|
|
|
ruleFileSize -= signatureLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Open a converter to take the rule file to UTF-16
|
|
|
|
//
|
|
|
|
UConverter* conv;
|
|
|
|
conv = ucnv_open(encoding, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
fprintf(stderr, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status));
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Convert the rules to UChar.
|
|
|
|
// Preflight first to determine required buffer size.
|
|
|
|
//
|
|
|
|
uint32_t destCap = ucnv_toUChars(conv,
|
|
|
|
NULL, // dest,
|
|
|
|
0, // destCapacity,
|
|
|
|
ruleSourceC,
|
|
|
|
ruleFileSize,
|
|
|
|
&status);
|
|
|
|
if (status != U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
fprintf(stderr, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status));
|
|
|
|
exit(status);
|
|
|
|
};
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
UChar *ruleSourceU = new UChar[destCap+1];
|
|
|
|
ucnv_toUChars(conv,
|
|
|
|
ruleSourceU, // dest,
|
|
|
|
destCap+1,
|
|
|
|
ruleSourceC,
|
|
|
|
ruleFileSize,
|
|
|
|
&status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
fprintf(stderr, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status));
|
|
|
|
exit(status);
|
|
|
|
};
|
|
|
|
ucnv_close(conv);
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Put the source rules into a UnicodeString
|
|
|
|
//
|
|
|
|
UnicodeString ruleSourceS(FALSE, ruleSourceU, destCap);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create the break iterator from the rules
|
|
|
|
// This will compile the rules.
|
|
|
|
//
|
|
|
|
UParseError parseError;
|
2004-11-11 23:34:58 +00:00
|
|
|
parseError.line = 0;
|
|
|
|
parseError.offset = 0;
|
2002-06-25 17:23:07 +00:00
|
|
|
RuleBasedBreakIterator *bi = new RuleBasedBreakIterator(ruleSourceS, parseError, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
fprintf(stderr, "createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n",
|
2004-05-17 06:33:22 +00:00
|
|
|
u_errorName(status), (int)parseError.line, (int)parseError.offset);
|
2002-06-25 17:23:07 +00:00
|
|
|
exit(status);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the compiled rule data from the break iterator.
|
|
|
|
//
|
|
|
|
uint32_t outDataSize;
|
|
|
|
const uint8_t *outData;
|
2002-06-27 01:50:22 +00:00
|
|
|
outData = bi->getBinaryRules(outDataSize);
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create the output file
|
|
|
|
//
|
|
|
|
size_t bytesWritten;
|
2003-09-03 21:12:50 +00:00
|
|
|
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);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
// Write the data itself.
|
2003-09-03 21:12:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
if (bytesWritten != outDataSize) {
|
2003-09-03 21:12:50 +00:00
|
|
|
fprintf(stderr, "Error writing to output file \"%s\"\n", outFileName);
|
2002-06-25 17:23:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete bi;
|
2002-12-06 07:21:53 +00:00
|
|
|
delete[] ruleSourceU;
|
|
|
|
delete[] ruleBufferC;
|
2002-06-25 17:23:07 +00:00
|
|
|
u_cleanup();
|
|
|
|
|
|
|
|
|
|
|
|
printf("genbrk: tool completed successfully.\n");
|
|
|
|
return 0;
|
2002-10-01 01:26:49 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
|
2003-09-03 15:25:40 +00:00
|
|
|
}
|
|
|
|
|