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

@@ -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();
}