X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Futil.cpp;h=30874ebadcaf6bd9df0babe5065b7fe62f9ba376;hb=13dedf19c628b9967e4c4106ab0fd967f1df0b90;hp=eeedb61ff19fddeab479a322c9dc0eedec681c36;hpb=32d6a193150b17fbbde109348ad5a8107bba5529;p=evaf
diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp
index eeedb61..30874eb 100644
--- a/src/libs/Common/util.cpp
+++ b/src/libs/Common/util.cpp
@@ -87,6 +87,11 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
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));
}
@@ -97,3 +102,171 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & 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;
+}