From: timoore Date: Sat, 19 Jul 2008 16:01:20 +0000 (+0000) Subject: Add repeat option to SGFile. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=942fa53ed926e6e37e75163c575ce2c6f541a26b;p=simgear.git Add repeat option to SGFile. This supports auto-looping over a playback file, for demos and such. --- diff --git a/simgear/io/sg_file.cxx b/simgear/io/sg_file.cxx index 1f978b1d..60d553cd 100644 --- a/simgear/io/sg_file.cxx +++ b/simgear/io/sg_file.cxx @@ -39,10 +39,10 @@ SG_USING_STD(string); -SGFile::SGFile( const string &file) { +SGFile::SGFile(const string &file, bool repeat_) + : file_name(file), fp(-1), eof_flag(true), repeat(repeat_) +{ set_type( sgFileType ); - file_name = file; - eof_flag = true; } @@ -84,7 +84,19 @@ 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) { + // 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 +110,12 @@ 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 && pos != 0) { + pos = ::lseek(fp, 0, SEEK_SET); + result = ::read(fp, buf, length); + } else { + eof_flag = true; + } } // find the end of line and reset position diff --git a/simgear/io/sg_file.hxx b/simgear/io/sg_file.hxx index 9f91af34..2eb87d42 100644 --- a/simgear/io/sg_file.hxx +++ b/simgear/io/sg_file.hxx @@ -55,6 +55,7 @@ class SGFile : public SGIOChannel { string file_name; int fp; bool eof_flag; + bool repeat; public: @@ -64,8 +65,9 @@ public: * name. This file is not opened immediately, but instead will be * opened when the open() method is called. * @param file name of file to open + * @param repeat On eof restart at the beginning of the file */ - SGFile( const string& file ); + SGFile( const string& file, bool repeat_ = false ); /** Destructor */ ~SGFile();