X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;ds=inline;f=src%2Flibs%2FCommon%2Futil.cpp;h=3e8a3755608a48314a097e865f88051c4e8d5185;hb=HEAD;hp=1a07a4a0c33f7d7c081f6a4a3a8cdbbb1e8ef66b;hpb=926606ce83f3a702d67c5c2c42478b091f78fdc4;p=evaf
diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp
index 1a07a4a..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,45 +62,48 @@ 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::toEscapedString(QString const & str)
+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()) + ";");
+ rval.append("" + QByteArray::number(c.unicode(), 16) + ";");
else if (c == '\"')
rval.append(""");
else if (c == '&')
@@ -115,13 +115,13 @@ QByteArray eVaf::Common::toEscapedString(QString const & str)
else if (c == '>')
rval.append(">");
else
- rval.append((char const)c.unicode());
+ rval.append(static_cast(c.unicode()));
}
return rval;
}
-QString eVaf::Common::fromEscapedString(QByteArray const & str)
+QString eVaf::Common::strFromEscapedCharArray(QByteArray const & str)
{
QString rval;
@@ -134,7 +134,7 @@ QString eVaf::Common::fromEscapedString(QByteArray const & str)
ref = "&";
}
else
- rval.append(QChar((ushort)c));
+ rval.append(QChar(static_cast(c)));
}
else {
ref.append(c);
@@ -145,7 +145,7 @@ QString eVaf::Common::fromEscapedString(QByteArray const & str)
if (ref.startsWith("")) {
// Numeric character reference in the HEX format
bool ok;
- ushort ucode = ref.mid(3, ref.size() - 4).toUInt(&ok, 16);
+ ushort const ucode = static_cast(ref.mid(3, ref.size() - 4).toUInt(&ok, 16));
if (ok)
rval.append(QChar(ucode));
else
@@ -155,7 +155,7 @@ QString eVaf::Common::fromEscapedString(QByteArray const & str)
else if (ref.startsWith("")) {
// Numeric character reference in the DEC format
bool ok;
- ushort ucode = ref.mid(2, ref.size() - 3).toUInt(&ok, 10);
+ ushort const ucode = static_cast(ref.mid(2, ref.size() - 3).toUInt(&ok, 10));
if (ok)
rval.append(QChar(ucode));
else
@@ -181,3 +181,87 @@ QString eVaf::Common::fromEscapedString(QByteArray const & str)
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;
+}