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

@@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0017 NEW) # need include() with .cmake cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(pip) project(pip)
set(pip_MAJOR 2) set(pip_MAJOR 2)
set(pip_MINOR 24) set(pip_MINOR 25)
set(pip_REVISION 1) set(pip_REVISION 0)
set(pip_SUFFIX ) set(pip_SUFFIX )
set(pip_COMPANY SHS) set(pip_COMPANY SHS)
set(pip_DOMAIN org.SHS) set(pip_DOMAIN org.SHS)

View File

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

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

View File

@@ -358,10 +358,6 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
.removeAll(' ') .removeAll(' ')
.toUpperCase() .toUpperCase()
+ "_H"; + "_H";
bool inc_h, inc_cpp;
inc_h = streams || texts;
inc_cpp = !inc_h && getters;
PISet<PIString> inc_files; PISet<PIString> inc_files;
piForeachC (PICodeParser::Entity * e, parser.entities) piForeachC (PICodeParser::Entity * e, parser.entities)
if (e->name.find("::") < 0 && !e->name.startsWith("_PI")) if (e->name.find("::") < 0 && !e->name.startsWith("_PI"))