summaryrefslogtreecommitdiffstats
path: root/tools/source/reversemap
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /tools/source/reversemap
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/source/reversemap')
-rw-r--r--tools/source/reversemap/bestreversemap.cxx154
1 files changed, 154 insertions, 0 deletions
diff --git a/tools/source/reversemap/bestreversemap.cxx b/tools/source/reversemap/bestreversemap.cxx
new file mode 100644
index 000000000..f124140c1
--- /dev/null
+++ b/tools/source/reversemap/bestreversemap.cxx
@@ -0,0 +1,154 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+#include <rtl/textcvt.h>
+
+#include <cstdlib>
+#include <iterator>
+#include <stdio.h>
+
+namespace {
+
+struct Encoder
+{
+ rtl_UnicodeToTextConverter m_aConverter;
+ bool m_bCapable;
+ const char *m_pEncoding;
+ Encoder(rtl_TextEncoding nEncoding, const char *pEncoding)
+ : m_aConverter(rtl_createUnicodeToTextConverter(nEncoding))
+ , m_bCapable(true)
+ , m_pEncoding(pEncoding)
+ {
+ }
+ ~Encoder()
+ {
+ rtl_destroyUnicodeToTextConverter(m_aConverter);
+ }
+ void checkSupports(sal_Unicode c)
+ {
+ char aTempArray[8];
+ sal_Size nTempSize;
+ sal_uInt32 nCvtInfo;
+
+ sal_Size nChars = rtl_convertUnicodeToText(m_aConverter,
+ nullptr, &c, 1, aTempArray, sizeof(aTempArray),
+ RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
+ &nCvtInfo, &nTempSize);
+ m_bCapable = nChars > 0;
+ }
+ void reset()
+ {
+ m_bCapable = true;
+ }
+ bool isOK() const
+ {
+ return m_bCapable;
+ }
+ const char* getName() const
+ {
+ return m_pEncoding;
+ }
+
+};
+
+}
+
+int main()
+{
+# define EXP(x) Encoder(x, #x)
+
+ Encoder aConverters[15] =
+ {
+ EXP(RTL_TEXTENCODING_MS_1361),
+ EXP(RTL_TEXTENCODING_MS_950),
+ EXP(RTL_TEXTENCODING_MS_949),
+ EXP(RTL_TEXTENCODING_MS_936),
+ EXP(RTL_TEXTENCODING_MS_932),
+ EXP(RTL_TEXTENCODING_MS_874),
+ EXP(RTL_TEXTENCODING_MS_1258),
+ EXP(RTL_TEXTENCODING_MS_1257),
+ EXP(RTL_TEXTENCODING_MS_1256),
+ EXP(RTL_TEXTENCODING_MS_1255),
+ EXP(RTL_TEXTENCODING_MS_1254),
+ EXP(RTL_TEXTENCODING_MS_1253),
+ EXP(RTL_TEXTENCODING_MS_1251),
+ EXP(RTL_TEXTENCODING_MS_1250),
+ EXP(RTL_TEXTENCODING_MS_1252)
+ };
+
+ printf("//Do not edit manually, generated from bestreversemap.cxx\n");
+ printf("#include <rtl/textenc.h>\n");
+ printf("#include <tools/tenccvt.hxx>\n");
+ printf("rtl_TextEncoding getBestMSEncodingByChar(sal_Unicode c)\n");
+ printf("{\n");
+
+ sal_Unicode c = 0;
+ while (c < 0xFFFF)
+ {
+ for (size_t i = 0; i < std::size(aConverters); ++i)
+ aConverters[i].reset();
+
+ int nMostCapable = -1;
+
+ while(c < 0xFFFF)
+ {
+ bool bSomethingCapable = false;
+ for (size_t i = 0; i < std::size(aConverters); ++i)
+ {
+ if (aConverters[i].isOK())
+ aConverters[i].checkSupports(c);
+ if (aConverters[i].isOK())
+ {
+ bSomethingCapable = true;
+ nMostCapable = i;
+ }
+ }
+ if (!bSomethingCapable)
+ break;
+ ++c;
+ }
+ sal_Unicode cEnd = c;
+ printf(" if (c < 0x%x)\n", c);
+ printf(" return %s;\n", aConverters[nMostCapable].getName());
+ while(c < 0xFFFF)
+ {
+ bool bNothingCapable = true;
+ for (size_t i = 0; i < std::size(aConverters); ++i)
+ {
+ aConverters[i].checkSupports(c);
+ if (aConverters[i].isOK())
+ {
+ bNothingCapable = false;
+ break;
+ }
+ }
+ if (!bNothingCapable)
+ break;
+ ++c;
+ }
+ if (cEnd != c)
+ {
+ if (c < 0xFFFF)
+ {
+ printf(" if (c < 0x%x)\n", c);
+ printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
+ }
+ else
+ printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
+ }
+ }
+
+ printf("}\n");
+ fflush(stdout);
+
+ return EXIT_SUCCESS;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */