This commit is contained in:
bart
2026-11-08 16:15:49 +01:00
parent 5af4581182
commit 1140dd562b
34 changed files with 940 additions and 44 deletions
+43 -12
View File
@@ -1,6 +1,7 @@
#include "yamlparser.h"
#include <QFile>
#include <QTextStream>
#include <vector>
YamlNode YamlParser::parseFile(const QString &filePath)
{
@@ -16,26 +17,56 @@ YamlNode YamlParser::parseString(const QString &content)
{
YamlNode root;
QStringList lines = content.split('\n');
QString currentKey = "";
struct StackEntry {
int indent;
YamlNode *node;
};
std::vector<StackEntry> stack;
stack.push_back({-1, &root});
for (int i = 0; i < lines.size(); ++i) {
QString line = lines[i];
QString rawLine = lines[i];
// Strip comments
int commentIdx = line.indexOf('#');
int commentIdx = rawLine.indexOf('#');
if (commentIdx != -1) {
line = line.left(commentIdx);
rawLine = rawLine.left(commentIdx);
}
QString trimmed = line.trimmed();
// Calculate leading indentation spaces (tabs expanded to 2 spaces)
int indent = 0;
int idx = 0;
while (idx < rawLine.length()) {
if (rawLine[idx] == ' ') {
indent++;
idx++;
} else if (rawLine[idx] == '\t') {
indent += 2;
idx++;
} else {
break;
}
}
QString trimmed = rawLine.trimmed();
if (trimmed.isEmpty()) continue;
// Pop stack until parent has smaller indent
while (stack.size() > 1 && stack.back().indent >= indent) {
stack.pop_back();
}
YamlNode *parent = stack.back().node;
// Check for list item: "- something"
if (trimmed.startsWith("- ")) {
QString itemVal = trimmed.mid(2).trimmed();
if (!currentKey.isEmpty()) {
root.children[currentKey].listValues.append(itemVal);
if ((itemVal.startsWith('"') && itemVal.endsWith('"')) || (itemVal.startsWith('\'') && itemVal.endsWith('\''))) {
itemVal = itemVal.mid(1, itemVal.length() - 2);
}
parent->listValues.append(itemVal);
continue;
}
@@ -45,15 +76,15 @@ YamlNode YamlParser::parseString(const QString &content)
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;
parent->children[key] = YamlNode();
YamlNode *newNode = &(parent->children[key]);
newNode->value = val;
stack.push_back({indent, newNode});
}
}