7.12.2010 - add PIString

This commit is contained in:
peri4
2010-12-07 01:55:30 +03:00
parent e321b5187d
commit 08f214dbfa
10 changed files with 360 additions and 134 deletions

View File

@@ -1,64 +1,64 @@
#include "piconfig.h"
PIConfig::PIConfig(const string & path): PIFile(path) {
PIConfig::PIConfig(const PIString & path): PIFile(path) {
if (!isOpened())
open(path, Read | Write | New);
parse();
}
bool PIConfig::getValue(const string & vname, const bool def, bool * exist) const {
bool PIConfig::getValue(const PIString & vname, const bool def, bool * exist) const {
return atob(getValue(vname, btos(def), exist));}
char PIConfig::getValue(const string & vname, const char def, bool * exist) const {
return atoi(getValue(vname, string(1, def), exist).c_str());}
short PIConfig::getValue(const string & vname, const short def, bool * exist) const {
return atoi(getValue(vname, itos(def), exist).c_str());}
int PIConfig::getValue(const string & vname, const int def, bool * exist) const {
return atoi(getValue(vname, itos(def), exist).c_str());}
long PIConfig::getValue(const string & vname, const long def, bool * exist) const {
return atoi(getValue(vname, ltos(def), exist).c_str());}
uchar PIConfig::getValue(const string & vname, const uchar def, bool * exist) const {
return atoi(getValue(vname, uitos(def), exist).c_str());}
ushort PIConfig::getValue(const string & vname, const ushort def, bool * exist) const {
return atoi(getValue(vname, uitos(def), exist).c_str());}
uint PIConfig::getValue(const string & vname, const uint def, bool * exist) const {
return atoi(getValue(vname, uitos(def), exist).c_str());}
ulong PIConfig::getValue(const string & vname, const ulong def, bool * exist) const {
return atoi(getValue(vname, ultos(def), exist).c_str());}
float PIConfig::getValue(const string & vname, const float def, bool * exist) const {
return atof(getValue(vname, ftos(def), exist).c_str());}
double PIConfig::getValue(const string & vname, const double def, bool * exist) const {
return atof(getValue(vname, dtos(def), exist).c_str());}
char PIConfig::getValue(const PIString & vname, const char def, bool * exist) const {
return getValue(vname, PIString(1, 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 string & name, const char * value, bool write) {
setValue(name, string(value), "s", write);}
void PIConfig::setValue(const string & name, const bool value, bool 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 string & name, const char value, bool write) {
setValue(name, string(1, value), "s", write);}
void PIConfig::setValue(const string & name, const short value, bool 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 string & name, const int value, bool write) {
void PIConfig::setValue(const PIString & name, const int value, bool write) {
setValue(name, itos(value), "n", write);}
void PIConfig::setValue(const string & name, const long value, bool write) {
void PIConfig::setValue(const PIString & name, const long value, bool write) {
setValue(name, ltos(value), "n", write);}
void PIConfig::setValue(const string & name, const uchar value, bool write) {
void PIConfig::setValue(const PIString & name, const uchar value, bool write) {
setValue(name, uitos(value), "n", write);}
void PIConfig::setValue(const string & name, const ushort value, bool write) {
void PIConfig::setValue(const PIString & name, const ushort value, bool write) {
setValue(name, uitos(value), "n", write);}
void PIConfig::setValue(const string & name, const uint value, bool write) {
void PIConfig::setValue(const PIString & name, const uint value, bool write) {
setValue(name, uitos(value), "n", write);}
void PIConfig::setValue(const string & name, const ulong value, bool write) {
void PIConfig::setValue(const PIString & name, const ulong value, bool write) {
setValue(name, ultos(value), "n", write);}
void PIConfig::setValue(const string & name, const float value, bool write) {
void PIConfig::setValue(const PIString & name, const float value, bool write) {
setValue(name, ftos(value), "f", write);}
void PIConfig::setValue(const string & name, const double value, bool write) {
void PIConfig::setValue(const PIString & name, const double value, bool write) {
setValue(name, dtos(value), "f", write);}
string PIConfig::getValue(const string & vname, const string & def, bool * exist) const {
PIString PIConfig::getValue(const PIString & vname, const PIString & def, bool * exist) const {
for (int i = 0; i < settname.size(); i++) {
if (settname[i] == vname) {
if (exist != 0) *exist = true;
@@ -70,27 +70,27 @@ string PIConfig::getValue(const string & vname, const string & def, bool * exist
}
void PIConfig::setValue(const string & name, const string & value, const string & type, bool write) {
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);
return;
}
string tmp = settname[number] + " = " + value + " #" + setttype[number] + " " + settcom[number];
PIString tmp = settname[number] + " = " + value + " #" + setttype[number] + " " + settcom[number];
settval[number] = value;
all[settlines[number]] = tmp;
if (write) writeAll();
}
bool PIConfig::existsValue(const string & name) {
bool PIConfig::existsValue(const PIString & name) {
for (int i = 0; i < settname.size(); i++)
if (settname[i] == name) return true;
return false;
}
void PIConfig::addLine(const string & name, const string & value, const string & type) {
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);
@@ -105,7 +105,7 @@ void PIConfig::addLine(const string & name, const string & value, const string &
}
void PIConfig::insertLine(int number, const string & name, const string & value, const string & type) {
void PIConfig::insertLine(int number, const PIString & name, const PIString & value, const PIString & type) {
if (number >= settname.size()) {
addLine(name, value, type);
return;
@@ -123,7 +123,7 @@ void PIConfig::insertLine(int number, const string & name, const string & value,
}
int PIConfig::getNumber(const string & name) {
int PIConfig::getNumber(const PIString & name) {
for (int i = 0; i < settname.size(); i++)
if (settname[i] == name)
return i;
@@ -131,39 +131,39 @@ int PIConfig::getNumber(const string & name) {
}
void PIConfig::setValue(int number, const string & value, bool write) {
string tmp = settname[number] + " = " + value + " #" + setttype[number] + " " + settcom[number];
void PIConfig::setValue(int number, const PIString & value, bool write) {
PIString tmp = settname[number] + " = " + value + " #" + setttype[number] + " " + settcom[number];
settval[number] = value;
all[settlines[number]] = tmp;
if (write) writeAll();
}
void PIConfig::setName(int number, const string & name) {
string tmp = name + " = " + settval[number] + " #" + setttype[number] + " " + settcom[number];
void PIConfig::setName(int number, const PIString & name) {
PIString tmp = name + " = " + settval[number] + " #" + setttype[number] + " " + settcom[number];
settname[number] = name;
all[settlines[number]] = tmp;
writeAll();
}
void PIConfig::setType(int number, const string & type) {
string tmp = settname[number] + " = " + settval[number] + " #" + type + " " + settcom[number];
void PIConfig::setType(int number, const PIString & type) {
PIString tmp = settname[number] + " = " + settval[number] + " #" + type + " " + settcom[number];
setttype[number] = type;
all[settlines[number]] = tmp;
writeAll();
}
void PIConfig::setComment(int number, const string & comment) {
string tmp = settname[number] + " = " + settval[number] + " #" + setttype[number] + " " + comment;
void PIConfig::setComment(int number, const PIString & comment) {
PIString tmp = settname[number] + " = " + settval[number] + " #" + setttype[number] + " " + comment;
settcom[number] = comment;
all[settlines[number]] = tmp;
writeAll();
}
void PIConfig::deleteLine(const string & name) {
void PIConfig::deleteLine(const PIString & name) {
bool exist = false;
int i;
for (i = 0; i < settname.size(); i++) {
@@ -220,31 +220,32 @@ void PIConfig::readAll() {
void PIConfig::parse() {
string str, tab, comm;
PIString str, tab, comm;
int ind, sind;
if (!isOpened()) return;
seek(0);
lines = 0;
while (!isEnd()) {
str = readLine();
tab = s_left(str, str.find_first_of(s_left(s_trimmed(str), 1)));
str = s_trimmed(str);
//cout << str << endl;
tab = str.left(str.find(str.trimmed().left(1)));
str = str.trimmed();
all.push_back(str);
ind = str.find_first_of('=');
ind = str.find('=');
if ((ind > 0) && !(str[0] == '#')) {
sind = str.find_first_of('#');
sind = str.find('#');
if (sind > 0) {
comm = s_trimmed(s_right(str, str.size() - sind - 1));
setttype.push_back(s_left(comm, 1));
comm = s_trimmed(s_right(comm, comm.size() - 1));
comm = str.right(str.size() - sind - 1).trimmed();
setttype.push_back(comm[0]);
comm = comm.right(comm.size() - 1).trimmed();
settcom.push_back(comm);
str = s_left(str, sind);
str = str.left(sind);
} else {
setttype.push_back("s");
settcom.push_back("");
}
settname.push_back(s_trimmed(s_left(str, ind)));
settval.push_back(s_trimmed(s_right(str, str.size() - ind - 1)));
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);
}