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>
141 if (idx >= recordPositions.size())
145 input.seekg(recordPositions[idx]);
149 if ((state =
static_cast<ReaderImpl*
>(
this)->readData(input, obj, overwrite))) {
151 this->invokeIOCallbacks(1.0);
157 template <
typename DataType,
typename ReaderImpl>
163 if ((state =
static_cast<ReaderImpl*
>(
this)->skipData(input))) {
165 this->invokeIOCallbacks(1.0);
171 template <
typename DataType,
typename ReaderImpl>
174 return static_cast<ReaderImpl*
>(
this)->moreData(input);
177 template <
typename DataType,
typename ReaderImpl>
183 template <
typename DataType,
typename ReaderImpl>
188 if (idx > recordPositions.size())
193 if (idx == recordPositions.size())
194 input.seekg(0, std::ios_base::end);
196 input.seekg(recordPositions[idx]);
201 template <
typename DataType,
typename ReaderImpl>
206 return recordPositions.size();
209 template <
typename DataType,
typename ReaderImpl>
212 return (state ?
this : 0);
215 template <
typename DataType,
typename ReaderImpl>
221 template <
typename DataType,
typename ReaderImpl>
227 streamScanned =
true;
229 std::size_t saved_rec_index = recordIndex;
234 input.seekg(0, std::ios_base::end);
236 std::istream::pos_type end_pos = input.tellg();
238 input.seekg(initStreamPos);
240 while (hasMoreData()) {
241 std::istream::pos_type record_pos = input.tellg();
244 if (!(state =
static_cast<ReaderImpl*
>(
this)->skipData(input)))
247 recordPositions.push_back(record_pos);
250 this->invokeIOCallbacks(record_pos /
double(end_pos));
253 this->invokeIOCallbacks(1.0);
255 if (saved_rec_index < recordPositions.size()) {
256 recordIndex = saved_rec_index;
259 input.seekg(recordPositions[recordIndex]);
Definition of exception classes.
Definition of 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:216
std::size_t getNumRecords()
Returns the total number of available data records.
Definition: StreamDataReader.hpp:202
bool hasMoreData()
Tells if there are any data records left to read.
Definition: StreamDataReader.hpp:172
Base::DataReader< DataType > & skip()
Skips the data record at the current record index.
Definition: StreamDataReader.hpp:159
std::size_t getRecordIndex() const
Returns the index of the current data record.
Definition: StreamDataReader.hpp:178
void setRecordIndex(std::size_t idx)
Sets the index of the current data record to idx.
Definition: StreamDataReader.hpp:184
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.