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