Add repeat option to SGFile.
This supports auto-looping over a playback file, for demos and such.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user