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;
}
// 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;
}
// 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
string file_name;
int fp;
bool eof_flag;
+ bool repeat;
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();