29 #ifndef CDPL_UTIL_STREAMDATAREADER_HPP
30 #define CDPL_UTIL_STREAMDATAREADER_HPP
71 template <
typename DataType,
typename ReaderImpl>
88 operator const void*()
const;
97 input(is), recordIndex(0), initStreamPos(is.tellg()), state(is.good()), streamScanned(false) {}
102 void scanDataStream();
104 typedef std::vector<std::istream::pos_type> RecordStreamPosTable;
107 std::size_t recordIndex;
108 std::istream::pos_type initStreamPos;
111 RecordStreamPosTable recordPositions;
119 template <
typename DataType,
typename ReaderImpl>
125 if ((state =
static_cast<ReaderImpl*
>(
this)->readData(input, obj, overwrite))) {
127 this->invokeIOCallbacks(1.0);
133 template <
typename DataType,
typename ReaderImpl>
139 return read(obj, overwrite);
142 template <
typename DataType,
typename ReaderImpl>
148 if ((state =
static_cast<ReaderImpl*
>(
this)->skipData(input))) {
150 this->invokeIOCallbacks(1.0);
156 template <
typename DataType,
typename ReaderImpl>
159 return static_cast<ReaderImpl*
>(
this)->moreData(input);
162 template <
typename DataType,
typename ReaderImpl>
168 template <
typename DataType,
typename ReaderImpl>
173 if (idx > recordPositions.size())
178 if (idx == recordPositions.size())
179 input.seekg(0, std::ios_base::end);
181 input.seekg(recordPositions[idx]);
186 template <
typename DataType,
typename ReaderImpl>
191 return recordPositions.size();
194 template <
typename DataType,
typename ReaderImpl>
197 return (state ?
this : 0);
200 template <
typename DataType,
typename ReaderImpl>
206 template <
typename DataType,
typename ReaderImpl>
212 streamScanned =
true;
214 std::size_t saved_rec_index = recordIndex;
219 input.seekg(0, std::ios_base::end);
221 std::istream::pos_type end_pos = input.tellg();
223 input.seekg(initStreamPos);
225 while (hasMoreData()) {
226 std::istream::pos_type record_pos = input.tellg();
229 if (!(state =
static_cast<ReaderImpl*
>(
this)->skipData(input)))
232 recordPositions.push_back(record_pos);
235 this->invokeIOCallbacks(record_pos /
double(end_pos));
238 this->invokeIOCallbacks(1.0);
240 if (saved_rec_index < recordPositions.size()) {
241 recordIndex = saved_rec_index;
244 input.seekg(recordPositions[recordIndex]);
Definition of exception classes.
Definition of the class CDPL::Base::DataReader.
An interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
Pharmacophore DataType
The type of the read data objects.
Definition: DataReader.hpp:79
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
A helper class that implements Base::DataReader for std::istream based data readers.
Definition: StreamDataReader.hpp:73
bool operator!() const
Tells whether the reader is in a bad state.
Definition: StreamDataReader.hpp:201
std::size_t getNumRecords()
Returns the total number of available data records.
Definition: StreamDataReader.hpp:187
bool hasMoreData()
Tells if there are any data records left to read.
Definition: StreamDataReader.hpp:157
Base::DataReader< DataType > & skip()
Skips the data record at the current record index.
Definition: StreamDataReader.hpp:144
std::size_t getRecordIndex() const
Returns the index of the current data record.
Definition: StreamDataReader.hpp:163
void setRecordIndex(std::size_t idx)
Sets the index of the current data record to idx.
Definition: StreamDataReader.hpp:169
Base::DataReader< DataType > & read(DataType &obj, bool overwrite=true)
Reads the data record at the current record index and stores the read data in obj.
Definition: StreamDataReader.hpp:121
Base::DataReader< DataType > & read(std::size_t idx, DataType &obj, bool overwrite=true)
Reads the data record at index idx and stores the read data in obj.
Definition: StreamDataReader.hpp:135
StreamDataReader(std::istream &is)
Constructs a StreamDataReader instance that will read from the input stream is.
Definition: StreamDataReader.hpp:96
The namespace of the Chemical Data Processing Library.