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