Chemical Data Processing Library C++ API - Version 1.1.1
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 
49  template <typename ReaderImpl, typename DataType = typename ReaderImpl::DataType>
50  class FileDataReader : public Base::DataReader<DataType>
51  {
52 
53  public:
54  FileDataReader(const std::string& file_name,
55  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary);
56 
57  FileDataReader& read(DataType& obj, bool overwrite = true);
58 
59  FileDataReader& read(std::size_t idx, DataType& obj, bool overwrite = true);
60 
62 
63  bool hasMoreData();
64 
65  std::size_t getRecordIndex() const;
66 
67  void setRecordIndex(std::size_t idx);
68 
69  std::size_t getNumRecords();
70 
71  operator const void*() const;
72  bool operator!() const;
73 
74  void close();
75 
76  private:
77  std::ifstream stream;
78  std::string fileName;
79  ReaderImpl reader;
80  };
81  } // namespace Util
82 } // namespace CDPL
83 
84 
85 // Implementation
86 
87 template <typename ReaderImpl, typename DataType>
88 CDPL::Util::FileDataReader<ReaderImpl, DataType>::FileDataReader(const std::string& file_name, std::ios_base::openmode mode):
89  stream(file_name.c_str(), mode), fileName(file_name), reader(stream)
90 {
91  reader.setParent(this);
92  reader.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
93 }
94 
95 template <typename ReaderImpl, typename DataType>
98 {
99  try {
100  reader.read(obj, overwrite);
101 
102  } catch (const std::exception& e) {
103  throw Base::IOError("FileDataReader: while reading file '" + fileName + "': " + e.what());
104  }
105 
106  return *this;
107 }
108 
109 template <typename ReaderImpl, typename DataType>
111 CDPL::Util::FileDataReader<ReaderImpl, DataType>::read(std::size_t idx, DataType& obj, bool overwrite)
112 {
113  try {
114  reader.read(idx, obj, overwrite);
115 
116  } catch (const std::exception& e) {
117  throw Base::IOError("FileDataReader: while reading file '" + fileName + "': " + e.what());
118  }
119 
120  return *this;
121 }
122 
123 template <typename ReaderImpl, typename DataType>
126 {
127  try {
128  reader.skip();
129 
130  } catch (const std::exception& e) {
131  throw Base::IOError("FileDataReader: while reading file '" + fileName + "': " + e.what());
132  }
133 
134  return *this;
135 }
136 
137 template <typename ReaderImpl, typename DataType>
139 {
140  return reader.hasMoreData();
141 }
142 
143 template <typename ReaderImpl, typename DataType>
145 {
146  return reader.getRecordIndex();
147 }
148 
149 template <typename ReaderImpl, typename DataType>
151 {
152  reader.setRecordIndex(idx);
153 }
154 
155 template <typename ReaderImpl, typename DataType>
157 {
158  return reader.getNumRecords();
159 }
160 
161 template <typename ReaderImpl, typename DataType>
163 {
164  return reader.operator const void*();
165 }
166 
167 template <typename ReaderImpl, typename DataType>
169 {
170  return reader.operator!();
171 }
172 
173 template <typename ReaderImpl, typename DataType>
175 {
176  reader.close();
177  stream.close();
178 }
179 
180 #endif // CDPL_UTIL_FILEDATAREADER_HPP
CDPL::Util::FileDataReader::FileDataReader
FileDataReader(const std::string &file_name, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Definition: FileDataReader.hpp:88
CDPL::Util::FileDataReader::operator!
bool operator!() const
Definition: FileDataReader.hpp:168
CDPL::Util::FileDataReader::hasMoreData
bool hasMoreData()
Tells if there are any data records left to read.
Definition: FileDataReader.hpp:138
CDPL::Base::DataReader< typename ReaderImpl::DataType >::DataType
typename ReaderImpl::DataType DataType
The type of the read data objects.
Definition: DataReader.hpp:79
DataReader.hpp
Definition of the class CDPL::Base::DataReader.
CDPL::Base::IOError
Thrown to indicate that an I/O operation has failed because of physical (e.g. broken pipe) or logical...
Definition: Base/Exceptions.hpp:250
CDPL::Base::DataIOBase::invokeIOCallbacks
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
CDPL::Util::FileDataReader::skip
FileDataReader & skip()
Skips the data record at the current record index.
Definition: FileDataReader.hpp:125
CDPL::Base::DataReader
An interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
CDPL::Util::FileDataReader::setRecordIndex
void setRecordIndex(std::size_t idx)
Sets the index of the current data record to idx.
Definition: FileDataReader.hpp:150
CDPL::Util::FileDataReader::close
void close()
Performs a reader specific shutdown operation (if required).
Definition: FileDataReader.hpp:174
Exceptions.hpp
Definition of exception classes.
CDPL::Util::FileDataReader::getRecordIndex
std::size_t getRecordIndex() const
Definition: FileDataReader.hpp:144
CDPL::Util::FileDataReader
FileDataReader.
Definition: FileDataReader.hpp:51
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::FileDataReader::read
FileDataReader & read(DataType &obj, bool overwrite=true)
Definition: FileDataReader.hpp:97
CDPL::Util::FileDataReader::getNumRecords
std::size_t getNumRecords()
Returns the total number of available data records.
Definition: FileDataReader.hpp:156