git-svn-id: svn://db.shs.com.ru/libs@75 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "qpiconfig.h"
|
||||
#include <QFileInfo>
|
||||
#include <QDir>
|
||||
|
||||
|
||||
int QString2int(const QString & string) {
|
||||
@@ -272,8 +274,50 @@ QPIConfig::QPIConfig(QString * str, QPIConfig::FileType type_) {
|
||||
}
|
||||
|
||||
|
||||
QPIConfig::QPIConfig(const QString & path, QStringList dirs) {
|
||||
init();
|
||||
type = Config;
|
||||
internal = true;
|
||||
dev = new QFile(path);
|
||||
dev->open(QIODevice::ReadOnly);
|
||||
incdirs = dirs;
|
||||
incdirs << QFileInfo(path).absoluteDir().path();
|
||||
QString cp = path;
|
||||
while (!dev->isOpen()) {
|
||||
if (dirs.isEmpty()) break;
|
||||
cp = dirs.back();
|
||||
if (cp.endsWith("/") || cp.endsWith("\\")) cp.chop(1);
|
||||
cp += "/" + path;
|
||||
dev->setFileName(cp);
|
||||
dev->open(QIODevice::ReadOnly);
|
||||
dirs.pop_back();
|
||||
}
|
||||
if (!dev->isOpen()) {
|
||||
delete dev;
|
||||
dev = 0;
|
||||
return;
|
||||
}
|
||||
dev->close();
|
||||
setFileName(cp);
|
||||
open(QIODevice::ReadOnly);
|
||||
parse();
|
||||
}
|
||||
|
||||
|
||||
QPIConfig::~QPIConfig() {
|
||||
stream.setDevice(0);
|
||||
root.deleteBranch();
|
||||
foreach (QPIConfig * c, inc_devs)
|
||||
delete c;
|
||||
inc_devs.clear();
|
||||
includes.clear();
|
||||
}
|
||||
|
||||
|
||||
void QPIConfig::init() {
|
||||
internal = false;
|
||||
buffer = 0;
|
||||
dev = 0;
|
||||
delim = ".";
|
||||
root._name = "root";
|
||||
root.delim = delim;
|
||||
@@ -544,6 +588,22 @@ void QPIConfig::writeAll() {
|
||||
}
|
||||
|
||||
|
||||
QString QPIConfig::getPrefixFromLine(QString line, bool * exists) {
|
||||
line = line.trimmed();
|
||||
if (line.left(1) == "#") {if (exists) *exists = false; return QString();}
|
||||
int ci = line.indexOf("#");
|
||||
if (ci >= 0) line = line.left(ci).trimmed();
|
||||
if (line.indexOf("=") >= 0) {if (exists) *exists = false; return QString();}
|
||||
if (line.indexOf("[") >= 0 && line.indexOf("]") >= 0) {
|
||||
if (exists) *exists = true;
|
||||
line.remove(0, 1);
|
||||
return line.left(line.lastIndexOf("]")).trimmed();
|
||||
}
|
||||
if (exists) *exists = false;
|
||||
return QString();
|
||||
}
|
||||
|
||||
|
||||
QString QPIConfig::writeAllToString() {
|
||||
QString str;
|
||||
QTextStream s(&str);
|
||||
@@ -606,14 +666,47 @@ bool QPIConfig::entryExists(const Entry * e, const QString & name) const {
|
||||
}
|
||||
|
||||
|
||||
void QPIConfig::updateIncludes() {
|
||||
if (internal) return;
|
||||
all_includes.clear();
|
||||
foreach (QPIConfig * c, includes)
|
||||
all_includes << c->allLeaves();
|
||||
}
|
||||
|
||||
|
||||
QString QPIConfig::entryValue(QString v) {
|
||||
int i = -1, l = 0;
|
||||
while (1) {
|
||||
i = v.indexOf("${");
|
||||
if (i < 0) break;
|
||||
l = v.indexOf("}", i + 1);
|
||||
QString w = v.mid(i + 2, l - i - 2), r;
|
||||
l = w.length() + 3;
|
||||
w = w.trimmed();
|
||||
foreach (QPIConfig::Entry * e, all_includes) {
|
||||
if (e->_full_name == w) {
|
||||
r = e->_value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
v.replace(i, l, r);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
void QPIConfig::parse(QString content) {
|
||||
root.deleteBranch();
|
||||
root.clear();
|
||||
QString src, str, tab, comm, all, name, type;
|
||||
QString src, str, tab, comm, all, name, type, prefix, tprefix;
|
||||
QStringList tree;
|
||||
Entry * entry, * te, * ce;
|
||||
int ind, sind;
|
||||
bool isNew;
|
||||
bool isNew, isPrefix;
|
||||
foreach (QPIConfig * c, inc_devs)
|
||||
delete c;
|
||||
inc_devs.clear();
|
||||
includes.clear();
|
||||
if (content.isEmpty()) {
|
||||
if (buffer == 0) {
|
||||
if (!isOpen()) return;
|
||||
@@ -629,6 +722,12 @@ void QPIConfig::parse(QString content) {
|
||||
while (!stream.atEnd()) {
|
||||
other.push_back(QString());
|
||||
src = str = stream.readLine();
|
||||
tprefix = getPrefixFromLine(src, &isPrefix);
|
||||
if (isPrefix) {
|
||||
prefix = tprefix;
|
||||
if (!prefix.isEmpty())
|
||||
prefix += delim;
|
||||
}
|
||||
//cout << str << endl;
|
||||
tab = str.left(str.indexOf(str.trimmed().left(1)));
|
||||
str = str.trimmed();
|
||||
@@ -647,44 +746,59 @@ void QPIConfig::parse(QString content) {
|
||||
comm = "";
|
||||
}
|
||||
//name = str.left(ind).trimmed();
|
||||
tree = str.left(ind).trimmed().split(delim);
|
||||
name = tree.back();
|
||||
tree.pop_back();
|
||||
entry = &root;
|
||||
foreach (QString i, tree) {
|
||||
te = entry->findChild(i);
|
||||
if (te == 0) {
|
||||
tree = (prefix + str.left(ind).trimmed()).split(delim);
|
||||
if (tree.front() == "include") {
|
||||
name = str.right(str.length() - ind - 1).trimmed();
|
||||
QPIConfig * iconf = new QPIConfig(name, incdirs);
|
||||
if (!iconf->dev) {
|
||||
delete iconf;
|
||||
} else {
|
||||
inc_devs << iconf;
|
||||
includes << iconf << iconf->includes;
|
||||
updateIncludes();
|
||||
}
|
||||
//piCout << "includes" << includes;
|
||||
other.back() = src;
|
||||
} else {
|
||||
name = tree.back();
|
||||
tree.pop_back();
|
||||
entry = &root;
|
||||
foreach (QString i, tree) {
|
||||
te = entry->findChild(i);
|
||||
if (te == 0) {
|
||||
ce = new Entry();
|
||||
ce->delim = delim;
|
||||
ce->_tab = tab;
|
||||
ce->_line = lines;
|
||||
ce->_name = i;
|
||||
ce->_parent = entry;
|
||||
entry->_children << ce;
|
||||
entry = ce;
|
||||
} else entry = te;
|
||||
}
|
||||
isNew = false;
|
||||
ce = entry->findChild(name);
|
||||
if (ce == 0) {
|
||||
ce = new Entry();
|
||||
ce->delim = delim;
|
||||
ce->_tab = tab;
|
||||
ce->_line = lines;
|
||||
ce->_name = i;
|
||||
isNew = true;
|
||||
}
|
||||
ce->delim = delim;
|
||||
ce->_tab = tab;
|
||||
ce->_name = name;
|
||||
ce->_value = entryValue(str.right(str.length() - ind - 1).trimmed());
|
||||
ce->_type = type;
|
||||
ce->_comment = comm;
|
||||
ce->_line = lines;
|
||||
ce->_all = all;
|
||||
if (isNew) {
|
||||
ce->_parent = entry;
|
||||
entry->_children << ce;
|
||||
entry = ce;
|
||||
} else entry = te;
|
||||
}
|
||||
isNew = false;
|
||||
ce = entry->findChild(name);
|
||||
if (ce == 0) {
|
||||
ce = new Entry();
|
||||
isNew = true;
|
||||
}
|
||||
ce->delim = delim;
|
||||
ce->_tab = tab;
|
||||
ce->_name = name;
|
||||
ce->_value = str.right(str.length() - ind - 1).trimmed();
|
||||
ce->_type = type;
|
||||
ce->_comment = comm;
|
||||
ce->_line = lines;
|
||||
ce->_all = all;
|
||||
if (isNew) {
|
||||
ce->_parent = entry;
|
||||
entry->_children << ce;
|
||||
}
|
||||
}
|
||||
} else other.back() = src;
|
||||
lines++;
|
||||
}
|
||||
setEntryDelim(&root, delim);
|
||||
buildFullNames(&root);
|
||||
//if (content.isEmpty()) stream.setDevice(this);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user