29.07.2011 - fundamental new

This commit is contained in:
peri4
2011-07-29 08:17:24 +04:00
parent b21a0496cd
commit 29190ea465
49 changed files with 4704 additions and 1052 deletions

View File

@@ -1,259 +1,474 @@
#include "piconfig.h"
PIConfig::PIConfig(const PIString & path, Flags<Mode> mode): PIFile(path, mode) {
PIConfig::Entry PIConfig::Branch::_empty;
PIConfig::Entry PIConfig::Entry::_empty;
PIConfig::Branch PIConfig::Branch::allLeaves() {
Branch b;
b.delim = delim;
piForeachCA (i, *this) {
if (i->isLeaf()) b << i;
else allLeaves(b, i);
}
return b;
}
PIConfig::Entry & PIConfig::Branch::getValue(const PIString & vname, const PIString & def, bool * exist) {
if (vname.isEmpty()) {
_empty.clear();
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
PIStringList tree = vname.split(delim);
PIString name = tree.front();
tree.pop_front();
Entry * ce = 0;
piForeachCA (i, *this)
if (i->_name == name) {
ce = i;
break;
}
if (ce == 0) {
_empty._name = vname;
_empty._value = def;
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
piForeachCA (i, tree) {
ce = ce->findChild(i);
if (ce == 0) {
_empty._name = vname;
_empty._value = def;
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
}
if (exist != 0) *exist = true;
return *ce;
}
PIConfig::Branch PIConfig::Branch::getValues(const PIString & name) {
Branch b;
b.delim = delim;
piForeachA (i, *this) {
if (i->isLeaf()) {
if (i->_name.find(name) >= 0)
b << i;
} else {
piForeachA (j, i->_children)
if (j->_name.find(name) >= 0)
b << j;
}
}
return b;
}
PIConfig::Branch PIConfig::Branch::getLeaves() {
Branch b;
b.delim = delim;
piForeachA (i, *this)
if (i->isLeaf())
b << i;
return b;
}
PIConfig::Branch PIConfig::Branch::getBranches() {
Branch b;
b.delim = delim;
piForeachA (i, *this)
if (!i->isLeaf())
b << i;
return b;
}
PIConfig::Branch & PIConfig::Branch::filter(const PIString & f) {
for (int i = 0; i < size_s(); ++i) {
if (at(i)->_name.find(f) < 0) {
remove(i);
--i;
}
}
return *this;
}
bool PIConfig::Branch::entryExists(const Entry * e, const PIString & name) const {
if (e->_children.isEmpty()) {
if (e->_name == name) return true;
else return false;
}
piForeachCA (i, e->_children)
if (entryExists(i, name)) return true;
return false;
}
PIConfig::Entry & PIConfig::Entry::getValue(const PIString & vname, const PIString & def, bool * exist) {
PIStringList tree = vname.split(delim);
Entry * ce = this;
piForeachCA (i, tree) {
ce = ce->findChild(i);
if (ce == 0) {
_empty._name = vname;
_empty._value = def;
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
}
if (exist != 0) *exist = true;
return *ce;
}
PIConfig::Branch PIConfig::Entry::getValues(const PIString & vname) {
Branch b;
b.delim = delim;
piForeachA (i, _children)
if (i->_name.find(vname) >= 0)
b << i;
return b;
};
bool PIConfig::Entry::entryExists(const Entry * e, const PIString & name) const {
if (e->_children.isEmpty()) {
if (e->_name == name) return true;
else return false;
}
piForeachCA (i, e->_children)
if (entryExists(i, name)) return true;
return false;
}
PIConfig::PIConfig(const PIString & path, PIFlags<Mode> mode): PIFile(path, mode) {
delim = ".";
root.delim = delim;
empty.delim = delim;
empty._parent = 0;
if (!isOpened() && (mode[Write] || mode[New]))
open(path, Read | Write | New);
parse();
}
PIStringList PIConfig::getValue(const PIString & vname, const PIStringList & def, bool * exist) const {
return getValue(vname, def.join("%|%"), exist).split("%|%");}
bool PIConfig::getValue(const PIString & vname, const bool def, bool * exist) const {
return atob(getValue(vname, btos(def), exist));}
char PIConfig::getValue(const PIString & vname, const char def, bool * exist) const {
return getValue(vname, PIString(def), exist)[0];}
short PIConfig::getValue(const PIString & vname, const short def, bool * exist) const {
return getValue(vname, itos(def), exist).toShort();}
int PIConfig::getValue(const PIString & vname, const int def, bool * exist) const {
return getValue(vname, itos(def), exist).toInt();}
long PIConfig::getValue(const PIString & vname, const long def, bool * exist) const {
return getValue(vname, ltos(def), exist).toLong();}
uchar PIConfig::getValue(const PIString & vname, const uchar def, bool * exist) const {
return getValue(vname, uitos(def), exist).toInt();}
ushort PIConfig::getValue(const PIString & vname, const ushort def, bool * exist) const {
return getValue(vname, uitos(def), exist).toShort();}
uint PIConfig::getValue(const PIString & vname, const uint def, bool * exist) const {
return getValue(vname, uitos(def), exist).toInt();}
ulong PIConfig::getValue(const PIString & vname, const ulong def, bool * exist) const {
return getValue(vname, ultos(def), exist).toLong();}
float PIConfig::getValue(const PIString & vname, const float def, bool * exist) const {
return getValue(vname, ftos(def), exist).toFloat();}
double PIConfig::getValue(const PIString & vname, const double def, bool * exist) const {
return getValue(vname, dtos(def), exist).toDouble();}
void PIConfig::setValue(const PIString & name, const PIStringList & value, bool write) {
setValue(name, value.join("%|%"), "l", write);}
void PIConfig::setValue(const PIString & name, const char * value, bool write) {
setValue(name, PIString(value), "s", write);}
void PIConfig::setValue(const PIString & name, const bool value, bool write) {
setValue(name, btos(value), "b", write);}
void PIConfig::setValue(const PIString & name, const char value, bool write) {
setValue(name, PIString(1, value), "s", write);}
void PIConfig::setValue(const PIString & name, const short value, bool write) {
setValue(name, itos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const int value, bool write) {
setValue(name, itos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const long value, bool write) {
setValue(name, ltos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const uchar value, bool write) {
setValue(name, uitos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const ushort value, bool write) {
setValue(name, uitos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const uint value, bool write) {
setValue(name, uitos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const ulong value, bool write) {
setValue(name, ultos(value), "n", write);}
void PIConfig::setValue(const PIString & name, const float value, bool write) {
setValue(name, ftos(value), "f", write);}
void PIConfig::setValue(const PIString & name, const double value, bool write) {
setValue(name, dtos(value), "f", write);}
PIString PIConfig::getValue(const PIString & vname, const PIString & def, bool * exist) const {
for (uint i = 0; i < settname.size(); i++) {
if (settname[i] == vname) {
if (exist != 0) *exist = true;
return settval[i];
PIConfig::Entry & PIConfig::getValue(const PIString & vname, const PIString & def, bool * exist) {
PIStringList tree = vname.split(delim);
Entry * ce = &root;
piForeachCA (i, tree) {
ce = ce->findChild(i);
if (ce == 0) {
if (exist != 0) *exist = false;
empty._name = vname;
empty._value = def;
empty.delim = delim;
return empty;
}
}
if (exist != 0) *exist = false;
return def;
if (exist != 0) *exist = true;
return *ce;
}
PIConfig::Branch PIConfig::getValues(const PIString & vname) {
Branch b;
b.delim = delim;
piForeachA (i, root._children)
if (i->_name.find(vname) >= 0)
b << i;
return b;
};
void PIConfig::addEntry(const PIString & name, const PIString & value, const PIString & type, bool write) {
if (getValue(name)._parent != 0)
return;
bool toRoot = false;
PIStringList tree = name.split(delim);
PIString ename = tree.back();
tree.pop_back();
Entry * te, * ce, * entry = &root;
if (tree.isEmpty()) toRoot = true;
piForeachA (i, tree) {
te = entry->findChild(i);
if (te == 0) {
ce = new Entry();
ce->delim = delim;
ce->_tab = entry->_tab;
ce->_line = entry->_line;
ce->_name = i;
ce->_parent = entry;
entry->_children << ce;
entry = ce;
} else entry = te;
}
PIConfig::Branch ch = entry->_children;
std::sort(ch.begin(), ch.end(), PIConfig::Entry::compare);
te = (entry->isLeaf() ? 0 : ch.back());
ce = new Entry();
ce->delim = delim;
ce->_name = ename;
ce->_value = value;
ce->_type = type;
if (te == 0) {
ce->_tab = entry->_tab;
if (toRoot) ce->_line = other.size_s() - 1;
else ce->_line = entry->_line;
} else {
ce->_tab = te->_tab;
if (toRoot) ce->_line = other.size_s() - 1;
else {
ch = entry->_parent->_children;
std::sort(ch.begin(), ch.end(), PIConfig::Entry::compare);
ce->_line = ch.back()->_line + 1;
}
}
ce->_parent = entry;
entry->_children << ce;
other.insert(ce->_line, "");
Branch b = allLeaves();
bool found = false;
for (int i = 0; i < b.size_s(); ++i) {
if (found) {
b[i]->_line++;
continue;
}
if (b[i] == ce) {
found = true;
if (i > 0)
if (b[i - 1]->_line == b[i]->_line)
b[i - 1]->_line++;
}
}
if (write) writeAll();
}
void PIConfig::setValue(const PIString & name, const PIString & value, const PIString & type, bool write) {
int number = getNumber(name);
if (number == -1) {
addLine(name, value, type);
Entry & e(getValue(name));
if (&e == &empty) {
addEntry(name, value, type);
return;
}
PIString tmp = settname[number] + " = " + value + " #" + setttype[number] + " " + settcom[number];
settval[number] = value;
all[settlines[number]] = tmp;
e._value = value;
e._type = type;
if (write) writeAll();
}
bool PIConfig::existsValue(const PIString & name) {
for (uint i = 0; i < settname.size(); i++)
if (settname[i] == name) return true;
return false;
}
void PIConfig::addLine(const PIString & name, const PIString & value, const PIString & type) {
if (setttab.size() > 0) *this << setttab[setttab.size() - 1] << name << " = " << value << " #" << type << "\n";
else *this << name << " = " << value << " #" << type << "\n";
settname.push_back(name);
settval.push_back(value);
settcom.push_back("");
setttype.push_back(type);
if (setttab.size() > 0) setttab.push_back(setttab[setttab.size() - 1]);
else setttab.push_back("\t");
settlines.push_back(all.size());
all.push_back(name + " = " + value + " #" + type);
flush();
}
void PIConfig::insertLine(uint number, const PIString & name, const PIString & value, const PIString & type) {
if (number >= settname.size()) {
addLine(name, value, type);
return;
int PIConfig::entryIndex(const PIString & name) {
PIStringList tree = name.split(delim);
Entry * ce = &root;
piForeachCA (i, tree) {
ce = ce->findChild(i);
if (ce == 0)
return -1;
}
settname.insert(number, name);
settval.insert(number, value);
settcom.insert(number, "");
setttab.insert(number, setttab[number]);
setttype.insert(number, type);
settlines.insert(number, settlines[number]);
for (uint i = number + 1; i < settlines.size(); i++) settlines[i]++;
all.insert(settlines[number], name + " = " + value + " #" + type);
flush();
writeAll();
}
int PIConfig::getNumber(const PIString & name) {
for (uint i = 0; i < settname.size(); i++)
if (settname[i] == name)
return i;
return -1;
Branch b = allLeaves();
return allLeaves().indexOf(ce);
}
void PIConfig::setValue(uint number, const PIString & value, bool write) {
PIString tmp = settname[number] + " = " + value + " #" + setttype[number] + " " + settcom[number];
settval[number] = value;
all[settlines[number]] = tmp;
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._value = value;
if (write) writeAll();
}
void PIConfig::setName(uint number, const PIString & name) {
PIString tmp = name + " = " + settval[number] + " #" + setttype[number] + " " + settcom[number];
settname[number] = name;
all[settlines[number]] = tmp;
writeAll();
void PIConfig::setName(uint number, const PIString & name, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._name = name;
if (write) writeAll();
}
void PIConfig::setType(uint number, const PIString & type) {
PIString tmp = settname[number] + " = " + settval[number] + " #" + type + " " + settcom[number];
setttype[number] = type;
all[settlines[number]] = tmp;
writeAll();
void PIConfig::setType(uint number, const PIString & type, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._type = type;
if (write) writeAll();
}
void PIConfig::setComment(uint number, const PIString & comment) {
PIString tmp = settname[number] + " = " + settval[number] + " #" + setttype[number] + " " + comment;
settcom[number] = comment;
all[settlines[number]] = tmp;
writeAll();
void PIConfig::setComment(uint number, const PIString & comment, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._comment = comment;
if (write) writeAll();
}
void PIConfig::deleteLine(const PIString & name) {
bool exist = false;
uint i;
for (i = 0; i < settname.size(); i++) {
if (settname[i] == name) {
exist = true;
break;
}
void PIConfig::removeEntry(const PIString & name, bool write) {
Entry & e(getValue(name));
if (&e == &empty) return;
Branch b = allLeaves();
removeEntry(b, &e);
if (write) writeAll();
}
void PIConfig::removeEntry(uint number, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
Branch b = allLeaves();
removeEntry(b, &e);
if (write) writeAll();
}
void PIConfig::removeEntry(Branch & b, PIConfig::Entry * e) {
bool leaf = true;
if (!e->isLeaf() && !e->_value.isEmpty()) {
e->_value.clear();
leaf = false;
} else {
int cc = e->_children.size_s();
piForTimes (cc)
removeEntry(b, e->_children.back());
}
if (!exist) return;
deleteLine(i);
}
void PIConfig::deleteLine(uint number) {
settname.erase(settname.begin() + number);
settval.erase(settval.begin() + number);
settcom.erase(settcom.begin() + number);
setttab.erase(setttab.begin() + number);
setttype.erase(setttype.begin() + number);
all.erase(all.begin() + settlines[number]);
for (uint i = number; i < settlines.size(); i++) settlines[i]--;
settlines.erase(settlines.begin() + number);
writeAll();
bool found = false;
for (int i = 0; i < b.size_s(); ++i) {
if (found) {
b[i]->_line--;
continue;
}
if (b[i] == e) found = true;
}
if (!leaf) return;
e->_parent->_children.remove(e);
b.remove(e);
other.remove(e->_line);
delete e;
}
void PIConfig::writeAll() {
clear();
uint c = 0;
for (uint i = 0; i < all.size() - 1; i++) {
if (c < settlines.size() && c < setttab.size()) {
if (settlines[c] == i) {
*this << setttab[c];
c++;
//writeEntry(&root);
buildFullNames(&root);
Branch b = allLeaves();
int j = 0;
for (int i = 0; i < other.size_s(); ++i) {
//cout << j << endl;
if (j >= 0 && j < b.size_s()) {
if (b[j]->_line == i) {
b[j]->buildLine();
*this << b[j]->_all << '\n';
++j;
} else {
*this << other[i];
if (i < other.size_s() - 1) *this << '\n';
}
} else {
*this << other[i];
if (i < other.size_s() - 1) *this << '\n';
}
*this << all[i] << "\n";
}
flush();
readAll();
}
void PIConfig::readAll() {
settname.clear();
settval.clear();
settcom.clear();
setttab.clear();
settlines.clear();
setttype.clear();
all.clear();
root.clear();
flush();
parse();
}
bool PIConfig::entryExists(const Entry * e, const PIString & name) const {
if (e->_children.isEmpty()) {
if (e->_name == name) return true;
else return false;
}
piForeachCA (i, e->_children)
if (entryExists(i, name)) return true;
return false;
}
void PIConfig::parse() {
PIString str, tab, comm;
PIString src, str, tab, comm, all, name, type;
PIStringList tree;
Entry * entry, * te, * ce;
int ind, sind;
bool isNew;
if (!isOpened()) return;
seek(0);
lines = 0;
seekToBegin();
other.clear();
lines = centry = 0;
while (!isEnd()) {
str = readLine();
other.push_back(PIString());
src = str = readLine();
//cout << str << endl;
tab = str.left(str.find(str.trimmed().left(1)));
str.trim();
all.push_back(str);
all = str;
ind = str.find('=');
if ((ind > 0) && !(str[0] == '#')) {
sind = str.find('#');
if (sind > 0) {
comm = str.right(str.size() - sind - 1).trimmed();
if (comm.length() > 0) setttype.push_back(comm[0]);
else setttype.push_back("s");
comm = comm.right(comm.size() - 1).trimmed();
settcom.push_back(comm);
comm = str.right(str.length() - sind - 1).trimmed();
if (comm.length() > 0) type = comm[0];
else type = "s";
comm = comm.right(comm.length() - 1).trimmed();
str = str.left(sind);
} else {
setttype.push_back("s");
settcom.push_back("");
type = "s";
comm = "";
}
settname.push_back(str.left(ind).trimmed());
settval.push_back(str.right(str.size() - ind - 1).trimmed());
setttab.push_back(tab);
settlines.push_back(lines);
}
//name = str.left(ind).trimmed();
tree = str.left(ind).trimmed().split(delim);
name = tree.back();
tree.pop_back();
entry = &root;
piForeachA (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();
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);
}