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/ustring.hxx>
33 
34 #define MAX_ADDRESS 0x30000
35 #define MAX_INDEX MAX_ADDRESS/0x100
36 
37 using namespace ::rtl;
38 
39 /* Main Procedure */
40 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)41 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
42 {
43 	FILE *fp;
44 
45 	if (argc < 4) exit(-1);
46 
47 	fp = fopen(argv[1], "rb");	// open the source file for read;
48 	if (fp == NULL) {
49 	    printf("Open the rule source file failed.");
50         return 1;
51     }
52 
53 
54     sal_Int32 i, j, k;
55     sal_Int32 address[MAX_ADDRESS];
56     for (i=0; i<MAX_ADDRESS; i++) address[i]=-1;
57     OUString sep=OUString(sal_Unicode('|'));
58     OUString result=sep;
59     sal_Int32 max=0;
60 
61 	sal_Char str[1024];
62 	while (fgets(str, 1024, fp)) {
63 	    // don't convert last new line character to Ostr.
64         sal_Int32 len = strlen(str) - 1;
65         // skip comment line
66         if (len == 0 || str[0] == '#')
67             continue;
68 
69 	    // input file is in UTF-8 encoding
70 	    OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8);
71         len = Ostr.getLength();
72         if (len == 0)
73             continue; // skip empty line.
74 
75         sal_Int32 nPos=0;
76         sal_uInt32 nChar = Ostr.iterateCodePoints(&nPos, 2);
77         if (nChar > MAX_ADDRESS) {
78             printf("Code point 0x%lx exceeds MAX_ADDRESS 0x%x, Please increase MAX_ADDRESS", static_cast<long unsigned int>(nChar), MAX_ADDRESS);
79             exit(1);
80         }
81         OUString key=Ostr.copy(nPos)+sep;
82         sal_Int32 idx = result.indexOf(key);
83         if (key.getLength() > max) max=key.getLength();
84         if (idx >= 0) {
85             address[nChar]=idx;
86         } else {
87             address[nChar]=result.getLength();
88             result+=key;
89         }
90 	}
91 	fclose(fp);
92 
93 	fp = fopen(argv[2], "wb");
94 	if (fp == NULL) {
95 	    printf("Can't create the C source file.");
96         return 1;
97 	}
98 
99 	fprintf(fp, "/*\n");
100 	fprintf(fp, " * Copyright(c) 1999 - 2006, Sun Microsystems, Inc.\n");
101 	fprintf(fp, " * All Rights Reserved.\n");
102 	fprintf(fp, " */\n\n");
103 	fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
104     fprintf(fp, "#include <sal/types.h>\n");
105     fprintf(fp, "\nextern \"C\" {\n");
106 
107     sal_Int32 index[MAX_INDEX];
108     sal_Int32 max_index=0;
109     for (i=k=0; i<MAX_INDEX; i++) {
110         index[i] = 0xFFFF;
111         for (j=0; j<0x100; j++) {
112             if (address[i*0x100+j] >=0) {
113                 max_index=i;
114                 index[i]=0x100*k++;
115                 break;
116             }
117         }
118     }
119 
120 	fprintf(fp, "\nstatic const sal_uInt16 idx1[] = {");
121     for (i = k = 0; i <= max_index;  i++) {
122         if (k++ % 16 == 0) fprintf(fp, "\n\t");
123         fprintf(
124             fp, "0x%04lx, ", sal::static_int_cast< unsigned long >(index[i]));
125     }
126 	fprintf(fp, "\n};\n\n");
127 
128     sal_Int32 len=result.getLength();
129     const sal_Unicode *ustr=result.getStr();
130 	fprintf(fp, "\nstatic const sal_uInt16 idx2[] = {");
131     for (i = k = 0; i <= max_index; i++) {
132         if (index[i] != 0xFFFF) {
133             for (j = 0; j<0x100; j++) {
134                 if (k++ % 16 == 0) fprintf(fp, "\n\t");
135                 sal_Int32 ad=address[i*0x100+j];
136                 fprintf(
137                     fp, "0x%04lx, ",
138                     sal::static_int_cast< unsigned long >(
139                         ad == -1 ? 0 : max == 2 ? ustr[ad] : ad));
140             }
141             fprintf(fp, "\n\t");
142         }
143     }
144 	fprintf(fp, "\n};\n\n");
145 
146     if (max == 2) {
147         fprintf(fp, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n");
148     } else {
149         fprintf(fp, "\nstatic const sal_uInt16 idx3[] = {");
150         for (i = k = 0; i < len;  i++) {
151             if (k++ % 16 == 0) fprintf(fp, "\n\t");
152             fprintf(fp, "0x%04x, ", (sep.toChar() == ustr[i]) ? 0 : ustr[i]);
153         }
154         fprintf(fp, "\n};\n\n");
155     }
156 
157     fprintf(fp, "SAL_DLLPUBLIC_EXPORT const sal_uInt16** get_%s(sal_Int16 &max_index)\n{\n\tstatic const sal_uInt16 *idx[]={idx1, idx2, idx3};\n\tmax_index=0x%x;\n\treturn idx;\n}\n\n", argv[3], static_cast<unsigned int>(max_index));
158     fprintf (fp, "}\n");
159 
160 	fclose(fp);
161     return 0;
162 }	// End of main
163