1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2002-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
12 //--------------------------------------------------------------------
14 // Tool for generating RuleBasedBreakIterator data files (.brk files).
15 // .brk files contain the precompiled rules for standard types
16 // of iterators - word, line, sentence, etc.
18 // Usage: genbrk [options] -r rule-file.txt -o output-file.brk
20 // options: -v verbose
23 // The input rule file is a plain text file containing break rules
24 // in the input format accepted by RuleBasedBreakIterators. The
25 // file can be encoded as utf-8, or utf-16 (either endian), or
26 // in the default code page (platform dependent.). utf encoded
27 // files must include a BOM.
29 //--------------------------------------------------------------------
31 #include "unicode/utypes.h"
32 #include "unicode/ucnv.h"
33 #include "unicode/unistr.h"
34 #include "unicode/rbbi.h"
35 #include "unicode/uclean.h"
36 #include "unicode/udata.h"
37 #include "unicode/putil.h"
51 static char *progName
;
52 static UOption options
[]={
53 UOPTION_HELP_H
, /* 0 */
54 UOPTION_HELP_QUESTION_MARK
, /* 1 */
55 UOPTION_VERBOSE
, /* 2 */
56 { "rules", NULL
, NULL
, NULL
, 'r', UOPT_REQUIRES_ARG
, 0 }, /* 3 */
57 { "out", NULL
, NULL
, NULL
, 'o', UOPT_REQUIRES_ARG
, 0 }, /* 4 */
58 UOPTION_ICUDATADIR
, /* 5 */
59 UOPTION_DESTDIR
, /* 6 */
60 UOPTION_COPYRIGHT
, /* 7 */
61 UOPTION_QUIET
, /* 8 */
64 void usageAndDie(int retCode
) {
65 printf("Usage: %s [-v] [-options] -r rule-file -o output-file\n", progName
);
66 printf("\tRead in break iteration rules text and write out the binary data\n"
68 "\t-h or -? or --help this usage text\n"
69 "\t-V or --version show a version message\n"
70 "\t-c or --copyright include a copyright notice\n"
71 "\t-v or --verbose turn on verbose output\n"
72 "\t-q or --quiet do not display warnings and progress\n"
73 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
74 "\t followed by path, defaults to %s\n"
75 "\t-d or --destdir destination directory, followed by the path\n",
76 u_getDataDirectory());
81 #if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO
83 /* dummy UDataInfo cf. udata.h */
84 static UDataInfo dummyDataInfo
= {
93 { 0, 0, 0, 0 }, /* dummy dataFormat */
94 { 0, 0, 0, 0 }, /* dummy formatVersion */
95 { 0, 0, 0, 0 } /* dummy dataVersion */
101 // Set up the ICU data header, defined in ucmndata.h
104 {sizeof(DataHeader
), // Struct MappedData
108 { // struct UDataInfo
109 sizeof(UDataInfo
), // size
116 { 0x42, 0x72, 0x6b, 0x20 }, // dataFormat="Brk "
117 { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values
118 // from the RBBI rule builder. The values declared
119 // here should never appear in any real RBBI data.
120 { 4, 1, 0, 0 } // dataVersion (Unicode version)
125 //----------------------------------------------------------------------------
129 //----------------------------------------------------------------------------
130 int main(int argc
, char **argv
) {
131 UErrorCode status
= U_ZERO_ERROR
;
132 const char *ruleFileName
;
133 const char *outFileName
;
134 const char *outDir
= NULL
;
135 const char *copyright
= NULL
;
138 // Pick up and check the command line arguments,
139 // using the standard ICU tool utils option handling.
141 U_MAIN_INIT_ARGS(argc
, argv
);
143 argc
=u_parseArgs(argc
, argv
, UPRV_LENGTHOF(options
), options
);
145 // Unrecognized option
146 fprintf(stderr
, "error in command line argument \"%s\"\n", argv
[-argc
]);
147 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
150 if(options
[0].doesOccur
|| options
[1].doesOccur
) {
151 // -? or -h for help.
155 if (!(options
[3].doesOccur
&& options
[4].doesOccur
)) {
156 fprintf(stderr
, "rule file and output file must both be specified.\n");
157 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
159 ruleFileName
= options
[3].value
;
160 outFileName
= options
[4].value
;
162 if (options
[5].doesOccur
) {
163 u_setDataDirectory(options
[5].value
);
166 status
= U_ZERO_ERROR
;
168 /* Combine the directory with the file name */
169 if(options
[6].doesOccur
) {
170 outDir
= options
[6].value
;
172 if (options
[7].doesOccur
) {
173 copyright
= U_COPYRIGHT_STRING
;
176 #if UCONFIG_NO_BREAK_ITERATION || UCONFIG_NO_FILE_IO
178 UNewDataMemory
*pData
;
181 /* write message with just the name */
182 sprintf(msg
, "genbrk writes dummy %s because of UCONFIG_NO_BREAK_ITERATION and/or UCONFIG_NO_FILE_IO, see uconfig.h", outFileName
);
183 fprintf(stderr
, "%s\n", msg
);
185 /* write the dummy data file */
186 pData
= udata_create(outDir
, NULL
, outFileName
, &dummyDataInfo
, NULL
, &status
);
187 udata_writeBlock(pData
, msg
, strlen(msg
));
188 udata_finish(pData
, &status
);
194 if (U_FAILURE(status
)) {
195 fprintf(stderr
, "%s: can not initialize ICU. status = %s\n",
196 argv
[0], u_errorName(status
));
199 status
= U_ZERO_ERROR
;
202 // Read in the rule source file
209 file
= fopen(ruleFileName
, "rb");
211 fprintf(stderr
, "Could not open file \"%s\"\n", ruleFileName
);
214 fseek(file
, 0, SEEK_END
);
215 ruleFileSize
= ftell(file
);
216 fseek(file
, 0, SEEK_SET
);
217 ruleBufferC
= new char[ruleFileSize
+10];
219 result
= (long)fread(ruleBufferC
, 1, ruleFileSize
, file
);
220 if (result
!= ruleFileSize
) {
221 fprintf(stderr
, "Error reading file \"%s\"\n", ruleFileName
);
224 ruleBufferC
[ruleFileSize
]=0;
228 // Look for a Unicode Signature (BOM) on the rule file
230 int32_t signatureLength
;
231 const char * ruleSourceC
= ruleBufferC
;
232 const char* encoding
= ucnv_detectUnicodeSignature(
233 ruleSourceC
, ruleFileSize
, &signatureLength
, &status
);
234 if (U_FAILURE(status
)) {
238 ruleSourceC
+= signatureLength
;
239 ruleFileSize
-= signatureLength
;
243 // Open a converter to take the rule file to UTF-16
246 conv
= ucnv_open(encoding
, &status
);
247 if (U_FAILURE(status
)) {
248 fprintf(stderr
, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status
));
253 // Convert the rules to UChar.
254 // Preflight first to determine required buffer size.
256 uint32_t destCap
= ucnv_toUChars(conv
,
262 if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
263 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
267 status
= U_ZERO_ERROR
;
268 UChar
*ruleSourceU
= new UChar
[destCap
+1];
270 ruleSourceU
, // dest,
275 if (U_FAILURE(status
)) {
276 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
283 // Put the source rules into a UnicodeString
285 UnicodeString
ruleSourceS(FALSE
, ruleSourceU
, destCap
);
288 // Create the break iterator from the rules
289 // This will compile the rules.
291 UParseError parseError
;
293 parseError
.offset
= 0;
294 RuleBasedBreakIterator
*bi
= new RuleBasedBreakIterator(ruleSourceS
, parseError
, status
);
295 if (U_FAILURE(status
)) {
296 fprintf(stderr
, "createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n",
297 u_errorName(status
), (int)parseError
.line
, (int)parseError
.offset
);
303 // Get the compiled rule data from the break iterator.
305 uint32_t outDataSize
;
306 const uint8_t *outData
;
307 outData
= bi
->getBinaryRules(outDataSize
);
309 // Copy the data format version numbers from the RBBI data header into the UDataMemory header.
310 uprv_memcpy(dh
.info
.formatVersion
, ((RBBIDataHeader
*)outData
)->fFormatVersion
, sizeof(dh
.info
.formatVersion
));
313 // Create the output file
316 UNewDataMemory
*pData
;
317 pData
= udata_create(outDir
, NULL
, outFileName
, &(dh
.info
), copyright
, &status
);
318 if(U_FAILURE(status
)) {
319 fprintf(stderr
, "genbrk: Could not open output file \"%s\", \"%s\"\n",
320 outFileName
, u_errorName(status
));
325 // Write the data itself.
326 udata_writeBlock(pData
, outData
, outDataSize
);
328 bytesWritten
= udata_finish(pData
, &status
);
329 if(U_FAILURE(status
)) {
330 fprintf(stderr
, "genbrk: error %d writing the output file\n", status
);
334 if (bytesWritten
!= outDataSize
) {
335 fprintf(stderr
, "Error writing to output file \"%s\"\n", outFileName
);
340 delete[] ruleSourceU
;
341 delete[] ruleBufferC
;
345 if(!options
[8].doesOccur
) {
346 printf("genbrk: tool completed successfully.\n");
350 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */