Chemical Data Processing Library C++ API - Version 1.4.0
FileDataReader.hpp
Go to the documentation of this file.
1 /*
2  * FileDataReader.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_FILEDATAREADER_HPP
30 #define CDPL_UTIL_FILEDATAREADER_HPP
31 
32 #include <fstream>
33 #include <string>
34 #include <functional>
35 
36 #include "CDPL/Base/DataReader.hpp"
37 #include "CDPL/Base/Exceptions.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Util
44  {
45 
53  template <typename ReaderImpl, typename DataType = typename ReaderImpl::DataType>
54  class FileDataReader : public Base::DataReader<DataType>
55  {
56 
57  public:
65  FileDataReader(const std::string& file_name,
66  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary);
67 
75  FileDataReader& read(DataType& obj, bool overwrite = true);
76 
85  FileDataReader& read(std::size_t idx, DataType& obj, bool overwrite = true);
86 
93 
98  bool hasMoreData();
99 
104  std::size_t getRecordIndex() const;
105 
110  void setRecordIndex(std::size_t idx);
111 
116  std::size_t getNumRecords();
117 
122  operator const void*() const;
123 
128  bool operator!() const;
129 
133  void close();
134 
135  private:
136  std::ifstream stream;
137  std::string fileName;
138  ReaderImpl reader;
139  };
140  } // namespace Util
141 } // namespace CDPL
142 
143 
144 // Implementation
145 
146 template <typename ReaderImpl, typename DataType>
147 CDPL::Util::FileDataReader<ReaderImpl, DataType>::FileDataReader(const std::string& file_name, std::ios_base::openmode mode):
148  stream(file_name.c_str(), mode), fileName(file_name), reader(stream)
149 {
150  if (!stream.good())
151  throw Base::IOError("FileDataReader: could not open file");
152 
153  reader.setParent(this);
154  reader.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
155 }
156 
157 template <typename ReaderImpl, typename DataType>
160 {
161  try {
162  reader.read(obj, overwrite);
163 
164  } catch (const std::exception& e) {
165  throw Base::IOError("FileDataReader: while reading file '" + fileName + "': " + e.what());
166  }
167 
168  return *this;
169 }
170 
171 template <typename ReaderImpl, typename DataType>
173 CDPL::Util::FileDataReader<ReaderImpl, DataType>::read(std::size_t idx, DataType& obj, bool overwrite)
174 {
175  try {
176  reader.read(idx, obj, overwrite);
177 
178  } catch (const std::exception& e) {
179  throw Base::IOError("FileDataReader: while reading file '" + fileName + "': " + e.what());
180  }
181 
182  return *this;
183 }
184 
185 template <typename ReaderImpl, typename DataType>
188 {
189  try {
190  reader.skip();
191 
192  } catch (const std::exception& e) {
193  throw Base::IOError("FileDataReader: while reading file '" + fileName + "': " + e.what());
194  }
195 
196  return *this;
197 }
198 
199 template <typename ReaderImpl, typename DataType>
201 {
202  return reader.hasMoreData();
203 }
204 
205 template <typename ReaderImpl, typename DataType>
207 {
208  return reader.getRecordIndex();
209 }
210 
211 template <typename ReaderImpl, typename DataType>
213 {
214  reader.setRecordIndex(idx);
215 }
216 
217 template <typename ReaderImpl, typename DataType>
219 {
220  return reader.getNumRecords();
221 }
222 
223 template <typename ReaderImpl, typename DataType>
225 {
226  return reader.operator const void*();
227 }
228 
229 template <typename ReaderImpl, typename DataType>
231 {
232  return reader.operator!();
233 }
234 
235 template <typename ReaderImpl, typename DataType>
237 {
238  reader.close();
239  stream.close();
240 }
241 
242 #endif // CDPL_UTIL_FILEDATAREADER_HPP
Definition of exception classes.
Definition of class CDPL::Base::DataReader.
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
Interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
typename ReaderImpl::DataType DataType
The type of the read data objects.
Definition: DataReader.hpp:79
Thrown to indicate that an I/O operation has failed because of physical (e.g. broken pipe) or logical...
Definition: Base/Exceptions.hpp:250
Convenience wrapper that adapts a stream-based reader implementation ReaderImpl into a file-based Bas...
Definition: FileDataReader.hpp:55
void close()
Closes the wrapped reader and the underlying file stream.
Definition: FileDataReader.hpp:236
bool hasMoreData()
Tells whether the wrapped reader has more records to read.
Definition: FileDataReader.hpp:200
FileDataReader(const std::string &file_name, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Constructs a FileDataReader instance that opens the file file_name in the given mode and forwards all...
Definition: FileDataReader.hpp:147
FileDataReader & read(DataType &obj, bool overwrite=true)
Reads the next record into obj via the wrapped reader.
Definition: FileDataReader.hpp:159
std::size_t getNumRecords()
Returns the total number of records as reported by the wrapped reader.
Definition: FileDataReader.hpp:218
void setRecordIndex(std::size_t idx)
Sets the current record index of the wrapped reader.
Definition: FileDataReader.hpp:212
bool operator!() const
Tells whether the reader is in a bad (non-readable) state.
Definition: FileDataReader.hpp:230
std::size_t getRecordIndex() const
Returns the current record index of the wrapped reader.
Definition: FileDataReader.hpp:206
FileDataReader & skip()
Skips the next record via the wrapped reader.
Definition: FileDataReader.hpp:187
The namespace of the Chemical Data Processing Library.