39 : comment(
'#'), assignment(
'='), section_name(
'[',
']')
50 (
const std::string& value )
const 62 (
const std::string& key,
const std::string& value )
const 73 (
const std::string& name )
const 83 const std::string claw::configuration_file::s_unknow_field_value;
117 section_content_ptr current_section = &m_noname_section;
119 while ( get_line(is, syntax, line) && ok )
121 text::trim_right(line,
" \t");
124 ok = process_line( line, syntax, current_section );
139 if ( !m_noname_section.empty() )
141 save_section_content( m_noname_section, os, syntax );
145 file_content::const_iterator it;
146 for ( it=m_sections.begin(); it!=m_sections.end(); ++it )
149 save_section_content( it->second, os, syntax );
160 const std::string& claw::configuration_file::operator()
161 (
const std::string& section,
const std::string& field )
const 163 file_content::const_iterator sect = m_sections.find(section);
165 if ( sect == m_sections.end() )
166 return s_unknow_field_value;
169 section_content::const_iterator fld = sect->second.find(field);
171 if ( fld == sect->second.end() )
172 return s_unknow_field_value;
188 section_content::const_iterator fld = m_noname_section.find(field);
190 if ( fld == m_noname_section.end() )
191 return s_unknow_field_value;
203 (
const std::string& section,
const std::string& field )
const 230 (
const std::string& section,
const std::string& field,
const std::string& val )
232 file_content::iterator it = m_sections.find(section);
234 if ( it!=m_sections.end() )
235 it->second.erase(field);
251 (
const std::string& field,
const std::string& val )
253 m_noname_section.erase(field);
267 (
const std::string& section,
const std::string& field,
const std::string& val )
269 m_sections[section].insert( section_content::value_type(field, val) );
281 (
const std::string& field,
const std::string& val )
283 m_noname_section.insert( section_content::value_type(field, val) );
293 m_sections.erase(section);
304 (
const std::string& section,
const std::string& field )
const 306 file_content::const_iterator it = m_sections.find(section);
308 if (it == m_sections.end())
322 (
const std::string& section,
const std::string& field )
const 324 file_content::const_iterator it = m_sections.find(section);
326 if (it == m_sections.end())
390 file_content::const_iterator it = m_sections.find(section);
392 if (it == m_sections.end())
406 file_content::const_iterator it = m_sections.find(section);
408 if (it == m_sections.end())
441 bool claw::configuration_file::get_line
444 bool result = text::getline(is, line).good();
448 text::trim_left(line,
" \t");
449 escape_line(is, syntax, line);
462 bool claw::configuration_file::process_line
464 section_content_ptr& section )
470 if ( (line.size() >= 2)
474 std::string
section_name( line.substr(1, line.length()-2) );
475 text::trim( section_name,
" \t" );
480 std::string::size_type pos = line.find_first_of(syntax.
assignment);
482 if (pos != std::string::npos)
484 std::string field( line.substr(0, pos) );
487 if ( (pos+1) != line.length() )
489 value = ( line.substr(pos+1) );
490 text::trim(value,
" \t");
493 text::trim(field,
" \t");
494 section->insert( section_content::value_type(field, value) );
510 void claw::configuration_file::escape_line
513 std::string input_line(line);
514 std::string::iterator it, last;
518 last = input_line.begin();
520 for (it = last; (it!=input_line.end()) && !stop; )
523 else if (*it ==
'\\')
525 line += std::string(last, it);
528 if ( it == input_line.end() )
530 std::string remaining;
531 get_line(is, syntax, remaining);
536 escape_char(*it, syntax, line);
545 line += std::string(last, it);
555 void claw::configuration_file::escape_char
560 case '\'' : str +=
"\'";
break;
561 case '\"' : str +=
"\"";
break;
562 case '\\' : str +=
"\\";
break;
563 case 'a' : str +=
"\a";
break;
564 case 'b' : str +=
"\b";
break;
565 case 'f' : str +=
"\f";
break;
566 case 'n' : str +=
"\n";
break;
567 case 'r' : str +=
"\r";
break;
568 case 't' : str +=
"\t";
break;
569 case 'v' : str +=
"\v";
break;
571 if ( escaped == syntax.
comment )
574 (str +=
"\\") += escaped;
585 void claw::configuration_file::save_section_content
586 (
const section_content& c, std::ostream& os,
589 section_content::const_iterator it;
591 for (it=c.begin(); it!=c.end(); ++it)
const_section_iterator section_end() const
Get an iterator past the last field name of a section.
const_file_iterator file_end() const
Get an iterator just past the last named section.
std::string make_section_name(const std::string &name) const
Create a section name from a string.
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.
std::string make_comment(const std::string &value) const
Create a comment from a string.
std::string make_assignment(const std::string &key, const std::string &value) const
Make an assignment of a value to a key.
char assignment
Symbol used to assign a value to a field.
A class to get the content of a configuration file.
Base class for wrapped iterators.
syntax_description()
Default constructor.
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.
const_file_iterator file_begin() const
Get an iterator on the first named section.
paired_symbol section_name
Pair of symbols around a section name.
bool open(std::istream &is, const syntax_description &syntax=syntax_description())
Read the configuration from a stream.
void add_value(const std::string §ion, const std::string &field, const std::string &val)
Add a value to a field.
Some assert macros to strengthen you code.
const std::string & operator()(const std::string §ion, const std::string &field) const
Get the value of a field.
void save(std::ostream &os, const syntax_description &syntax=syntax_description())
Write the configuration in a stream.
void clear_section(const std::string §ion)
Remove a section and its fields.
configuration_file()
Default constructor.
const_section_iterator section_begin() const
Get an iterator on the field names of a section.
bool has_field(const std::string §ion, const std::string &field) const
Tell if a field exists.
This class tells us how to parse the input file.
void set_value(const std::string §ion, const std::string &field, const std::string &val)
Set the value of 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.
char comment
Symbol used to comment the rest of the line.
This class is an iterator on the values set for a same field name.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Generic algorithms on strings.
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.