1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <sal/main.h>
31 #include <sal/types.h>
32 #include <rtl/ustrbuf.hxx>
33 
34 #include "warnings_guard_unicode_tblcoll.h"
35 
36 U_CAPI void U_EXPORT2 uprv_free(void *mem);
37 
38 using namespace ::rtl;
39 
40 /* Main Procedure */
41 
42 void data_write(char* file, char* name, sal_uInt8 *data, sal_Int32 len)
43 {
44 	FILE *fp = fopen(file, "wb");
45 	if (fp == NULL) {
46 	    printf("Can't create the C source file.");
47         return;
48 	}
49 
50 	fprintf(fp, "/*\n");
51 	fprintf(fp, " * Copyright(c) 1999 - 2000, Sun Microsystems, Inc.\n");
52 	fprintf(fp, " * All Rights Reserved.\n");
53 	fprintf(fp, " */\n\n");
54 	fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
55     fprintf(fp, "#include <sal/types.h>\n");
56     fprintf(fp, "\nextern \"C\" {\n");
57 
58 	// generate main dict. data array
59 	fprintf(fp, "\nstatic const sal_uInt8 %s[] = {", name);
60 
61 	sal_Int32 count = 0;
62     for (sal_Int32 i = 0; i < len; i++) {
63 
64         if (count++ % 16 == 0)
65             fprintf(fp, "\n\t");
66 
67         fprintf(fp, "0x%04x, ", data[i]);
68     }
69 	fprintf(fp, "\n};\n\n");
70 
71     fprintf(fp, "const sal_uInt8* get_%s() { return %s; }\n\n", name, name);
72     fprintf (fp, "}\n");
73 
74 	fclose(fp);
75 
76 }
77 
78 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
79 {
80 	FILE *fp;
81 
82 	if (argc < 4) exit(-1);
83 
84 	fp = fopen(argv[1], "rb");	// open the source file for read;
85 	if (fp == NULL)
86 	    printf("Open the rule source file failed.");
87 
88 
89 	sal_Char str[1024];
90     OUStringBuffer Obuf;
91 	while (fgets(str, 1024, fp)) {
92 	    // don't convert last new line character to Ostr.
93         sal_Int32 len = strlen(str) - 1;
94         // skip comment line
95         if (len == 0 || str[0] == '#')
96             continue;
97 
98 	    // input file is in UTF-8 encoding
99 	    OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8).trim();
100 
101         len = Ostr.getLength();
102         if (len == 0)
103             continue; // skip empty line.
104 
105         Obuf.append(Ostr);
106 	}
107 	fclose(fp);
108 
109     UErrorCode status = U_ZERO_ERROR;
110     //UParseError parseError;
111     //UCollator *coll = ucol_openRules(Obuf.getStr(), Obuf.getLength(), UCOL_OFF,
112     //        UCOL_DEFAULT_STRENGTH, &parseError, &status);
113 
114     RuleBasedCollator *coll = new RuleBasedCollator(reinterpret_cast<const UChar *>(Obuf.getStr()), status);	// UChar != sal_Unicode in MinGW
115 
116     if (U_SUCCESS(status)) {
117 
118         int32_t len = 0;
119         uint8_t *data = coll->cloneRuleData(len, status);
120 
121         if (U_SUCCESS(status) && data != NULL)
122             data_write(argv[2], argv[3], data, len);
123         else {
124             printf("Could not get rule data from collator\n");
125         }
126 
127 	if (data) uprv_free(data);
128     } else {
129         printf("\nRule parsering error\n");
130     }
131 
132     if (coll)
133         delete coll;
134 
135     return U_SUCCESS(status) ? 0 : 1;
136 }	// End of main
137