git-svn-id: svn://db.shs.com.ru/pip@470 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-04-25 12:55:47 +00:00
parent 826db9e87d
commit ed059005f9
15 changed files with 101 additions and 78 deletions

View File

@@ -237,12 +237,14 @@ bool PIConfig::Entry::entryExists(const Entry * e, const PIString & name) const
}
#ifdef PIP_STD_IOSTREAM
void PIConfig::Entry::coutt(std::ostream & s, const PIString & p) const {
PIString nl = p + " ";
if (!_value.isEmpty()) s << p << _name << " = " << _value << std::endl;
else std::cout << p << _name << std::endl;
piForeachC (Entry * i, _children) i->coutt(s, nl);
}
#endif
void PIConfig::Entry::piCoutt(PICout s, const PIString & p) const {
@@ -815,6 +817,7 @@ void PIConfig::parse() {
}
#ifdef PIP_STD_IOSTREAM
std::ostream &operator <<(std::ostream & s, const PIConfig::Entry & v) {
s << v.value();
return s;
@@ -825,3 +828,4 @@ std::ostream &operator <<(std::ostream & s, const PIConfig::Branch & v) {
v.coutt(s, "");
return s;
}
#endif

View File

@@ -75,7 +75,9 @@ public:
class PIP_EXPORT Branch: public PIVector<Entry * > {
friend class PIConfig;
friend class Entry;
#ifdef PIP_STD_IOSTREAM
friend std::ostream & operator <<(std::ostream & s, const Branch & v);
#endif
friend PICout operator <<(PICout s, const Branch & v);
public:
Branch() {;}
@@ -97,7 +99,9 @@ public:
private:
bool entryExists(const Entry * e, const PIString & name) const;
void allLeaves(Branch & b, Entry * e) {piForeach (Entry * i, e->_children) {if (i->isLeaf()) b << i; else allLeaves(b, i);}}
#ifdef PIP_STD_IOSTREAM
void coutt(std::ostream & s, const PIString & p) const {piForeachC (Entry * i, *this) i->coutt(s, p);}
#endif
void piCoutt(PICout s, const PIString & p) const {piForeachC (Entry * i, *this) i->piCoutt(s, p);}
static Entry _empty;
@@ -303,7 +307,9 @@ public:
bool entryExists(const Entry * e, const PIString & name) const;
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}
#ifdef PIP_STD_IOSTREAM
void coutt(std::ostream & s, const PIString & p) const;
#endif
void piCoutt(PICout s, const PIString & p) const;
void deleteBranch() {piForeach (Entry * i, _children) {i->deleteBranch(); delete i;}}
@@ -500,8 +506,11 @@ private:
};
#ifdef PIP_STD_IOSTREAM
std::ostream & operator <<(std::ostream & s, const PIConfig::Branch & v);
std::ostream & operator <<(std::ostream & s, const PIConfig::Entry & v);
#endif
inline PICout operator <<(PICout s, const PIConfig::Branch & v) {s.setControl(0, true); v.piCoutt(s, ""); s.restoreControl(); return s;}
inline PICout operator <<(PICout s, const PIConfig::Entry & v) {s << v.value(); return s;}

View File

@@ -85,6 +85,10 @@
REGISTER_DEVICE(PIFile)
PRIVATE_DEFINITION_START(PIFile)
FILE * fd;
PRIVATE_DEFINITION_END(PIFile)
PIString PIFile::FileInfo::name() const {
if (path.isEmpty()) return PIString();
@@ -123,14 +127,14 @@ PIString PIFile::FileInfo::dir() const {
PIFile::PIFile(): PIIODevice() {
fd = 0;
PRIVATE->fd = 0;
fdi = -1;
setPrecision(5);
}
PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
fd = 0;
PRIVATE->fd = 0;
fdi = -1;
setPrecision(5);
if (!path.isEmpty())
@@ -144,7 +148,7 @@ bool PIFile::openTemporary(PIIODevice::DeviceMode mode) {
//PIFile::PIFile(const PIFile & other) {
// fd = 0;
// PRIVATE->fd = 0;
// fdi = -1;
// setPrecision(other.prec_);
// setPath(other.path());
@@ -162,29 +166,29 @@ bool PIFile::openDevice() {
if (fd != 0) fclose(fd);
}
}
fd = _fopen_call_(p.data(), strType(mode_).data());
//piCout << "fopen " << path() << ": " << strType(mode_).data() << fd;
bool opened = (fd != 0);
PRIVATE->fd = _fopen_call_(p.data(), strType(mode_).data());
//piCout << "fopen " << path() << ": " << strType(mode_).data() << PRIVATE->fd;
bool opened = (PRIVATE->fd != 0);
if (opened) {
fdi = fileno(fd);
fdi = fileno(PRIVATE->fd);
#ifndef WINDOWS
fcntl(fdi, F_SETFL, O_NONBLOCK);
#endif
_fseek_call_(fd, 0, SEEK_SET);
clearerr(fd);
_fseek_call_(PRIVATE->fd, 0, SEEK_SET);
clearerr(PRIVATE->fd);
}
//piCout << "open file" << fd << opened_;
//piCout << "open file" << PRIVATE->fd << opened_;
return opened;
}
bool PIFile::closeDevice() {
//piCout << "close file" << fd << opened_;
if (isClosed() || fd == 0) return true;
bool cs = (fclose(fd) == 0);
if (cs) fd = 0;
//piCout << "close file" << PRIVATE->fd << opened_;
if (isClosed() || PRIVATE->fd == 0) return true;
bool cs = (fclose(PRIVATE->fd) == 0);
if (cs) PRIVATE->fd = 0;
fdi = -1;
//piCout << "closed file" << fd << opened_;
//piCout << "closed file" << PRIVATE->fd << opened_;
return cs;
}
@@ -194,7 +198,7 @@ PIString PIFile::readLine() {
if (isClosed()) return str;
int cc;
while (!isEnd()) {
cc = fgetc(fd);
cc = fgetc(PRIVATE->fd);
if (char(cc) == '\n' || cc == EOF) break;
str.push_back(char(cc));
}
@@ -243,9 +247,9 @@ PIByteArray PIFile::readAll(bool forceRead) {
llong PIFile::size() const {
if (isClosed()) return -1;
llong s, cp = pos();
_fseek_call_(fd, 0, SEEK_END); clearerr(fd);
_fseek_call_(PRIVATE->fd, 0, SEEK_END); clearerr(PRIVATE->fd);
s = pos();
_fseek_call_(fd, cp, SEEK_SET); clearerr(fd);
_fseek_call_(PRIVATE->fd, cp, SEEK_SET); clearerr(PRIVATE->fd);
return s;
}
@@ -313,29 +317,29 @@ PIString PIFile::strType(const PIIODevice::DeviceMode type) {
void PIFile::flush() {
if (isOpened()) fflush(fd);
if (isOpened()) fflush(PRIVATE->fd);
}
void PIFile::seek(llong position) {
if (isClosed()) return;
if (position == pos()) return;
_fseek_call_(fd, position, SEEK_SET);
clearerr(fd);
_fseek_call_(PRIVATE->fd, position, SEEK_SET);
clearerr(PRIVATE->fd);
}
void PIFile::seekToBegin() {
if (isClosed()) return;
_fseek_call_(fd, 0, SEEK_SET);
clearerr(fd);
_fseek_call_(PRIVATE->fd, 0, SEEK_SET);
clearerr(PRIVATE->fd);
}
void PIFile::seekToEnd() {
if (isClosed()) return;
_fseek_call_(fd, 0, SEEK_END);
clearerr(fd);
_fseek_call_(PRIVATE->fd, 0, SEEK_END);
clearerr(PRIVATE->fd);
}
@@ -343,12 +347,12 @@ void PIFile::seekToLine(llong line) {
if (isClosed()) return;
seekToBegin();
piForTimes(line) readLine();
clearerr(fd);
clearerr(PRIVATE->fd);
}
char PIFile::readChar() {
return (char)fgetc(fd);
return (char)fgetc(PRIVATE->fd);
}
@@ -360,13 +364,13 @@ void PIFile::setPath(const PIString & path) {
llong PIFile::pos() const {
if (isClosed()) return -1;
return _ftell_call_(fd);
return _ftell_call_(PRIVATE->fd);
}
bool PIFile::isEnd() const {
if (isClosed()) return true;
return (feof(fd) || ferror(fd));
return (feof(PRIVATE->fd) || ferror(PRIVATE->fd));
}
@@ -378,169 +382,169 @@ void PIFile::setPrecision(int prec) {
PIFile &PIFile::operator <<(double v) {
if (canWrite() && fd != 0) ret = fprintf(fd, ("%" + prec_str + "lf").data(), v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, ("%" + prec_str + "lf").data(), v);
return *this;
}
PIFile &PIFile::operator >>(double & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%lf", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%lf", &v);
return *this;
}
PIFile &PIFile::operator >>(float & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%f", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%f", &v);
return *this;
}
PIFile &PIFile::operator >>(ullong & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%lln", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%lln", &v);
return *this;
}
PIFile &PIFile::operator >>(ulong & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%ln", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%ln", &v);
return *this;
}
PIFile &PIFile::operator >>(uint & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%n", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%n", &v);
return *this;
}
PIFile &PIFile::operator >>(ushort & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%hn", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%hn", &v);
return *this;
}
PIFile &PIFile::operator >>(uchar & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%hhn", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%hhn", &v);
return *this;
}
PIFile &PIFile::operator >>(llong & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%lln", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%lln", &v);
return *this;
}
PIFile &PIFile::operator >>(long & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%ln", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%ln", &v);
return *this;
}
PIFile &PIFile::operator >>(int & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%n", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%n", &v);
return *this;
}
PIFile &PIFile::operator >>(short & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%hn", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%hn", &v);
return *this;
}
PIFile &PIFile::operator >>(char & v) {
if (canRead() && fd != 0) ret = fscanf(fd, "%hhn", &v);
if (canRead() && PRIVATE->fd != 0) ret = fscanf(PRIVATE->fd, "%hhn", &v);
return *this;
}
PIFile &PIFile::operator <<(float v) {
if (canWrite() && fd != 0) ret = fprintf(fd, ("%" + prec_str + "f").data(), v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, ("%" + prec_str + "f").data(), v);
return *this;
}
PIFile &PIFile::operator <<(ullong v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%llu", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%llu", v);
return *this;
}
PIFile &PIFile::operator <<(ulong v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%lu", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%lu", v);
return *this;
}
PIFile &PIFile::operator <<(uint v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%u", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%u", v);
return *this;
}
PIFile &PIFile::operator <<(ushort v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%hu", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%hu", v);
return *this;
}
PIFile &PIFile::operator <<(uchar v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%u", int(v));
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%u", int(v));
return *this;
}
PIFile &PIFile::operator <<(llong v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%lld", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%lld", v);
return *this;
}
PIFile &PIFile::operator <<(long v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%ld", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%ld", v);
return *this;
}
PIFile &PIFile::operator <<(int v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%d", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%d", v);
return *this;
}
PIFile &PIFile::operator <<(short v) {
if (canWrite() && fd != 0) ret = fprintf(fd, "%hd", v);
if (canWrite() && PRIVATE->fd != 0) ret = fprintf(PRIVATE->fd, "%hd", v);
return *this;
}
PIFile &PIFile::operator <<(const PIByteArray & v) {
if (canWrite() && fd != 0) write(v.data(), v.size());
if (canWrite() && PRIVATE->fd != 0) write(v.data(), v.size());
return *this;
}
PIFile &PIFile::operator <<(const char v) {
if (canWrite() && fd != 0) write(&v, 1);
if (canWrite() && PRIVATE->fd != 0) write(&v, 1);
return *this;
}
int PIFile::readDevice(void * read_to, int max_size) {
if (!canRead() || fd == 0) return -1;
return fread(read_to, 1, max_size, fd);
if (!canRead() || PRIVATE->fd == 0) return -1;
return fread(read_to, 1, max_size, PRIVATE->fd);
}
int PIFile::writeDevice(const void * data, int max_size) {
if (!canWrite() || fd == 0) return -1;
return fwrite(data, 1, max_size, fd);
if (!canWrite() || PRIVATE->fd == 0) return -1;
return fwrite(data, 1, max_size, PRIVATE->fd);
}
PIFile &PIFile::operator <<(const PIString & v) {
if (canWrite() && fd != 0)
if (canWrite() && PRIVATE->fd != 0)
*this << v.toCharset(defaultCharset());
return *this;
}
@@ -548,9 +552,9 @@ PIFile &PIFile::operator <<(const PIString & v) {
void PIFile::clear() {
close();
fd = fopen(path().data(), "w");
if (fd != 0) fclose(fd);
fd = 0;
PRIVATE->fd = fopen(path().data(), "w");
if (PRIVATE->fd != 0) fclose(PRIVATE->fd);
PRIVATE->fd = 0;
opened_ = false;
open();
}

View File

@@ -297,7 +297,7 @@ protected:
private:
PIString strType(const PIIODevice::DeviceMode type);
FILE * fd;
PRIVATE_DECLARATION
int ret, prec_, fdi;
PIString prec_str;