]> vaikene.ee Git - evaf/blob - src/libs/Common/util.cpp
30874ebadcaf6bd9df0babe5065b7fe62f9ba376
[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 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 v = value.toUInt(&ok, 0);
37 if (ok)
38 return QVariant(v);
39 else
40 return defaultValue;
41 break;
42 }
43 case QVariant::Int: {
44 bool ok;
45 int v = value.toInt(&ok, 0);
46 if (ok)
47 return QVariant(v);
48 else
49 return defaultValue;
50 break;
51 }
52 case QVariant::Double: {
53 bool ok;
54 double v = value.toDouble(&ok);
55 if (ok)
56 return QVariant(v);
57 else
58 return defaultValue;
59 break;
60 }
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);
66 else {
67 bool ok;
68 uint v = value.toUInt(&ok, 0);
69 if (ok)
70 return QVariant(v);
71 else
72 return defaultValue;
73 }
74 break;
75 }
76 case QVariant::Char: {
77 if (value.size() > 0) {
78 if (value.startsWith("\\0x")) {
79 bool ok;
80 char c = value.mid(1).toUInt(&ok, 16);
81 if (ok)
82 return QVariant(c);
83 }
84 else if (value.startsWith("\\0")) {
85 bool ok;
86 char c = value.mid(1).toUInt(&ok, 8);
87 if (ok)
88 return QVariant(c);
89 }
90 else if (value.startsWith('&')) {
91 QString c = strFromEscapedCharArray(value.toLatin1());
92 if (c.size() > 0)
93 return QVariant(c.at(0));
94 }
95 else
96 return QVariant(value.at(0));
97 }
98 return defaultValue;
99 break;
100 }
101 default:
102 return QVariant(value);
103 }
104 }
105
106 QByteArray eVaf::Common::strToEscapedCharArray(QString const & str)
107 {
108 QByteArray rval;
109 foreach (QChar c, str) {
110 if (c.unicode() < 32 || c.unicode() >= 127)
111 rval.append("&#x" + QByteArray::number(c.unicode(), 16) + ";");
112 else if (c == '\"')
113 rval.append("&quot;");
114 else if (c == '&')
115 rval.append("&amp;");
116 else if (c == '\'')
117 rval.append("&apos;");
118 else if (c == '<')
119 rval.append("&lt;");
120 else if (c == '>')
121 rval.append("&gt;");
122 else
123 rval.append((char const)c.unicode());
124 }
125
126 return rval;
127 }
128
129 QString eVaf::Common::strFromEscapedCharArray(QByteArray const & str)
130 {
131 QString rval;
132
133 bool e = false;
134 QByteArray ref;
135 foreach (char c, str) {
136 if (!e) {
137 if (c == '&') {
138 e = true;
139 ref = "&";
140 }
141 else
142 rval.append(QChar((ushort)c));
143 }
144 else {
145 ref.append(c);
146 if (c == ';') {
147 e = false;
148 ref = ref.toLower();
149
150 if (ref.startsWith("&#x")) {
151 // Numeric character reference in the HEX format
152 bool ok;
153 ushort ucode = ref.mid(3, ref.size() - 4).toUInt(&ok, 16);
154 if (ok)
155 rval.append(QChar(ucode));
156 else
157 // Invalid numeric character reference; output as is
158 rval.append(ref);
159 }
160 else if (ref.startsWith("&#")) {
161 // Numeric character reference in the DEC format
162 bool ok;
163 ushort ucode = ref.mid(2, ref.size() - 3).toUInt(&ok, 10);
164 if (ok)
165 rval.append(QChar(ucode));
166 else
167 // Invalid numeric character reference; output as is
168 rval.append(ref);
169 }
170 else if (ref == "&quot;")
171 rval.append('\"');
172 else if (ref == "&amp;")
173 rval.append('&');
174 else if (ref == "&apos;")
175 rval.append('\'');
176 else if (ref == "&lt;")
177 rval.append('<');
178 else if (ref == "&gt;")
179 rval.append('>');
180 else
181 // Unknown reference, output as is
182 rval.append(ref);
183 }
184 }
185 }
186
187 return rval;
188 }
189
190 QByteArray eVaf::Common::binToEscapedCharArray(QByteArray const & src)
191 {
192 QByteArray rval;
193 foreach (uchar c, src) {
194 if (c < 32 || c >= 127)
195 rval.append("&#x" + QByteArray::number(c, 16) + ";");
196 else if (c == '\"')
197 rval.append("&quot;");
198 else if (c == '&')
199 rval.append("&amp;");
200 else if (c == '\'')
201 rval.append("&apos;");
202 else if (c == '<')
203 rval.append("&lt;");
204 else if (c == '>')
205 rval.append("&gt;");
206 else
207 rval.append(c);
208 }
209
210 return rval;
211 }
212
213 QByteArray eVaf::Common::binFromEscapedCharArray(QByteArray const & str)
214 {
215 QByteArray rval;
216
217 bool e = false;
218 QByteArray ref;
219 foreach (char c, str) {
220 if (!e) {
221 if (c == '&') {
222 e = true;
223 ref = "&";
224 }
225 else
226 rval.append(c);
227 }
228 else {
229 ref.append(c);
230 if (c == ';') {
231 e = false;
232 ref = ref.toLower();
233
234 if (ref.startsWith("&#x")) {
235 // Numeric character reference in the HEX format
236 bool ok;
237 uchar ucode = ref.mid(3, ref.size() - 4).toUInt(&ok, 16);
238 if (ok)
239 rval.append(ucode);
240 else
241 // Invalid numeric character reference; output as is
242 rval.append(ref);
243 }
244 else if (ref.startsWith("&#")) {
245 // Numeric character reference in the DEC format
246 bool ok;
247 uchar ucode = ref.mid(2, ref.size() - 3).toUInt(&ok, 10);
248 if (ok)
249 rval.append(ucode);
250 else
251 // Invalid numeric character reference; output as is
252 rval.append(ref);
253 }
254 else if (ref == "&quot;")
255 rval.append('\"');
256 else if (ref == "&amp;")
257 rval.append('&');
258 else if (ref == "&apos;")
259 rval.append('\'');
260 else if (ref == "&lt;")
261 rval.append('<');
262 else if (ref == "&gt;")
263 rval.append('>');
264 else
265 // Unknown reference, output as is
266 rval.append(ref);
267 }
268 }
269 }
270
271 return rval;
272 }