bump product version to 6.3.0.0.beta1
[LibreOffice.git] / i18npool / source / indexentry / genindex_data.cxx
bloba6f054138df332ce1717e173dbeab8733fe89e30
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <sal/main.h>
26 #include <sal/types.h>
27 #include <rtl/ustring.hxx>
28 #include <rtl/ustrbuf.hxx>
30 #define MAX_ADDRESS 0x30000
31 #define MAX_INDEX MAX_ADDRESS/0x100
33 /* Main Procedure */
35 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
37 FILE *fp;
39 if (argc < 4) exit(-1);
41 fp = fopen(argv[1], "rb"); // open the source file for read;
42 if (fp == nullptr) {
43 fprintf(stderr, "Opening the rule source file %s for reading failed: %s\n", argv[1], strerror(errno));
44 exit(1);
48 sal_Int32 i, j, k;
49 sal_Int32 address[MAX_ADDRESS];
50 for (i=0; i<MAX_ADDRESS; i++) address[i]=-1;
51 OUString sep('|');
52 OUStringBuffer result=sep;
53 sal_Int32 max=0;
55 sal_Char str[1024];
56 while (fgets(str, 1024, fp)) {
57 // don't convert last new line character to Ostr.
58 sal_Int32 len = strlen(str) - 1;
59 // skip comment line
60 if (len == 0 || str[0] == '#')
61 continue;
63 // input file is in UTF-8 encoding
64 OUString Ostr = OUString(str, len, RTL_TEXTENCODING_UTF8);
65 len = Ostr.getLength();
66 if (len == 0)
67 continue; // skip empty line.
69 sal_Int32 nPos=0;
70 sal_uInt32 nChar = Ostr.iterateCodePoints(&nPos, 2);
71 if (nChar >= MAX_ADDRESS) {
72 printf("Code point 0x%lx exceeds MAX_ADDRESS 0x%x, Please increase MAX_ADDRESS", static_cast<long unsigned int>(nChar), MAX_ADDRESS);
73 exit(1);
75 OUString key=Ostr.copy(nPos)+sep;
76 sal_Int32 idx = result.indexOf(key);
77 if (key.getLength() > max) max=key.getLength();
78 if (idx >= 0) {
79 address[nChar]=idx;
80 } else {
81 address[nChar]=result.getLength();
82 result.append(key);
85 fclose(fp);
87 fp = fopen(argv[2], "wb");
88 if (fp == nullptr) {
89 fprintf(stderr, "Opening %s for writing failed: %s\n", argv[2], strerror(errno));
90 exit(1);
93 fprintf(fp, "/*\n");
94 fprintf(fp, " * Copyright(c) 1999 - 2006, Sun Microsystems, Inc.\n");
95 fprintf(fp, " * All Rights Reserved.\n");
96 fprintf(fp, " */\n\n");
97 fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
98 fprintf(fp, "#include <sal/types.h>\n");
99 fprintf(fp, "\nextern \"C\" {\n");
101 sal_Int32 index[MAX_INDEX];
102 sal_Int32 max_index=0;
103 for (i=k=0; i<MAX_INDEX; i++) {
104 index[i] = 0xFFFF;
105 for (j=0; j<0x100; j++) {
106 if (address[i*0x100+j] >=0) {
107 max_index=i;
108 index[i]=0x100*k++;
109 break;
114 fprintf(fp, "\nstatic const sal_uInt16 idx1[] = {");
115 for (i = k = 0; i <= max_index; i++) {
116 if (k++ % 16 == 0) fprintf(fp, "\n\t");
117 fprintf(
118 fp, "0x%04lx, ", sal::static_int_cast< unsigned long >(index[i]));
120 fprintf(fp, "\n};\n\n");
122 sal_Int32 len=result.getLength();
123 fprintf(fp, "\nstatic const sal_uInt16 idx2[] = {");
124 for (i = k = 0; i <= max_index; i++) {
125 if (index[i] != 0xFFFF) {
126 for (j = 0; j<0x100; j++) {
127 if (k++ % 16 == 0) fprintf(fp, "\n\t");
128 sal_Int32 ad=address[i*0x100+j];
129 fprintf(
130 fp, "0x%04lx, ",
131 sal::static_int_cast< unsigned long >(
132 ad == -1 ? 0 : max == 2 ? result[ad] : ad));
134 fprintf(fp, "\n\t");
137 fprintf(fp, "\n};\n\n");
139 if (max == 2) {
140 fprintf(fp, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n");
141 } else {
142 fprintf(fp, "\nstatic const sal_uInt16 idx3[] = {");
143 for (i = k = 0; i < len; i++) {
144 if (k++ % 16 == 0) fprintf(fp, "\n\t");
145 fprintf(fp, "0x%04x, ", (sep.toChar() == result[i]) ? 0 : result[i]);
147 fprintf(fp, "\n};\n\n");
150 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));
151 fprintf (fp, "}\n");
153 fclose(fp);
154 return 0;
155 } // End of main
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */