summaryrefslogtreecommitdiff
path: root/i18npool/source/indexentry/genindex_data.cxx
blob: 91f9725f493fe1a6ff176a1ceab3a1d00298d173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_i18npool.hxx"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sal/main.h>
#include <sal/types.h>
#include <rtl/ustring.hxx>

#define MAX_ADDRESS 0x30000
#define MAX_INDEX MAX_ADDRESS/0x100

using namespace ::rtl;

/* Main Procedure */

SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
{
    FILE *fp;

    if (argc < 4) exit(-1);

    fp = fopen(argv[1], "rb");  // open the source file for read;
    if (fp == NULL) {
        printf("Open the rule source file failed.");
        return 1;
    }


    sal_Int32 i, j, k;
    sal_Int32 address[MAX_ADDRESS];
    for (i=0; i<MAX_ADDRESS; i++) address[i]=-1;
    OUString sep=OUString(sal_Unicode('|'));
    OUString result=sep;
    sal_Int32 max=0;

    sal_Char str[1024];
    while (fgets(str, 1024, fp)) {
        // don't convert last new line character to Ostr.
        sal_Int32 len = strlen(str) - 1;
        // skip comment line
        if (len == 0 || str[0] == '#')
            continue;

        // input file is in UTF-8 encoding
        OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8);
        len = Ostr.getLength();
        if (len == 0)
            continue; // skip empty line.

        sal_Int32 nPos=0;
        sal_uInt32 nChar = Ostr.iterateCodePoints(&nPos, 2);
        if (nChar > MAX_ADDRESS) {
            printf("Code point 0x%lx exceeds MAX_ADDRESS 0x%x, Please increase MAX_ADDRESS", static_cast<long unsigned int>(nChar), MAX_ADDRESS);
            exit(1);
        }
        OUString key=Ostr.copy(nPos)+sep;
        sal_Int32 idx = result.indexOf(key);
        if (key.getLength() > max) max=key.getLength();
        if (idx >= 0) {
            address[nChar]=idx;
        } else {
            address[nChar]=result.getLength();
            result+=key;
        }
    }
    fclose(fp);

    fp = fopen(argv[2], "wb");
    if (fp == NULL) {
        printf("Can't create the C source file.");
        return 1;
    }

    fprintf(fp, "/*\n");
    fprintf(fp, " * Copyright(c) 1999 - 2006, Sun Microsystems, Inc.\n");
    fprintf(fp, " * All Rights Reserved.\n");
    fprintf(fp, " */\n\n");
    fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
    fprintf(fp, "#include <sal/types.h>\n");
    fprintf(fp, "\nextern \"C\" {\n");

    sal_Int32 index[MAX_INDEX];
    sal_Int32 max_index=0;
    for (i=k=0; i<MAX_INDEX; i++) {
        index[i] = 0xFFFF;
        for (j=0; j<0x100; j++) {
            if (address[i*0x100+j] >=0) {
                max_index=i;
                index[i]=0x100*k++;
                break;
            }
        }
    }

    fprintf(fp, "\nstatic const sal_uInt16 idx1[] = {");
    for (i = k = 0; i <= max_index;  i++) {
        if (k++ % 16 == 0) fprintf(fp, "\n\t");
        fprintf(
            fp, "0x%04lx, ", sal::static_int_cast< unsigned long >(index[i]));
    }
    fprintf(fp, "\n};\n\n");

    sal_Int32 len=result.getLength();
    const sal_Unicode *ustr=result.getStr();
    fprintf(fp, "\nstatic const sal_uInt16 idx2[] = {");
    for (i = k = 0; i <= max_index; i++) {
        if (index[i] != 0xFFFF) {
            for (j = 0; j<0x100; j++) {
                if (k++ % 16 == 0) fprintf(fp, "\n\t");
                sal_Int32 ad=address[i*0x100+j];
                fprintf(
                    fp, "0x%04lx, ",
                    sal::static_int_cast< unsigned long >(
                        ad == -1 ? 0 : max == 2 ? ustr[ad] : ad));
            }
            fprintf(fp, "\n\t");
        }
    }
    fprintf(fp, "\n};\n\n");

    if (max == 2) {
        fprintf(fp, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n");
    } else {
        fprintf(fp, "\nstatic const sal_uInt16 idx3[] = {");
        for (i = k = 0; i < len;  i++) {
            if (k++ % 16 == 0) fprintf(fp, "\n\t");
            fprintf(fp, "0x%04x, ", (sep.toChar() == ustr[i]) ? 0 : ustr[i]);
        }
        fprintf(fp, "\n};\n\n");
    }

    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));
    fprintf (fp, "}\n");

    fclose(fp);
    return 0;
}   // End of main