LibreOffice Module i18npool (master) 1
gencoll_rule.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <memory>
23#include <vector>
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <sal/main.h>
29#include <sal/types.h>
30#include <rtl/ustrbuf.hxx>
31
32#include <unicode/tblcoll.h>
33
34/* Main Procedure */
35
36static void data_write(char* file, char* name, sal_uInt8 *data, sal_Int32 len)
37{
38 FILE *fp = fopen(file, "wb");
39 if (fp == nullptr) {
40 fprintf(stderr, "Opening %s for writing failed: %s\n", file, strerror(errno));
41 exit(1);
42 }
43
44 fprintf(fp, "/*\n");
45 fprintf(fp, " * Copyright(c) 1999 - 2000, Sun Microsystems, Inc.\n");
46 fprintf(fp, " * All Rights Reserved.\n");
47 fprintf(fp, " */\n\n");
48 fprintf(fp, "/* !!!The file is generated automatically. DO NOT edit the file manually!!! */\n\n");
49 fprintf(fp, "#include <sal/types.h>\n");
50 fprintf(fp, "\nextern \"C\" {\n");
51
52 // generate main dict. data array
53 fprintf(fp, "\nalignas(4) static const sal_uInt8 %s[] = {", name);
54
55 sal_Int32 count = 0;
56 for (sal_Int32 i = 0; i < len; i++) {
57
58 if (count++ % 16 == 0)
59 fprintf(fp, "\n\t");
60
61 fprintf(fp, "0x%04x, ", data[i]);
62 }
63 fprintf(fp, "\n};\n\n");
64
65 fprintf(fp, "#ifndef DISABLE_DYNLOADING\n");
66 fprintf(fp, "SAL_DLLPUBLIC_EXPORT const sal_uInt8* get_%s() { return %s; }\n", name, name);
67 fprintf(fp, "SAL_DLLPUBLIC_EXPORT size_t get_%s_length() { return sizeof(%s); }\n", name, name);
68 fprintf(fp, "#else\n");
69 fprintf(fp, "SAL_DLLPUBLIC_EXPORT const sal_uInt8* get_collator_data_%s() { return %s; }\n", name, name);
70 fprintf(fp, "SAL_DLLPUBLIC_EXPORT size_t get_collator_data_%s_length() { return sizeof(%s); }\n", name, name);
71 fprintf(fp, "#endif\n");
72 fprintf(fp, "\n");
73 fprintf (fp, "}\n");
74
75 fclose(fp);
76
77}
78
80{
81 FILE *fp;
82
83 if (argc < 4) exit(-1);
84
85 fp = fopen(argv[1], "rb"); // open the source file for read;
86 if (fp == nullptr){
87 fprintf(stderr, "Opening the rule source file %s for reading failed: %s\n", argv[1], strerror(errno));
88 exit(1);
89 }
90
91 fseek(fp, 0, SEEK_END);
92 int fileSize = ftell(fp);
93 rewind(fp);
94
95 char str[1024];
96 OUStringBuffer Obuf(fileSize);
97 while (fgets(str, 1024, fp)) {
98 // don't convert last new line character to Ostr.
99 sal_Int32 len = strlen(str) - 1;
100 // skip comment line
101 if (len == 0 || str[0] == '#')
102 continue;
103
104 // input file is in UTF-8 encoding
105 OUString Ostr = OUString(str, len, RTL_TEXTENCODING_UTF8).trim();
106
107 len = Ostr.getLength();
108 if (len == 0)
109 continue; // skip empty line.
110
111 Obuf.append(Ostr);
112 }
113 fclose(fp);
114
115 UErrorCode status = U_ZERO_ERROR;
116 //UParseError parseError;
117 //UCollator *coll = ucol_openRules(Obuf.getStr(), Obuf.getLength(), UCOL_OFF,
118 // UCOL_DEFAULT_STRENGTH, &parseError, &status);
119
120 auto coll = std::make_unique<icu::RuleBasedCollator>(reinterpret_cast<const UChar *>(Obuf.getStr()), status);
121
122 if (U_SUCCESS(status)) {
123 std::vector<uint8_t> data;
124 int32_t len = coll->cloneBinary(nullptr, 0, status);
125 if (status == U_BUFFER_OVERFLOW_ERROR) {
126 data.resize(len);
127 status = U_ZERO_ERROR;
128 len = coll->cloneBinary(data.data(), len, status);
129 }
130 if (U_SUCCESS(status))
131 data_write(argv[2], argv[3], data.data(), len);
132 else {
133 printf("Could not get rule data from collator\n");
134 }
135 } else {
136 printf("\nRule parsing error\n");
137 }
138
139 return U_SUCCESS(status) ? 0 : 1;
140} // End of main
141
142/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void data_write(char *file, char *name, sal_uInt8 *data, sal_Int32 len)
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
const char * name
int i
unsigned char sal_uInt8