2 **********************************************************************
3 * Copyright (C) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 //--------------------------------------------------------------------
12 // Tool for generating RuleBasedBreakIterator data files (.brk files).
13 // .brk files contain the precompiled rules for standard types
14 // of iterators - word, line, sentence, etc.
16 // Usage: genbrk [options] -r rule-file.txt -o output-file.brk
18 // options: -v verbose
21 // The input rule file is a plain text file containing break rules
22 // in the input format accepted by RuleBasedBreakIterators. The
23 // file can be encoded as utf-8, or utf-16 (either endian), or
24 // in the default code page (platform dependent.). utf encoded
25 // files must include a BOM.
27 //--------------------------------------------------------------------
29 #include "unicode/utypes.h"
30 #include "unicode/ucnv.h"
31 #include "unicode/unistr.h"
32 #include "unicode/rbbi.h"
33 #include "unicode/uclean.h"
34 #include "unicode/udata.h"
35 #include "unicode/putil.h"
45 #define DATA_TYPE "brk"
47 static char *progName
;
48 static UOption options
[]={
49 UOPTION_HELP_H
, /* 0 */
50 UOPTION_HELP_QUESTION_MARK
, /* 1 */
51 UOPTION_VERBOSE
, /* 2 */
52 { "rules", NULL
, NULL
, NULL
, 'r', UOPT_REQUIRES_ARG
, 0 }, /* 3 */
53 { "out", NULL
, NULL
, NULL
, 'o', UOPT_REQUIRES_ARG
, 0 }, /* 4 */
54 UOPTION_ICUDATADIR
, /* 5 */
55 UOPTION_DESTDIR
, /* 6 */
56 UOPTION_COPYRIGHT
, /* 7 */
59 void usageAndDie(int retCode
) {
60 printf("Usage: %s [-v] [-options] -r rule-file -o output-file\n", progName
);
61 printf("\tRead in break iteration rules text and write out the binary data\n"
63 "\t-h or -? or --help this usage text\n"
64 "\t-V or --version show a version message\n"
65 "\t-c or --copyright include a copyright notice\n"
66 "\t-v or --verbose turn on verbose output\n"
67 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
68 "\t followed by path, defaults to %s\n"
69 "\t-d or --destdir destination directory, followed by the path\n",
70 u_getDataDirectory());
75 #if UCONFIG_NO_BREAK_ITERATION
77 /* dummy UDataInfo cf. udata.h */
78 static UDataInfo dummyDataInfo
= {
87 { 0, 0, 0, 0 }, /* dummy dataFormat */
88 { 0, 0, 0, 0 }, /* dummy formatVersion */
89 { 0, 0, 0, 0 } /* dummy dataVersion */
95 // Set up the ICU data header, defined in ucmndata.h
98 {sizeof(DataHeader
), // Struct MappedData
102 { // struct UDataInfo
103 sizeof(UDataInfo
), // size
110 { 0x42, 0x72, 0x6b, 0x20 }, // dataFormat="Brk "
111 { 3, 0, 0, 0 }, // formatVersion
112 { 4, 0, 0, 0 } // dataVersion (Unicode version)
117 //----------------------------------------------------------------------------
121 //----------------------------------------------------------------------------
122 int main(int argc
, char **argv
) {
123 UErrorCode status
= U_ZERO_ERROR
;
124 const char *ruleFileName
;
125 const char *outFileName
;
126 const char *outDir
= NULL
;
127 const char *copyright
= NULL
;
130 // Pick up and check the command line arguments,
131 // using the standard ICU tool utils option handling.
133 U_MAIN_INIT_ARGS(argc
, argv
);
135 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
137 // Unrecognized option
138 fprintf(stderr
, "error in command line argument \"%s\"\n", argv
[-argc
]);
139 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
142 if(options
[0].doesOccur
|| options
[1].doesOccur
) {
143 // -? or -h for help.
147 if (!(options
[3].doesOccur
&& options
[4].doesOccur
)) {
148 fprintf(stderr
, "rule file and output file must both be specified.\n");
149 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
151 ruleFileName
= options
[3].value
;
152 outFileName
= options
[4].value
;
154 if (options
[5].doesOccur
) {
155 u_setDataDirectory(options
[5].value
);
160 if (U_FAILURE(status
)) {
161 fprintf(stderr
, "%s: can not initialize ICU. status = %s\n",
162 argv
[0], u_errorName(status
));
165 status
= U_ZERO_ERROR
;
167 /* Combine the directory with the file name */
168 if(options
[6].doesOccur
) {
169 outDir
= options
[6].value
;
171 if (options
[7].doesOccur
) {
172 copyright
= U_COPYRIGHT_STRING
;
175 #if UCONFIG_NO_BREAK_ITERATION
177 UNewDataMemory
*pData
;
178 char msg
[2048], folder
[2048], name
[32];
182 /* split the outFileName into folder + name + type */
183 strcpy(folder
, outFileName
);
184 basename
= strrchr(folder
, U_FILE_SEP_CHAR
);
185 if(basename
== NULL
) {
191 /* copy the data name and remove it from the folder */
192 strcpy(name
, basename
);
195 /* write message with just the name */
196 sprintf(msg
, "genbrk writes dummy %s because of UCONFIG_NO_BREAK_ITERATION, see uconfig.h", name
);
197 fprintf(stderr
, "%s\n", msg
);
199 /* remove the type suffix (hardcode to DATA_TYPE) */
200 length
= strlen(name
);
201 if(length
> 4 && name
[length
- 4] == '.') {
202 name
[length
- 4] = 0;
205 /* write the dummy data file */
206 pData
= udata_create(folder
, DATA_TYPE
, name
, &dummyDataInfo
, NULL
, &status
);
207 udata_writeBlock(pData
, msg
, strlen(msg
));
208 udata_finish(pData
, &status
);
214 // Read in the rule source file
221 file
= fopen(ruleFileName
, "rb");
223 fprintf(stderr
, "Could not open file \"%s\"\n", ruleFileName
);
226 fseek(file
, 0, SEEK_END
);
227 ruleFileSize
= ftell(file
);
228 fseek(file
, 0, SEEK_SET
);
229 ruleBufferC
= new char[ruleFileSize
+10];
231 result
= (long)fread(ruleBufferC
, 1, ruleFileSize
, file
);
232 if (result
!= ruleFileSize
) {
233 fprintf(stderr
, "Error reading file \"%s\"\n", ruleFileName
);
236 ruleBufferC
[ruleFileSize
]=0;
240 // Look for a Unicode Signature (BOM) on the rule file
242 int32_t signatureLength
;
243 const char * ruleSourceC
= ruleBufferC
;
244 const char* encoding
= ucnv_detectUnicodeSignature(
245 ruleSourceC
, ruleFileSize
, &signatureLength
, &status
);
246 if (U_FAILURE(status
)) {
250 ruleSourceC
+= signatureLength
;
251 ruleFileSize
-= signatureLength
;
255 // Open a converter to take the rule file to UTF-16
258 conv
= ucnv_open(encoding
, &status
);
259 if (U_FAILURE(status
)) {
260 fprintf(stderr
, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status
));
265 // Convert the rules to UChar.
266 // Preflight first to determine required buffer size.
268 uint32_t destCap
= ucnv_toUChars(conv
,
274 if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
275 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
279 status
= U_ZERO_ERROR
;
280 UChar
*ruleSourceU
= new UChar
[destCap
+1];
282 ruleSourceU
, // dest,
287 if (U_FAILURE(status
)) {
288 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
295 // Put the source rules into a UnicodeString
297 UnicodeString
ruleSourceS(FALSE
, ruleSourceU
, destCap
);
300 // Create the break iterator from the rules
301 // This will compile the rules.
303 UParseError parseError
;
305 parseError
.offset
= 0;
306 RuleBasedBreakIterator
*bi
= new RuleBasedBreakIterator(ruleSourceS
, parseError
, status
);
307 if (U_FAILURE(status
)) {
308 fprintf(stderr
, "createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n",
309 u_errorName(status
), (int)parseError
.line
, (int)parseError
.offset
);
315 // Get the compiled rule data from the break iterator.
317 uint32_t outDataSize
;
318 const uint8_t *outData
;
319 outData
= bi
->getBinaryRules(outDataSize
);
323 // Create the output file
326 UNewDataMemory
*pData
;
327 pData
= udata_create(outDir
, NULL
, outFileName
, &(dh
.info
), copyright
, &status
);
328 if(U_FAILURE(status
)) {
329 fprintf(stderr
, "genbrk: Could not open output file \"%s\", \"%s\"\n",
330 outFileName
, u_errorName(status
));
333 // Write the data itself.
334 udata_writeBlock(pData
, outData
, outDataSize
);
336 bytesWritten
= udata_finish(pData
, &status
);
337 if(U_FAILURE(status
)) {
338 fprintf(stderr
, "genbrk: error %d writing the output file\n", status
);
342 if (bytesWritten
!= outDataSize
) {
343 fprintf(stderr
, "Error writing to output file \"%s\"\n", outFileName
);
348 delete[] ruleSourceU
;
349 delete[] ruleBufferC
;
353 printf("genbrk: tool completed successfully.\n");
356 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */