libpniio
fio_reader.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 //============================================================================
19 //
20 // Created on: Apr 30, 2012
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 #pragma once
25 
26 #include <iostream>
27 #include <sstream>
28 #include <map>
29 #include <boost/regex.hpp>
30 #include <boost/current_function.hpp>
31 
32 #include <pni/core/arrays.hpp>
33 #include "../spreadsheet_reader.hpp"
34 
35 namespace pni{
36 namespace io{
37 
47  {
48  private:
50  std::map<pni::core::string,std::streampos> _param_map;
52  std::streampos _data_offset;
53 
54  //====================private member methods=======================
62  void _parse_file(std::ifstream &stream);
63 
64  //-----------------------------------------------------------------
72  void _parse_parameters(std::ifstream &stream);
73 
74  //-----------------------------------------------------------------
83  void _parse_data(std::ifstream &stream);
84 
85  //-----------------------------------------------------------------
94  static pni::core::type_id_t _typestr2id(const pni::core::string &tstr);
95 
96  //-----------------------------------------------------------------
104  static column_info _read_column_info(const pni::core::string &line);
105 
106  //------------------------------------------------------------------
116  static std::vector<pni::core::string> _read_data_line(const pni::core::string &line);
117 
118  //-----------------------------------------------------------------
127  template<typename T>
128  void _get_parameter_data(std::ifstream &stream,T &value) const;
129 
130  //------------------------------------------------------------------
139  void _get_parameter_data(std::ifstream &stream,pni::core::string &value) const;
140 
141  //-------------------------------------------------------------------
152  template<typename CTYPE>
153  void _read_column(size_t index,CTYPE &array) const;
154 
155  public:
156  //==============constructor and destructor=========================
158  fio_reader();
159 
161  fio_reader(const fio_reader &r) = delete;
162 
164  fio_reader(fio_reader &&r);
165 
167  fio_reader(const pni::core::string &n);
168 
170  ~fio_reader();
171 
172  //===================assignment operators==========================
174  fio_reader &operator=(const fio_reader &r) = delete;
175 
178 
179  //=================public member methods===========================
186  size_t nparameters() const;
187 
188  //-----------------------------------------------------------------
196  std::vector<pni::core::string> parameter_names() const;
197 
198  //-----------------------------------------------------------------
207  template<typename T>
208  T parameter(const pni::core::string &name) const;
209 
210  //-----------------------------------------------------------------
221  template<typename CTYPE>
222  CTYPE column(const pni::core::string &n) const;
223 
224  //-----------------------------------------------------------------
237  template<typename CTYPE>
238  void column(const pni::core::string &n,CTYPE &c) const;
239 
240  };
241 
242  //==========implementation of private template methods=====================
243  template<typename T>
244  void fio_reader::_get_parameter_data(std::ifstream &stream,T &value) const
245  {
246  stream>>value;
247  }
248 
249  //======================template implementation============================
250  template<typename T>
251  T fio_reader::parameter(const pni::core::string &name) const
252  {
253  std::ifstream &stream = this->_get_stream();
254  std::streampos opos = stream.tellg(); //backup old stream position
255 
256  //obtain stream offset
257  stream.seekg(this->_param_map.find(name)->second,std::ios::beg);
258 
259  //read data
260  T value;
261 
262  this->_get_parameter_data(stream,value);
263 
264  //reset stream position
265  stream.seekg(opos,std::ios::beg);
266 
267  return value;
268  }
269 
270  //-------------------------------------------------------------------------
271  template<typename CTYPE>
272  void fio_reader::column(const pni::core::string &n,CTYPE &c) const
273  {
274  using namespace pni::core;
275  size_t cindex = 0; //column index
276 
277  try
278  {
279  cindex = this->column_index(n);
280  }
281  catch(key_error &error)
282  {
283  //append a new issuer to the exception
284  error.append(EXCEPTION_RECORD);
285  throw error;
286  }
287 
288  try
289  {
290  this->_read_column(cindex,c);
291  }
292  catch(file_error &error)
293  {
294  error.append(EXCEPTION_RECORD);
295  throw error;
296  }
297  }
298 
299 
300  //-------------------------------------------------------------------------
301  template<typename CTYPE>
302  CTYPE fio_reader::column(const pni::core::string &n) const
303  {
304  //create the container
305  //allocate a new array
306  std::vector<size_t> s{this->nrecords()};
307  CTYPE data(this->nrecords());
308 
309  this->column(n,data);
310 
311  return data;
312  }
313 
314  //-------------------------------------------------------------------------
315  template<typename CTYPE>
316  void fio_reader::_read_column(size_t index,CTYPE &c) const
317  {
318  using namespace pni::core;
319  std::ifstream &stream = this->_get_stream();
320  std::streampos orig_pos = stream.tellg();
321  //move stream to data section
322  stream.seekg(_data_offset,std::ios::beg);
323 
324  pni::core::string linebuffer;
325 #ifdef NOFOREACH
326  for(auto iter = c.begin();iter!=c.end();++iter)
327  {
328  typename CTYPE::value_type &v = *iter;
329 #else
330  for(typename CTYPE::value_type &v: c)
331  {
332 #endif
333  //read a single data line
334  try
335  {
336  std::getline(stream,linebuffer);
337  }
338  catch(...)
339  {
340  //set file stream back to its original position
341  stream.seekg(orig_pos,std::ios::beg);
342  //throw FileError if reading data form the file failed
343  throw file_error(EXCEPTION_RECORD,"Error reading data from file!");
344  }
345  //split data line
346  std::vector<pni::core::string> string_data = this->_read_data_line(linebuffer);
347  //set requested element to the string buffer
348  std::stringstream ss(string_data[index]);
349 
350  //write element to the container
351  ss>>v;
352  }
353 
354 
355  //reset the stream
356  stream.seekg(orig_pos,std::ios::beg);
357  }
358 
359 //end of namespace
360 }
361 }
void _parse_file(std::ifstream &stream)
initial file parseing
Definition: fio_reader.cpp:33
size_t column_index(const pni::core::string &name) const
get column index
Definition: spreadsheet_reader.cpp:86
void _read_column(size_t index, CTYPE &array) const
read column data
Definition: fio_reader.hpp:316
void _parse_parameters(std::ifstream &stream)
parse parameter section
Definition: fio_reader.cpp:64
CTYPE column(const pni::core::string &n) const
get single column
Definition: fio_reader.hpp:302
static std::vector< pni::core::string > _read_data_line(const pni::core::string &line)
read data line
Definition: fio_reader.cpp:198
FIO data reader.
Definition: fio_reader.hpp:46
std::ifstream & _get_stream() const
get stream
Definition: data_reader.hpp:84
std::vector< pni::core::string > parameter_names() const
get parameter names
Definition: fio_reader.cpp:263
size_t nrecords() const
get record number
Definition: spreadsheet_reader.hpp:171
static column_info _read_column_info(const pni::core::string &line)
get ColumnInfo from line
Definition: fio_reader.cpp:186
Definition: cbf_reader.hpp:41
base class for spreadsheet reader
Definition: spreadsheet_reader.hpp:44
void _get_parameter_data(std::ifstream &stream, T &value) const
read parameter data
Definition: fio_reader.hpp:244
std::map< pni::core::string, std::streampos > _param_map
parameter stream positions
Definition: fio_reader.hpp:50
fio_reader()
default constructor
Definition: fio_reader.cpp:217
size_t nparameters() const
get number of parameters
Definition: fio_reader.cpp:256
void _parse_data(std::ifstream &stream)
parse the data section
Definition: fio_reader.cpp:112
Spreadsheet column information.
Definition: column_info.hpp:43
~fio_reader()
destructor
Definition: fio_reader.cpp:239
fio_reader & operator=(const fio_reader &r)=delete
copy assignment is deleted
static pni::core::type_id_t _typestr2id(const pni::core::string &tstr)
type id from type string
Definition: fio_reader.cpp:175
T parameter(const pni::core::string &name) const
get parameter by name
Definition: fio_reader.hpp:251
std::streampos _data_offset
offset where real data starts
Definition: fio_reader.hpp:52