31 if (! filename.empty())
40 void mcs::Conf::open(
string filename)
43 string sect, s, key,
val, comm;
45 pcrecpp::RE re_brackets(
"\\[(\\w+[-+]?\\w+)\\]");
46 pcrecpp::RE re_comments(
"(.*);(.*)");
47 pcrecpp::RE re_equal_split(
"(.+)=(.+)");
52 in.open(filename.c_str());
54 throw MCS_ERROR(MSG_CANT_OPEN_FILE, filename.csz);
63 while (getline(in, s)) {
68 if (re_brackets.FullMatch(s, &val)) {
72 if (re_equal_split.FullMatch(s)) {
73 re_equal_split.FullMatch(s, &key, &val);
76 if (re_comments.FullMatch(val)) {
78 re_comments.FullMatch(s, &val, &comm);
107 section =
trim(section);
113 if (
keys[i] == key) {
124 if (
index(section, key) > -1)
128 throw MCS_ERROR(MSG_ENTRY_UNAVAILABLE, section.csz, key.csz);
138 return lastval.
sval();
145 return lastval.
ival();
152 return lastval.
lval();
158 if (section.length() + key.length() > 1)
167 return lastval.
sval();
176 return lastval.
ival();
185 return lastval.
lval();
194 comment =
trim(comment);
195 int i =
index(section, key);
233 if (filename.empty())
234 filename = this->filename;
236 out.open(filename.c_str());
238 throw MCS_ERROR(MSG_CANT_OPEN_FILE, filename.csz);
243 out <<
"[" << sect <<
"]" << endl;
string itos(int i)
Convert an integer to a string.
string sval(string section, string key)
Search for the specified section/key value and return it as a string.
vector< string > keys
Internal vector containing keys names.
bool search(string section, string key, enum ThrowExceptions throwexc=DONT_THROW)
Search for the specified section/key value.
string sval(bool addWhiteSpaces=false) const
Convert internal data to a string object.
vector< string > comments
Internal vector containing comments, this is used in the save() method.
long long int lval(string section, string key)
Search for the specified section/key value and return it as a long integer.
void save(string filename="")
Save all the values in a configuration file.
void setNull(bool null=true)
Set null flag. Following call to isNull() returns the value used here as parameter.
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
void setval(string section, string key, string val, string comment="")
Set a new value for the specified section/key pair.
Data & val(string section="", string key="")
Return a Data object containing the last value found.
int index(string section, string key)
Search the index at which section and key are found.
vector< string > sections
Internal vector containing section names.
string remTabs(string s)
Remove any tab.
string filename
Path to the configuration file.
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.
A general purpose data type.
ThrowExceptions
Values to be used with throwexc parameters.
string trim(string s)
Remove any leading or trailing blanks.
int ival() const
Convert internal data to a integer value.
vector< string > values
Internal vector containing values names.
int ival(string section, string key)
Search for the specified section/key value and return it as an integer.
Namespace for MCS library.