39 : comment(
'#'), assignment(
'='), section_name(
'[',
']')
50 (
const std::string& value )
const
52 return comment + (
' ' + value);
62 (
const std::string& key,
const std::string& value )
const
64 return key +
' ' + assignment +
' ' + value;
73 (
const std::string& name )
const
75 return section_name.first + name + section_name.second;
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 )
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
442 ( std::istream& is,
const syntax_description& syntax, std::string& line )
const
449 escape_line(is, syntax, line);
462 bool claw::configuration_file::process_line
463 (
const std::string& line,
const syntax_description& syntax,
464 section_content_ptr& section )
470 if ( (line.size() >= 2)
471 && (line[0] == syntax.section_name.first)
472 && ( *(--line.end()) == syntax.section_name.second) )
474 std::string section_name( line.substr(1, line.length()-2) );
476 section = &m_sections[section_name];
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) );
494 section->insert( section_content::value_type(field, value) );
510 void claw::configuration_file::escape_line
511 ( std::istream& is,
const syntax_description& syntax, std::string& line )
const
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; )
521 if (*it == syntax.comment)
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
556 (
char escaped,
const syntax_description& syntax, std::string& str )
const
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 )
572 str += syntax.comment;
574 (str +=
"\\") += escaped;
585 void claw::configuration_file::save_section_content
586 (
const section_content& c, std::ostream& os,
587 const syntax_description& syntax )
const
589 section_content::const_iterator it;
591 for (it=c.begin(); it!=c.end(); ++it)
592 os << syntax.make_assignment(it->first, it->second) <<
'\n';