49 const std::string& value)
const
60 const std::string& key,
const std::string& value)
const
70 const std::string& name)
const
76const std::string claw::configuration_file::s_unknow_field_value;
107 section_content_ptr current_section = &m_noname_section;
109 while(get_line(is, syntax, line) && ok)
111 text::trim_right(line,
" \t");
114 ok = process_line(line, syntax, current_section);
128 if(!m_noname_section.empty())
130 save_section_content(m_noname_section, os, syntax);
134 file_content::const_iterator it;
135 for(it = m_sections.begin(); it != m_sections.end(); ++it)
138 save_section_content(it->second, os, syntax);
150 const std::string& field)
const
152 file_content::const_iterator sect = m_sections.find(section);
154 if(sect == m_sections.end())
155 return s_unknow_field_value;
158 section_content::const_iterator fld = sect->second.find(field);
160 if(fld == sect->second.end())
161 return s_unknow_field_value;
176 section_content::const_iterator fld = m_noname_section.find(field);
178 if(fld == m_noname_section.end())
179 return s_unknow_field_value;
190 const std::string& field)
const
215 const std::string& field,
216 const std::string& val)
218 file_content::iterator it = m_sections.find(section);
220 if(it != m_sections.end())
221 it->second.erase(field);
236 const std::string& val)
238 m_noname_section.erase(field);
251 const std::string& field,
252 const std::string& val)
254 m_sections[section].insert(section_content::value_type(field, val));
265 const std::string& val)
267 m_noname_section.insert(section_content::value_type(field, val));
276 m_sections.erase(section);
286 const std::string& field)
const
288 file_content::const_iterator it = m_sections.find(section);
290 if(it == m_sections.end())
303 const std::string& field)
const
305 file_content::const_iterator it = m_sections.find(section);
307 if(it == m_sections.end())
366 file_content::const_iterator it = m_sections.find(section);
368 if(it == m_sections.end())
381 file_content::const_iterator it = m_sections.find(section);
383 if(it == m_sections.end())
413bool claw::configuration_file::get_line(std::istream& is,
414 const syntax_description& syntax,
415 std::string& line)
const
417 const bool result(text::getline(is, line));
421 text::trim_left(line,
" \t");
422 escape_line(is, syntax, line);
434bool claw::configuration_file::process_line(
const std::string& line,
436 section_content_ptr& section)
442 if((line.size() >= 2) && (line[0] == syntax.section_name.first)
443 && (*(--line.end()) == syntax.section_name.second))
445 std::string section_name(line.substr(1, line.length() - 2));
446 text::trim(section_name,
" \t");
447 section = &m_sections[section_name];
451 std::string::size_type pos = line.find_first_of(syntax.assignment);
453 if(pos != std::string::npos)
455 std::string field(line.substr(0, pos));
458 if((pos + 1) != line.length())
460 value = (line.substr(pos + 1));
461 text::trim(value,
" \t");
464 text::trim(field,
" \t");
465 section->insert(section_content::value_type(field, value));
480void claw::configuration_file::escape_line(std::istream& is,
482 std::string& line)
const
484 std::string input_line(line);
485 std::string::iterator it, last;
489 last = input_line.begin();
491 for(it = last; (it != input_line.end()) && !stop;)
492 if(*it == syntax.comment)
496 line += std::string(last, it);
499 if(it == input_line.end())
501 std::string remaining;
502 get_line(is, syntax, remaining);
507 escape_char(*it, syntax, line);
516 line += std::string(last, it);
525void claw::configuration_file::escape_char(
char escaped,
527 std::string& str)
const
562 if(escaped == syntax.comment)
563 str += syntax.comment;
565 (str +=
"\\") += escaped;
575void claw::configuration_file::save_section_content(
576 const section_content& c, std::ostream& os,
579 section_content::const_iterator it;
581 for(it = c.begin(); it != c.end(); ++it)
582 os << syntax.make_assignment(it->first, it->second) <<
'\n';
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
This class is an iterator on the values set for a same field name.
const_section_iterator section_begin() const
Get an iterator on the field names of a section.
void add_value(const std::string §ion, const std::string &field, const std::string &val)
Add a value to a field.
const_file_iterator file_begin() const
Get an iterator on the first named section.
void set_value(const std::string §ion, 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_end(const std::string §ion, const std::string &field) const
Get an iterator past the last value set for a field.
void save(std::ostream &os, const syntax_description &syntax=syntax_description())
Write the configuration in a stream.
const_section_iterator section_end() const
Get an iterator past the last field name of a section.
claw::wrapped_iterator< constfile_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.
bool has_field(const std::string §ion, const std::string &field) const
Tell if a field exists.
claw::wrapped_iterator< constsection_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.
configuration_file()
Default constructor.
bool open(std::istream &is, const syntax_description &syntax=syntax_description())
Read the configuration from a stream.
const std::string & operator()(const std::string §ion, const std::string &field) const
Get the value of a field.
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_file_iterator file_end() const
Get an iterator just past the last named section.
A class to get the content of a configuration file.
Generic algorithms on strings.
This class tells us how to parse the input file.
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.
std::string make_section_name(const std::string &name) const
Create a section name from a string.
paired_symbol section_name
Pair of symbols around a section name.
char comment
Symbol used to comment the rest of the line.
char assignment
Symbol used to assign a value to a field.
syntax_description()
Default constructor.