214 lines
5.4 KiB
C++
214 lines
5.4 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
PIValueTree conversions
|
|
Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#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) {
|
|
PIValueTree ret;
|
|
for (const auto & i: ps) {
|
|
PIValueTree v;
|
|
v.setName(i.name);
|
|
v.setValue(i.value);
|
|
v.setComment(i.comment);
|
|
ret.addChild(v);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
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());
|
|
ret.setComment(json["comment"].toString());
|
|
PIString value_str = json["value"].toString();
|
|
PIVariant value = PIVariant::fromType(json["type"].toString());
|
|
if (value_str.isNotEmpty()) {
|
|
if (value.isValid())
|
|
value.setValueFromString(value_str);
|
|
else
|
|
value = value_str;
|
|
}
|
|
ret.setValue(value);
|
|
const PIJSON & attr(json["attributes"]);
|
|
for (const auto & i: attr.object())
|
|
ret.attributes()[i.first] = i.second.value();
|
|
bool is_array = ret.isArray();
|
|
int chindex = 0;
|
|
const PIJSON & chl(json["children"]);
|
|
for (const auto & i: chl.array()) {
|
|
PIValueTree c = fromJSONTree(i);
|
|
if (is_array) c.setName(PIString::fromNumber(chindex++));
|
|
ret.addChild(c);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIValueTree PIValueTreeConversions::fromJSON(const PIJSON & json) {
|
|
PIValueTree ret;
|
|
if (json.isObject()) ret = fromJSONTree(json);
|
|
if (json.isArray()) {
|
|
for (const auto & i: json.array())
|
|
ret.addChild(fromJSONTree(i));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
PIJSON toJSONTree(const PIValueTree & root) {
|
|
PIJSON ret = PIJSON::newObject();
|
|
if (root.name().isNotEmpty())
|
|
ret["name"] = root.name();
|
|
if (root.comment().isNotEmpty())
|
|
ret["comment"] = root.comment();
|
|
if (root.value().isValid()) {
|
|
ret["type"] = root.value().typeName();
|
|
ret["value"] = root.value().toString();
|
|
}
|
|
if (root.attributes().isNotEmpty()) {
|
|
PIJSON j;
|
|
for (const auto & a: root.attributes())
|
|
j[a.first] = a.second.toString();
|
|
ret["attributes"] = j;
|
|
}
|
|
if (root.hasChildren()) {
|
|
PIJSON j;
|
|
for (const auto & c: root.children())
|
|
j << toJSONTree(c);
|
|
ret["children"] = j;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIJSON PIValueTreeConversions::toJSON(const PIValueTree & root) {
|
|
PIJSON ret = PIJSON::newArray();
|
|
for (const auto & c: root.children())
|
|
ret << toJSONTree(c);
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIString PIValueTreeConversions::toText(const PIValueTree & root) {
|
|
PIString ret;
|
|
return ret;
|
|
}
|