X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Futil.cpp;h=3e8a3755608a48314a097e865f88051c4e8d5185;hb=refs%2Fheads%2Fmaster;hp=ccabdab922921a0ac2b927149cfd8e6dc22c86d8;hpb=814d12e0a340ae11fa4a22077b37316aa41716d7;p=evaf
diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp
index ccabdab..3e8a375 100644
--- a/src/libs/Common/util.cpp
+++ b/src/libs/Common/util.cpp
@@ -3,7 +3,7 @@
* @brief Global utility functions for eVaf
* @author Enar Vaikene
*
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2019 Enar Vaikene
*
* This file is part of the eVaf C++ cross-platform application development framework.
*
@@ -19,6 +19,7 @@
#include "util.h"
+#include
//-------------------------------------------------------------------
@@ -32,50 +33,235 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
switch (defaultValue.type()) {
case QVariant::UInt: {
bool ok;
- uint v = value.toUInt(&ok, 0);
+ uint const v = value.toUInt(&ok, 0);
if (ok)
return QVariant(v);
else
return defaultValue;
- break;
}
case QVariant::Int: {
bool ok;
- int v = value.toInt(&ok, 0);
+ int const v = value.toInt(&ok, 0);
if (ok)
return QVariant(v);
else
return defaultValue;
- break;
}
case QVariant::Double: {
bool ok;
- double v = value.toDouble(&ok);
+ double const v = value.toDouble(&ok);
if (ok)
return QVariant(v);
else
return defaultValue;
- break;
}
case QVariant::Bool: {
- bool ok;
- uint v = value.toUInt(&ok, 0);
if (eVaf::Common::isTrue(value))
return QVariant(true);
else if (eVaf::Common::isFalse(value))
return QVariant(false);
- else
- return QVariant(v);
- break;
+ else {
+ bool ok;
+ uint const v = value.toUInt(&ok, 0);
+ if (ok)
+ return QVariant(v);
+ else
+ return defaultValue;
+ }
}
case QVariant::Char: {
- if (value.size() > 0)
- return QVariant(value.at(0));
- else
- return defaultValue;
- break;
+ if (value.size() > 0) {
+ if (value.startsWith("\\0x")) {
+ bool ok;
+ char const c = static_cast(value.mid(1).toUInt(&ok, 16));
+ if (ok)
+ return QVariant(c);
+ }
+ else if (value.startsWith("\\0")) {
+ bool ok;
+ char const c = static_cast(value.mid(1).toUInt(&ok, 8));
+ if (ok)
+ return QVariant(c);
+ }
+ else if (value.startsWith('&')) {
+ QString const c = strFromEscapedCharArray(value.toLatin1());
+ if (c.size() > 0)
+ return QVariant(c.at(0));
+ }
+ else
+ return QVariant(value.at(0));
+ }
+ return defaultValue;
}
default:
return QVariant(value);
}
}
+
+QByteArray eVaf::Common::strToEscapedCharArray(QString const & str)
+{
+ QByteArray rval;
+ foreach (QChar c, str) {
+ if (c.unicode() < 32 || c.unicode() >= 127)
+ rval.append("" + QByteArray::number(c.unicode(), 16) + ";");
+ else if (c == '\"')
+ rval.append(""");
+ else if (c == '&')
+ rval.append("&");
+ else if (c == '\'')
+ rval.append("'");
+ else if (c == '<')
+ rval.append("<");
+ else if (c == '>')
+ rval.append(">");
+ else
+ rval.append(static_cast(c.unicode()));
+ }
+
+ return rval;
+}
+
+QString eVaf::Common::strFromEscapedCharArray(QByteArray const & str)
+{
+ QString rval;
+
+ bool e = false;
+ QByteArray ref;
+ foreach (char c, str) {
+ if (!e) {
+ if (c == '&') {
+ e = true;
+ ref = "&";
+ }
+ else
+ rval.append(QChar(static_cast(c)));
+ }
+ else {
+ ref.append(c);
+ if (c == ';') {
+ e = false;
+ ref = ref.toLower();
+
+ if (ref.startsWith("")) {
+ // Numeric character reference in the HEX format
+ bool ok;
+ ushort const ucode = static_cast(ref.mid(3, ref.size() - 4).toUInt(&ok, 16));
+ if (ok)
+ rval.append(QChar(ucode));
+ else
+ // Invalid numeric character reference; output as is
+ rval.append(ref);
+ }
+ else if (ref.startsWith("")) {
+ // Numeric character reference in the DEC format
+ bool ok;
+ ushort const ucode = static_cast(ref.mid(2, ref.size() - 3).toUInt(&ok, 10));
+ if (ok)
+ rval.append(QChar(ucode));
+ else
+ // Invalid numeric character reference; output as is
+ rval.append(ref);
+ }
+ else if (ref == """)
+ rval.append('\"');
+ else if (ref == "&")
+ rval.append('&');
+ else if (ref == "'")
+ rval.append('\'');
+ else if (ref == "<")
+ rval.append('<');
+ else if (ref == ">")
+ rval.append('>');
+ else
+ // Unknown reference, output as is
+ rval.append(ref);
+ }
+ }
+ }
+
+ return rval;
+}
+
+QByteArray eVaf::Common::binToEscapedCharArray(QByteArray const & src)
+{
+ QByteArray rval;
+ foreach (char const c, src) {
+ if (c < 32 || c >= 127)
+ rval.append("" + QByteArray::number(c, 16) + ";");
+ else if (c == '\"')
+ rval.append(""");
+ else if (c == '&')
+ rval.append("&");
+ else if (c == '\'')
+ rval.append("'");
+ else if (c == '<')
+ rval.append("<");
+ else if (c == '>')
+ rval.append(">");
+ else
+ rval.append(c);
+ }
+
+ return rval;
+}
+
+QByteArray eVaf::Common::binFromEscapedCharArray(QByteArray const & str)
+{
+ QByteArray rval;
+
+ bool e = false;
+ QByteArray ref;
+ foreach (char c, str) {
+ if (!e) {
+ if (c == '&') {
+ e = true;
+ ref = "&";
+ }
+ else
+ rval.append(c);
+ }
+ else {
+ ref.append(c);
+ if (c == ';') {
+ e = false;
+ ref = ref.toLower();
+
+ if (ref.startsWith("")) {
+ // Numeric character reference in the HEX format
+ bool ok;
+ char const ucode = static_cast(ref.mid(3, ref.size() - 4).toUInt(&ok, 16));
+ if (ok)
+ rval.append(ucode);
+ else
+ // Invalid numeric character reference; output as is
+ rval.append(ref);
+ }
+ else if (ref.startsWith("")) {
+ // Numeric character reference in the DEC format
+ bool ok;
+ char const ucode = static_cast(ref.mid(2, ref.size() - 3).toUInt(&ok, 10));
+ if (ok)
+ rval.append(ucode);
+ else
+ // Invalid numeric character reference; output as is
+ rval.append(ref);
+ }
+ else if (ref == """)
+ rval.append('\"');
+ else if (ref == "&")
+ rval.append('&');
+ else if (ref == "'")
+ rval.append('\'');
+ else if (ref == "<")
+ rval.append('<');
+ else if (ref == ">")
+ rval.append('>');
+ else
+ // Unknown reference, output as is
+ rval.append(ref);
+ }
+ }
+ }
+
+ return rval;
+}