Chemical Data Processing Library C++ API - Version 1.1.1
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  StreamDataReader& operator=(const StreamDataReader& reader);
103 
104  void scanDataStream();
105 
106  typedef std::vector<std::istream::pos_type> RecordStreamPosTable;
107 
108  std::istream& input;
109  std::size_t recordIndex;
110  std::istream::pos_type initStreamPos;
111  bool state;
112  bool streamScanned;
113  RecordStreamPosTable recordPositions;
114  };
115  } // namespace Util
116 } // namespace CDPL
117 
118 
119 // Implementation
120 
121 template <typename DataType, typename ReaderImpl>
124 {
125  state = false;
126 
127  if ((state = static_cast<ReaderImpl*>(this)->readData(input, obj, overwrite))) {
128  recordIndex++;
129  this->invokeIOCallbacks(1.0);
130  }
131 
132  return *this;
133 }
134 
135 template <typename DataType, typename ReaderImpl>
137 CDPL::Util::StreamDataReader<DataType, ReaderImpl>::read(std::size_t idx, DataType& obj, bool overwrite)
138 {
139  setRecordIndex(idx);
140 
141  return read(obj, overwrite);
142 }
143 
144 template <typename DataType, typename ReaderImpl>
147 {
148  state = false;
149 
150  if ((state = static_cast<ReaderImpl*>(this)->skipData(input))) {
151  recordIndex++;
152  this->invokeIOCallbacks(1.0);
153  }
154 
155  return *this;
156 }
157 
158 template <typename DataType, typename ReaderImpl>
160 {
161  return static_cast<ReaderImpl*>(this)->moreData(input);
162 }
163 
164 template <typename DataType, typename ReaderImpl>
166 {
167  return recordIndex;
168 }
169 
170 template <typename DataType, typename ReaderImpl>
172 {
173  scanDataStream();
174 
175  if (idx > recordPositions.size())
176  throw Base::IndexError("StreamDataReader: record index out of bounds");
177 
178  input.clear();
179 
180  if (idx == recordPositions.size())
181  input.seekg(0, std::ios_base::end);
182  else
183  input.seekg(recordPositions[idx]);
184 
185  recordIndex = idx;
186 }
187 
188 template <typename DataType, typename ReaderImpl>
190 {
191  scanDataStream();
192 
193  return recordPositions.size();
194 }
195 
196 template <typename DataType, typename ReaderImpl>
198 {
199  return (state ? this : 0);
200 }
201 
202 template <typename DataType, typename ReaderImpl>
204 {
205  return !state;
206 }
207 
208 template <typename DataType, typename ReaderImpl>
210 {
211  if (streamScanned)
212  return;
213 
214  streamScanned = true;
215 
216  std::size_t saved_rec_index = recordIndex;
217 
218  recordIndex = 0;
219 
220  input.clear();
221  input.seekg(0, std::ios_base::end);
222 
223  std::istream::pos_type end_pos = input.tellg();
224 
225  input.seekg(initStreamPos);
226 
227  while (hasMoreData()) {
228  std::istream::pos_type record_pos = input.tellg();
229  state = false;
230 
231  if (!(state = static_cast<ReaderImpl*>(this)->skipData(input)))
232  break;
233 
234  recordPositions.push_back(record_pos);
235  recordIndex++;
236 
237  this->invokeIOCallbacks(record_pos / double(end_pos));
238  }
239 
240  this->invokeIOCallbacks(1.0);
241 
242  if (saved_rec_index < recordPositions.size()) {
243  recordIndex = saved_rec_index;
244 
245  input.clear();
246  input.seekg(recordPositions[recordIndex]);
247  }
248 }
249 
250 #endif // CDPL_UTIL_STREAMDATAREADER_HPP
CDPL::Util::StreamDataReader::getNumRecords
std::size_t getNumRecords()
Returns the total number of available data records.
Definition: StreamDataReader.hpp:189
CDPL::Util::StreamDataReader::operator!
bool operator!() const
Definition: StreamDataReader.hpp:203
CDPL::Util::StreamDataReader::read
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:123
CDPL::Base::DataReader< Reaction >::DataType
Reaction DataType
The type of the read data objects.
Definition: DataReader.hpp:79
CDPL::Util::StreamDataReader::setRecordIndex
void setRecordIndex(std::size_t idx)
Sets the index of the current data record to idx.
Definition: StreamDataReader.hpp:171
DataReader.hpp
Definition of the class CDPL::Base::DataReader.
CDPL::Util::StreamDataReader::getRecordIndex
std::size_t getRecordIndex() const
Definition: StreamDataReader.hpp:165
CDPL::Base::IndexError
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
CDPL::Base::DataReader
An interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
Exceptions.hpp
Definition of exception classes.
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::StreamDataReader::skip
Base::DataReader< DataType > & skip()
Skips the data record at the current record index.
Definition: StreamDataReader.hpp:146
CDPL::Util::StreamDataReader::read
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:137
CDPL::Util::StreamDataReader::StreamDataReader
StreamDataReader(std::istream &is)
Constructs a StreamDataReader instance that will read from the input stream is.
Definition: StreamDataReader.hpp:96
CDPL::Util::StreamDataReader
A helper class that implements Base::DataReader for std::istream based data readers.
Definition: StreamDataReader.hpp:73
CDPL::Util::StreamDataReader::hasMoreData
bool hasMoreData()
Tells if there are any data records left to read.
Definition: StreamDataReader.hpp:159