X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Futil.cpp;h=30874ebadcaf6bd9df0babe5065b7fe62f9ba376;hb=cf45ef016ce162419f74f9165c4267a184714956;hp=ccabdab922921a0ac2b927149cfd8e6dc22c86d8;hpb=814d12e0a340ae11fa4a22077b37316aa41716d7;p=evaf
diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp
index ccabdab..30874eb 100644
--- a/src/libs/Common/util.cpp
+++ b/src/libs/Common/util.cpp
@@ -19,6 +19,7 @@
#include "util.h"
+#include
//-------------------------------------------------------------------
@@ -58,24 +59,214 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
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);
+ else {
+ bool ok;
+ uint v = value.toUInt(&ok, 0);
+ if (ok)
+ return QVariant(v);
+ else
+ return defaultValue;
+ }
break;
}
case QVariant::Char: {
- if (value.size() > 0)
- return QVariant(value.at(0));
- else
- return defaultValue;
+ if (value.size() > 0) {
+ if (value.startsWith("\\0x")) {
+ bool ok;
+ char c = value.mid(1).toUInt(&ok, 16);
+ if (ok)
+ return QVariant(c);
+ }
+ else if (value.startsWith("\\0")) {
+ bool ok;
+ char c = value.mid(1).toUInt(&ok, 8);
+ if (ok)
+ return QVariant(c);
+ }
+ else if (value.startsWith('&')) {
+ QString c = strFromEscapedCharArray(value.toLatin1());
+ if (c.size() > 0)
+ return QVariant(c.at(0));
+ }
+ else
+ return QVariant(value.at(0));
+ }
+ return defaultValue;
break;
}
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((char const)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((ushort)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 ucode = 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 ucode = 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 (uchar 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;
+ uchar ucode = 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;
+ uchar ucode = 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;
+}