Update ooo320-m1
[ooovba.git] / i18npool / source / indexentry / genindex_data.cxx
blobaf650ffad4b01a7f058d8f7418204c439257b294
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: genindex_data.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sal/main.h>
38 #include <sal/types.h>
39 #include <rtl/ustring.hxx>
41 #define MAX_ADDRESS 0x30000
42 #define MAX_INDEX MAX_ADDRESS/0x100
44 using namespace ::rtl;
46 /* Main Procedure */
48 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
50 FILE *fp;
52 if (argc < 4) exit(-1);
54 fp = fopen(argv[1], "rb"); // open the source file for read;
55 if (fp == NULL) {
56 printf("Open the rule source file failed.");
57 return 1;
61 sal_Int32 i, j, k;
62 sal_Int32 address[MAX_ADDRESS];
63 for (i=0; i<MAX_ADDRESS; i++) address[i]=-1;
64 OUString sep=OUString(sal_Unicode('|'));
65 OUString result=sep;
66 sal_Int32 max=0;
68 sal_Char str[1024];
69 while (fgets(str, 1024, fp)) {
70 // don't convert last new line character to Ostr.
71 sal_Int32 len = strlen(str) - 1;
72 // skip comment line
73 if (len == 0 || str[0] == '#')
74 continue;
76 // input file is in UTF-8 encoding
77 OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8);
78 len = Ostr.getLength();
79 if (len == 0)
80 continue; // skip empty line.
82 sal_Int32 nPos=0;
83 sal_uInt32 nChar = Ostr.iterateCodePoints(&nPos, 2);
84 if (nChar > MAX_ADDRESS) {
85 printf("Code point 0x%lx exceeds MAX_ADDRESS 0x%x, Please increase MAX_ADDRESS", static_cast<long unsigned int>(nChar), MAX_ADDRESS);
86 exit(1);
88 OUString key=Ostr.copy(nPos)+sep;
89 sal_Int32 idx = result.indexOf(key);
90 if (key.getLength() > max) max=key.getLength();
91 if (idx >= 0) {
92 address[nChar]=idx;
93 } else {
94 address[nChar]=result.getLength();
95 result+=key;
98 fclose(fp);
100 fp = fopen(argv[2], "wb");
101 if (fp == NULL) {
102 printf("Can't create the C source file.");
103 return 1;
106 fprintf(fp, "/*\n");
107 fprintf(fp, " * Copyright(c) 1999 - 2006, Sun Microsystems, Inc.\n");
108 fprintf(fp, " * All Rights Reserved.\n");
109 fprintf(fp, " */\n\n");
110 fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
111 fprintf(fp, "#include <sal/types.h>\n");
112 fprintf(fp, "\nextern \"C\" {\n");
114 sal_Int32 index[MAX_INDEX];
115 sal_Int32 max_index=0;
116 for (i=k=0; i<MAX_INDEX; i++) {
117 index[i] = 0xFFFF;
118 for (j=0; j<0x100; j++) {
119 if (address[i*0x100+j] >=0) {
120 max_index=i;
121 index[i]=0x100*k++;
122 break;
127 fprintf(fp, "\nstatic const sal_uInt16 idx1[] = {");
128 for (i = k = 0; i <= max_index; i++) {
129 if (k++ % 16 == 0) fprintf(fp, "\n\t");
130 fprintf(
131 fp, "0x%04lx, ", sal::static_int_cast< unsigned long >(index[i]));
133 fprintf(fp, "\n};\n\n");
135 sal_Int32 len=result.getLength();
136 const sal_Unicode *ustr=result.getStr();
137 fprintf(fp, "\nstatic const sal_uInt16 idx2[] = {");
138 for (i = k = 0; i <= max_index; i++) {
139 if (index[i] != 0xFFFF) {
140 for (j = 0; j<0x100; j++) {
141 if (k++ % 16 == 0) fprintf(fp, "\n\t");
142 sal_Int32 ad=address[i*0x100+j];
143 fprintf(
144 fp, "0x%04lx, ",
145 sal::static_int_cast< unsigned long >(
146 ad == -1 ? 0 : max == 2 ? ustr[ad] : ad));
148 fprintf(fp, "\n\t");
151 fprintf(fp, "\n};\n\n");
153 if (max == 2) {
154 fprintf(fp, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n");
155 } else {
156 fprintf(fp, "\nstatic const sal_uInt16 idx3[] = {");
157 for (i = k = 0; i < len; i++) {
158 if (k++ % 16 == 0) fprintf(fp, "\n\t");
159 fprintf(fp, "0x%04x, ", (sep.toChar() == ustr[i]) ? 0 : ustr[i]);
161 fprintf(fp, "\n};\n\n");
164 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));
165 fprintf (fp, "}\n");
167 fclose(fp);
168 return 0;
169 } // End of main