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"
48 static char *progName
;
49 static UOption options
[]={
50 UOPTION_HELP_H
, /* 0 */
51 UOPTION_HELP_QUESTION_MARK
, /* 1 */
52 UOPTION_VERBOSE
, /* 2 */
53 { "out", NULL
, NULL
, NULL
, 'o', UOPT_REQUIRES_ARG
, 0 }, /* 3 */
54 UOPTION_ICUDATADIR
, /* 4 */
55 UOPTION_DESTDIR
, /* 5 */
56 UOPTION_COPYRIGHT
, /* 6 */
59 void usageAndDie(int retCode
) {
60 printf("Usage: %s [-v] [-options] -o output-file dictionary-file\n", progName
);
61 printf("\tRead in word list and write out compact trie dictionary\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 { 0x54, 0x72, 0x44, 0x63 }, // "TrDc" Trie Dictionary
111 { 1, 0, 0, 0 }, // 1.0.0.0
112 { 0, 0, 0, 0 }, // Irrelevant for this data type
117 //----------------------------------------------------------------------------
121 //----------------------------------------------------------------------------
122 int main(int argc
, char **argv
) {
123 UErrorCode status
= U_ZERO_ERROR
;
124 const char *wordFileName
;
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
|| argc
< 2) {
148 fprintf(stderr
, "input and output file must both be specified.\n");
149 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
151 outFileName
= options
[3].value
;
152 wordFileName
= argv
[1];
154 if (options
[4].doesOccur
) {
155 u_setDataDirectory(options
[4].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
[5].doesOccur
) {
169 outDir
= options
[5].value
;
171 if (options
[6].doesOccur
) {
172 copyright
= U_COPYRIGHT_STRING
;
175 #if UCONFIG_NO_BREAK_ITERATION
177 UNewDataMemory
*pData
;
180 /* write message with just the name */
181 sprintf(msg
, "genctd writes dummy %s because of UCONFIG_NO_BREAK_ITERATION, see uconfig.h", outFileName
);
182 fprintf(stderr
, "%s\n", msg
);
184 /* write the dummy data file */
185 pData
= udata_create(outDir
, NULL
, outFileName
, &dummyDataInfo
, NULL
, &status
);
186 udata_writeBlock(pData
, msg
, strlen(msg
));
187 udata_finish(pData
, &status
);
193 // Read in the dictionary source file
200 file
= fopen(wordFileName
, "rb");
202 fprintf(stderr
, "Could not open file \"%s\"\n", wordFileName
);
205 fseek(file
, 0, SEEK_END
);
206 wordFileSize
= ftell(file
);
207 fseek(file
, 0, SEEK_SET
);
208 wordBufferC
= new char[wordFileSize
+10];
210 result
= (long)fread(wordBufferC
, 1, wordFileSize
, file
);
211 if (result
!= wordFileSize
) {
212 fprintf(stderr
, "Error reading file \"%s\"\n", wordFileName
);
215 wordBufferC
[wordFileSize
]=0;
219 // Look for a Unicode Signature (BOM) on the word file
221 int32_t signatureLength
;
222 const char * wordSourceC
= wordBufferC
;
223 const char* encoding
= ucnv_detectUnicodeSignature(
224 wordSourceC
, wordFileSize
, &signatureLength
, &status
);
225 if (U_FAILURE(status
)) {
229 wordSourceC
+= signatureLength
;
230 wordFileSize
-= signatureLength
;
234 // Open a converter to take the rule file to UTF-16
237 conv
= ucnv_open(encoding
, &status
);
238 if (U_FAILURE(status
)) {
239 fprintf(stderr
, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status
));
244 // Convert the words to UChar.
245 // Preflight first to determine required buffer size.
247 uint32_t destCap
= ucnv_toUChars(conv
,
253 if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
254 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
258 status
= U_ZERO_ERROR
;
259 UChar
*wordSourceU
= new UChar
[destCap
+1];
261 wordSourceU
, // dest,
266 if (U_FAILURE(status
)) {
267 fprintf(stderr
, "ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
272 // Get rid of the original file buffer
273 delete[] wordBufferC
;
275 // Create a MutableTrieDictionary, and loop through all the lines, inserting
278 // First, pick a median character.
279 UChar
*current
= wordSourceU
+ (destCap
/2);
280 UChar uc
= *current
++;
282 breaks
.add(0x000A); // Line Feed
283 breaks
.add(0x000D); // Carriage Return
284 breaks
.add(0x2028); // Line Separator
285 breaks
.add(0x2029); // Paragraph Separator
288 // Look for line break
289 while (uc
&& !breaks
.contains(uc
)) {
292 // Now skip to first non-line-break
293 while (uc
&& breaks
.contains(uc
)) {
297 while (uc
&& (breaks
.contains(uc
) || u_isspace(uc
)));
299 MutableTrieDictionary
*mtd
= new MutableTrieDictionary(uc
, status
);
301 if (U_FAILURE(status
)) {
302 fprintf(stderr
, "new MutableTrieDictionary: ICU Error \"%s\"\n", u_errorName(status
));
306 // Now add the words. Words are non-space characters at the beginning of
307 // lines, and must be at least one UChar.
308 current
= wordSourceU
;
309 UChar
*candidate
= current
;
314 while (uc
&& !u_isspace(uc
)) {
319 mtd
->addWord(candidate
, length
, status
);
320 if (U_FAILURE(status
)) {
321 fprintf(stderr
, "MutableTrieDictionary::addWord: ICU Error \"%s\"\n",
322 u_errorName(status
));
326 // Find beginning of next line
327 while (uc
&& !breaks
.contains(uc
)) {
330 while (uc
&& breaks
.contains(uc
)) {
333 candidate
= current
-1;
337 // Get rid of the Unicode text buffer
338 delete[] wordSourceU
;
340 // Now, create a CompactTrieDictionary from the mutable dictionary
341 CompactTrieDictionary
*ctd
= new CompactTrieDictionary(*mtd
, status
);
342 if (U_FAILURE(status
)) {
343 fprintf(stderr
, "new CompactTrieDictionary: ICU Error \"%s\"\n", u_errorName(status
));
347 // Get rid of the MutableTrieDictionary
351 // Get the binary data from the dictionary.
353 uint32_t outDataSize
= ctd
->dataSize();
354 const uint8_t *outData
= (const uint8_t *)ctd
->data();
357 // Create the output file
360 UNewDataMemory
*pData
;
361 pData
= udata_create(outDir
, NULL
, outFileName
, &(dh
.info
), copyright
, &status
);
362 if(U_FAILURE(status
)) {
363 fprintf(stderr
, "genctd: Could not open output file \"%s\", \"%s\"\n",
364 outFileName
, u_errorName(status
));
369 // Write the data itself.
370 udata_writeBlock(pData
, outData
, outDataSize
);
372 bytesWritten
= udata_finish(pData
, &status
);
373 if(U_FAILURE(status
)) {
374 fprintf(stderr
, "genctd: error \"%s\" writing the output file\n", u_errorName(status
));
378 if (bytesWritten
!= outDataSize
) {
379 fprintf(stderr
, "Error writing to output file \"%s\"\n", outFileName
);
383 // Get rid of the CompactTrieDictionary
388 printf("genctd: tool completed successfully.\n");
391 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */