Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / i18npool / source / indexentry / genindex_data.cxx
blob1acd230d0b88a0803e0bbf1a881e8b4a5041a73a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <sal/main.h>
35 #include <sal/types.h>
36 #include <rtl/ustring.hxx>
38 #define MAX_ADDRESS 0x30000
39 #define MAX_INDEX MAX_ADDRESS/0x100
41 using namespace ::rtl;
43 /* Main Procedure */
45 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
47 FILE *fp;
49 if (argc < 4) exit(-1);
51 fp = fopen(argv[1], "rb"); // open the source file for read;
52 if (fp == NULL) {
53 fprintf(stderr, "Opening the rule source file %s for reading failed: %s\n", argv[1], strerror(errno));
54 exit(1);
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;
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;
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.
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);
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;
95 fclose(fp);
97 fp = fopen(argv[2], "wb");
98 if (fp == NULL) {
99 fprintf(stderr, "Opening %s for writing failed: %s\n", argv[2], strerror(errno));
100 exit(1);
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");
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;
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]));
130 fprintf(fp, "\n};\n\n");
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));
145 fprintf(fp, "\n\t");
148 fprintf(fp, "\n};\n\n");
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]);
158 fprintf(fp, "\n};\n\n");
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");
164 fclose(fp);
165 return 0;
166 } // End of main
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */