CEBL  2.1
Session.cpp
Go to the documentation of this file.
1 
8 #include <fstream>
9 #include <iostream>
10 #include <sstream>
11 #include <algorithm>
12 #include <boost/regex.hpp>
13 using namespace std;
14 using namespace boost;
15 
16 #include "Session.hpp"
17 #include "../Exceptions.hpp"
18 
19 
20 
21 //----------------------------------------------------------------------
22 // CONSTRUCTORS / DESTRUCTORS
23 
24 //default constructor
26 {
27  saved_once = false;
28  saved_filename = "";
29  current_section = "default";
30 }
31 //destructor
33 {}
34 
35 //copy constructor
37 {
38  this->params = s2.params;
39  this->saved_once = s2.saved_once;
40  this->saved_filename = s2.saved_filename;
41  this->current_section = s2.current_section;
42 }
43 
44 //----------------------------------------------------------------------
45 // LOADING and SAVING
46 
47 
49 void Session::load(const char *filename)
50 {
51  //open file
52  ifstream is;
53  is.open(filename);
54  if(is.fail())
55  {
56  cerr << "Failed to open file " << filename << " for reading.\n";
57  throw FileException("Failed to open file " + string(filename) + " for writing.\n");
58  }
59 
60  //now load file
61  string line;
62  string current_section = "default";
63  string buffer;
64  stringstream channels_config;
65 
66  regex comment_regexp("^[[:space:]]*#.*");
67  regex blank_regexp("^[[:space:]]*$");
68  regex valid_section_regexp("^[[:space:]]*\\[(\\w*)\\][[:space:]]*");
69  regex valid_param_regexp("^[[:space:]]*(\\w+)[[:space:]]*=[[:space:]]*(.+)[[:space:]]*");
70 
71  //loop through file
72  while(!is.eof())
73  {
74  // get the next line
75  getline(is, buffer);
76  line = buffer;
77  //check if this line is a comment
78  if(regex_search(line,comment_regexp)
79  ||
80  regex_search(line,blank_regexp))
81  {
82  continue;
83  }
84  //otherwise extract the section and value
85  bool section = regex_search(line,valid_section_regexp);
86  bool param = regex_search(line,valid_param_regexp);
87 
88  //check if either search failed
89  if(!section && !param)
90  {
91  cerr << "Session::LoadFile(): Bad line: " << line << endl;
92  continue;
93  }
94 
95  //SECTION
96  if(section)
97  {
98  cmatch what;
99  regex_match(line.c_str(), what, valid_section_regexp);
100  current_section = what[1];
101  }
102 
103  //PARAM
104  if(param)
105  {
106  cmatch what;
107  regex_match(line.c_str(), what, valid_param_regexp);
108  string temp = what[2];
109  params[current_section][what[1]] = decodeString(temp);
110  }
111  }
112 
113  saved_once = true;
114  saved_filename = filename;
115 }
116 
118 void Session::save(const char *filename)
119 {
120  ofstream os;
121  os.open(filename);
122  if(os.fail())
123  {
124  cerr << "Failed to open file " << filename << " for writing.\n";
125  throw FileException("Failed to open file " + string(filename) + " for writing.\n");
126  return;
127  }
128  //send to ostream
129  this->printToStream(os);
130 
131  //update members
132  saved_once = true;
133  saved_filename = filename;
134 }
135 
138 {
139  if(saved_once)
140  save(saved_filename.c_str());
141 }
144 {
145  return !saved_once;
146 }
147 
149 ostream & operator<<(ostream &os, Session &s1)
150 {
151  s1.printToStream(os);
152  return os;
153 }
154 
155 //----------------------------------------------------------------------
156 // Accessors
157 
158 bool Session::exists(string param, string section)
159 {
160  if(section=="")
161  section = current_section;
162 
163  if(params.count(section) > 0)
164  if(params[section].count(param) > 0)
165  return true;
166  return false;
167 }
168 
169 
170 
171 
172 //----------------------------------------------------------------------
173 // Private Methods
174 
175 void Session::printToStream(ostream &os)
176 {
177  typedef map<string, string> inner;
178  typedef map<string, map<string, string> > outer;
179  outer::iterator oit = params.begin();
180 
181  inner::iterator iit;
182 
183 
184  for(;oit != params.end();
185  ++oit)
186  {
187  os << "[" << oit->first << "]\n";
188  for(iit = oit->second.begin();
189  iit != oit->second.end();
190  ++iit)
191  {
192  string key = iit->first;
193  string value = encodeString(iit->second);
194  os << key << " = " << value << endl;
195  }
196  }
197 }
198 
199 
200 //----------------------------------------------------------------------
201 //decode and encode strings
202 
203 string Session::encodeString(string str)
204 {
205  std::replace(str.begin(), str.end(), '\n', char(2));
206  std::replace(str.begin(), str.end(), '\t', char(3));
207  std::replace(str.begin(), str.end(), ' ', char(4));
208  return str;
209 }
210 
211 string Session::decodeString(string str)
212 {
213  std::replace(str.begin(), str.end(), char(2), '\n');
214  std::replace(str.begin(), str.end(), char(3), '\t');
215  std::replace(str.begin(), str.end(), char(4), ' ');
216  return str;
217 }