7.12.2010 - add PIString
This commit is contained in:
4
main.cpp
4
main.cpp
@@ -1,5 +1,5 @@
|
||||
#include "pitimer.h"
|
||||
#include "pip.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
cout << date2string(currentDate()) << endl;
|
||||
cout << PIString::fromDouble(1.25E+1).toFloat() << endl;
|
||||
};
|
||||
|
||||
127
piconfig.cpp
127
piconfig.cpp
@@ -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);
|
||||
}
|
||||
|
||||
90
piconfig.h
90
piconfig.h
@@ -6,51 +6,51 @@
|
||||
class PIConfig: public PIFile
|
||||
{
|
||||
public:
|
||||
PIConfig(const string & path);
|
||||
PIConfig(const PIString & path);
|
||||
~PIConfig() {;}
|
||||
|
||||
string getValue(const string & vname, const string & def = "", bool * exist = 0) const;
|
||||
string getValue(const string & vname, const char * def = "", bool * exist = 0) const;
|
||||
bool getValue(const string & vname, const bool def = false, bool * exist = 0) const;
|
||||
char getValue(const string & vname, const char def = 0, bool * exist = 0) const;
|
||||
short getValue(const string & vname, const short def = 0, bool * exist = 0) const;
|
||||
int getValue(const string & vname, const int def = 0, bool * exist = 0) const;
|
||||
long getValue(const string & vname, const long def = 0, bool * exist = 0) const;
|
||||
uchar getValue(const string & vname, const uchar def = 0, bool * exist = 0) const;
|
||||
ushort getValue(const string & vname, const ushort def = 0, bool * exist = 0) const;
|
||||
uint getValue(const string & vname, const uint def = 0, bool * exist = 0) const;
|
||||
ulong getValue(const string & vname, const ulong def = 0, bool * exist = 0) const;
|
||||
float getValue(const string & vname, const float def = 0., bool * exist = 0) const;
|
||||
double getValue(const string & vname, const double def = 0., bool * exist = 0) const;
|
||||
PIString getValue(const PIString & vname, const PIString & def = "", bool * exist = 0) const;
|
||||
PIString getValue(const PIString & vname, const char * def = "", bool * exist = 0) const;
|
||||
bool getValue(const PIString & vname, const bool def = false, bool * exist = 0) const;
|
||||
char getValue(const PIString & vname, const char def = 0, bool * exist = 0) const;
|
||||
short getValue(const PIString & vname, const short def = 0, bool * exist = 0) const;
|
||||
int getValue(const PIString & vname, const int def = 0, bool * exist = 0) const;
|
||||
long getValue(const PIString & vname, const long def = 0, bool * exist = 0) const;
|
||||
uchar getValue(const PIString & vname, const uchar def = 0, bool * exist = 0) const;
|
||||
ushort getValue(const PIString & vname, const ushort def = 0, bool * exist = 0) const;
|
||||
uint getValue(const PIString & vname, const uint def = 0, bool * exist = 0) const;
|
||||
ulong getValue(const PIString & vname, const ulong def = 0, bool * exist = 0) const;
|
||||
float getValue(const PIString & vname, const float def = 0., bool * exist = 0) const;
|
||||
double getValue(const PIString & vname, const double def = 0., bool * exist = 0) const;
|
||||
|
||||
void setValue(const string & name, const string & value, const string & type = "s", bool write = true);
|
||||
void setValue(const string & name, const char * value, bool write = true);
|
||||
void setValue(const string & name, const bool value, bool write = true);
|
||||
void setValue(const string & name, const char value, bool write = true);
|
||||
void setValue(const string & name, const short value, bool write = true);
|
||||
void setValue(const string & name, const int value, bool write = true);
|
||||
void setValue(const string & name, const long value, bool write = true);
|
||||
void setValue(const string & name, const uchar value, bool write = true);
|
||||
void setValue(const string & name, const ushort value, bool write = true);
|
||||
void setValue(const string & name, const uint value, bool write = true);
|
||||
void setValue(const string & name, const ulong value, bool write = true);
|
||||
void setValue(const string & name, const float value, bool write = true);
|
||||
void setValue(const string & name, const double value, bool write = true);
|
||||
void setValue(const PIString & name, const PIString & value, const PIString & type = "s", bool write = true);
|
||||
void setValue(const PIString & name, const char * value, bool write = true);
|
||||
void setValue(const PIString & name, const bool value, bool write = true);
|
||||
void setValue(const PIString & name, const char value, bool write = true);
|
||||
void setValue(const PIString & name, const short value, bool write = true);
|
||||
void setValue(const PIString & name, const int value, bool write = true);
|
||||
void setValue(const PIString & name, const long value, bool write = true);
|
||||
void setValue(const PIString & name, const uchar value, bool write = true);
|
||||
void setValue(const PIString & name, const ushort value, bool write = true);
|
||||
void setValue(const PIString & name, const uint value, bool write = true);
|
||||
void setValue(const PIString & name, const ulong value, bool write = true);
|
||||
void setValue(const PIString & name, const float value, bool write = true);
|
||||
void setValue(const PIString & name, const double value, bool write = true);
|
||||
|
||||
string getValue(int number) const {return settval[number];}
|
||||
string getName(int number) const {return settname[number];}
|
||||
string getComment(int number) const {return settcom[number];}
|
||||
void setValue(int number, const string & value, bool write = true);
|
||||
bool existsValue(const string & name);
|
||||
PIString getValue(int number) const {return settval[number];}
|
||||
PIString getName(int number) const {return settname[number];}
|
||||
PIString getComment(int number) const {return settcom[number];}
|
||||
void setValue(int number, const PIString & value, bool write = true);
|
||||
bool existsValue(const PIString & name);
|
||||
char getType(int number) const {return setttype[number][0];}
|
||||
int getNumber(const string & name);
|
||||
void setName(int number, const string & name);
|
||||
void setType(int number, const string & type);
|
||||
void setComment(int number, const string & comment);
|
||||
int getNumber(const PIString & name);
|
||||
void setName(int number, const PIString & name);
|
||||
void setType(int number, const PIString & type);
|
||||
void setComment(int number, const PIString & comment);
|
||||
int numValues() const {return settval.size();}
|
||||
void addLine(const string & name, const string & value, const string & type = "s");
|
||||
void insertLine(int number, const string & name, const string & value, const string & type = "s");
|
||||
void deleteLine(const string & name);
|
||||
void addLine(const PIString & name, const PIString & value, const PIString & type = "s");
|
||||
void insertLine(int number, const PIString & name, const PIString & value, const PIString & type = "s");
|
||||
void deleteLine(const PIString & name);
|
||||
void deleteLine(int number);
|
||||
void readAll();
|
||||
void writeAll();
|
||||
@@ -58,12 +58,12 @@ public:
|
||||
private:
|
||||
void parse();
|
||||
|
||||
vector<string> settname;
|
||||
vector<string> settval;
|
||||
vector<string> settcom;
|
||||
vector<string> setttab;
|
||||
vector<string> setttype;
|
||||
vector<string> all;
|
||||
vector<PIString> settname;
|
||||
vector<PIString> settval;
|
||||
vector<PIString> settcom;
|
||||
vector<PIString> setttab;
|
||||
vector<PIString> setttype;
|
||||
vector<PIString> all;
|
||||
vector<int> settlines;
|
||||
int lines;
|
||||
|
||||
|
||||
10
pifile.cpp
10
pifile.cpp
@@ -1,20 +1,20 @@
|
||||
#include "pifile.h"
|
||||
|
||||
|
||||
bool PIFile::open(const string & path_, Flags<Mode> mode_) {
|
||||
bool PIFile::open(const PIString & path_, Flags<Mode> mode_) {
|
||||
cpath = path_;
|
||||
cmode = mode_;
|
||||
if (cmode[New]) {
|
||||
stream.open(cpath.c_str(), fstream::in | fstream::out | fstream::trunc);
|
||||
stream.open(cpath, fstream::in | fstream::out | fstream::trunc);
|
||||
stream.close();
|
||||
cmode &= ~New;
|
||||
stream.open(cpath.c_str(), (fstream::openmode)(int)cmode | fstream::binary);
|
||||
} else stream.open(cpath.c_str(), (fstream::openmode)(int)cmode | fstream::binary);
|
||||
stream.open(cpath, (fstream::openmode)(int)cmode | fstream::binary);
|
||||
} else stream.open(cpath, (fstream::openmode)(int)cmode | fstream::binary);
|
||||
return isOpened();
|
||||
}
|
||||
|
||||
|
||||
string PIFile::readLine() {
|
||||
PIString PIFile::readLine() {
|
||||
char * buff = new char[4096];
|
||||
stream.getline(buff, 4096);
|
||||
return string(buff);
|
||||
|
||||
16
pifile.h
16
pifile.h
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <fstream>
|
||||
#include "piincludes.h"
|
||||
#include "pistring.h"
|
||||
|
||||
using std::fstream;
|
||||
|
||||
@@ -11,19 +12,19 @@ class PIFile
|
||||
public:
|
||||
PIFile() {;}
|
||||
enum Mode {Read = fstream::in, Write = fstream::out, Truncate = fstream::trunc, New = fstream::app};
|
||||
PIFile(const string & path, Flags<Mode> mode = Read | Write) {open(path, mode);}
|
||||
PIFile(const PIString & path, Flags<Mode> mode = Read | Write) {open(path, mode);}
|
||||
~PIFile() {if (isOpened()) close();}
|
||||
|
||||
bool open(const string & path, Flags<Mode> mode = Read | Write);
|
||||
bool open(const PIString & path, Flags<Mode> mode = Read | Write);
|
||||
void close() {stream.close();}
|
||||
void clear() {close(); stream.open(cpath.c_str(), fstream::trunc | (fstream::openmode)(int)cmode);}
|
||||
void clear() {close(); stream.open(cpath, fstream::trunc | (fstream::openmode)(int)cmode);}
|
||||
void seek(int position);
|
||||
void resize(int new_size, char fill = 0);
|
||||
void fill(char c) {stream.fill(c);}
|
||||
void flush() {stream.flush();}
|
||||
string readLine();
|
||||
PIString readLine();
|
||||
|
||||
string path() const {return cpath;}
|
||||
PIString path() const {return cpath;}
|
||||
Flags<Mode> mode() const {return cmode;}
|
||||
int size();
|
||||
int pos();
|
||||
@@ -45,7 +46,8 @@ public:
|
||||
PIFile & writeBinary(const double v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
|
||||
PIFile & operator <<(const char & v) {stream.write(&v, 1); return *this;}
|
||||
PIFile & operator <<(const string & v) {stream.write(v.c_str(), v.size()); return *this;}
|
||||
//PIFile & operator <<(const string & v) {stream.write(v.c_str(), v.size()); return *this;}
|
||||
PIFile & operator <<(const PIString & v) {stream.write(v.data(), v.size()); return *this;}
|
||||
PIFile & operator <<(const short & v) {stream << v; return *this;}
|
||||
PIFile & operator <<(const int & v) {stream << v; return *this;}
|
||||
PIFile & operator <<(const long & v) {stream << v; return *this;}
|
||||
@@ -69,7 +71,7 @@ public:
|
||||
|
||||
private:
|
||||
fstream stream;
|
||||
string cpath;
|
||||
PIString cpath;
|
||||
Flags<Mode> cmode;
|
||||
|
||||
};
|
||||
|
||||
12
piincludes.h
12
piincludes.h
@@ -96,17 +96,5 @@ inline string dtos(const double num) {
|
||||
char ch[256];
|
||||
sprintf(ch, "%g", num);
|
||||
return string(ch); };
|
||||
inline string s_left(const string & str, int len) {return str.substr(0, len);};
|
||||
inline string s_right(const string & str, int len) {return str.size() < len ? str : str.substr(str.size() - len, len);};
|
||||
inline string s_trimmed(const string & str) {
|
||||
int st = 0, fn = 0;
|
||||
for (int i = 0; i < str.size(); ++i)
|
||||
if (str[i] != ' ' && str[i] != '\t')
|
||||
{st = i; break;}
|
||||
for (int i = str.size() - 1; i >= 0; --i)
|
||||
if (str[i] != ' ' && str[i] != '\t')
|
||||
{fn = i; break;}
|
||||
return str.substr(st, fn - st + 1);
|
||||
};
|
||||
|
||||
#endif // PIINCLUDES_H
|
||||
|
||||
4
pip.h
Normal file
4
pip.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#include "pitimer.h"
|
||||
#include "piconfig.h"
|
||||
#include "piconsole.h"
|
||||
#include "piprotocol.h"
|
||||
@@ -39,6 +39,7 @@ public:
|
||||
void setSpeed(int speed) {ospeed = ispeed = speed;}
|
||||
void setOutSpeed(int speed) {ospeed = speed;}
|
||||
void setInSpeed(int speed) {ispeed = speed;}
|
||||
void setDevice(const string & dev) {devName = dev;}
|
||||
void setParameters(Flags<PISerial::Parameters> parameters) {params = parameters;}
|
||||
void setReadData(void * headerPtr, int headerSize, int dataSize) {this->headerPtr = headerPtr;
|
||||
this->headerSize = headerSize;
|
||||
|
||||
147
pistring.cpp
Normal file
147
pistring.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#include "pistring.h"
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const char * str) {
|
||||
int i = 0;
|
||||
while (str[i] != '\0') push_back(str[i++]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const string & str) {
|
||||
int l = str.size();
|
||||
for (int i = 0; i < l; ++i) push_back(str[i]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const PIString & str) {
|
||||
int l = str.size();
|
||||
for (int i = 0; i < l; ++i) push_back(str[i]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool PIString::operator ==(const PIString & str) const {
|
||||
int l = str.size();
|
||||
if (size() != l) return false;
|
||||
for (int i = 0; i < l; ++i)
|
||||
if (str[i] != at(i))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::mid(const int start, const int len) const {
|
||||
PIString str;
|
||||
int s = start, l = len;
|
||||
if (l == 0) return str;
|
||||
if (s < 0) {
|
||||
l += s;
|
||||
s = 0;
|
||||
}
|
||||
if (l < 0) {
|
||||
for (int i = s; i < size(); ++i)
|
||||
str += at(i);
|
||||
} else {
|
||||
if (l > size() - s)
|
||||
l = size() - s;
|
||||
for (int i = s; i < s + l; ++i)
|
||||
str += at(i);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::cutMid(const int start, const int len) {
|
||||
int s = start, l = len;
|
||||
if (l == 0) return *this;
|
||||
if (s < 0) {
|
||||
l += s;
|
||||
s = 0;
|
||||
}
|
||||
if (l < 0)
|
||||
erase(begin() + s, end());
|
||||
else {
|
||||
if (l > size() - s)
|
||||
l = size() - s;
|
||||
erase(begin() + s, begin() + s + l);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::trimmed() const {
|
||||
int st = 0, fn = 0;
|
||||
for (int i = 0; i < size(); ++i)
|
||||
if (at(i) != ' ' && at(i) != '\t')
|
||||
{st = i; break;}
|
||||
for (int i = size() - 1; i >= 0; --i)
|
||||
if (at(i) != ' ' && at(i) != '\t')
|
||||
{fn = i; break;}
|
||||
return mid(st, fn - st + 1);
|
||||
}
|
||||
|
||||
|
||||
int PIString::find(const char str, const int start) {
|
||||
for (int i = start; i < size(); ++i)
|
||||
if (at(i) == str)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int PIString::find(const PIString str, const int start) {
|
||||
int l = str.size();
|
||||
for (int i = start; i < size() - l + 1; ++i)
|
||||
if (mid(i, l) == str)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int PIString::findLast(const char str, const int start) {
|
||||
for (int i = size() - 1; i >= start; --i)
|
||||
if (at(i) == str)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int PIString::findLast(const PIString str, const int start) {
|
||||
int l = str.size();
|
||||
for (int i = size() - l; i >= start; --i)
|
||||
if (mid(i, l) == str)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::toUpperCase() const {
|
||||
PIString str(*this);
|
||||
int l = str.size();
|
||||
for (int i = 0; i < l; ++i) str[i] = chrUpr(str[i]);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::toLowerCase() const {
|
||||
PIString str(*this);
|
||||
int l = str.size();
|
||||
for (int i = 0; i < l; ++i) str[i] = chrLwr(str[i]);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
char chrUpr(char c) {
|
||||
if (c >= 'a' && c <= 'z') return c + 'A' - 'a';
|
||||
//if (c >= 'а' && c <= 'я') return c + 'А' - 'а';
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
char chrLwr(char c) {
|
||||
if (c >= 'A' && c <= 'Z') return c + 'a' - 'A';
|
||||
//if (c >= 'А' && c <= 'Я') return c + 'а' - 'А';
|
||||
return c;
|
||||
}
|
||||
83
pistring.h
Normal file
83
pistring.h
Normal file
@@ -0,0 +1,83 @@
|
||||
#ifndef PISTRING_H
|
||||
#define PISTRING_H
|
||||
|
||||
#include "piincludes.h"
|
||||
|
||||
class PIString: public vector<char>
|
||||
{
|
||||
public:
|
||||
PIString() {;}
|
||||
PIString(const char & c) {*this += c;}
|
||||
PIString(const char * str) {*this += str;}
|
||||
PIString(const string & str) {*this += str;}
|
||||
PIString(const PIString & str) {*this += str;}
|
||||
PIString(const int len, const char c = ' ') {resize(len, c);}
|
||||
|
||||
operator const char*() {return (size() == 0) ? "" : string(&at(0), size()).c_str();}
|
||||
operator const string() {return (size() == 0) ? string() : string(&at(0), size());}
|
||||
inline char & operator [](const int pos) {return at(pos);}
|
||||
inline const char operator [](const int pos) const {return at(pos);}
|
||||
|
||||
PIString & operator +=(const PIString & str);
|
||||
inline PIString & operator +=(const char & c) {push_back(c); return *this;}
|
||||
PIString & operator +=(const char * str);
|
||||
PIString & operator +=(const string & str);
|
||||
|
||||
bool operator ==(const PIString & str) const;
|
||||
inline bool operator ==(const char & c) const {return *this == PIString(c);}
|
||||
inline bool operator ==(const char * str) const {return *this == PIString(str);}
|
||||
inline bool operator ==(const string & str) const {return *this == PIString(str);}
|
||||
|
||||
PIString mid(const int start, const int len = -1) const;
|
||||
inline PIString left(const int len) const {return len <= 0 ? PIString() : mid(0, len);}
|
||||
inline PIString right(const int len) const {return len <= 0 ? PIString() : mid(size() - len, len);}
|
||||
PIString & cutMid(const int start, const int len);
|
||||
inline PIString & cutLeft(const int len) {return len <= 0 ? *this : cutMid(0, len);}
|
||||
inline PIString & cutRight(const int len) {return len <= 0 ? *this : cutMid(size() - len, len);}
|
||||
PIString trimmed() const;
|
||||
const char * data() const {return ((size() == 0) ? string() : string(&at(0), size())).c_str();}
|
||||
string stdString() const {return (size() == 0) ? string() : string(&at(0), size());}
|
||||
|
||||
PIString toUpperCase() const;
|
||||
PIString toLowerCase() const;
|
||||
|
||||
int find(const char str, const int start = 0);
|
||||
int find(const PIString str, const int start = 0);
|
||||
inline int find(const char * str, const int start = 0) {return find(PIString(str), start);}
|
||||
inline int find(const string str, const int start = 0) {return find(PIString(str), start);}
|
||||
int findLast(const char str, const int start = 0);
|
||||
int findLast(const PIString str, const int start = 0);
|
||||
inline int findLast(const char * str, const int start = 0) {return findLast(PIString(str), start);}
|
||||
inline int findLast(const string str, const int start = 0) {return findLast(PIString(str), start);}
|
||||
|
||||
inline int length() const {return size();}
|
||||
inline bool isEmpty() const {return size() == 0;}
|
||||
|
||||
int toInt() const {return atoi(data());}
|
||||
short toShort() const {return (short)atoi(data());}
|
||||
long toLong() const {return atol(data());}
|
||||
float toFloat() const {return (float)atof(data());}
|
||||
double toDouble() const {return atof(data());}
|
||||
|
||||
inline static PIString fromInt(const int value) {return PIString(itos(value));}
|
||||
inline static PIString fromShort(const short value) {return PIString(itos(value));}
|
||||
inline static PIString fromLong(const long value) {return PIString(ltos(value));}
|
||||
inline static PIString fromFloat(const float value) {return PIString(ftos(value));}
|
||||
inline static PIString fromDouble(const double value) {return PIString(dtos(value));}
|
||||
|
||||
};
|
||||
|
||||
inline PIString operator +(const PIString & str, const PIString & f) {PIString s(str); s += f; return s;}
|
||||
|
||||
inline PIString operator +(const PIString & f, const char & c) {PIString s(f); s.push_back(c); return s;}
|
||||
inline PIString operator +(const PIString & f, const char * str) {PIString s(f); s += str; return s;}
|
||||
inline PIString operator +(const PIString & f, const string & str) {PIString s(f); s += str; return s;}
|
||||
|
||||
inline PIString operator +(const char & c, const PIString & f) {return PIString(c) + f;}
|
||||
inline PIString operator +(const char * str, const PIString & f) {return PIString(str) + f;}
|
||||
inline PIString operator +(const string & str, const PIString & f) {return PIString(str) + f;}
|
||||
|
||||
char chrUpr(char c);
|
||||
char chrLwr(char c);
|
||||
|
||||
#endif // PISTRING_H
|
||||
Reference in New Issue
Block a user