Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / tools / source / reversemap / bestreversemap.cxx
blob90679654cbbea5f046b8a87c5243453a2cf4ebc1
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 struct Encoder
18 rtl_UnicodeToTextConverter const m_aConverter;
19 bool m_bCapable;
20 const char *m_pEncoding;
21 Encoder(rtl_TextEncoding nEncoding, const char *pEncoding)
22 : m_aConverter(rtl_createUnicodeToTextConverter(nEncoding))
23 , m_bCapable(true)
24 , m_pEncoding(pEncoding)
27 ~Encoder()
29 rtl_destroyUnicodeToTextConverter(m_aConverter);
31 void checkSupports(sal_Unicode c)
33 sal_Char aTempArray[8];
34 sal_Size nTempSize;
35 sal_uInt32 nCvtInfo;
37 sal_Size nChars = rtl_convertUnicodeToText(m_aConverter,
38 nullptr, &c, 1, aTempArray, sizeof(aTempArray),
39 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
40 &nCvtInfo, &nTempSize);
41 m_bCapable = nChars > 0;
43 void reset()
45 m_bCapable = true;
47 bool isOK() const
49 return m_bCapable;
51 const char* getName() const
53 return m_pEncoding;
58 int main()
60 # define EXP(x) Encoder(x, #x)
62 Encoder aConverters[15] =
64 EXP(RTL_TEXTENCODING_MS_1361),
65 EXP(RTL_TEXTENCODING_MS_950),
66 EXP(RTL_TEXTENCODING_MS_949),
67 EXP(RTL_TEXTENCODING_MS_936),
68 EXP(RTL_TEXTENCODING_MS_932),
69 EXP(RTL_TEXTENCODING_MS_874),
70 EXP(RTL_TEXTENCODING_MS_1258),
71 EXP(RTL_TEXTENCODING_MS_1257),
72 EXP(RTL_TEXTENCODING_MS_1256),
73 EXP(RTL_TEXTENCODING_MS_1255),
74 EXP(RTL_TEXTENCODING_MS_1254),
75 EXP(RTL_TEXTENCODING_MS_1253),
76 EXP(RTL_TEXTENCODING_MS_1251),
77 EXP(RTL_TEXTENCODING_MS_1250),
78 EXP(RTL_TEXTENCODING_MS_1252)
81 printf("//Do not edit manually, generated from bestreversemap.cxx\n");
82 printf("#include <rtl/textenc.h>\n");
83 printf("#include <tools/tenccvt.hxx>\n");
84 printf("rtl_TextEncoding getBestMSEncodingByChar(sal_Unicode c)\n");
85 printf("{\n");
87 sal_Unicode c = 0;
88 while (c < 0xFFFF)
90 for (size_t i = 0; i < SAL_N_ELEMENTS(aConverters); ++i)
91 aConverters[i].reset();
93 int nMostCapable = -1;
95 while(c < 0xFFFF)
97 bool bSomethingCapable = false;
98 for (size_t i = 0; i < SAL_N_ELEMENTS(aConverters); ++i)
100 if (aConverters[i].isOK())
101 aConverters[i].checkSupports(c);
102 if (aConverters[i].isOK())
104 bSomethingCapable = true;
105 nMostCapable = i;
108 if (!bSomethingCapable)
109 break;
110 ++c;
112 sal_Unicode cEnd = c;
113 printf(" if (c < 0x%x)\n", c);
114 printf(" return %s;\n", aConverters[nMostCapable].getName());
115 while(c < 0xFFFF)
117 bool bNothingCapable = true;
118 for (size_t i = 0; i < SAL_N_ELEMENTS(aConverters); ++i)
120 aConverters[i].checkSupports(c);
121 if (aConverters[i].isOK())
123 bNothingCapable = false;
124 break;
127 if (!bNothingCapable)
128 break;
129 ++c;
131 if (cEnd != c)
133 if (c < 0xFFFF)
135 printf(" if (c < 0x%x)\n", c);
136 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
138 else
139 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
143 printf("}\n");
144 fflush(stdout);
146 return EXIT_SUCCESS;
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */