merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / collator / gencoll_rule.cxx
blobd450cbaec78a9f670ba83dc34c3eb22c432c2c22
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: gencoll_rule.cxx,v $
10 * $Revision: 1.12 $
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/ustrbuf.hxx>
41 #include "warnings_guard_unicode_tblcoll.h"
43 U_CAPI void U_EXPORT2 uprv_free(void *mem);
45 using namespace ::rtl;
47 /* Main Procedure */
49 void data_write(char* file, char* name, sal_uInt8 *data, sal_Int32 len)
51 FILE *fp = fopen(file, "wb");
52 if (fp == NULL) {
53 printf("Can't create the C source file.");
54 return;
57 fprintf(fp, "/*\n");
58 fprintf(fp, " * Copyright(c) 1999 - 2000, Sun Microsystems, Inc.\n");
59 fprintf(fp, " * All Rights Reserved.\n");
60 fprintf(fp, " */\n\n");
61 fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
62 fprintf(fp, "#include <sal/types.h>\n");
63 fprintf(fp, "\nextern \"C\" {\n");
65 // generate main dict. data array
66 fprintf(fp, "\nstatic const sal_uInt8 %s[] = {", name);
68 sal_Int32 count = 0;
69 for (sal_Int32 i = 0; i < len; i++) {
71 if (count++ % 16 == 0)
72 fprintf(fp, "\n\t");
74 fprintf(fp, "0x%04x, ", data[i]);
76 fprintf(fp, "\n};\n\n");
78 fprintf(fp, "const sal_uInt8* get_%s() { return %s; }\n\n", name, name);
79 fprintf (fp, "}\n");
81 fclose(fp);
85 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
87 FILE *fp;
89 if (argc < 4) exit(-1);
91 fp = fopen(argv[1], "rb"); // open the source file for read;
92 if (fp == NULL)
93 printf("Open the rule source file failed.");
96 sal_Char str[1024];
97 OUStringBuffer Obuf;
98 while (fgets(str, 1024, fp)) {
99 // don't convert last new line character to Ostr.
100 sal_Int32 len = strlen(str) - 1;
101 // skip comment line
102 if (len == 0 || str[0] == '#')
103 continue;
105 // input file is in UTF-8 encoding
106 OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8).trim();
108 len = Ostr.getLength();
109 if (len == 0)
110 continue; // skip empty line.
112 Obuf.append(Ostr);
114 fclose(fp);
116 UErrorCode status = U_ZERO_ERROR;
117 //UParseError parseError;
118 //UCollator *coll = ucol_openRules(Obuf.getStr(), Obuf.getLength(), UCOL_OFF,
119 // UCOL_DEFAULT_STRENGTH, &parseError, &status);
121 RuleBasedCollator *coll = new RuleBasedCollator(reinterpret_cast<const UChar *>(Obuf.getStr()), status); // UChar != sal_Unicode in MinGW
123 if (U_SUCCESS(status)) {
125 int32_t len = 0;
126 uint8_t *data = coll->cloneRuleData(len, status);
128 if (U_SUCCESS(status) && data != NULL)
129 data_write(argv[2], argv[3], data, len);
130 else {
131 printf("Could not get rule data from collator\n");
134 if (data) uprv_free(data);
135 } else {
136 printf("\nRule parsering error\n");
139 if (coll)
140 delete coll;
142 return U_SUCCESS(status) ? 0 : 1;
143 } // End of main