1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*449ab281SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*449ab281SAndrew Rist * distributed with this work for additional information
6*449ab281SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*449ab281SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*449ab281SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*449ab281SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist * software distributed under the License is distributed on an
15*449ab281SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist * KIND, either express or implied. See the License for the
17*449ab281SAndrew Rist * specific language governing permissions and limitations
18*449ab281SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*449ab281SAndrew Rist *************************************************************/
21*449ab281SAndrew Rist
22*449ab281SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir #include <sal/main.h>
31cdf0e10cSrcweir #include <sal/types.h>
32cdf0e10cSrcweir #include <rtl/ustring.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #define MAX_ADDRESS 0x30000
35cdf0e10cSrcweir #define MAX_INDEX MAX_ADDRESS/0x100
36cdf0e10cSrcweir
37cdf0e10cSrcweir using namespace ::rtl;
38cdf0e10cSrcweir
39cdf0e10cSrcweir /* Main Procedure */
40cdf0e10cSrcweir
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)41cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir FILE *fp;
44cdf0e10cSrcweir
45cdf0e10cSrcweir if (argc < 4) exit(-1);
46cdf0e10cSrcweir
47cdf0e10cSrcweir fp = fopen(argv[1], "rb"); // open the source file for read;
48cdf0e10cSrcweir if (fp == NULL) {
49cdf0e10cSrcweir printf("Open the rule source file failed.");
50cdf0e10cSrcweir return 1;
51cdf0e10cSrcweir }
52cdf0e10cSrcweir
53cdf0e10cSrcweir
54cdf0e10cSrcweir sal_Int32 i, j, k;
55cdf0e10cSrcweir sal_Int32 address[MAX_ADDRESS];
56cdf0e10cSrcweir for (i=0; i<MAX_ADDRESS; i++) address[i]=-1;
57cdf0e10cSrcweir OUString sep=OUString(sal_Unicode('|'));
58cdf0e10cSrcweir OUString result=sep;
59cdf0e10cSrcweir sal_Int32 max=0;
60cdf0e10cSrcweir
61cdf0e10cSrcweir sal_Char str[1024];
62cdf0e10cSrcweir while (fgets(str, 1024, fp)) {
63cdf0e10cSrcweir // don't convert last new line character to Ostr.
64cdf0e10cSrcweir sal_Int32 len = strlen(str) - 1;
65cdf0e10cSrcweir // skip comment line
66cdf0e10cSrcweir if (len == 0 || str[0] == '#')
67cdf0e10cSrcweir continue;
68cdf0e10cSrcweir
69cdf0e10cSrcweir // input file is in UTF-8 encoding
70cdf0e10cSrcweir OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8);
71cdf0e10cSrcweir len = Ostr.getLength();
72cdf0e10cSrcweir if (len == 0)
73cdf0e10cSrcweir continue; // skip empty line.
74cdf0e10cSrcweir
75cdf0e10cSrcweir sal_Int32 nPos=0;
76cdf0e10cSrcweir sal_uInt32 nChar = Ostr.iterateCodePoints(&nPos, 2);
77cdf0e10cSrcweir if (nChar > MAX_ADDRESS) {
78cdf0e10cSrcweir printf("Code point 0x%lx exceeds MAX_ADDRESS 0x%x, Please increase MAX_ADDRESS", static_cast<long unsigned int>(nChar), MAX_ADDRESS);
79cdf0e10cSrcweir exit(1);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir OUString key=Ostr.copy(nPos)+sep;
82cdf0e10cSrcweir sal_Int32 idx = result.indexOf(key);
83cdf0e10cSrcweir if (key.getLength() > max) max=key.getLength();
84cdf0e10cSrcweir if (idx >= 0) {
85cdf0e10cSrcweir address[nChar]=idx;
86cdf0e10cSrcweir } else {
87cdf0e10cSrcweir address[nChar]=result.getLength();
88cdf0e10cSrcweir result+=key;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir }
91cdf0e10cSrcweir fclose(fp);
92cdf0e10cSrcweir
93cdf0e10cSrcweir fp = fopen(argv[2], "wb");
94cdf0e10cSrcweir if (fp == NULL) {
95cdf0e10cSrcweir printf("Can't create the C source file.");
96cdf0e10cSrcweir return 1;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir fprintf(fp, "/*\n");
100cdf0e10cSrcweir fprintf(fp, " * Copyright(c) 1999 - 2006, Sun Microsystems, Inc.\n");
101cdf0e10cSrcweir fprintf(fp, " * All Rights Reserved.\n");
102cdf0e10cSrcweir fprintf(fp, " */\n\n");
103cdf0e10cSrcweir fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
104cdf0e10cSrcweir fprintf(fp, "#include <sal/types.h>\n");
105cdf0e10cSrcweir fprintf(fp, "\nextern \"C\" {\n");
106cdf0e10cSrcweir
107cdf0e10cSrcweir sal_Int32 index[MAX_INDEX];
108cdf0e10cSrcweir sal_Int32 max_index=0;
109cdf0e10cSrcweir for (i=k=0; i<MAX_INDEX; i++) {
110cdf0e10cSrcweir index[i] = 0xFFFF;
111cdf0e10cSrcweir for (j=0; j<0x100; j++) {
112cdf0e10cSrcweir if (address[i*0x100+j] >=0) {
113cdf0e10cSrcweir max_index=i;
114cdf0e10cSrcweir index[i]=0x100*k++;
115cdf0e10cSrcweir break;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 idx1[] = {");
121cdf0e10cSrcweir for (i = k = 0; i <= max_index; i++) {
122cdf0e10cSrcweir if (k++ % 16 == 0) fprintf(fp, "\n\t");
123cdf0e10cSrcweir fprintf(
124cdf0e10cSrcweir fp, "0x%04lx, ", sal::static_int_cast< unsigned long >(index[i]));
125cdf0e10cSrcweir }
126cdf0e10cSrcweir fprintf(fp, "\n};\n\n");
127cdf0e10cSrcweir
128cdf0e10cSrcweir sal_Int32 len=result.getLength();
129cdf0e10cSrcweir const sal_Unicode *ustr=result.getStr();
130cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 idx2[] = {");
131cdf0e10cSrcweir for (i = k = 0; i <= max_index; i++) {
132cdf0e10cSrcweir if (index[i] != 0xFFFF) {
133cdf0e10cSrcweir for (j = 0; j<0x100; j++) {
134cdf0e10cSrcweir if (k++ % 16 == 0) fprintf(fp, "\n\t");
135cdf0e10cSrcweir sal_Int32 ad=address[i*0x100+j];
136cdf0e10cSrcweir fprintf(
137cdf0e10cSrcweir fp, "0x%04lx, ",
138cdf0e10cSrcweir sal::static_int_cast< unsigned long >(
139cdf0e10cSrcweir ad == -1 ? 0 : max == 2 ? ustr[ad] : ad));
140cdf0e10cSrcweir }
141cdf0e10cSrcweir fprintf(fp, "\n\t");
142cdf0e10cSrcweir }
143cdf0e10cSrcweir }
144cdf0e10cSrcweir fprintf(fp, "\n};\n\n");
145cdf0e10cSrcweir
146cdf0e10cSrcweir if (max == 2) {
147cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n");
148cdf0e10cSrcweir } else {
149cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 idx3[] = {");
150cdf0e10cSrcweir for (i = k = 0; i < len; i++) {
151cdf0e10cSrcweir if (k++ % 16 == 0) fprintf(fp, "\n\t");
152cdf0e10cSrcweir fprintf(fp, "0x%04x, ", (sep.toChar() == ustr[i]) ? 0 : ustr[i]);
153cdf0e10cSrcweir }
154cdf0e10cSrcweir fprintf(fp, "\n};\n\n");
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
157cdf0e10cSrcweir fprintf(fp, "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));
158cdf0e10cSrcweir fprintf (fp, "}\n");
159cdf0e10cSrcweir
160cdf0e10cSrcweir fclose(fp);
161cdf0e10cSrcweir return 0;
162cdf0e10cSrcweir } // End of main
163