]>
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 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 v
= value
.toUInt(&ok
, 0);
45 int v
= value
.toInt(&ok
, 0);
52 case QVariant::Double
: {
54 double v
= value
.toDouble(&ok
);
61 case QVariant::Bool
: {
62 if (eVaf::Common::isTrue(value
))
63 return QVariant(true);
64 else if (eVaf::Common::isFalse(value
))
65 return QVariant(false);
68 uint v
= value
.toUInt(&ok
, 0);
76 case QVariant::Char
: {
77 if (value
.size() > 0) {
78 if (value
.startsWith("\\0x")) {
80 char c
= value
.mid(1).toUInt(&ok
, 16);
84 else if (value
.startsWith("\\0")) {
86 char c
= value
.mid(1).toUInt(&ok
, 8);
91 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((char const)c
.unicode());
124 QString
eVaf::Common::strFromEscapedCharArray(QByteArray
const & str
)
130 foreach (char c
, str
) {
137 rval
.append(QChar((ushort
)c
));
145 if (ref
.startsWith("&#x")) {
146 // Numeric character reference in the HEX format
148 ushort ucode
= 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 ucode
= 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 (uchar 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 uchar ucode
= 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 uchar ucode
= 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