PIVariant string conversions, PIDateTime::fromString

This commit is contained in:
2022-11-30 22:40:28 +03:00
parent a27353d42d
commit 5bb9477b5b
8 changed files with 254 additions and 13 deletions

View File

@@ -19,7 +19,9 @@
#include "pivaluetree_conversions.h"
#include "pipropertystorage.h"
#include "pijson.h"
#include "pifile.h"
#include "piiostring.h"
#include "piiostream.h"
PIValueTree PIValueTreeConversions::fromPropertyStorage(const PIPropertyStorage & ps) {
@@ -35,6 +37,18 @@ PIValueTree PIValueTreeConversions::fromPropertyStorage(const PIPropertyStorage
}
PIValueTree PIValueTreeConversions::fromVariantMap(const PIVariantMap & vm) {
PIValueTree ret;
for (const auto & i: vm) {
PIValueTree v;
v.setName(i.first);
v.setValue(i.second);
ret.addChild(v);
}
return ret;
}
PIValueTree fromJSONTree(const PIJSON & json) {
PIValueTree ret;
ret.setName(json["name"].toString());
@@ -76,6 +90,85 @@ PIValueTree PIValueTreeConversions::fromJSON(const PIJSON & json) {
PIValueTree PIValueTreeConversions::fromText(PIIODevice * device) {
PIValueTree ret;
if (!device) return ret;
PIString base_path;
if (device->isTypeOf<PIFile>()) base_path = PIFile::FileInfo(device->path()).dir();
PIIOTextStream ts(device);
PIString line, comm;
PIVariant value;
PIChar type;
PIStringList prefix, path;
while (!ts.isEnd()) {
PIString l = ts.readLine().trim();
if (l.endsWith(" \\")) {
line += l.cutRight(2).trim() + "\n";
continue;
}
line += l;
//piCout << line.replacedAll("\n", "\\n");
if (line.size() < 2) {
line.clear();
continue;
}
if (line.startsWith('[')) {
prefix = line.inBrackets('[', ']').trim().split('.');
line.clear();
continue;
}
type = 's';
comm.clear();
int ind = line.find('#');
if (ind >= 0) {
comm = line.takeMid(ind + 1);
bool typed = false;
if (comm.isNotEmpty()) {
if (comm.size() == 1) typed = true;
else if (comm[0].isAlpha() && comm[1].isSpace()) typed = true;
}
if (typed) {
type = comm.takeLeft(1)[0];
}
comm.trim();
line.cutRight(1).trim();
}
ind = line.find('=');
if ((ind > 0) && (line[0] != '#')) {
path = prefix;
path << line.takeLeft(ind).trim().split('.');
line.cutLeft(1).trim();
if (path.front() == "include") {
/*name = line.mid(ind + 1).trimmed();
PIConfig * iconf = new PIConfig(name, incdirs);
//piCout << "include" << name << iconf->dev;
if (!iconf->dev) {
delete iconf;
} else {
inc_devs << iconf;
includes << iconf << iconf->includes;
updateIncludes();
}
other.back() = src;*/
}
}
//piCout << path << line << comm;
switch (type.toAscii()) {
case 'b': value = line.toBool(); break;
case 'n': value = line.toInt(); break;
case 'f': value = line.toDouble(); break;
case 'l': value = line.split("%|%"); break;
case 'c': value = PIVariantTypes::Color(line.toUInt()); break;
case 'p':
case 'v': value = PIVariant(line).toPoint(); break;
case 'r':
case 'a': value = PIVariant(line).toRect(); break;
default: value = line;
}
PIValueTree & leaf(ret[path]);
value.setValueFromString(line);
leaf.setComment(comm);
leaf.setValue(value);
line.clear();
}
return ret;
}