]>
vaikene.ee Git - evaf/blob - src/libs/Common/util.cpp
2 * @file Common/util.cpp
3 * @brief Global utility functions for eVaf
6 * Copyright (c) 2011-2019 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
24 //-------------------------------------------------------------------
26 QVariant
eVaf::Common::toVariant(QString
const & value
, QVariant
const & defaultValue
)
28 // If the default value is not valid, return the string value without conversions
29 if (!defaultValue
.isValid())
30 return QVariant(value
);
32 // Convert to the expected type
33 switch (defaultValue
.type()) {
34 case QVariant::UInt
: {
36 uint
const v
= value
.toUInt(&ok
, 0);
44 int const v
= value
.toInt(&ok
, 0);
50 case QVariant::Double
: {
52 double const v
= value
.toDouble(&ok
);
58 case QVariant::Bool
: {
59 if (eVaf::Common::isTrue(value
))
60 return QVariant(true);
61 else if (eVaf::Common::isFalse(value
))
62 return QVariant(false);
65 uint
const v
= value
.toUInt(&ok
, 0);
72 case QVariant::Char
: {
73 if (value
.size() > 0) {
74 if (value
.startsWith("\\0x")) {
76 char const c
= static_cast<char>(value
.mid(1).toUInt(&ok
, 16));
80 else if (value
.startsWith("\\0")) {
82 char const c
= static_cast<char>(value
.mid(1).toUInt(&ok
, 8));
86 else if (value
.startsWith('&')) {
87 QString
const c
= strFromEscapedCharArray(value
.toLatin1());
89 return QVariant(c
.at(0));
92 return QVariant(value
.at(0));
97 return QVariant(value
);
101 QByteArray
eVaf::Common::strToEscapedCharArray(QString
const & str
)
104 foreach (QChar c
, str
) {
105 if (c
.unicode() < 32 || c
.unicode() >= 127)
106 rval
.append("&#x" + QByteArray::number(c
.unicode(), 16) + ";");
108 rval
.append(""");
110 rval
.append("&");
112 rval
.append("'");
118 rval
.append(static_cast<char const>(c
.unicode()));
124 QString
eVaf::Common::strFromEscapedCharArray(QByteArray
const & str
)
130 foreach (char c
, str
) {
137 rval
.append(QChar(static_cast<ushort
>(c
)));
145 if (ref
.startsWith("&#x")) {
146 // Numeric character reference in the HEX format
148 ushort
const ucode
= static_cast<ushort
>(ref
.mid(3, ref
.size() - 4).toUInt(&ok
, 16));
150 rval
.append(QChar(ucode
));
152 // Invalid numeric character reference; output as is
155 else if (ref
.startsWith("&#")) {
156 // Numeric character reference in the DEC format
158 ushort
const ucode
= static_cast<ushort
>(ref
.mid(2, ref
.size() - 3).toUInt(&ok
, 10));
160 rval
.append(QChar(ucode
));
162 // Invalid numeric character reference; output as is
165 else if (ref
== """)
167 else if (ref
== "&")
169 else if (ref
== "'")
171 else if (ref
== "<")
173 else if (ref
== ">")
176 // Unknown reference, output as is
185 QByteArray
eVaf::Common::binToEscapedCharArray(QByteArray
const & src
)
188 foreach (char const c
, src
) {
189 if (c
< 32 || c
>= 127)
190 rval
.append("&#x" + QByteArray::number(c
, 16) + ";");
192 rval
.append(""");
194 rval
.append("&");
196 rval
.append("'");
208 QByteArray
eVaf::Common::binFromEscapedCharArray(QByteArray
const & str
)
214 foreach (char c
, str
) {
229 if (ref
.startsWith("&#x")) {
230 // Numeric character reference in the HEX format
232 char const ucode
= static_cast<char>(ref
.mid(3, ref
.size() - 4).toUInt(&ok
, 16));
236 // Invalid numeric character reference; output as is
239 else if (ref
.startsWith("&#")) {
240 // Numeric character reference in the DEC format
242 char const ucode
= static_cast<char>(ref
.mid(2, ref
.size() - 3).toUInt(&ok
, 10));
246 // Invalid numeric character reference; output as is
249 else if (ref
== """)
251 else if (ref
== "&")
253 else if (ref
== "'")
255 else if (ref
== "<")
257 else if (ref
== ">")
260 // Unknown reference, output as is