Chemical Data Processing Library C++ API - Version 1.4.0
StreamDataReader.hpp
Go to the documentation of this file.
1 /*
2  * StreamDataReader.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_STREAMDATAREADER_HPP
30 #define CDPL_UTIL_STREAMDATAREADER_HPP
31 
32 #include <istream>
33 #include <vector>
34 
35 #include "CDPL/Base/DataReader.hpp"
36 #include "CDPL/Base/Exceptions.hpp"
37 
38 
39 namespace CDPL
40 {
41 
42  namespace Util
43  {
44 
71  template <typename DataType, typename ReaderImpl>
72  class StreamDataReader : public Base::DataReader<DataType>
73  {
74 
75  public:
82  Base::DataReader<DataType>& read(DataType& obj, bool overwrite = true);
83 
92  Base::DataReader<DataType>& read(std::size_t idx, DataType& obj, bool overwrite = true);
93 
99 
104  bool hasMoreData();
105 
110  std::size_t getRecordIndex() const;
111 
117  void setRecordIndex(std::size_t idx);
118 
123  std::size_t getNumRecords();
124 
129  operator const void*() const;
130 
135  bool operator!() const;
136 
137  protected:
142  StreamDataReader(std::istream& is):
143  input(is), recordIndex(0), initStreamPos(is.tellg()), state(is.good()), streamScanned(false) {}
144 
145  private:
146  StreamDataReader(const StreamDataReader& reader);
147 
148  void scanDataStream();
149 
150  typedef std::vector<std::istream::pos_type> RecordStreamPosTable;
151 
152  std::istream& input;
153  std::size_t recordIndex;
154  std::istream::pos_type initStreamPos;
155  bool state;
156  bool streamScanned;
157  RecordStreamPosTable recordPositions;
158  };
159  } // namespace Util
160 } // namespace CDPL
161 
162 
163 // Implementation
164 
165 template <typename DataType, typename ReaderImpl>
168 {
169  state = false;
170 
171  if ((state = static_cast<ReaderImpl*>(this)->readData(input, obj, overwrite))) {
172  recordIndex++;
173  this->invokeIOCallbacks(1.0);
174  }
175 
176  return *this;
177 }
178 
179 template <typename DataType, typename ReaderImpl>
181 CDPL::Util::StreamDataReader<DataType, ReaderImpl>::read(std::size_t idx, DataType& obj, bool overwrite)
182 {
183  state = false;
184 
185  scanDataStream();
186 
187  if (idx >= recordPositions.size())
188  throw Base::IndexError("StreamDataReader: record index out of bounds");
189 
190  input.clear();
191  input.seekg(recordPositions[idx]);
192 
193  recordIndex = idx;
194 
195  if ((state = static_cast<ReaderImpl*>(this)->readData(input, obj, overwrite))) {
196  recordIndex++;
197  this->invokeIOCallbacks(1.0);
198  }
199 
200  return *this;
201 }
202 
203 template <typename DataType, typename ReaderImpl>
206 {
207  state = false;
208 
209  if ((state = static_cast<ReaderImpl*>(this)->skipData(input))) {
210  recordIndex++;
211  this->invokeIOCallbacks(1.0);
212  }
213 
214  return *this;
215 }
216 
217 template <typename DataType, typename ReaderImpl>
219 {
220  return static_cast<ReaderImpl*>(this)->moreData(input);
221 }
222 
223 template <typename DataType, typename ReaderImpl>
225 {
226  return recordIndex;
227 }
228 
229 template <typename DataType, typename ReaderImpl>
231 {
232  scanDataStream();
233 
234  if (idx > recordPositions.size())
235  throw Base::IndexError("StreamDataReader: record index out of bounds");
236 
237  input.clear();
238 
239  if (idx == recordPositions.size())
240  input.seekg(0, std::ios_base::end);
241  else
242  input.seekg(recordPositions[idx]);
243 
244  recordIndex = idx;
245 }
246 
247 template <typename DataType, typename ReaderImpl>
249 {
250  scanDataStream();
251 
252  return recordPositions.size();
253 }
254 
255 template <typename DataType, typename ReaderImpl>
257 {
258  return (state ? this : 0);
259 }
260 
261 template <typename DataType, typename ReaderImpl>
263 {
264  return !state;
265 }
266 
267 template <typename DataType, typename ReaderImpl>
269 {
270  if (streamScanned)
271  return;
272 
273  streamScanned = true;
274 
275  std::size_t saved_rec_index = recordIndex;
276 
277  recordIndex = 0;
278 
279  input.clear();
280  input.seekg(0, std::ios_base::end);
281 
282  std::istream::pos_type end_pos = input.tellg();
283 
284  input.seekg(initStreamPos);
285 
286  while (hasMoreData()) {
287  std::istream::pos_type record_pos = input.tellg();
288  state = false;
289 
290  if (!(state = static_cast<ReaderImpl*>(this)->skipData(input)))
291  break;
292 
293  recordPositions.push_back(record_pos);
294  recordIndex++;
295 
296  this->invokeIOCallbacks(record_pos / double(end_pos));
297  }
298 
299  this->invokeIOCallbacks(1.0);
300 
301  if (saved_rec_index < recordPositions.size()) {
302  recordIndex = saved_rec_index;
303 
304  input.clear();
305  input.seekg(recordPositions[recordIndex]);
306  }
307 }
308 
309 #endif // CDPL_UTIL_STREAMDATAREADER_HPP
Definition of exception classes.
Definition of class CDPL::Base::DataReader.
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
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 (non-readable) state.
Definition: StreamDataReader.hpp:262
std::size_t getNumRecords()
Returns the total number of records (scanning the stream on first call).
Definition: StreamDataReader.hpp:248
bool hasMoreData()
Tells whether more records are available by delegating to ReaderImpl::moreData().
Definition: StreamDataReader.hpp:218
Base::DataReader< DataType > & skip()
Skips the next data record by delegating to ReaderImpl::skipData().
Definition: StreamDataReader.hpp:205
std::size_t getRecordIndex() const
Returns the current record index.
Definition: StreamDataReader.hpp:224
void setRecordIndex(std::size_t idx)
Sets the current record index.
Definition: StreamDataReader.hpp:230
Base::DataReader< DataType > & read(DataType &obj, bool overwrite=true)
Reads the next data record into obj by delegating to ReaderImpl::readData().
Definition: StreamDataReader.hpp:167
Base::DataReader< DataType > & read(std::size_t idx, DataType &obj, bool overwrite=true)
Reads the data record at index idx into obj by delegating to ReaderImpl::readData().
Definition: StreamDataReader.hpp:181
StreamDataReader(std::istream &is)
Constructs a StreamDataReader instance that will read from the input stream is.
Definition: StreamDataReader.hpp:142
The namespace of the Chemical Data Processing Library.