12 #include <boost/regex.hpp>
14 using namespace boost;
17 #include "../Exceptions.hpp"
29 current_section =
"default";
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;
56 cerr <<
"Failed to open file " << filename <<
" for reading.\n";
57 throw FileException(
"Failed to open file " +
string(filename) +
" for writing.\n");
62 string current_section =
"default";
64 stringstream channels_config;
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:]]*");
78 if(regex_search(line,comment_regexp)
80 regex_search(line,blank_regexp))
85 bool section = regex_search(line,valid_section_regexp);
86 bool param = regex_search(line,valid_param_regexp);
89 if(!section && !param)
91 cerr <<
"Session::LoadFile(): Bad line: " << line << endl;
99 regex_match(line.c_str(), what, valid_section_regexp);
100 current_section = what[1];
107 regex_match(line.c_str(), what, valid_param_regexp);
108 string temp = what[2];
109 params[current_section][what[1]] = decodeString(temp);
114 saved_filename = filename;
124 cerr <<
"Failed to open file " << filename <<
" for writing.\n";
125 throw FileException(
"Failed to open file " +
string(filename) +
" for writing.\n");
129 this->printToStream(os);
133 saved_filename = filename;
140 save(saved_filename.c_str());
151 s1.printToStream(os);
161 section = current_section;
163 if(params.count(section) > 0)
164 if(params[section].
count(param) > 0)
175 void Session::printToStream(ostream &os)
177 typedef map<string, string> inner;
178 typedef map<string, map<string, string> > outer;
179 outer::iterator oit = params.begin();
184 for(;oit != params.end();
187 os <<
"[" << oit->first <<
"]\n";
188 for(iit = oit->second.begin();
189 iit != oit->second.end();
192 string key = iit->first;
193 string value = encodeString(iit->second);
194 os << key <<
" = " << value << endl;
203 string Session::encodeString(
string str)
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));
211 string Session::decodeString(
string str)
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),
' ');