2 **********************************************************************
3 * Copyright (C) 2002-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 //--------------------------------------------------------------------
12 // Tool for generating CompactTrieDictionary data files (.ctd files).
14 // Usage: genctd [options] -o output-file.ctd input-file
16 // options: -v verbose
19 // The input file is a plain text file containing words, one per line.
20 // Words end at the first whitespace; lines beginning with whitespace
22 // The file can be encoded as utf-8, or utf-16 (either endian), or
23 // in the default code page (platform dependent.). utf encoded
24 // files must include a BOM.
26 //--------------------------------------------------------------------
28 #include "unicode/utypes.h"
29 #include "unicode/uchar.h"
30 #include "unicode/ucnv.h"
31 #include "unicode/uniset.h"
32 #include "unicode/unistr.h"
33 #include "unicode/uclean.h"
34 #include "unicode/udata.h"
35 #include "unicode/putil.h"
50 static char *progName
;
51 static UOption options
[]={
52 UOPTION_HELP_H
, /* 0 */
53 UOPTION_HELP_QUESTION_MARK
, /* 1 */
54 UOPTION_VERBOSE
, /* 2 */
55 { "out", NULL
, NULL
, NULL
, 'o', UOPT_REQUIRES_ARG
, 0 }, /* 3 */
56 UOPTION_ICUDATADIR
, /* 4 */
57 UOPTION_DESTDIR
, /* 5 */
58 UOPTION_COPYRIGHT
, /* 6 */
61 void usageAndDie(int retCode
) {
62 printf("Usage: %s [-v] [-options] -o output-file dictionary-file\n", progName
);
63 printf("\tRead in word list and write out compact trie dictionary\n"
65 "\t-h or -? or --help this usage text\n"
66 "\t-V or --version show a version message\n"
67 "\t-c or --copyright include a copyright notice\n"
68 "\t-v or --verbose turn on verbose output\n"
69 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
70 "\t followed by path, defaults to %s\n"
71 "\t-d or --destdir destination directory, followed by the path\n",
72 u_getDataDirectory());
77 #if UCONFIG_NO_BREAK_ITERATION
79 /* dummy UDataInfo cf. udata.h */
80 static UDataInfo dummyDataInfo
= {
89 { 0, 0, 0, 0 }, /* dummy dataFormat */
90 { 0, 0, 0, 0 }, /* dummy formatVersion */
91 { 0, 0, 0, 0 } /* dummy dataVersion */
97 // Set up the ICU data header, defined in ucmndata.h
100 {sizeof(DataHeader
), // Struct MappedData
104 { // struct UDataInfo
105 sizeof(UDataInfo
), // size
112 { 0x54, 0x72, 0x44, 0x63 }, // "TrDc" Trie Dictionary
113 { 1, 0, 0, 0 }, // 1.0.0.0
114 { 0, 0, 0, 0 }, // Irrelevant for this data type
119 //----------------------------------------------------------------------------
123 //----------------------------------------------------------------------------
124 int main(int argc
, char **argv
) {
125 UErrorCode status
= U_ZERO_ERROR
;
126 const char *wordFileName
;
127 const char *outFileName
;
128 const char *outDir
= NULL
;
129 const char *copyright
= NULL
;
132 // Pick up and check the command line arguments,
133 // using the standard ICU tool utils option handling.
135 U_MAIN_INIT_ARGS(argc
, argv
);
137 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
139 // Unrecognized option
140 fprintf(stderr
, "error in command line argument \"%s\"\n", argv
[-argc
]);
141 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
144 if(options
[0].doesOccur
|| options
[1].doesOccur
) {
145 // -? or -h for help.
149 if (!options
[3].doesOccur
|| argc
< 2) {
150 fprintf(stderr
, "input and output file must both be specified.\n");
151 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
153 outFileName
= options
[3].value
;
154 wordFileName
= argv
[1];
156 if (options
[4].doesOccur
) {
157 u_setDataDirectory(options
[4].value
);
162 if (U_FAILURE(status
)) {
163 fprintf(stderr
, "%s: can not initialize ICU. status = %s\n",
164 argv
[0], u_errorName(status
));
167 status
= U_ZERO_ERROR
;
169 /* Combine the directory with the file name */
170 if(options
[5].doesOccur
) {
171 outDir
= options
[5].value
;
173 if (options
[6].doesOccur
) {
174 copyright
= U_COPYRIGHT_STRING
;
177 #if UCONFIG_NO_BREAK_ITERATION
179 UNewDataMemory
*pData
;
182 /* write message with just the name */
183 sprintf(msg
, "genctd writes dummy %s because of UCONFIG_NO_BREAK_ITERATION, see uconfig.h", outFileName
);
184 fprintf(stderr
, "%s\n", msg
);
186 /* write the dummy data file */
187 pData
= udata_create(outDir
, NULL
, outFileName
, &dummyDataInfo
, NULL
, &status
);
188 udata_writeBlock(pData
, msg
, strlen(msg
));
189 udata_finish(pData
, &status
);
195 // Read in the dictionary source file
202 file
= fopen(wordFileName
, "rb");
204 fprintf(stderr
, "Could not open file \"%s\"\n", wordFileName
);
207 fseek(file
, 0, SEEK_END
);
208 wordFileSize
= ftell(file
);
209 fseek(file
, 0, SEEK_SET
);
210 wordBufferC
= new char[wordFileSize
+10];
212 result
= (long)fread(wordBufferC
, 1, wordFileSize
, file
);
213 if (result
!= wordFileSize
) {
214 fprintf(stderr
, "Error reading file \"%s\"\n", wordFileName
);
217 wordBufferC
[wordFileSize
]=0;
221 // Look for a Unicode Signature (BOM) on the word file
223 int32_t signatureLength
;
224 const char * wordSourceC
= wordBufferC
;
225 const char* encoding
= ucnv_detectUnicodeSignature(
226 wordSourceC
, wordFileSize
, &signatureLength
, &status
);
227 if (U_FAILURE(status
)) {
231 wordSourceC
+= signatureLength
;
232 wordFileSize
-= signatureLength
;
236 // Open a converter to take the rule file to UTF-16
239 conv
= ucnv_open(encoding
, &status
);
240 if (U_FAILURE(status
)) {
241 fprintf(stderr
, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status
));
246 // Convert the words to UChar.
247 // Preflight first to determine required buffer size.
249 uint32_t destCap
= ucnv_toUChars(conv
,
255 if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
256 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
260 status
= U_ZERO_ERROR
;
261 UChar
*wordSourceU
= new UChar
[destCap
+1];
263 wordSourceU
, // dest,
268 if (U_FAILURE(status
)) {
269 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
274 // Get rid of the original file buffer
275 delete[] wordBufferC
;
277 // Create a MutableTrieDictionary, and loop through all the lines, inserting
280 // First, pick a median character.
281 UChar
*current
= wordSourceU
+ (destCap
/2);
282 UChar uc
= *current
++;
284 breaks
.add(0x000A); // Line Feed
285 breaks
.add(0x000D); // Carriage Return
286 breaks
.add(0x2028); // Line Separator
287 breaks
.add(0x2029); // Paragraph Separator
290 // Look for line break
291 while (uc
&& !breaks
.contains(uc
)) {
294 // Now skip to first non-line-break
295 while (uc
&& breaks
.contains(uc
)) {
299 while (uc
&& (breaks
.contains(uc
) || u_isspace(uc
)));
301 MutableTrieDictionary
*mtd
= new MutableTrieDictionary(uc
, status
);
303 if (U_FAILURE(status
)) {
304 fprintf(stderr
, "new MutableTrieDictionary: ICU Error \"%s\"\n", u_errorName(status
));
308 // Now add the words. Words are non-space characters at the beginning of
309 // lines, and must be at least one UChar.
310 current
= wordSourceU
;
311 UChar
*candidate
= current
;
316 while (uc
&& !u_isspace(uc
)) {
321 mtd
->addWord(candidate
, length
, status
);
322 if (U_FAILURE(status
)) {
323 fprintf(stderr
, "MutableTrieDictionary::addWord: ICU Error \"%s\"\n",
324 u_errorName(status
));
328 // Find beginning of next line
329 while (uc
&& !breaks
.contains(uc
)) {
332 while (uc
&& breaks
.contains(uc
)) {
335 candidate
= current
-1;
339 // Get rid of the Unicode text buffer
340 delete[] wordSourceU
;
342 // Now, create a CompactTrieDictionary from the mutable dictionary
343 CompactTrieDictionary
*ctd
= new CompactTrieDictionary(*mtd
, status
);
344 if (U_FAILURE(status
)) {
345 fprintf(stderr
, "new CompactTrieDictionary: ICU Error \"%s\"\n", u_errorName(status
));
349 // Get rid of the MutableTrieDictionary
353 // Get the binary data from the dictionary.
355 uint32_t outDataSize
= ctd
->dataSize();
356 const uint8_t *outData
= (const uint8_t *)ctd
->data();
359 // Create the output file
362 UNewDataMemory
*pData
;
363 pData
= udata_create(outDir
, NULL
, outFileName
, &(dh
.info
), copyright
, &status
);
364 if(U_FAILURE(status
)) {
365 fprintf(stderr
, "genctd: Could not open output file \"%s\", \"%s\"\n",
366 outFileName
, u_errorName(status
));
371 // Write the data itself.
372 udata_writeBlock(pData
, outData
, outDataSize
);
374 bytesWritten
= udata_finish(pData
, &status
);
375 if(U_FAILURE(status
)) {
376 fprintf(stderr
, "genctd: error \"%s\" writing the output file\n", u_errorName(status
));
380 if (bytesWritten
!= outDataSize
) {
381 fprintf(stderr
, "Error writing to output file \"%s\"\n", outFileName
);
385 // Get rid of the CompactTrieDictionary
390 printf("genctd: tool completed successfully.\n");
393 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */