BibleTime
cp1252.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "cp1252.h"
14 
15 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
16 #include <QStringDecoder>
17 #include <QStringEncoder>
18 #else
19 #include <QTextCodec>
20 #endif
21 #include "btassert.h"
22 
23 
24 namespace util {
25 namespace cp1252 {
26 
27 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
28 namespace {
29 
30 QTextCodec const & codec() {
31  static auto const * const codec =
32  QTextCodec::codecForName(QByteArrayLiteral("Windows-1252"));
33  BT_ASSERT(codec);
34  return *codec;
35 }
36 
37 } // anonymous namespace
38 #endif
39 
40 QString toUnicode(QByteArray const & data) {
41  #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
42  return codec().toUnicode(data);
43  #else
44  QStringDecoder decoder("Windows-1252", QStringDecoder::Flag::Stateless);
45  // Do not use auto here due to QTBUG-117705/QTBUG-117902:
46  QString result = decoder(data);
47  BT_ASSERT(!decoder.hasError());
48  return result;
49  #endif
50 }
51 
52 QByteArray fromUnicode(QString const & str) {
53  #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
54  return codec().fromUnicode(str);
55  #else
56  QStringEncoder encoder("Windows-1252", QStringEncoder::Flag::Stateless);
57  // Do not use auto here due to QTBUG-117705/QTBUG-117902:
58  QByteArray result = encoder(str);
59  BT_ASSERT(!encoder.hasError());
60  return result;
61  #endif
62 }
63 
64 } /* namespace cp1252 { */
65 } /* namespace util { */
#define BT_ASSERT(...)
Definition: btassert.h:17
QString toUnicode(QByteArray const &data)
Definition: cp1252.cpp:40
QByteArray fromUnicode(QString const &str)
Definition: cp1252.cpp:52
Definition: cp1252.cpp:24