1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir #include <stdlib.h>
34*cdf0e10cSrcweir #include <sal/main.h>
35*cdf0e10cSrcweir #include <sal/types.h>
36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "warnings_guard_unicode_tblcoll.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir U_CAPI void U_EXPORT2 uprv_free(void *mem);
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace ::rtl;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /* Main Procedure */
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir void data_write(char* file, char* name, sal_uInt8 *data, sal_Int32 len)
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir 	FILE *fp = fopen(file, "wb");
49*cdf0e10cSrcweir 	if (fp == NULL) {
50*cdf0e10cSrcweir 	    printf("Can't create the C source file.");
51*cdf0e10cSrcweir         return;
52*cdf0e10cSrcweir 	}
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	fprintf(fp, "/*\n");
55*cdf0e10cSrcweir 	fprintf(fp, " * Copyright(c) 1999 - 2000, Sun Microsystems, Inc.\n");
56*cdf0e10cSrcweir 	fprintf(fp, " * All Rights Reserved.\n");
57*cdf0e10cSrcweir 	fprintf(fp, " */\n\n");
58*cdf0e10cSrcweir 	fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
59*cdf0e10cSrcweir     fprintf(fp, "#include <sal/types.h>\n");
60*cdf0e10cSrcweir     fprintf(fp, "\nextern \"C\" {\n");
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	// generate main dict. data array
63*cdf0e10cSrcweir 	fprintf(fp, "\nstatic const sal_uInt8 %s[] = {", name);
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	sal_Int32 count = 0;
66*cdf0e10cSrcweir     for (sal_Int32 i = 0; i < len; i++) {
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         if (count++ % 16 == 0)
69*cdf0e10cSrcweir             fprintf(fp, "\n\t");
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         fprintf(fp, "0x%04x, ", data[i]);
72*cdf0e10cSrcweir     }
73*cdf0e10cSrcweir 	fprintf(fp, "\n};\n\n");
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     fprintf(fp, "const sal_uInt8* get_%s() { return %s; }\n\n", name, name);
76*cdf0e10cSrcweir     fprintf (fp, "}\n");
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	fclose(fp);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	FILE *fp;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	if (argc < 4) exit(-1);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	fp = fopen(argv[1], "rb");	// open the source file for read;
89*cdf0e10cSrcweir 	if (fp == NULL)
90*cdf0e10cSrcweir 	    printf("Open the rule source file failed.");
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	sal_Char str[1024];
94*cdf0e10cSrcweir     OUStringBuffer Obuf;
95*cdf0e10cSrcweir 	while (fgets(str, 1024, fp)) {
96*cdf0e10cSrcweir 	    // don't convert last new line character to Ostr.
97*cdf0e10cSrcweir         sal_Int32 len = strlen(str) - 1;
98*cdf0e10cSrcweir         // skip comment line
99*cdf0e10cSrcweir         if (len == 0 || str[0] == '#')
100*cdf0e10cSrcweir             continue;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	    // input file is in UTF-8 encoding
103*cdf0e10cSrcweir 	    OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8).trim();
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         len = Ostr.getLength();
106*cdf0e10cSrcweir         if (len == 0)
107*cdf0e10cSrcweir             continue; // skip empty line.
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         Obuf.append(Ostr);
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 	fclose(fp);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     UErrorCode status = U_ZERO_ERROR;
114*cdf0e10cSrcweir     //UParseError parseError;
115*cdf0e10cSrcweir     //UCollator *coll = ucol_openRules(Obuf.getStr(), Obuf.getLength(), UCOL_OFF,
116*cdf0e10cSrcweir     //        UCOL_DEFAULT_STRENGTH, &parseError, &status);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     RuleBasedCollator *coll = new RuleBasedCollator(reinterpret_cast<const UChar *>(Obuf.getStr()), status);	// UChar != sal_Unicode in MinGW
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     if (U_SUCCESS(status)) {
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         int32_t len = 0;
123*cdf0e10cSrcweir         uint8_t *data = coll->cloneRuleData(len, status);
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         if (U_SUCCESS(status) && data != NULL)
126*cdf0e10cSrcweir             data_write(argv[2], argv[3], data, len);
127*cdf0e10cSrcweir         else {
128*cdf0e10cSrcweir             printf("Could not get rule data from collator\n");
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	if (data) uprv_free(data);
132*cdf0e10cSrcweir     } else {
133*cdf0e10cSrcweir         printf("\nRule parsering error\n");
134*cdf0e10cSrcweir     }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     if (coll)
137*cdf0e10cSrcweir         delete coll;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     return U_SUCCESS(status) ? 0 : 1;
140*cdf0e10cSrcweir }	// End of main
141