BayesOpt
fileparser.hpp
Go to the documentation of this file.
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 #ifndef _FILEPARSER_HPP_
25 #define _FILEPARSER_HPP_
26 
27 #include <vector>
28 
29 #include <string.h>
30 
31 #include <fstream>
32 #include <iostream>
33 #include <sstream>
34 #include <iomanip>
35 
36 #include <boost/numeric/ublas/vector.hpp>
37 
38 #include "parser.hpp"
39 
40 namespace bayesopt
41 {
42  namespace utils
43  {
44  class FileParser{
45  public:
46  FileParser(std::string filename, int prec = 10);
47  FileParser(int prec = 10);
48  ~FileParser();
49 
50  /* Write stream and read stream open/close functions*/
51  void open(bool readMode);
52  void openOutput();
53  void openInput();
54  void close();
55  bool isReading();
56  bool isWriting();
57 
58  void setPrecision(int prec);
59  bool fileExists();
60 
61  /* Data write/read function */
62  void write(std::string name, std::string value);
63  void read(std::string name, std::string &value);
64  std::string read(std::string name);
65 
66  /* Array write/read function */
67  void write(std::string name, const std::vector<std::string> &arr, const std::vector<int> &dims);
68  void read(std::string name, std::vector<std::string> &arr, std::vector<int> &dims);
69 
70  /* Template definitions and implementation */
71  template <typename T> void write(std::string name, T value){
72  std::string str = FileParser::to_string<T>(value);
73  write(name, str);
74  }
75  template <typename T> void read(std::string name, T &value){
76  value = to_value<T>(FileParser::read(name));
77  }
78 
79  /* Reads or writes a variable based on the open stream */
80  template <typename T> void readOrWrite(std::string name, T&value){
81  if(isReading()){
82  read(name, value);
83  }
84  else if(isWriting()){
85  T v = value;
86  write(name,v);
87  }
88  }
89 
90  /*
91  * Non-templated functions (types that requires special treatment)
92  */
93  void write_chars(std::string name, char* value);
94  void read_chars(std::string name, char* value);
95  void readOrWrite(std::string name, char* value);
96 
97  void write_ublas(std::string name, boost::numeric::ublas::vector<double> &values);
98  void read_ublas(std::string name, boost::numeric::ublas::vector<double> &values);
99  void readOrWrite(std::string name, boost::numeric::ublas::vector<double> &values);
100 
101  void write_vecOfvec(std::string name, std::vector<boost::numeric::ublas::vector<double> > &values);
102  void read_vecOfvec(std::string name, std::vector<boost::numeric::ublas::vector<double> > &values);
103  void readOrWrite(std::string name, std::vector<boost::numeric::ublas::vector<double> > &values);
104 
105  void write_double_array(std::string name, double values[], size_t length);
106  void read_double_array(std::string name, double values[], size_t length);
107  void readOrWrite(std::string name, double values[], size_t length);
108 
109  /* Template definitions and implementation */
110  template <typename T>
111  std::string to_string(T value)
112  {
113  std::ostringstream os;
114  os << std::setprecision(precision) << value ;
115  return os.str();
116  }
117 
118  template <typename T>
119  T to_value(std::string str)
120  {
121  std::istringstream ss(str);
122  T result;
123  return (ss >> std::setprecision(precision) >> result) ? result : 0;
124  }
125  private:
126  /* Search variables in file */
127  bool movePointer(std::string name, std::string &content);
128  bool startsWith(std::string all, std::string sub);
129 
130  /* Parses the contents as an array */
131  void parseArray(std::string contents, std::vector<std::string> &arr, std::vector<int> &dims);
132 
133  /* private members */
134  std::string filename;
135  std::ofstream output;
136  std::ifstream input;
137 
138  std::string currentLine;
139  int precision;
140 
141 
142  };
143  } //namespace utils
144 } //namespace bayesopt
145 
146 #endif
Namespace of the library interface.
Definition: using.dox:1
Functions to parse strings.