1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gencoll_rule.cxx,v $
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"
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
;
49 void data_write(char* file
, char* name
, sal_uInt8
*data
, sal_Int32 len
)
51 FILE *fp
= fopen(file
, "wb");
53 printf("Can't create the C source file.");
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
);
69 for (sal_Int32 i
= 0; i
< len
; i
++) {
71 if (count
++ % 16 == 0)
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
);
85 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
89 if (argc
< 4) exit(-1);
91 fp
= fopen(argv
[1], "rb"); // open the source file for read;
93 printf("Open the rule source file failed.");
98 while (fgets(str
, 1024, fp
)) {
99 // don't convert last new line character to Ostr.
100 sal_Int32 len
= strlen(str
) - 1;
102 if (len
== 0 || str
[0] == '#')
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();
110 continue; // skip empty line.
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
)) {
126 uint8_t *data
= coll
->cloneRuleData(len
, status
);
128 if (U_SUCCESS(status
) && data
!= NULL
)
129 data_write(argv
[2], argv
[3], data
, len
);
131 printf("Could not get rule data from collator\n");
134 if (data
) uprv_free(data
);
136 printf("\nRule parsering error\n");
142 return U_SUCCESS(status
) ? 0 : 1;