]> vaikene.ee Git - evaf/blob - src/libs/Common/util.cpp
Warning fixes and copyright update.
[evaf] / src / libs / Common / util.cpp
1 /**
2 * @file Common/util.cpp
3 * @brief Global utility functions for eVaf
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011-2019 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
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.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "util.h"
21
22 #include <stdio.h>
23
24 //-------------------------------------------------------------------
25
26 QVariant eVaf::Common::toVariant(QString const & value, QVariant const & defaultValue)
27 {
28 // If the default value is not valid, return the string value without conversions
29 if (!defaultValue.isValid())
30 return QVariant(value);
31
32 // Convert to the expected type
33 switch (defaultValue.type()) {
34 case QVariant::UInt: {
35 bool ok;
36 uint const v = value.toUInt(&ok, 0);
37 if (ok)
38 return QVariant(v);
39 else
40 return defaultValue;
41 }
42 case QVariant::Int: {
43 bool ok;
44 int const v = value.toInt(&ok, 0);
45 if (ok)
46 return QVariant(v);
47 else
48 return defaultValue;
49 }
50 case QVariant::Double: {
51 bool ok;
52 double const v = value.toDouble(&ok);
53 if (ok)
54 return QVariant(v);
55 else
56 return defaultValue;
57 }
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);
63 else {
64 bool ok;
65 uint const v = value.toUInt(&ok, 0);
66 if (ok)
67 return QVariant(v);
68 else
69 return defaultValue;
70 }
71 }
72 case QVariant::Char: {
73 if (value.size() > 0) {
74 if (value.startsWith("\\0x")) {
75 bool ok;
76 char const c = static_cast<char>(value.mid(1).toUInt(&ok, 16));
77 if (ok)
78 return QVariant(c);
79 }
80 else if (value.startsWith("\\0")) {
81 bool ok;
82 char const c = static_cast<char>(value.mid(1).toUInt(&ok, 8));
83 if (ok)
84 return QVariant(c);
85 }
86 else if (value.startsWith('&')) {
87 QString const c = strFromEscapedCharArray(value.toLatin1());
88 if (c.size() > 0)
89 return QVariant(c.at(0));
90 }
91 else
92 return QVariant(value.at(0));
93 }
94 return defaultValue;
95 }
96 default:
97 return QVariant(value);
98 }
99 }
100
101 QByteArray eVaf::Common::strToEscapedCharArray(QString const & str)
102 {
103 QByteArray rval;
104 foreach (QChar c, str) {
105 if (c.unicode() < 32 || c.unicode() >= 127)
106 rval.append("&#x" + QByteArray::number(c.unicode(), 16) + ";");
107 else if (c == '\"')
108 rval.append("&quot;");
109 else if (c == '&')
110 rval.append("&amp;");
111 else if (c == '\'')
112 rval.append("&apos;");
113 else if (c == '<')
114 rval.append("&lt;");
115 else if (c == '>')
116 rval.append("&gt;");
117 else
118 rval.append(static_cast<char const>(c.unicode()));
119 }
120
121 return rval;
122 }
123
124 QString eVaf::Common::strFromEscapedCharArray(QByteArray const & str)
125 {
126 QString rval;
127
128 bool e = false;
129 QByteArray ref;
130 foreach (char c, str) {
131 if (!e) {
132 if (c == '&') {
133 e = true;
134 ref = "&";
135 }
136 else
137 rval.append(QChar(static_cast<ushort>(c)));
138 }
139 else {
140 ref.append(c);
141 if (c == ';') {
142 e = false;
143 ref = ref.toLower();
144
145 if (ref.startsWith("&#x")) {
146 // Numeric character reference in the HEX format
147 bool ok;
148 ushort const ucode = static_cast<ushort>(ref.mid(3, ref.size() - 4).toUInt(&ok, 16));
149 if (ok)
150 rval.append(QChar(ucode));
151 else
152 // Invalid numeric character reference; output as is
153 rval.append(ref);
154 }
155 else if (ref.startsWith("&#")) {
156 // Numeric character reference in the DEC format
157 bool ok;
158 ushort const ucode = static_cast<ushort>(ref.mid(2, ref.size() - 3).toUInt(&ok, 10));
159 if (ok)
160 rval.append(QChar(ucode));
161 else
162 // Invalid numeric character reference; output as is
163 rval.append(ref);
164 }
165 else if (ref == "&quot;")
166 rval.append('\"');
167 else if (ref == "&amp;")
168 rval.append('&');
169 else if (ref == "&apos;")
170 rval.append('\'');
171 else if (ref == "&lt;")
172 rval.append('<');
173 else if (ref == "&gt;")
174 rval.append('>');
175 else
176 // Unknown reference, output as is
177 rval.append(ref);
178 }
179 }
180 }
181
182 return rval;
183 }
184
185 QByteArray eVaf::Common::binToEscapedCharArray(QByteArray const & src)
186 {
187 QByteArray rval;
188 foreach (char const c, src) {
189 if (c < 32 || c >= 127)
190 rval.append("&#x" + QByteArray::number(c, 16) + ";");
191 else if (c == '\"')
192 rval.append("&quot;");
193 else if (c == '&')
194 rval.append("&amp;");
195 else if (c == '\'')
196 rval.append("&apos;");
197 else if (c == '<')
198 rval.append("&lt;");
199 else if (c == '>')
200 rval.append("&gt;");
201 else
202 rval.append(c);
203 }
204
205 return rval;
206 }
207
208 QByteArray eVaf::Common::binFromEscapedCharArray(QByteArray const & str)
209 {
210 QByteArray rval;
211
212 bool e = false;
213 QByteArray ref;
214 foreach (char c, str) {
215 if (!e) {
216 if (c == '&') {
217 e = true;
218 ref = "&";
219 }
220 else
221 rval.append(c);
222 }
223 else {
224 ref.append(c);
225 if (c == ';') {
226 e = false;
227 ref = ref.toLower();
228
229 if (ref.startsWith("&#x")) {
230 // Numeric character reference in the HEX format
231 bool ok;
232 char const ucode = static_cast<char>(ref.mid(3, ref.size() - 4).toUInt(&ok, 16));
233 if (ok)
234 rval.append(ucode);
235 else
236 // Invalid numeric character reference; output as is
237 rval.append(ref);
238 }
239 else if (ref.startsWith("&#")) {
240 // Numeric character reference in the DEC format
241 bool ok;
242 char const ucode = static_cast<char>(ref.mid(2, ref.size() - 3).toUInt(&ok, 10));
243 if (ok)
244 rval.append(ucode);
245 else
246 // Invalid numeric character reference; output as is
247 rval.append(ref);
248 }
249 else if (ref == "&quot;")
250 rval.append('\"');
251 else if (ref == "&amp;")
252 rval.append('&');
253 else if (ref == "&apos;")
254 rval.append('\'');
255 else if (ref == "&lt;")
256 rval.append('<');
257 else if (ref == "&gt;")
258 rval.append('>');
259 else
260 // Unknown reference, output as is
261 rval.append(ref);
262 }
263 }
264 }
265
266 return rval;
267 }