units
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#include "yamlparser.h"
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
YamlNode YamlParser::parseFile(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
return YamlNode();
|
||||
}
|
||||
QTextStream in(&file);
|
||||
return parseString(in.readAll());
|
||||
}
|
||||
|
||||
YamlNode YamlParser::parseString(const QString &content)
|
||||
{
|
||||
YamlNode root;
|
||||
QStringList lines = content.split('\n');
|
||||
QString currentKey = "";
|
||||
|
||||
for (int i = 0; i < lines.size(); ++i) {
|
||||
QString line = lines[i];
|
||||
|
||||
// Strip comments
|
||||
int commentIdx = line.indexOf('#');
|
||||
if (commentIdx != -1) {
|
||||
line = line.left(commentIdx);
|
||||
}
|
||||
|
||||
QString trimmed = line.trimmed();
|
||||
if (trimmed.isEmpty()) continue;
|
||||
|
||||
// Check for list item: "- something"
|
||||
if (trimmed.startsWith("- ")) {
|
||||
QString itemVal = trimmed.mid(2).trimmed();
|
||||
if (!currentKey.isEmpty()) {
|
||||
root.children[currentKey].listValues.append(itemVal);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for key: value
|
||||
int colonIdx = trimmed.indexOf(':');
|
||||
if (colonIdx != -1) {
|
||||
QString key = trimmed.left(colonIdx).trimmed();
|
||||
QString val = trimmed.mid(colonIdx + 1).trimmed();
|
||||
|
||||
// Strip enclosing quotes if any
|
||||
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith('\'') && val.endsWith('\''))) {
|
||||
val = val.mid(1, val.length() - 2);
|
||||
}
|
||||
|
||||
currentKey = key;
|
||||
YamlNode node;
|
||||
node.value = val;
|
||||
root.children[key] = node;
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
Reference in New Issue
Block a user