diff --git a/main.cpp b/main.cpp index 33fd7276..7cbb1ba5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,17 @@ #include "pikbdlistener.h" #include "piconnection.h" +#include "piconfig.h" int main (int argc, char * argv[]) { - PIKbdListener k; + /*PIKbdListener k; k.enableExitCapture(); PIConnection conn; conn.configureFromConfig("c.conf", "c"); conn.start(); k.start(); WAIT_FOR_EXIT - return 0; + return 0;*/ + PIConfig c("mems.conf"); + piCout << c.allTree(); } diff --git a/src/io/piconfig.cpp b/src/io/piconfig.cpp index 5e4f27b0..77eb49a1 100755 --- a/src/io/piconfig.cpp +++ b/src/io/piconfig.cpp @@ -262,10 +262,38 @@ PIConfig::PIConfig(PIIODevice * device, PIIODevice::DeviceMode mode) { } +PIConfig::PIConfig(const PIString & path, PIStringList dirs) { + _init(); + internal = true; + own_dev = true; + dev = new PIFile(path, PIIODevice::ReadOnly); + incdirs = dirs; + incdirs << PIFile::fileInfo(path).dir(); + while (!dev->isOpened()) { + if (dirs.isEmpty()) break; + PIString cp = dirs.back(); + if (cp.endsWith("/") || cp.endsWith("\\")) cp.pop_back(); + cp += "/" + path; + dev->open(cp, PIIODevice::ReadOnly); + dirs.pop_back(); + } + if (!dev->isOpened()) { + delete dev; + dev = 0; + return; + } + parse(); +} + + PIConfig::~PIConfig() { root.deleteBranch(); if (own_dev && dev) delete dev; dev = 0; + piForeach (PIConfig * c, inc_devs) + delete c; + inc_devs.clear(); + includes.clear(); } @@ -290,6 +318,7 @@ bool PIConfig::open(PIString * string, PIIODevice::DeviceMode mode) { void PIConfig::_init() { + internal = false; delim = PIStringAscii("."); root.delim = delim; empty.delim = delim; @@ -629,12 +658,43 @@ bool PIConfig::entryExists(const Entry * e, const PIString & name) const { } +void PIConfig::updateIncludes() { + if (internal) return; + all_includes.clear(); + piForeach (PIConfig * c, includes) + all_includes << c->allLeaves(); +} + + +PIString PIConfig::entryValue(PIString v) { + int i = -1, l = 0; + while (1) { + i = v.find("$("); + if (i < 0) break; + PIString w = v.mid(i + 1).takeRange('(', ')'), r; + l = w.length() + 3; + w.trim(); + piForeachC (PIConfig::Entry * e, all_includes) + if (e->_full_name == w) { + r = e->_value; + break; + } + v.replace(i, l, r); + } + return v; +} + + void PIConfig::parse() { PIString src, str, tab, comm, all, name, type, prefix, tprefix; PIStringList tree; Entry * entry, * te, * ce; int ind, sind; bool isNew, isPrefix; + piForeach (PIConfig * c, inc_devs) + delete c; + inc_devs.clear(); + includes.clear(); if (!isOpened()) return; _seekToBeginDev(); other.clear(); @@ -668,39 +728,54 @@ void PIConfig::parse() { } //name = str.left(ind).trimmed(); tree = (prefix + str.left(ind).trimmed()).split(delim); - name = tree.back(); - tree.pop_back(); - entry = &root; - piForeachC (PIString & i, tree) { - te = entry->findChild(i); - if (te == 0) { + if (tree.front() == "include") { + name = str.right(str.length() - 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(); + } + //piCout << "includes" << includes; + other.back() = src; + } else { + name = tree.back(); + tree.pop_back(); + entry = &root; + piForeachC (PIString & 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++; diff --git a/src/io/piconfig.h b/src/io/piconfig.h index 63001d0e..fd8f88ad 100755 --- a/src/io/piconfig.h +++ b/src/io/piconfig.h @@ -466,6 +466,7 @@ public: void setDelimiter(const PIString & d) {delim = d; setEntryDelim(&root, d); readAll();} private: + PIConfig(const PIString & path, PIStringList dirs); void _init(); void _clearDev(); void _flushDev(); @@ -482,12 +483,17 @@ private: void removeEntry(Branch & b, Entry * e); void deleteEntry(Entry * e) {piForeach (Entry * i, e->_children) deleteEntry(i); delete e;} PIString getPrefixFromLine(PIString line, bool * exists); + void updateIncludes(); + PIString entryValue(PIString v); void parse(); int centry; - bool own_dev; + bool own_dev, internal; + PIVector includes, inc_devs; + Branch all_includes; PIIODevice * dev; PIString delim; + PIStringList incdirs; Entry root, empty; uint lines; PIStringList other;