Chemical Data Processing Library C++ API - Version 1.3.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:
76  Base::DataReader<DataType>& read(DataType& obj, bool overwrite = true);
77  Base::DataReader<DataType>& read(std::size_t idx, DataType& obj, bool overwrite = true);
78 
80 
81  bool hasMoreData();
82 
83  std::size_t getRecordIndex() const;
84  void setRecordIndex(std::size_t idx);
85 
86  std::size_t getNumRecords();
87 
88  operator const void*() const;
89  bool operator!() const;
90 
91  protected:
96  StreamDataReader(std::istream& is):
97  input(is), recordIndex(0), initStreamPos(is.tellg()), state(is.good()), streamScanned(false) {}
98 
99  private:
100  StreamDataReader(const StreamDataReader& reader);
101 
102  void scanDataStream();
103 
104  typedef std::vector<std::istream::pos_type> RecordStreamPosTable;
105 
106  std::istream& input;
107  std::size_t recordIndex;
108  std::istream::pos_type initStreamPos;
109  bool state;
110  bool streamScanned;
111  RecordStreamPosTable recordPositions;
112  };
113  } // namespace Util
114 } // namespace CDPL
115 
116 
117 // Implementation
118 
119 template <typename DataType, typename ReaderImpl>
122 {
123  state = false;
124 
125  if ((state = static_cast<ReaderImpl*>(this)->readData(input, obj, overwrite))) {
126  recordIndex++;
127  this->invokeIOCallbacks(1.0);
128  }
129 
130  return *this;
131 }
132 
133 template <typename DataType, typename ReaderImpl>
135 CDPL::Util::StreamDataReader<DataType, ReaderImpl>::read(std::size_t idx, DataType& obj, bool overwrite)
136 {
137  state = false;
138 
139  scanDataStream();
140 
141  if (idx >= recordPositions.size())
142  throw Base::IndexError("StreamDataReader: record index out of bounds");
143 
144  input.clear();
145  input.seekg(recordPositions[idx]);
146 
147  recordIndex = idx;
148 
149  if ((state = static_cast<ReaderImpl*>(this)->readData(input, obj, overwrite))) {
150  recordIndex++;
151  this->invokeIOCallbacks(1.0);
152  }
153 
154  return *this;
155 }
156 
157 template <typename DataType, typename ReaderImpl>
160 {
161  state = false;
162 
163  if ((state = static_cast<ReaderImpl*>(this)->skipData(input))) {
164  recordIndex++;
165  this->invokeIOCallbacks(1.0);
166  }
167 
168  return *this;
169 }
170 
171 template <typename DataType, typename ReaderImpl>
173 {
174  return static_cast<ReaderImpl*>(this)->moreData(input);
175 }
176 
177 template <typename DataType, typename ReaderImpl>
179 {
180  return recordIndex;
181 }
182 
183 template <typename DataType, typename ReaderImpl>
185 {
186  scanDataStream();
187 
188  if (idx > recordPositions.size())
189  throw Base::IndexError("StreamDataReader: record index out of bounds");
190 
191  input.clear();
192 
193  if (idx == recordPositions.size())
194  input.seekg(0, std::ios_base::end);
195  else
196  input.seekg(recordPositions[idx]);
197 
198  recordIndex = idx;
199 }
200 
201 template <typename DataType, typename ReaderImpl>
203 {
204  scanDataStream();
205 
206  return recordPositions.size();
207 }
208 
209 template <typename DataType, typename ReaderImpl>
211 {
212  return (state ? this : 0);
213 }
214 
215 template <typename DataType, typename ReaderImpl>
217 {
218  return !state;
219 }
220 
221 template <typename DataType, typename ReaderImpl>
223 {
224  if (streamScanned)
225  return;
226 
227  streamScanned = true;
228 
229  std::size_t saved_rec_index = recordIndex;
230 
231  recordIndex = 0;
232 
233  input.clear();
234  input.seekg(0, std::ios_base::end);
235 
236  std::istream::pos_type end_pos = input.tellg();
237 
238  input.seekg(initStreamPos);
239 
240  while (hasMoreData()) {
241  std::istream::pos_type record_pos = input.tellg();
242  state = false;
243 
244  if (!(state = static_cast<ReaderImpl*>(this)->skipData(input)))
245  break;
246 
247  recordPositions.push_back(record_pos);
248  recordIndex++;
249 
250  this->invokeIOCallbacks(record_pos / double(end_pos));
251  }
252 
253  this->invokeIOCallbacks(1.0);
254 
255  if (saved_rec_index < recordPositions.size()) {
256  recordIndex = saved_rec_index;
257 
258  input.clear();
259  input.seekg(recordPositions[recordIndex]);
260  }
261 }
262 
263 #endif // CDPL_UTIL_STREAMDATAREADER_HPP
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.