LibreOffice Module tools (master) 1
bestreversemap.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
10#include <sal/config.h>
11#include <rtl/textcvt.h>
12
13#include <cstdlib>
14#include <iterator>
15#include <stdio.h>
16
17namespace {
18
19struct Encoder
20{
21 rtl_UnicodeToTextConverter m_aConverter;
22 bool m_bCapable;
23 const char *m_pEncoding;
24 Encoder(rtl_TextEncoding nEncoding, const char *pEncoding)
25 : m_aConverter(rtl_createUnicodeToTextConverter(nEncoding))
26 , m_bCapable(true)
27 , m_pEncoding(pEncoding)
28 {
29 }
30 ~Encoder()
31 {
32 rtl_destroyUnicodeToTextConverter(m_aConverter);
33 }
34 void checkSupports(sal_Unicode c)
35 {
36 char aTempArray[8];
37 sal_Size nTempSize;
38 sal_uInt32 nCvtInfo;
39
40 sal_Size nChars = rtl_convertUnicodeToText(m_aConverter,
41 nullptr, &c, 1, aTempArray, sizeof(aTempArray),
42 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
43 &nCvtInfo, &nTempSize);
44 m_bCapable = nChars > 0;
45 }
46 void reset()
47 {
48 m_bCapable = true;
49 }
50 bool isOK() const
51 {
52 return m_bCapable;
53 }
54 const char* getName() const
55 {
56 return m_pEncoding;
57 }
58
59};
60
61}
62
63int main()
64{
65# define EXP(x) Encoder(x, #x)
66
67 Encoder aConverters[15] =
68 {
69 EXP(RTL_TEXTENCODING_MS_1361),
70 EXP(RTL_TEXTENCODING_MS_950),
71 EXP(RTL_TEXTENCODING_MS_949),
72 EXP(RTL_TEXTENCODING_MS_936),
73 EXP(RTL_TEXTENCODING_MS_932),
74 EXP(RTL_TEXTENCODING_MS_874),
75 EXP(RTL_TEXTENCODING_MS_1258),
76 EXP(RTL_TEXTENCODING_MS_1257),
77 EXP(RTL_TEXTENCODING_MS_1256),
78 EXP(RTL_TEXTENCODING_MS_1255),
79 EXP(RTL_TEXTENCODING_MS_1254),
80 EXP(RTL_TEXTENCODING_MS_1253),
81 EXP(RTL_TEXTENCODING_MS_1251),
82 EXP(RTL_TEXTENCODING_MS_1250),
83 EXP(RTL_TEXTENCODING_MS_1252)
84 };
85
86 printf("//Do not edit manually, generated from bestreversemap.cxx\n");
87 printf("#include <rtl/textenc.h>\n");
88 printf("#include <tools/tenccvt.hxx>\n");
89 printf("rtl_TextEncoding getBestMSEncodingByChar(sal_Unicode c)\n");
90 printf("{\n");
91
92 sal_Unicode c = 0;
93 while (c < 0xFFFF)
94 {
95 for (size_t i = 0; i < std::size(aConverters); ++i)
96 aConverters[i].reset();
97
98 int nMostCapable = -1;
99
100 while(c < 0xFFFF)
101 {
102 bool bSomethingCapable = false;
103 for (size_t i = 0; i < std::size(aConverters); ++i)
104 {
105 if (aConverters[i].isOK())
106 aConverters[i].checkSupports(c);
107 if (aConverters[i].isOK())
108 {
109 bSomethingCapable = true;
110 nMostCapable = i;
111 }
112 }
113 if (!bSomethingCapable)
114 break;
115 ++c;
116 }
117 sal_Unicode cEnd = c;
118 printf(" if (c < 0x%x)\n", c);
119 printf(" return %s;\n", aConverters[nMostCapable].getName());
120 while(c < 0xFFFF)
121 {
122 bool bNothingCapable = true;
123 for (size_t i = 0; i < std::size(aConverters); ++i)
124 {
125 aConverters[i].checkSupports(c);
126 if (aConverters[i].isOK())
127 {
128 bNothingCapable = false;
129 break;
130 }
131 }
132 if (!bNothingCapable)
133 break;
134 ++c;
135 }
136 if (cEnd != c)
137 {
138 if (c < 0xFFFF)
139 {
140 printf(" if (c < 0x%x)\n", c);
141 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
142 }
143 else
144 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
145 }
146 }
147
148 printf("}\n");
149 fflush(stdout);
150
151 return EXIT_SUCCESS;
152}
153
154/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define EXP(x)
int main()
int i
sal_uInt16 sal_Unicode