Chemical Data Processing Library C++ API - Version 1.2.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  setRecordIndex(idx);
138 
139  return read(obj, overwrite);
140 }
141 
142 template <typename DataType, typename ReaderImpl>
145 {
146  state = false;
147 
148  if ((state = static_cast<ReaderImpl*>(this)->skipData(input))) {
149  recordIndex++;
150  this->invokeIOCallbacks(1.0);
151  }
152 
153  return *this;
154 }
155 
156 template <typename DataType, typename ReaderImpl>
158 {
159  return static_cast<ReaderImpl*>(this)->moreData(input);
160 }
161 
162 template <typename DataType, typename ReaderImpl>
164 {
165  return recordIndex;
166 }
167 
168 template <typename DataType, typename ReaderImpl>
170 {
171  scanDataStream();
172 
173  if (idx > recordPositions.size())
174  throw Base::IndexError("StreamDataReader: record index out of bounds");
175 
176  input.clear();
177 
178  if (idx == recordPositions.size())
179  input.seekg(0, std::ios_base::end);
180  else
181  input.seekg(recordPositions[idx]);
182 
183  recordIndex = idx;
184 }
185 
186 template <typename DataType, typename ReaderImpl>
188 {
189  scanDataStream();
190 
191  return recordPositions.size();
192 }
193 
194 template <typename DataType, typename ReaderImpl>
196 {
197  return (state ? this : 0);
198 }
199 
200 template <typename DataType, typename ReaderImpl>
202 {
203  return !state;
204 }
205 
206 template <typename DataType, typename ReaderImpl>
208 {
209  if (streamScanned)
210  return;
211 
212  streamScanned = true;
213 
214  std::size_t saved_rec_index = recordIndex;
215 
216  recordIndex = 0;
217 
218  input.clear();
219  input.seekg(0, std::ios_base::end);
220 
221  std::istream::pos_type end_pos = input.tellg();
222 
223  input.seekg(initStreamPos);
224 
225  while (hasMoreData()) {
226  std::istream::pos_type record_pos = input.tellg();
227  state = false;
228 
229  if (!(state = static_cast<ReaderImpl*>(this)->skipData(input)))
230  break;
231 
232  recordPositions.push_back(record_pos);
233  recordIndex++;
234 
235  this->invokeIOCallbacks(record_pos / double(end_pos));
236  }
237 
238  this->invokeIOCallbacks(1.0);
239 
240  if (saved_rec_index < recordPositions.size()) {
241  recordIndex = saved_rec_index;
242 
243  input.clear();
244  input.seekg(recordPositions[recordIndex]);
245  }
246 }
247 
248 #endif // CDPL_UTIL_STREAMDATAREADER_HPP
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.