Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / collator / gencoll_rule.cxx
blob5ba9f5d31c10283b3db4d28e3c0006e81b66460e
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/ustrbuf.hxx>
29 #include <unicode/tblcoll.h>
31 U_CAPI void U_EXPORT2 uprv_free(void *mem);
33 using namespace ::rtl;
35 /* Main Procedure */
37 void data_write(char* file, char* name, sal_uInt8 *data, sal_Int32 len)
39 FILE *fp = fopen(file, "wb");
40 if (fp == NULL) {
41 fprintf(stderr, "Opening %s for writing failed: %s\n", file, strerror(errno));
42 exit(1);
45 fprintf(fp, "/*\n");
46 fprintf(fp, " * Copyright(c) 1999 - 2000, Sun Microsystems, Inc.\n");
47 fprintf(fp, " * All Rights Reserved.\n");
48 fprintf(fp, " */\n\n");
49 fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n");
50 fprintf(fp, "#include <sal/types.h>\n");
51 fprintf(fp, "\nextern \"C\" {\n");
53 // generate main dict. data array
54 fprintf(fp, "\nstatic const sal_uInt8 %s[] = {", name);
56 sal_Int32 count = 0;
57 for (sal_Int32 i = 0; i < len; i++) {
59 if (count++ % 16 == 0)
60 fprintf(fp, "\n\t");
62 fprintf(fp, "0x%04x, ", data[i]);
64 fprintf(fp, "\n};\n\n");
66 fprintf(fp, "#ifndef DISABLE_DYNLOADING\n");
67 fprintf(fp, "SAL_DLLPUBLIC_EXPORT const sal_uInt8* get_%s() { return %s; }\n", name, name);
68 fprintf(fp, "SAL_DLLPUBLIC_EXPORT size_t get_%s_length() { return sizeof(%s); }\n", name, name);
69 fprintf(fp, "#else\n");
70 fprintf(fp, "SAL_DLLPUBLIC_EXPORT const sal_uInt8* get_collator_data_%s() { return %s; }\n", name, name);
71 fprintf(fp, "SAL_DLLPUBLIC_EXPORT size_t get_collator_data_%s_length() { return sizeof(%s); }\n", name, name);
72 fprintf(fp, "#endif\n");
73 fprintf(fp, "\n");
74 fprintf (fp, "}\n");
76 fclose(fp);
80 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
82 FILE *fp;
84 if (argc < 4) exit(-1);
86 fp = fopen(argv[1], "rb"); // open the source file for read;
87 if (fp == NULL){
88 fprintf(stderr, "Opening the rule source file %s for reading failed: %s\n", argv[1], strerror(errno));
89 exit(1);
92 sal_Char str[1024];
93 OUStringBuffer Obuf;
94 while (fgets(str, 1024, fp)) {
95 // don't convert last new line character to Ostr.
96 sal_Int32 len = strlen(str) - 1;
97 // skip comment line
98 if (len == 0 || str[0] == '#')
99 continue;
101 // input file is in UTF-8 encoding
102 OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8).trim();
104 len = Ostr.getLength();
105 if (len == 0)
106 continue; // skip empty line.
108 Obuf.append(Ostr);
110 fclose(fp);
112 UErrorCode status = U_ZERO_ERROR;
113 //UParseError parseError;
114 //UCollator *coll = ucol_openRules(Obuf.getStr(), Obuf.getLength(), UCOL_OFF,
115 // UCOL_DEFAULT_STRENGTH, &parseError, &status);
117 RuleBasedCollator *coll = new RuleBasedCollator(reinterpret_cast<const UChar *>(Obuf.getStr()), status); // UChar != sal_Unicode in MinGW
119 if (U_SUCCESS(status)) {
121 int32_t len = 0;
122 uint8_t *data = coll->cloneRuleData(len, status);
124 if (U_SUCCESS(status) && data != NULL)
125 data_write(argv[2], argv[3], data, len);
126 else {
127 printf("Could not get rule data from collator\n");
130 if (data) uprv_free(data);
131 } else {
132 printf("\nRule parsering error\n");
135 if (coll)
136 delete coll;
138 return U_SUCCESS(status) ? 0 : 1;
139 } // End of main
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */