Chemical Data Processing Library C++ API - Version 1.1.1
MultiFormatDataReader.hpp
Go to the documentation of this file.
1 /*
2  * MultiFormatDataReader.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_MULTIFORMATDATAREADER_HPP
30 #define CDPL_UTIL_MULTIFORMATDATAREADER_HPP
31 
32 #include <string>
33 #include <functional>
34 #include <memory>
35 
37 #include "CDPL/Base/Exceptions.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Util
44  {
45 
49  template <typename DataType>
50  class MultiFormatDataReader : public Base::DataReader<DataType>
51  {
52 
53  public:
54  typedef std::shared_ptr<MultiFormatDataReader> SharedPointer;
55 
56  MultiFormatDataReader(const std::string& file_name,
57  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary);
58 
59  MultiFormatDataReader(const std::string& file_name, const std::string& fmt,
60  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary);
61 
62  MultiFormatDataReader(const std::string& file_name, const Base::DataFormat& fmt,
63  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary);
64 
65  MultiFormatDataReader(std::istream& is, const std::string& fmt);
66 
67  MultiFormatDataReader(std::istream& is, const Base::DataFormat& fmt);
68 
70 
72 
73  const Base::DataFormat& getDataFormat() const;
74 
75  MultiFormatDataReader& read(DataType& obj, bool overwrite = true);
76 
77  MultiFormatDataReader& read(std::size_t idx, DataType& obj, bool overwrite = true);
78 
80 
81  bool hasMoreData();
82 
83  std::size_t getRecordIndex() const;
84 
85  void setRecordIndex(std::size_t idx);
86 
87  std::size_t getNumRecords();
88 
89  operator const void*() const;
90 
91  bool operator!() const;
92 
93  void close();
94 
95  private:
96  void init();
97 
98  typedef typename Base::DataReader<DataType>::SharedPointer ReaderPtr;
99 
100  ReaderPtr readerPtr;
101  Base::DataFormat dataFormat;
102  };
103  } // namespace Util
104 } // namespace CDPL
105 
106 
107 // Implementation
108 
109 template <typename DataType>
110 CDPL::Util::MultiFormatDataReader<DataType>::MultiFormatDataReader(const std::string& file_name, std::ios_base::openmode mode)
111 {
112  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
113  const auto& handler = Base::DataIOManager<DataType>::getInputHandlerByFileExtension(file_name.substr(++i));
114 
115  if (!handler)
116  continue;
117 
118  readerPtr = handler->createReader(file_name, mode);
119  dataFormat = handler->getDataFormat();
120  break;
121  }
122 
123  if (!readerPtr)
124  throw Base::IOError("MultiFormatDataReader: could not deduce data format of '" + file_name + "'");
125 
126  init();
127 }
128 
129 template <typename DataType>
130 CDPL::Util::MultiFormatDataReader<DataType>::MultiFormatDataReader(const std::string& file_name, const std::string& fmt,
131  std::ios_base::openmode mode)
132 {
134 
135  if (!handler)
136  throw Base::IOError("MultiFormatDataReader: could not find handler for format '" + fmt + "'");
137 
138  readerPtr = handler->createReader(file_name, mode);
139  dataFormat = handler->getDataFormat();
140 
141  init();
142 }
143 
144 template <typename DataType>
146  std::ios_base::openmode mode):
147  dataFormat(fmt)
148 {
150 
151  if (!handler)
152  throw Base::IOError("MultiFormatDataReader: could not find handler for format '" + fmt.getName() + "'");
153 
154  readerPtr = handler->createReader(file_name, mode);
155 
156  init();
157 }
158 
159 template <typename DataType>
161 {
163 
164  if (!handler)
165  throw Base::IOError("MultiFormatDataReader: could not find handler for format '" + fmt + "'");
166 
167  readerPtr = handler->createReader(is);
168  dataFormat = handler->getDataFormat();
169 
170  init();
171 }
172 
173 template <typename DataType>
175  dataFormat(fmt)
176 {
178 
179  if (!handler)
180  throw Base::IOError("MultiFormatDataReader: could not find handler for format '" + fmt.getName() + "'");
181 
182  readerPtr = handler->createReader(is);
183 
184  init();
185 }
186 
187 template <typename DataType>
189 {
190  return dataFormat;
191 }
192 
193 template <typename DataType>
196 {
197  readerPtr->read(obj, overwrite);
198  return *this;
199 }
200 
201 template <typename DataType>
203 CDPL::Util::MultiFormatDataReader<DataType>::read(std::size_t idx, DataType& obj, bool overwrite)
204 {
205  readerPtr->read(idx, obj, overwrite);
206  return *this;
207 }
208 
209 template <typename DataType>
212 {
213  readerPtr->skip();
214  return *this;
215 }
216 
217 template <typename DataType>
219 {
220  return readerPtr->hasMoreData();
221 }
222 
223 template <typename DataType>
225 {
226  return readerPtr->getRecordIndex();
227 }
228 
229 template <typename DataType>
231 {
232  readerPtr->setRecordIndex(idx);
233 }
234 
235 template <typename DataType>
237 {
238  return readerPtr->getNumRecords();
239 }
240 
241 template <typename DataType>
243 {
244  return readerPtr->operator const void*();
245 }
246 
247 template <typename DataType>
249 {
250  return readerPtr->operator!();
251 }
252 
253 template <typename DataType>
255 {
256  readerPtr->close();
257 }
258 
259 template <typename DataType>
261 {
262  readerPtr->setParent(this);
263  readerPtr->registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
264 }
265 
266 #endif // CDPL_UTIL_MULTIFORMATDATAREADER_HPP
CDPL::Base::DataFormat
Provides meta-information about a particular data storage format.
Definition: Base/DataFormat.hpp:49
CDPL::Base::DataIOManager::getInputHandlerByFileExtension
static InputHandlerPointer getInputHandlerByFileExtension(const std::string &file_ext)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:573
CDPL::Base::DataIOManager::getInputHandlerByFormat
static InputHandlerPointer getInputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the specified da...
Definition: DataIOManager.hpp:562
CDPL::Util::MultiFormatDataReader::read
MultiFormatDataReader & read(DataType &obj, bool overwrite=true)
Reads the data record at the current record index and stores the read data in obj.
Definition: MultiFormatDataReader.hpp:195
CDPL::Base::DataReader< DataType >::DataType
DataType DataType
The type of the read data objects.
Definition: DataReader.hpp:79
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::Util::MultiFormatDataReader::getRecordIndex
std::size_t getRecordIndex() const
Definition: MultiFormatDataReader.hpp:224
CDPL::Base::DataIOBase::invokeIOCallbacks
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
CDPL::Util::MultiFormatDataReader::getNumRecords
std::size_t getNumRecords()
Returns the total number of available data records.
Definition: MultiFormatDataReader.hpp:236
CDPL::Util::MultiFormatDataReader
MultiFormatDataReader.
Definition: MultiFormatDataReader.hpp:51
CDPL::Util::MultiFormatDataReader::SharedPointer
std::shared_ptr< MultiFormatDataReader > SharedPointer
Definition: MultiFormatDataReader.hpp:54
CDPL::Util::MultiFormatDataReader::close
void close()
Performs a reader specific shutdown operation (if required).
Definition: MultiFormatDataReader.hpp:254
CDPL::Util::MultiFormatDataReader::skip
MultiFormatDataReader & skip()
Skips the data record at the current record index.
Definition: MultiFormatDataReader.hpp:211
CDPL::Base::DataReader
An interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
CDPL::Util::MultiFormatDataReader::setRecordIndex
void setRecordIndex(std::size_t idx)
Sets the index of the current data record to idx.
Definition: MultiFormatDataReader.hpp:230
CDPL::Util::MultiFormatDataReader::operator!
bool operator!() const
Definition: MultiFormatDataReader.hpp:248
CDPL::Base::DataIOManager
A singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHan...
Definition: DataIOManager.hpp:104
Exceptions.hpp
Definition of exception classes.
CDPL::Base::DataFormat::getName
const std::string & getName() const
Returns the short-name of the data format.
CDPL::Base::DataReader::SharedPointer
std::shared_ptr< DataReader > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated DataReader instances.
Definition: DataReader.hpp:84
CDPL::Base::ControlParameterContainer::setParent
void setParent(const ControlParameterContainer *cntnr)
Sets or removes the parent control-parameter container used to resolve requests for missing entries.
CDPL::Util::MultiFormatDataReader::hasMoreData
bool hasMoreData()
Tells if there are any data records left to read.
Definition: MultiFormatDataReader.hpp:218
CDPL
The namespace of the Chemical Data Processing Library.
DataIOManager.hpp
Definition of the class CDPL::Base::DataIOManager.
CDPL::Util::MultiFormatDataReader::getDataFormat
const Base::DataFormat & getDataFormat() const
Definition: MultiFormatDataReader.hpp:188
CDPL::Util::MultiFormatDataReader::MultiFormatDataReader
MultiFormatDataReader(const MultiFormatDataReader &)=delete
CDPL::Util::MultiFormatDataReader::operator=
MultiFormatDataReader & operator=(const MultiFormatDataReader &)=delete
CDPL::Util::MultiFormatDataReader::MultiFormatDataReader
MultiFormatDataReader(const std::string &file_name, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Definition: MultiFormatDataReader.hpp:110