Ref T415, utility function to parse "ini"-file

This commit is contained in:
Klaus Basan
2018-10-26 01:05:36 +02:00
parent ab4b4c5067
commit eed3eeb700
2 changed files with 23 additions and 1 deletions

View File

@@ -151,7 +151,7 @@ namespace BlackMisc
for (int i = 0; i < bytes.size(); i++)
{
const int b = static_cast<int>(bytes.at(i));
h.append(intToHex(b, 2));
h += intToHex(b, 2);
}
return h;
}
@@ -398,6 +398,24 @@ namespace BlackMisc
if (set.isEmpty()) { return QStringLiteral(""); }
return set.toList().join(separator);
}
QMap<QString, QString> parseIniValues(const QString &data)
{
QMap<QString, QString> map;
QList<QStringRef> lines = splitLinesRefs(data);
for (const QStringRef &l : lines)
{
if (l.isEmpty()) { continue; }
const int i = l.indexOf("=");
if (i < 0 || i >= l.length() + 1) { continue; }
const QString key = l.left(i).trimmed().toString();
const QString value = l.mid(i + 1).toString();
if (value.isEmpty()) { continue; }
map.insert(key, value);
}
return map;
}
} // ns
//! \endcond

View File

@@ -26,6 +26,7 @@
#include <QTextStream>
#include <QtGlobal>
#include <QSet>
#include <QMap>
#include <iosfwd>
#include <string>
#include <algorithm>
@@ -115,6 +116,9 @@ namespace BlackMisc
return s.trimmed();
}
//! Obtain ini file like values, e.g. foo=bar
BLACKMISC_EXPORT QMap<QString, QString> parseIniValues(const QString &data);
//! Is 0-9 char, isDigit allows a bunch of more characters
inline bool is09(const QChar &c) { return c >= u'0' && c <= u'9'; }