PIFile::skip() method and PIFile::isEnd() fix for read-only

This commit is contained in:
2021-05-14 00:36:12 +03:00
parent 05b18c3ab7
commit 1ec9481c19
4 changed files with 34 additions and 13 deletions

View File

@@ -136,16 +136,12 @@ PIString PIFile::FileInfo::dir() const {
PIFile::PIFile(): PIIODevice() {
PRIVATE->fd = 0;
fdi = -1;
setPrecision(5);
_init();
}
PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
PRIVATE->fd = 0;
fdi = -1;
setPrecision(5);
_init();
if (!path.isEmpty())
open();
}
@@ -180,6 +176,10 @@ bool PIFile::openDevice() {
#ifndef WINDOWS
fcntl(fdi, F_SETFL, O_NONBLOCK);
#endif
if (mode_ == PIIODevice::ReadOnly) {
_fseek_call_(PRIVATE->fd, 0, SEEK_END);
_size = _ftell_call_(PRIVATE->fd);
}
_fseek_call_(PRIVATE->fd, 0, SEEK_SET);
clearerr(PRIVATE->fd);
}
@@ -194,6 +194,7 @@ bool PIFile::closeDevice() {
bool cs = (fclose(PRIVATE->fd) == 0);
if (cs) PRIVATE->fd = 0;
fdi = -1;
_size = -1;
//piCout << "closed file" << PRIVATE->fd << opened_;
return cs;
}
@@ -337,6 +338,14 @@ PIString PIFile::strType(const PIIODevice::DeviceMode type) {
}
void PIFile::_init() {
PRIVATE->fd = 0;
fdi = -1;
_size = -1;
setPrecision(5);
}
void PIFile::flush() {
if (isOpened()) fflush(PRIVATE->fd);
}
@@ -372,6 +381,12 @@ void PIFile::seekToLine(llong line) {
}
void PIFile::skip(llong bytes) {
if (isClosed() || (bytes == 0)) return;
_fseek_call_(PRIVATE->fd, bytes, SEEK_CUR);
}
char PIFile::readChar() {
return (char)fgetc(PRIVATE->fd);
}
@@ -391,7 +406,12 @@ llong PIFile::pos() const {
bool PIFile::isEnd() const {
if (isClosed()) return true;
return (feof(PRIVATE->fd) || ferror(PRIVATE->fd));
bool ret = (feof(PRIVATE->fd) || ferror(PRIVATE->fd));
if (!ret && (_size >= 0)) {
if (pos() > _size)
ret = true;
}
return ret;
}

View File

@@ -107,6 +107,9 @@ public:
//! Move read/write position to text line number "line"
void seekToLine(llong line);
//! Skip "bytes" bytes (move position next to "bytes" bytes)
void skip(llong bytes);
//! Read one char and return it
char readChar();
@@ -295,9 +298,11 @@ protected:
private:
PIString strType(const PIIODevice::DeviceMode type);
void _init();
PRIVATE_DECLARATION(PIP_EXPORT)
int ret, prec_, fdi;
llong _size;
PIString prec_str;
};