2 *******************************************************************************
4 * Copyright (C) 2003-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: gensprep.c
10 * tab size: 8 (not used)
13 * created on: 2003-02-06
14 * created by: Ram Viswanadha
16 * This program reads the Profile.txt files,
17 * parses them, and extracts the data for StringPrep profile.
18 * It then preprocesses it and writes a binary file for efficient use
19 * in various StringPrep conversion processes.
22 #define USPREP_TYPE_NAMES_ARRAY 1
34 #include "unicode/udata.h"
35 #include "unicode/utypes.h"
36 #include "unicode/putil.h"
43 UBool beVerbose
=FALSE
, haveCopyright
=TRUE
;
45 #define NORM_CORRECTIONS_FILE_NAME "NormalizationCorrections.txt"
47 /* prototypes --------------------------------------------------------------- */
50 parseMappings(const char *filename
, UBool reportError
, UErrorCode
*pErrorCode
);
53 parseNormalizationCorrections(const char *filename
, UErrorCode
*pErrorCode
);
56 /* -------------------------------------------------------------------------- */
58 static UOption options
[]={
60 UOPTION_HELP_QUESTION_MARK
,
67 { "normalization", NULL
, NULL
, NULL
, 'n', UOPT_REQUIRES_ARG
, 0 },
68 { "check-bidi", NULL
, NULL
, NULL
, 'k', UOPT_NO_ARG
, 0},
69 { "unicode", NULL
, NULL
, NULL
, 'u', UOPT_REQUIRES_ARG
, 0 },
86 static int printHelp(int argc
, char* argv
[]){
88 * Broken into chucks because the C89 standard says the minimum
89 * required supported string length is 509 bytes.
92 "Usage: %s [-options] [file_name]\n"
94 "Read the files specified and\n"
95 "create a binary file [package-name]_[bundle-name]." DATA_TYPE
" with the StringPrep profile data\n"
100 "\t-h or -? or --help print this usage text\n"
101 "\t-v or --verbose verbose output\n"
102 "\t-c or --copyright include a copyright notice\n");
104 "\t-d or --destdir destination directory, followed by the path\n"
105 "\t-s or --sourcedir source directory of ICU data, followed by the path\n"
106 "\t-b or --bundle-name generate the ouput data file with the name specified\n"
107 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
108 "\t followed by path, defaults to %s\n",
109 u_getDataDirectory());
111 "\t-n or --normalize turn on the option for normalization and include mappings\n"
112 "\t from NormalizationCorrections.txt from the given path,\n"
113 "\t e.g: /test/icu/source/data/unidata\n"
114 "\t-k or --check-bidi turn on the option for checking for BiDi in the profile\n"
115 "\t-u or --unicode version of Unicode to be used with this profile followed by the version\n"
117 return argc
<0 ? U_ILLEGAL_ARGUMENT_ERROR
: U_ZERO_ERROR
;
122 main(int argc
, char* argv
[]) {
124 char* filename
= NULL
;
126 const char *srcDir
=NULL
, *destDir
=NULL
, *icuUniDataDir
=NULL
;
127 const char *bundleName
=NULL
, *inputFileName
= NULL
;
129 int32_t sprepOptions
= 0;
131 UErrorCode errorCode
=U_ZERO_ERROR
;
133 U_MAIN_INIT_ARGS(argc
, argv
);
135 /* preset then read command line options */
136 options
[DESTDIR
].value
=u_getDataDirectory();
137 options
[SOURCEDIR
].value
="";
138 options
[UNICODE_VERSION
].value
="0"; /* don't assume the unicode version */
139 options
[BUNDLE_NAME
].value
= DATA_NAME
;
140 options
[NORMALIZE
].value
= "";
142 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
144 /* error handling, printing usage message */
147 "error in command line argument \"%s\"\n",
150 if(argc
<0 || options
[HELP
].doesOccur
|| options
[HELP_QUESTION_MARK
].doesOccur
) {
151 return printHelp(argc
, argv
);
155 /* get the options values */
156 beVerbose
=options
[VERBOSE
].doesOccur
;
157 haveCopyright
=options
[COPYRIGHT
].doesOccur
;
158 srcDir
=options
[SOURCEDIR
].value
;
159 destDir
=options
[DESTDIR
].value
;
160 bundleName
= options
[BUNDLE_NAME
].value
;
161 icuUniDataDir
= options
[NORMALIZE
].value
;
164 /* print the help message */
165 return printHelp(argc
, argv
);
167 inputFileName
= argv
[1];
169 if(!options
[UNICODE_VERSION
].doesOccur
){
170 return printHelp(argc
, argv
);
172 if(options
[ICUDATADIR
].doesOccur
) {
173 u_setDataDirectory(options
[ICUDATADIR
].value
);
178 "gensprep writes dummy " U_ICUDATA_NAME
"_" DATA_NAME
"." DATA_TYPE
179 " because UCONFIG_NO_IDNA is set, \n"
180 "see icu/source/common/unicode/uconfig.h\n");
181 generateData(destDir
, bundleName
);
185 setUnicodeVersion(options
[UNICODE_VERSION
].value
);
186 filename
= (char* ) uprv_malloc(uprv_strlen(srcDir
) + 300); /* hopefully this should be enough */
188 /* prepare the filename beginning with the source dir */
189 if(uprv_strchr(srcDir
,U_FILE_SEP_CHAR
) == NULL
&& uprv_strchr(srcDir
,U_FILE_ALT_SEP_CHAR
) == NULL
){
191 filename
[1] = U_FILE_SEP_CHAR
;
192 uprv_strcpy(filename
+2,srcDir
);
194 uprv_strcpy(filename
, srcDir
);
197 basename
=filename
+uprv_strlen(filename
);
198 if(basename
>filename
&& *(basename
-1)!=U_FILE_SEP_CHAR
) {
199 *basename
++=U_FILE_SEP_CHAR
;
205 /* process the file */
206 uprv_strcpy(basename
,inputFileName
);
207 parseMappings(filename
,FALSE
, &errorCode
);
208 if(U_FAILURE(errorCode
)) {
209 fprintf(stderr
, "Could not open file %s for reading. Error: %s \n", filename
, u_errorName(errorCode
));
213 if(options
[NORMALIZE
].doesOccur
){
214 /* set up directory for NormalizationCorrections.txt */
215 uprv_strcpy(filename
,icuUniDataDir
);
216 basename
=filename
+uprv_strlen(filename
);
217 if(basename
>filename
&& *(basename
-1)!=U_FILE_SEP_CHAR
) {
218 *basename
++=U_FILE_SEP_CHAR
;
221 *basename
++=U_FILE_SEP_CHAR
;
222 uprv_strcpy(basename
,NORM_CORRECTIONS_FILE_NAME
);
224 parseNormalizationCorrections(filename
,&errorCode
);
225 if(U_FAILURE(errorCode
)){
226 fprintf(stderr
,"Could not open file %s for reading \n", filename
);
229 sprepOptions
|= _SPREP_NORMALIZATION_ON
;
232 if(options
[CHECK_BIDI
].doesOccur
){
233 sprepOptions
|= _SPREP_CHECK_BIDI_ON
;
236 setOptions(sprepOptions
);
238 /* process parsed data */
239 if(U_SUCCESS(errorCode
)) {
240 /* write the data file */
241 generateData(destDir
, bundleName
);
255 static void U_CALLCONV
256 normalizationCorrectionsLineFn(void *context
,
257 char *fields
[][2], int32_t fieldCount
,
258 UErrorCode
*pErrorCode
) {
259 uint32_t mapping
[40];
263 UVersionInfo version
;
264 UVersionInfo thisVersion
;
266 /* get the character code, field 0 */
267 code
=(uint32_t)uprv_strtoul(fields
[0][0], &end
, 16);
268 if(U_FAILURE(*pErrorCode
)) {
269 fprintf(stderr
, "gensprep: error parsing NormalizationCorrections.txt mapping at %s\n", fields
[0][0]);
272 /* Original (erroneous) decomposition */
275 /* parse the mapping string */
276 length
=u_parseCodePoints(s
, mapping
, sizeof(mapping
)/4, pErrorCode
);
278 /* ignore corrected decomposition */
280 u_versionFromString(version
,fields
[3][0] );
281 u_versionFromString(thisVersion
, "3.2.0");
285 if(U_FAILURE(*pErrorCode
)) {
286 fprintf(stderr
, "gensprep error parsing NormalizationCorrections.txt of U+%04lx - %s\n",
287 (long)code
, u_errorName(*pErrorCode
));
291 /* store the mapping */
292 if( version
[0] > thisVersion
[0] ||
293 ((version
[0]==thisVersion
[0]) && (version
[1] > thisVersion
[1]))
295 storeMapping(code
,mapping
, length
, USPREP_MAP
, pErrorCode
);
297 setUnicodeVersionNC(version
);
301 parseNormalizationCorrections(const char *filename
, UErrorCode
*pErrorCode
) {
304 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
308 u_parseDelimitedFile(filename
, ';', fields
, 4, normalizationCorrectionsLineFn
, NULL
, pErrorCode
);
310 /* fprintf(stdout,"Number of code points that have NormalizationCorrections mapping with length >1 : %i\n",len); */
312 if(U_FAILURE(*pErrorCode
) && ( *pErrorCode
!=U_FILE_ACCESS_ERROR
)) {
313 fprintf(stderr
, "gensprep error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename
, u_errorName(*pErrorCode
));
318 static void U_CALLCONV
319 strprepProfileLineFn(void *context
,
320 char *fields
[][2], int32_t fieldCount
,
321 UErrorCode
*pErrorCode
) {
322 uint32_t mapping
[40];
326 /*UBool* mapWithNorm = (UBool*) context;*/
327 const char* typeName
;
328 uint32_t rangeStart
=0,rangeEnd
=0;
329 const char* filename
= (const char*) context
;
331 typeName
= fields
[2][0];
334 if(uprv_strstr(typeName
, usprepTypeNames
[USPREP_UNASSIGNED
])!=NULL
){
336 u_parseCodePointRange(fields
[0][0], &rangeStart
,&rangeEnd
, pErrorCode
);
337 if(U_FAILURE(*pErrorCode
)){
338 fprintf(stderr
, "Could not parse code point range. Error: %s\n",u_errorName(*pErrorCode
));
342 /* store the range */
343 storeRange(rangeStart
,rangeEnd
,USPREP_UNASSIGNED
, pErrorCode
);
345 }else if(uprv_strstr(typeName
, usprepTypeNames
[USPREP_PROHIBITED
])!=NULL
){
347 u_parseCodePointRange(fields
[0][0], &rangeStart
,&rangeEnd
, pErrorCode
);
348 if(U_FAILURE(*pErrorCode
)){
349 fprintf(stderr
, "Could not parse code point range. Error: %s\n",u_errorName(*pErrorCode
));
353 /* store the range */
354 storeRange(rangeStart
,rangeEnd
,USPREP_PROHIBITED
, pErrorCode
);
356 }else if(uprv_strstr(typeName
, usprepTypeNames
[USPREP_MAP
])!=NULL
){
358 /* get the character code, field 0 */
359 code
=(uint32_t)uprv_strtoul(fields
[0][0], &end
, 16);
360 if(end
<=fields
[0][0] || end
!=fields
[0][1]) {
361 fprintf(stderr
, "gensprep: syntax error in field 0 at %s\n", fields
[0][0]);
362 *pErrorCode
=U_PARSE_ERROR
;
366 /* parse the mapping string */
367 length
=u_parseCodePoints(map
, mapping
, sizeof(mapping
)/4, pErrorCode
);
369 /* store the mapping */
370 storeMapping(code
,mapping
, length
,USPREP_MAP
, pErrorCode
);
373 *pErrorCode
= U_INVALID_FORMAT_ERROR
;
376 if(U_FAILURE(*pErrorCode
)) {
377 fprintf(stderr
, "gensprep error parsing %s line %s at %s. Error: %s\n",filename
,
378 fields
[0][0],fields
[2][0],u_errorName(*pErrorCode
));
385 parseMappings(const char *filename
, UBool reportError
, UErrorCode
*pErrorCode
) {
388 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
392 u_parseDelimitedFile(filename
, ';', fields
, 3, strprepProfileLineFn
, (void*)filename
, pErrorCode
);
394 /*fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len);*/
396 if(U_FAILURE(*pErrorCode
) && (reportError
|| *pErrorCode
!=U_FILE_ACCESS_ERROR
)) {
397 fprintf(stderr
, "gensprep error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename
, u_errorName(*pErrorCode
));
403 #endif /* #if !UCONFIG_NO_IDNA */
406 * Hey, Emacs, please set the following:
409 * indent-tabs-mode: nil