|
CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
#include <configuration_file.hpp>
Classes | |
| class | const_field_iterator |
| This class is an iterator on the values set for a same field name. More... | |
| struct | syntax_description |
| This class tells us how to parse the input file. More... | |
Public Types | |
| typedef claw::wrapped_iterator < const file_content::key_type, file_content::const_iterator, const_pair_first < file_content::value_type > >::iterator_type | const_file_iterator |
| Iterator on the name of the sections. | |
| typedef claw::wrapped_iterator < const section_content::key_type, section_content::const_iterator, const_pair_first < section_content::value_type > >::iterator_type | const_section_iterator |
| Iterator on the fields of a section. | |
Public Member Functions | |
| configuration_file () | |
| Default constructor. | |
| configuration_file (std::istream &is, const syntax_description &syntax=syntax_description()) | |
| Constructor. | |
| bool | open (std::istream &is, const syntax_description &syntax=syntax_description()) |
| Read the configuration from a stream. | |
| void | save (std::ostream &os, const syntax_description &syntax=syntax_description()) |
| Write the configuration in a stream. | |
| const std::string & | operator() (const std::string §ion, const std::string &field) const |
| Get the value of a field. | |
| const std::string & | operator() (const std::string &field) const |
| Get the value of a field. | |
| bool | has_field (const std::string §ion, const std::string &field) const |
| Tell if a field exists. | |
| bool | has_field (const std::string &field) const |
| Tell if a field exists. | |
| void | set_value (const std::string §ion, const std::string &field, const std::string &val) |
| Set the value of a field. | |
| void | set_value (const std::string &field, const std::string &val) |
| Set the value of a field. | |
| void | add_value (const std::string §ion, const std::string &field, const std::string &val) |
| Add a value to a field. | |
| void | add_value (const std::string &field, const std::string &val) |
| Set the value of a field. | |
| void | clear_section (const std::string §ion) |
| Remove a section and its fields. | |
| const_field_iterator | field_begin (const std::string §ion, const std::string &field) const |
| Get an iterator on the first value set for a field. | |
| const_field_iterator | field_end (const std::string §ion, const std::string &field) const |
| Get an iterator past the last value set for a field. | |
| const_field_iterator | field_begin (const std::string &field) const |
| Get an iterator on the first value set for a field. | |
| const_field_iterator | field_end (const std::string &field) const |
| Get an iterator past the last value set for a field. | |
| const_section_iterator | section_begin () const |
| Get an iterator on the field names of a section. | |
| const_section_iterator | section_end () const |
| Get an iterator past the last field name of a section. | |
| const_section_iterator | section_begin (const std::string §ion) const |
| Get an iterator on the field names of a section. | |
| const_section_iterator | section_end (const std::string §ion) const |
| Get an iterator past the last field name of a section. | |
| const_file_iterator | file_begin () const |
| Get an iterator on the first named section. | |
| const_file_iterator | file_end () const |
| Get an iterator just past the last named section. | |
Private Types | |
| typedef std::multimap < std::string, std::string > | section_content |
| The content of a section. | |
| typedef std::map< std::string, section_content > | file_content |
| The sections in the file. | |
| typedef section_content * | section_content_ptr |
| Pointer to a section_content. | |
Private Member Functions | |
| bool | get_line (std::istream &is, const syntax_description &syntax, std::string &line) const |
| Get a line in the stream. | |
| bool | process_line (const std::string &line, const syntax_description &syntax, section_content_ptr §ion) |
| Create a section or field with the content of a line. | |
| void | escape_line (std::istream &is, const syntax_description &syntax, std::string &line) const |
| Convert escaped symbols from a line. | |
| void | escape_char (char escaped, const syntax_description &syntax, std::string &str) const |
| Convert an escaped character and append it to a string. | |
| void | save_section_content (const section_content &c, std::ostream &os, const syntax_description &syntax) const |
| Write the content of a section in a stream. | |
Private Attributes | |
| section_content | m_noname_section |
| The fields set outside a section. | |
| file_content | m_sections |
| All the sections and their content. | |
Static Private Attributes | |
| static const std::string | s_unknow_field_value |
| String returned when asking for a not filled field. | |
Definition at line 42 of file configuration_file.hpp.
| typedef claw::wrapped_iterator< const file_content::key_type, file_content::const_iterator, const_pair_first<file_content::value_type> >::iterator_type claw::configuration_file::const_file_iterator |
Iterator on the name of the sections.
Definition at line 88 of file configuration_file.hpp.
| typedef claw::wrapped_iterator< const section_content::key_type, section_content::const_iterator, const_pair_first<section_content::value_type> >::iterator_type claw::configuration_file::const_section_iterator |
Iterator on the fields of a section.
Definition at line 95 of file configuration_file.hpp.
typedef std::map<std::string, section_content> claw::configuration_file::file_content [private] |
The sections in the file.
Definition at line 77 of file configuration_file.hpp.
typedef std::multimap<std::string, std::string> claw::configuration_file::section_content [private] |
The content of a section.
Definition at line 74 of file configuration_file.hpp.
typedef section_content* claw::configuration_file::section_content_ptr [private] |
Pointer to a section_content.
Definition at line 80 of file configuration_file.hpp.
| claw::configuration_file::configuration_file | ( | ) |
Default constructor.
Definition at line 89 of file configuration_file.cpp.
{
// nothing to do
} // configuration_file::configuration_file()
| claw::configuration_file::configuration_file | ( | std::istream & | is, |
| const syntax_description & | syntax = syntax_description() |
||
| ) |
Constructor.
| is | The stream to read from. |
| syntax | Description of the file's syntax. |
Definition at line 101 of file configuration_file.cpp.
References open().
{
open(is, syntax);
} // configuration_file::configuration_file()
| void claw::configuration_file::add_value | ( | const std::string & | section, |
| const std::string & | field, | ||
| const std::string & | val | ||
| ) |
Add a value to a field.
| section | The name of the section containing the field. |
| field | The name of the field to add. |
| val | The value. |
This method keeps all previous values for the given field.
Definition at line 267 of file configuration_file.cpp.
References m_sections.
Referenced by set_value().
{
m_sections[section].insert( section_content::value_type(field, val) );
} // configuration_file::add_value()
| void claw::configuration_file::add_value | ( | const std::string & | field, |
| const std::string & | val | ||
| ) |
Set the value of a field.
| field | The name of the field to Set. |
| val | The value. |
This method keeps all previous values for the given field.
Definition at line 281 of file configuration_file.cpp.
References m_noname_section.
{
m_noname_section.insert( section_content::value_type(field, val) );
} // configuration_file::add_value()
| void claw::configuration_file::clear_section | ( | const std::string & | section | ) |
Remove a section and its fields.
| section | The name of the section to remove. |
Definition at line 291 of file configuration_file.cpp.
References m_sections.
{
m_sections.erase(section);
} // configuration_file::clear_section()
| void claw::configuration_file::escape_char | ( | char | escaped, |
| const syntax_description & | syntax, | ||
| std::string & | str | ||
| ) | const [private] |
Convert an escaped character and append it to a string.
| escaped | The character that have been escaped. |
| syntax | Description of the file's syntax. |
| str | (out) The string in which we add the symbol. |
Definition at line 556 of file configuration_file.cpp.
References claw::configuration_file::syntax_description::comment.
Referenced by escape_line().
{
switch (escaped)
{
case '\'' : str += "\'"; break;
case '\"' : str += "\""; break;
case '\\' : str += "\\"; break;
case 'a' : str += "\a"; break;
case 'b' : str += "\b"; break;
case 'f' : str += "\f"; break;
case 'n' : str += "\n"; break;
case 'r' : str += "\r"; break;
case 't' : str += "\t"; break;
case 'v' : str += "\v"; break;
default :
if ( escaped == syntax.comment )
str += syntax.comment;
else
(str += "\\") += escaped;
}
} // configuration_file::escape_char()
| void claw::configuration_file::escape_line | ( | std::istream & | is, |
| const syntax_description & | syntax, | ||
| std::string & | line | ||
| ) | const [private] |
Convert escaped symbols from a line.
| is | The stream to read the line from. |
| syntax | Description of the file's syntax. |
| line | (out) The read line. |
Definition at line 511 of file configuration_file.cpp.
References claw::configuration_file::syntax_description::comment, escape_char(), and get_line().
Referenced by get_line().
{
std::string input_line(line);
std::string::iterator it, last;
bool stop = false;
line.clear();
last = input_line.begin();
for (it = last; (it!=input_line.end()) && !stop; )
if (*it == syntax.comment)
stop = true;
else if (*it == '\\')
{
line += std::string(last, it);
++it;
if ( it == input_line.end() )
{
std::string remaining;
get_line(is, syntax, remaining);
line += remaining;
}
else
{
escape_char(*it, syntax, line);
++it;
}
last = it;
}
else
++it;
line += std::string(last, it);
} // configuration_file::escape_line()
| claw::configuration_file::const_field_iterator claw::configuration_file::field_begin | ( | const std::string & | section, |
| const std::string & | field | ||
| ) | const |
Get an iterator on the first value set for a field.
| section | The name of the section in which is the field. |
| field | The name of the field to get. |
Definition at line 304 of file configuration_file.cpp.
References m_sections.
Referenced by has_field().
{
file_content::const_iterator it = m_sections.find(section);
if (it == m_sections.end())
return const_field_iterator();
else
return const_field_iterator( it->second.lower_bound(field) );
} // configuration_file::field_begin()
| claw::configuration_file::const_field_iterator claw::configuration_file::field_begin | ( | const std::string & | field | ) | const |
Get an iterator on the first value set for a field.
| field | The name of the field to get. |
Definition at line 340 of file configuration_file.cpp.
References m_noname_section.
{
return const_field_iterator( m_noname_section.lower_bound(field) );
} // configuration_file::field_begin()
| claw::configuration_file::const_field_iterator claw::configuration_file::field_end | ( | const std::string & | section, |
| const std::string & | field | ||
| ) | const |
Get an iterator past the last value set for a field.
| section | The name of the section in which is the field. |
| field | The name of the field to get. |
Definition at line 322 of file configuration_file.cpp.
References m_sections.
Referenced by has_field().
{
file_content::const_iterator it = m_sections.find(section);
if (it == m_sections.end())
return const_field_iterator();
else
return const_field_iterator( it->second.upper_bound(field) );
} // configuration_file::field_end()
| claw::configuration_file::const_field_iterator claw::configuration_file::field_end | ( | const std::string & | field | ) | const |
Get an iterator past the last value set for a field.
| field | The name of the field to get. |
Definition at line 353 of file configuration_file.cpp.
References m_noname_section.
{
return const_field_iterator( m_noname_section.upper_bound(field) );
} // configuration_file::field_end()
| claw::configuration_file::const_file_iterator claw::configuration_file::file_begin | ( | ) | const |
Get an iterator on the first named section.
Definition at line 419 of file configuration_file.cpp.
References m_sections.
{
return const_file_iterator( m_sections.begin() );
} // configuration_file::file_begin()
| claw::configuration_file::const_file_iterator claw::configuration_file::file_end | ( | ) | const |
Get an iterator just past the last named section.
Definition at line 429 of file configuration_file.cpp.
References m_sections.
{
return const_file_iterator( m_sections.end() );
} // configuration_file::file_end()
| bool claw::configuration_file::get_line | ( | std::istream & | is, |
| const syntax_description & | syntax, | ||
| std::string & | line | ||
| ) | const [private] |
Get a line in the stream.
| is | The stream to read the line from. |
| syntax | Description of the file's syntax. |
| line | (out) The read line. |
Definition at line 442 of file configuration_file.cpp.
References escape_line(), claw::text::getline(), and claw::text::trim_left().
Referenced by escape_line(), and open().
{
bool result = text::getline(is, line);
if ( result )
{
text::trim_left(line, " \t");
escape_line(is, syntax, line);
}
return result;
} // configuration_file::get_line()
| bool claw::configuration_file::has_field | ( | const std::string & | field | ) | const |
Tell if a field exists.
| field | The name of the field to test. |
Definition at line 215 of file configuration_file.cpp.
References field_begin(), and field_end().
{
return field_begin( field ) != field_end( field );
} // configuration_file::has_field()
| bool claw::configuration_file::has_field | ( | const std::string & | section, |
| const std::string & | field | ||
| ) | const |
Tell if a field exists.
| section | The name of the section containing the field. |
| field | The name of the field to test. |
Definition at line 203 of file configuration_file.cpp.
References field_begin(), and field_end().
{
return field_begin( section, field ) != field_end( section, field );
} // configuration_file::has_field()
| bool claw::configuration_file::open | ( | std::istream & | is, |
| const syntax_description & | syntax = syntax_description() |
||
| ) |
Read the configuration from a stream.
| is | The stream to read from. |
| syntax | Description of the file's syntax. |
Definition at line 113 of file configuration_file.cpp.
References get_line(), m_noname_section, process_line(), and claw::text::trim_right().
Referenced by configuration_file().
{
std::string line;
bool ok = true;
section_content_ptr current_section = &m_noname_section;
while ( get_line(is, syntax, line) && ok )
{
text::trim_right(line, " \t");
if ( !line.empty() )
ok = process_line( line, syntax, current_section );
}
return ok;
} // configuration_file::open()
| const std::string & claw::configuration_file::operator() | ( | const std::string & | field | ) | const |
Get the value of a field.
| field | The name of the field to get. |
Definition at line 186 of file configuration_file.cpp.
References m_noname_section, and s_unknow_field_value.
{
section_content::const_iterator fld = m_noname_section.find(field);
if ( fld == m_noname_section.end() )
return s_unknow_field_value;
else
return fld->second;
} // configuration_file::operator()()
| const std::string & claw::configuration_file::operator() | ( | const std::string & | section, |
| const std::string & | field | ||
| ) | const |
Get the value of a field.
| section | The name of the section in which is the field. |
| field | The name of the field to get. |
Definition at line 161 of file configuration_file.cpp.
References m_sections, and s_unknow_field_value.
{
file_content::const_iterator sect = m_sections.find(section);
if ( sect == m_sections.end() )
return s_unknow_field_value;
else
{
section_content::const_iterator fld = sect->second.find(field);
if ( fld == sect->second.end() )
return s_unknow_field_value;
else
return fld->second;
}
} // configuration_file::operator()()
| bool claw::configuration_file::process_line | ( | const std::string & | line, |
| const syntax_description & | syntax, | ||
| section_content_ptr & | section | ||
| ) | [private] |
Create a section or field with the content of a line.
| line | The line to process. |
| syntax | Description of the file's syntax. |
| section | The section we are filling. |
Definition at line 463 of file configuration_file.cpp.
References claw::configuration_file::syntax_description::assignment, CLAW_PRECOND, m_sections, claw::configuration_file::syntax_description::section_name, and claw::text::trim().
Referenced by open().
{
CLAW_PRECOND( !line.empty() );
bool result = true;
if ( (line.size() >= 2)
&& (line[0] == syntax.section_name.first)
&& ( *(--line.end()) == syntax.section_name.second) )
{
std::string section_name( line.substr(1, line.length()-2) );
text::trim( section_name, " \t" );
section = &m_sections[section_name];
}
else
{
std::string::size_type pos = line.find_first_of(syntax.assignment);
if (pos != std::string::npos)
{
std::string field( line.substr(0, pos) );
std::string value;
if ( (pos+1) != line.length() )
{
value = ( line.substr(pos+1) );
text::trim(value, " \t");
}
text::trim(field, " \t");
section->insert( section_content::value_type(field, value) );
}
else
result = false;
}
return result;
} // configuration_file::process_line()
| void claw::configuration_file::save | ( | std::ostream & | os, |
| const syntax_description & | syntax = syntax_description() |
||
| ) |
Write the configuration in a stream.
| os | The stream to write in. |
| syntax | Description of the file's syntax. |
Definition at line 137 of file configuration_file.cpp.
References m_noname_section, m_sections, claw::configuration_file::syntax_description::make_section_name(), and save_section_content().
{
if ( !m_noname_section.empty() )
{
save_section_content( m_noname_section, os, syntax );
os << '\n';
}
file_content::const_iterator it;
for ( it=m_sections.begin(); it!=m_sections.end(); ++it )
{
os << syntax.make_section_name(it->first) << '\n';
save_section_content( it->second, os, syntax );
os << '\n';
}
} // configuration_file::save()
| void claw::configuration_file::save_section_content | ( | const section_content & | c, |
| std::ostream & | os, | ||
| const syntax_description & | syntax | ||
| ) | const [private] |
Write the content of a section in a stream.
| os | The stream to write in. |
| c | The content to write. |
| syntax | Description of the file's syntax. |
Definition at line 586 of file configuration_file.cpp.
References claw::configuration_file::syntax_description::make_assignment().
Referenced by save().
{
section_content::const_iterator it;
for (it=c.begin(); it!=c.end(); ++it)
os << syntax.make_assignment(it->first, it->second) << '\n';
} // configuration_file::save_section_content()
| claw::configuration_file::const_section_iterator claw::configuration_file::section_begin | ( | ) | const |
Get an iterator on the field names of a section.
Definition at line 365 of file configuration_file.cpp.
References m_noname_section.
{
return const_section_iterator( m_noname_section.begin() );
} // configuration_file::section_begin()
| claw::configuration_file::const_section_iterator claw::configuration_file::section_begin | ( | const std::string & | section | ) | const |
Get an iterator on the field names of a section.
| section | The name of the section in which the fields are searched. |
Definition at line 388 of file configuration_file.cpp.
References m_sections.
{
file_content::const_iterator it = m_sections.find(section);
if (it == m_sections.end())
return const_section_iterator();
else
return const_section_iterator( it->second.begin() );
} // configuration_file::section_begin()
| claw::configuration_file::const_section_iterator claw::configuration_file::section_end | ( | const std::string & | section | ) | const |
Get an iterator past the last field name of a section.
| section | The name of the section in which the fields are searched. |
Definition at line 404 of file configuration_file.cpp.
References m_sections.
{
file_content::const_iterator it = m_sections.find(section);
if (it == m_sections.end())
return const_section_iterator();
else
return const_section_iterator( it->second.end() );
} // configuration_file::section_end()
| claw::configuration_file::const_section_iterator claw::configuration_file::section_end | ( | ) | const |
Get an iterator past the last field name of a section.
Definition at line 377 of file configuration_file.cpp.
References m_noname_section.
{
return const_section_iterator( m_noname_section.end() );
} // configuration_file::section_end()
| void claw::configuration_file::set_value | ( | const std::string & | section, |
| const std::string & | field, | ||
| const std::string & | val | ||
| ) |
Set the value of a field.
| section | The name of the section containing the field. |
| field | The name of the field to set. |
| val | The value. |
This method removes all previous values for the given field.
Definition at line 230 of file configuration_file.cpp.
References add_value(), and m_sections.
{
file_content::iterator it = m_sections.find(section);
if ( it!=m_sections.end() )
it->second.erase(field);
add_value(section, field, val);
} // configuration_file::set_value()
| void claw::configuration_file::set_value | ( | const std::string & | field, |
| const std::string & | val | ||
| ) |
Set the value of a field.
| field | The name of the field to Set. |
| val | The value. |
This method removes all previous values for the given field.
Definition at line 251 of file configuration_file.cpp.
References add_value(), and m_noname_section.
{
m_noname_section.erase(field);
add_value(field, val);
} // configuration_file::set_value()
The fields set outside a section.
Definition at line 237 of file configuration_file.hpp.
Referenced by add_value(), field_begin(), field_end(), open(), operator()(), save(), section_begin(), section_end(), and set_value().
All the sections and their content.
Definition at line 240 of file configuration_file.hpp.
Referenced by add_value(), clear_section(), field_begin(), field_end(), file_begin(), file_end(), operator()(), process_line(), save(), section_begin(), section_end(), and set_value().
const std::string claw::configuration_file::s_unknow_field_value [static, private] |
String returned when asking for a not filled field.
Definition at line 243 of file configuration_file.hpp.
Referenced by operator()().
1.7.3