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