Bump version to 21.06.18.1
[LibreOffice.git] / tools / source / reversemap / bestreversemap.cxx
blob02a81932b3f2022d89a41b27e42232dab3e4361c
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/.
8 */
10 #include <sal/config.h>
11 #include <rtl/textcvt.h>
13 #include <cstdlib>
14 #include <stdio.h>
16 namespace {
18 struct Encoder
20 rtl_UnicodeToTextConverter m_aConverter;
21 bool m_bCapable;
22 const char *m_pEncoding;
23 Encoder(rtl_TextEncoding nEncoding, const char *pEncoding)
24 : m_aConverter(rtl_createUnicodeToTextConverter(nEncoding))
25 , m_bCapable(true)
26 , m_pEncoding(pEncoding)
29 ~Encoder()
31 rtl_destroyUnicodeToTextConverter(m_aConverter);
33 void checkSupports(sal_Unicode c)
35 char aTempArray[8];
36 sal_Size nTempSize;
37 sal_uInt32 nCvtInfo;
39 sal_Size nChars = rtl_convertUnicodeToText(m_aConverter,
40 nullptr, &c, 1, aTempArray, sizeof(aTempArray),
41 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
42 &nCvtInfo, &nTempSize);
43 m_bCapable = nChars > 0;
45 void reset()
47 m_bCapable = true;
49 bool isOK() const
51 return m_bCapable;
53 const char* getName() const
55 return m_pEncoding;
62 int main()
64 # define EXP(x) Encoder(x, #x)
66 Encoder aConverters[15] =
68 EXP(RTL_TEXTENCODING_MS_1361),
69 EXP(RTL_TEXTENCODING_MS_950),
70 EXP(RTL_TEXTENCODING_MS_949),
71 EXP(RTL_TEXTENCODING_MS_936),
72 EXP(RTL_TEXTENCODING_MS_932),
73 EXP(RTL_TEXTENCODING_MS_874),
74 EXP(RTL_TEXTENCODING_MS_1258),
75 EXP(RTL_TEXTENCODING_MS_1257),
76 EXP(RTL_TEXTENCODING_MS_1256),
77 EXP(RTL_TEXTENCODING_MS_1255),
78 EXP(RTL_TEXTENCODING_MS_1254),
79 EXP(RTL_TEXTENCODING_MS_1253),
80 EXP(RTL_TEXTENCODING_MS_1251),
81 EXP(RTL_TEXTENCODING_MS_1250),
82 EXP(RTL_TEXTENCODING_MS_1252)
85 printf("//Do not edit manually, generated from bestreversemap.cxx\n");
86 printf("#include <rtl/textenc.h>\n");
87 printf("#include <tools/tenccvt.hxx>\n");
88 printf("rtl_TextEncoding getBestMSEncodingByChar(sal_Unicode c)\n");
89 printf("{\n");
91 sal_Unicode c = 0;
92 while (c < 0xFFFF)
94 for (size_t i = 0; i < SAL_N_ELEMENTS(aConverters); ++i)
95 aConverters[i].reset();
97 int nMostCapable = -1;
99 while(c < 0xFFFF)
101 bool bSomethingCapable = false;
102 for (size_t i = 0; i < SAL_N_ELEMENTS(aConverters); ++i)
104 if (aConverters[i].isOK())
105 aConverters[i].checkSupports(c);
106 if (aConverters[i].isOK())
108 bSomethingCapable = true;
109 nMostCapable = i;
112 if (!bSomethingCapable)
113 break;
114 ++c;
116 sal_Unicode cEnd = c;
117 printf(" if (c < 0x%x)\n", c);
118 printf(" return %s;\n", aConverters[nMostCapable].getName());
119 while(c < 0xFFFF)
121 bool bNothingCapable = true;
122 for (size_t i = 0; i < SAL_N_ELEMENTS(aConverters); ++i)
124 aConverters[i].checkSupports(c);
125 if (aConverters[i].isOK())
127 bNothingCapable = false;
128 break;
131 if (!bNothingCapable)
132 break;
133 ++c;
135 if (cEnd != c)
137 if (c < 0xFFFF)
139 printf(" if (c < 0x%x)\n", c);
140 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
142 else
143 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
147 printf("}\n");
148 fflush(stdout);
150 return EXIT_SUCCESS;
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */