2002-06-25 17:23:07 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 2002-2003, 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 <stdio.h>
|
|
|
|
#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"
|
|
|
|
|
|
|
|
#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-08-30 19:45:45 +00:00
|
|
|
UOPTION_DESTDIR /* 6 */
|
2002-06-25 17:23:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void usageAndDie(int retCode) {
|
2003-08-30 19:45:45 +00:00
|
|
|
printf("Usage: %s [-v] -r rule-file -o output-file\n", progName);
|
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 "
|
|
|
|
{ 2, 1, 0, 0 }, // formatVersion
|
|
|
|
{ 3, 1, 0, 0 } // dataVersion (Unicode version)
|
|
|
|
}};
|
|
|
|
|
|
|
|
#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-08-30 19:45:45 +00:00
|
|
|
char *outFullFileName;
|
|
|
|
int32_t outFullFileNameLen;
|
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;
|
2003-08-30 19:45:45 +00:00
|
|
|
outFullFileNameLen = strlen(outFileName);
|
2002-06-25 17:23:07 +00:00
|
|
|
|
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-08-30 19:45:45 +00:00
|
|
|
outFullFileNameLen += strlen(outDir);
|
2003-05-05 16:52:15 +00:00
|
|
|
}
|
2003-08-30 19:45:45 +00:00
|
|
|
outFullFileName = (char*)malloc(outFullFileNameLen + 2);
|
|
|
|
outFullFileName[0] = 0;
|
|
|
|
if (outDir) {
|
|
|
|
strcpy(outFullFileName, outDir);
|
|
|
|
strcat(outFullFileName, U_FILE_SEP_STRING);
|
2003-05-05 16:52:15 +00:00
|
|
|
}
|
2003-08-30 19:45:45 +00:00
|
|
|
strcat(outFullFileName, outFileName);
|
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
|
|
|
|
//
|
|
|
|
int result;
|
|
|
|
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];
|
|
|
|
|
|
|
|
result = fread(ruleBufferC, 1, ruleFileSize, file);
|
|
|
|
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;
|
2002-07-17 03:56:50 +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",
|
|
|
|
u_errorName(status), parseError.line, parseError.offset);
|
|
|
|
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-08-30 19:45:45 +00:00
|
|
|
file = fopen(outFullFileName, "wb");
|
|
|
|
if (file == 0) {
|
|
|
|
fprintf(stderr, "Could not open output file \"%s\"\n", outFullFileName);
|
|
|
|
exit(-1);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2003-08-30 19:45:45 +00:00
|
|
|
|
|
|
|
bytesWritten = fwrite(&dh, 1, sizeof(DataHeader), file);
|
|
|
|
|
|
|
|
//
|
2002-06-25 17:23:07 +00:00
|
|
|
// Write the data itself.
|
2003-08-30 19:45:45 +00:00
|
|
|
//
|
|
|
|
bytesWritten = fwrite(outData, 1, outDataSize, file);
|
2002-06-25 17:23:07 +00:00
|
|
|
if (bytesWritten != outDataSize) {
|
2003-08-30 19:45:45 +00:00
|
|
|
fprintf(stderr, "Error writing to output file \"%s\"\n", outFullFileName);
|
2002-06-25 17:23:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2003-08-30 19:45:45 +00:00
|
|
|
fclose(file);
|
2002-06-25 17:23:07 +00:00
|
|
|
delete bi;
|
2002-12-06 07:21:53 +00:00
|
|
|
delete[] ruleSourceU;
|
|
|
|
delete[] ruleBufferC;
|
2003-08-30 19:45:45 +00:00
|
|
|
free(outFullFileName);
|
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
|
|
|
}
|
|
|
|
|