X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=simgear%2Fio%2Fsg_file.cxx;h=0cf66f77d6f0139dc654e914ff89c3d403f3437d;hb=c8aa989ac776395a20229700e6b801ee510e4a7a;hp=1f978b1df79722996202a809e5281fbbed38b55b;hpb=9861d742217e91706ac7b187bafa7b55fe694bd4;p=simgear.git diff --git a/simgear/io/sg_file.cxx b/simgear/io/sg_file.cxx index 1f978b1d..0cf66f77 100644 --- a/simgear/io/sg_file.cxx +++ b/simgear/io/sg_file.cxx @@ -23,28 +23,42 @@ #include -#include STL_STRING +#include -#if defined(_MSC_VER) || defined(__MINGW32__) +#ifdef _WIN32 # include #endif #include +#include +#include +#include + +#if !defined(_MSC_VER) +# include +#endif + #include #include #include "sg_file.hxx" -SG_USING_STD(string); - -SGFile::SGFile( const string &file) { +SGFile::SGFile(const std::string &file, int repeat_) + : file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0) +{ set_type( sgFileType ); - file_name = file; - eof_flag = true; } +SGFile::SGFile( int existingFd ) : + fp(existingFd), + eof_flag(false), + repeat(1), + iteration(0) +{ + set_type( sgFileType ); +} SGFile::~SGFile() { } @@ -55,7 +69,7 @@ bool SGFile::open( const SGProtocolDir d ) { set_dir( d ); if ( get_dir() == SG_IO_OUT ) { -#if defined(_MSC_VER) || defined(__MINGW32__) +#ifdef _WIN32 int mode = 00666; #else mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; @@ -84,7 +98,20 @@ int SGFile::read( char *buf, int length ) { // read a chunk ssize_t result = ::read( fp, buf, length ); if ( length > 0 && result == 0 ) { - eof_flag = true; + if (repeat < 0 || iteration < repeat - 1) { + iteration++; + // loop reading the file, unless it is empty + off_t fileLen = ::lseek(fp, 0, SEEK_CUR); + if (fileLen == 0) { + eof_flag = true; + return 0; + } else { + ::lseek(fp, 0, SEEK_SET); + return ::read(fp, buf, length); + } + } else { + eof_flag = true; + } } return result; } @@ -98,7 +125,13 @@ int SGFile::readline( char *buf, int length ) { // read a chunk ssize_t result = ::read( fp, buf, length ); if ( length > 0 && result == 0 ) { - eof_flag = true; + if ((repeat < 0 || iteration < repeat - 1) && pos != 0) { + iteration++; + pos = ::lseek(fp, 0, SEEK_SET); + result = ::read(fp, buf, length); + } else { + eof_flag = true; + } } // find the end of line and reset position