X-Git-Url: https://vaikene.ee/gitweb/pswgen09.html?a=blobdiff_plain;ds=sidebyside;f=src%2Flibs%2FCommon%2Futil.cpp;h=3e8a3755608a48314a097e865f88051c4e8d5185;hb=refs%2Fheads%2Fmaster;hp=eeedb61ff19fddeab479a322c9dc0eedec681c36;hpb=32d6a193150b17fbbde109348ad5a8107bba5529;p=evaf
diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp
index eeedb61..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.
*
@@ -33,30 +33,27 @@ 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: {
if (eVaf::Common::isTrue(value))
@@ -65,35 +62,206 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
return QVariant(false);
else {
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::Char: {
if (value.size() > 0) {
if (value.startsWith("\\0x")) {
bool ok;
- char c = value.mid(1).toUInt(&ok, 16);
+ char const c = static_cast(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);
+ 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;
- 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(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;
+}