]>
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);
90 else if (value
.startsWith('&')) {
91 QString c
= strFromEscapedCharArray(value
.toLatin1());
93 return QVariant(c
.at(0));
96 return QVariant(value
.at(0));
102 return QVariant(value
);
106 QByteArray
eVaf::Common::strToEscapedCharArray(QString
const & str
)
109 foreach (QChar c
, str
) {
110 if (c
.unicode() < 32 || c
.unicode() >= 127)
111 rval
.append("&#x" + QByteArray::number(c
.unicode(), 16) + ";");
113 rval
.append(""");
115 rval
.append("&");
117 rval
.append("'");
123 rval
.append((char const)c
.unicode());
129 QString
eVaf::Common::strFromEscapedCharArray(QByteArray
const & str
)
135 foreach (char c
, str
) {
142 rval
.append(QChar((ushort
)c
));
150 if (ref
.startsWith("&#x")) {
151 // Numeric character reference in the HEX format
153 ushort ucode
= ref
.mid(3, ref
.size() - 4).toUInt(&ok
, 16);
155 rval
.append(QChar(ucode
));
157 // Invalid numeric character reference; output as is
160 else if (ref
.startsWith("&#")) {
161 // Numeric character reference in the DEC format
163 ushort ucode
= ref
.mid(2, ref
.size() - 3).toUInt(&ok
, 10);
165 rval
.append(QChar(ucode
));
167 // Invalid numeric character reference; output as is
170 else if (ref
== """)
172 else if (ref
== "&")
174 else if (ref
== "'")
176 else if (ref
== "<")
178 else if (ref
== ">")
181 // Unknown reference, output as is
190 QByteArray
eVaf::Common::binToEscapedCharArray(QByteArray
const & src
)
193 foreach (uchar c
, src
) {
194 if (c
< 32 || c
>= 127)
195 rval
.append("&#x" + QByteArray::number(c
, 16) + ";");
197 rval
.append(""");
199 rval
.append("&");
201 rval
.append("'");
213 QByteArray
eVaf::Common::binFromEscapedCharArray(QByteArray
const & str
)
219 foreach (char c
, str
) {
234 if (ref
.startsWith("&#x")) {
235 // Numeric character reference in the HEX format
237 uchar ucode
= ref
.mid(3, ref
.size() - 4).toUInt(&ok
, 16);
241 // Invalid numeric character reference; output as is
244 else if (ref
.startsWith("&#")) {
245 // Numeric character reference in the DEC format
247 uchar ucode
= ref
.mid(2, ref
.size() - 3).toUInt(&ok
, 10);
251 // Invalid numeric character reference; output as is
254 else if (ref
== """)
256 else if (ref
== "&")
258 else if (ref
== "'")
260 else if (ref
== "<")
262 else if (ref
== ">")
265 // Unknown reference, output as is