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 ************************************************************************/
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
;
45 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
49 if (argc
< 4) exit(-1);
51 fp
= fopen(argv
[1], "rb"); // open the source file for read;
53 fprintf(stderr
, "Opening the rule source file %s for reading failed: %s\n", argv
[1], strerror(errno
));
59 sal_Int32 address
[MAX_ADDRESS
];
60 for (i
=0; i
<MAX_ADDRESS
; i
++) address
[i
]=-1;
61 OUString sep
=OUString(sal_Unicode('|'));
66 while (fgets(str
, 1024, fp
)) {
67 // don't convert last new line character to Ostr.
68 sal_Int32 len
= strlen(str
) - 1;
70 if (len
== 0 || str
[0] == '#')
73 // input file is in UTF-8 encoding
74 OUString Ostr
= OUString((const sal_Char
*)str
, len
, RTL_TEXTENCODING_UTF8
);
75 len
= Ostr
.getLength();
77 continue; // skip empty line.
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
);
85 OUString key
=Ostr
.copy(nPos
)+sep
;
86 sal_Int32 idx
= result
.indexOf(key
);
87 if (key
.getLength() > max
) max
=key
.getLength();
91 address
[nChar
]=result
.getLength();
97 fp
= fopen(argv
[2], "wb");
99 fprintf(stderr
, "Opening %s for writing failed: %s\n", argv
[2], strerror(errno
));
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
++) {
115 for (j
=0; j
<0x100; j
++) {
116 if (address
[i
*0x100+j
] >=0) {
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");
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
];
142 sal::static_int_cast
< unsigned long >(
143 ad
== -1 ? 0 : max
== 2 ? ustr
[ad
] : ad
));
148 fprintf(fp
, "\n};\n\n");
151 fprintf(fp
, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n");
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
));
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */