MCS  0.3.3-alpha7
Conf.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 
23 #include "mcs.hh"
24 using namespace mcs;
25 
26 
27 //--------------------------------------------------------
28 mcs::Conf::Conf(string filename) :
29  lastval(STRING, 300)
30 {
31  if (! filename.empty())
32  open(filename);
33 }
34 
35 
37 {}
38 
39 
40 void mcs::Conf::open(string filename)
41 {
42  ifstream in;
43  string sect, s, key, val, comm;
44 
45  pcrecpp::RE re_brackets("\\[(\\w+[-+]?\\w+)\\]");
46  pcrecpp::RE re_comments("(.*);(.*)");
47  pcrecpp::RE re_equal_split("(.+)=(.+)");
48 
49 
50  this->filename = filename;
51 
52  in.open(filename.c_str());
53  if (!in)
54  throw MCS_ERROR(MSG_CANT_OPEN_FILE, filename.csz);
55 
56 
57  keys.clear();
58  values.clear();
59  sections.clear();
60  comments.clear();
61 
62 
63  while (getline(in, s)) {
64  s = trim(s);
65  s = remTabs(s);
66 
67  if (! s.empty()) {
68  if (re_brackets.FullMatch(s, &val)) { //This is a section
69  sect = val;
70  }
71  else {
72  if (re_equal_split.FullMatch(s)) {
73  re_equal_split.FullMatch(s, &key, &val);
74 
75  comm = "";
76  if (re_comments.FullMatch(val)) {
77  s = val;
78  re_comments.FullMatch(s, &val, &comm);
79  }
80 
81  keys.push_back(trim(key));
82  values.push_back(trim(val));
83  sections.push_back(sect);
84  comments.push_back(trim(comm));
85  }
86  }
87  }
88  }
89  in.close();
90 
91  // unsigned int i;
92  // for (i=0; i<sections.size(); i++)
93  // cout << "-" << sections[i] << "-\t"
94  // << "-" << keys[i] << "-\t"
95  // << "-" << values[i] << "-\t"
96  // << "-" << comments[i] << "-" << endl;
97 }
98 
99 
100 
101 
102 
103 
104 int mcs::Conf::index(string section, string key)
105 {
106  unsigned int i;
107  section = trim(section);
108  key = trim(key);
109  lastval.setNull();
110 
111  for (i=0; i<sections.size(); i++)
112  if (sections[i] == section)
113  if (keys[i] == key) {
114  lastval = values[i];
115  return (int) i;
116  }
117 
118  return -1;
119 }
120 
121 
122 bool mcs::Conf::search(string section, string key, enum ThrowExceptions throwexc)
123 {
124  if (index(section, key) > -1)
125  return true;
126  else
127  if (throwexc)
128  throw MCS_ERROR(MSG_ENTRY_UNAVAILABLE, section.csz, key.csz);
129  else
130  return false;
131 }
132 
133 
134 
135 string mcs::Conf::sval(string section, string key)
136 {
137  search(section, key, THROW);
138  return lastval.sval();
139 }
140 
141 
142 int mcs::Conf::ival(string section, string key)
143 {
144  search(section, key, THROW);
145  return lastval.ival();
146 }
147 
148 
149 long long int mcs::Conf::lval(string section, string key)
150 {
151  search(section, key, THROW);
152  return lastval.lval();
153 }
154 
155 
156 Data& mcs::Conf::val(string section, string key)
157 {
158  if (section.length() + key.length() > 1)
159  search(section, key, THROW);
160  return lastval;
161 }
162 
163 
164 string mcs::Conf::sval(string section, string key, string defval)
165 {
166  if (search(section, key, DONT_THROW))
167  return lastval.sval();
168  else
169  return defval;
170 }
171 
172 
173 int mcs::Conf::ival(string section, string key, int defval)
174 {
175  if (search(section, key, DONT_THROW))
176  return lastval.ival();
177  else
178  return defval;
179 }
180 
181 
182 long long int mcs::Conf::lval(string section, string key, int defval)
183 {
184  if (search(section, key, DONT_THROW))
185  return lastval.lval();
186  else
187  return defval;
188 }
189 
190 
191 void mcs::Conf::setval(string section, string key, string val, string comment)
192 {
193  val = trim(val);
194  comment = trim(comment);
195  int i = index(section, key);
196  if (i > -1) {
197  values[i] = val;
198  comments[i] = comment;
199  }
200  else {
201  sections.push_back(section);
202  keys.push_back(key);
203  values.push_back(val);
204  comments.push_back(comment);
205  }
206 }
207 
208 
209 void mcs::Conf::setval(string section, string key, Data* val, string comment)
210 {
211  setval(section, key, val->sval(), comment);
212 }
213 
214 
215 void mcs::Conf::setval(string section, string key, int val, string comment)
216 {
217  setval(section, key, itos(val), comment);
218 }
219 
220 
221 void mcs::Conf::setval(string section, string key, long long int val, string comment)
222 {
223  setval(section, key, itos(val), comment);
224 }
225 
226 
228 {
229  ofstream out;
230  string sect;
231  unsigned int i;
232 
233  if (filename.empty())
234  filename = this->filename;
235 
236  out.open(filename.c_str());
237  if (!out)
238  throw MCS_ERROR(MSG_CANT_OPEN_FILE, filename.csz);
239 
240  for (i=0; i<sections.size(); i++) {
241  if (sections[i] != sect) {
242  sect = sections[i];
243  out << "[" << sect << "]" << endl;
244  }
245 
246  out << keys[i] << " = " << values[i];
247 
248  if (! comments[i].empty())
249  out << " ;" << comments[i];
250 
251  out << endl;
252  }
253  out.close();
254 }
string itos(int i)
Convert an integer to a string.
Definition: Utils.cc:77
string sval(string section, string key)
Search for the specified section/key value and return it as a string.
Definition: Conf.cc:135
vector< string > keys
Internal vector containing keys names.
Definition: mcs.hh:4853
bool search(string section, string key, enum ThrowExceptions throwexc=DONT_THROW)
Search for the specified section/key value.
Definition: Conf.cc:122
string sval(bool addWhiteSpaces=false) const
Convert internal data to a string object.
Definition: Data.cc:1555
vector< string > comments
Internal vector containing comments, this is used in the save() method.
Definition: mcs.hh:4859
long long int lval(string section, string key)
Search for the specified section/key value and return it as a long integer.
Definition: Conf.cc:149
void save(string filename="")
Save all the values in a configuration file.
Definition: Conf.cc:227
void setNull(bool null=true)
Set null flag. Following call to isNull() returns the value used here as parameter.
Definition: Data.cc:1923
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:964
void setval(string section, string key, string val, string comment="")
Set a new value for the specified section/key pair.
Definition: Conf.cc:191
Data & val(string section="", string key="")
Return a Data object containing the last value found.
Definition: Conf.cc:156
int index(string section, string key)
Search the index at which section and key are found.
Definition: Conf.cc:104
vector< string > sections
Internal vector containing section names.
Definition: mcs.hh:4850
string remTabs(string s)
Remove any tab.
Definition: Utils.cc:172
string filename
Path to the configuration file.
Definition: mcs.hh:4847
Conf(const Conf &)
Declared to avoid using of default copy constructor.
Main include file for all MCS based applications.
long long int lval() const
Convert internal data to a long long integer value.
Definition: Data.cc:1362
A general purpose data type.
Definition: mcs.hh:3092
~Conf()
Destructor.
Definition: Conf.cc:36
ThrowExceptions
Values to be used with throwexc parameters.
Definition: mcs.hh:257
string trim(string s)
Remove any leading or trailing blanks.
Definition: Utils.cc:160
int ival() const
Convert internal data to a integer value.
Definition: Data.cc:1209
vector< string > values
Internal vector containing values names.
Definition: mcs.hh:4856
int ival(string section, string key)
Search for the specified section/key value and return it as an integer.
Definition: Conf.cc:142
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018