claw::configuration_file Class Reference

#include <configuration_file.hpp>

List of all members.

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 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.
const std::string & operator() (const std::string &section, 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.
const_field_iterator field_begin (const std::string &section, const std::string &field) const
 Get an iterator on the first value set for a field.
const_field_iterator field_end (const std::string &section, 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.

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_contentsection_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 &section)
 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.

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.

Detailed Description

Definition at line 39 of file configuration_file.hpp.


Member Typedef Documentation

typedef std::map<std::string, section_content> claw::configuration_file::file_content [private]

The sections in the file.

Definition at line 68 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 65 of file configuration_file.hpp.

Pointer to a section_content.

Definition at line 71 of file configuration_file.hpp.


Constructor & Destructor Documentation

claw::configuration_file::configuration_file (  ) 

Default constructor.

Definition at line 54 of file configuration_file.cpp.

00055 {
00056   // nothing to do
00057 } // configuration_file::configuration_file()

claw::configuration_file::configuration_file ( std::istream &  is,
const syntax_description syntax = syntax_description() 
)

Constructor.

Parameters:
is The stream to read from.
syntax Description of the file's syntax.

Definition at line 66 of file configuration_file.cpp.

00067 {
00068   open(is, syntax);
00069 } // configuration_file::configuration_file()


Member Function Documentation

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.

Parameters:
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 321 of file configuration_file.cpp.

References claw::configuration_file::syntax_description::comment.

00322 {
00323   switch (escaped)
00324     {
00325     case '\'' : str += "\'"; break;
00326     case '\"' : str += "\""; break;
00327     case '\\' : str += "\\"; break;
00328     case 'a' : str += "\a"; break;
00329     case 'b' : str += "\b"; break;
00330     case 'f' : str += "\f"; break;
00331     case 'n' : str += "\n"; break;
00332     case 'r' : str += "\r"; break;
00333     case 't' : str += "\t"; break;
00334     case 'v' : str += "\v"; break;
00335     default :
00336       if ( escaped == syntax.comment )
00337         str += syntax.comment;
00338       else
00339         (str += "\\") += escaped;
00340     }
00341 } // 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.

Parameters:
is The stream to read the line from.
syntax Description of the file's syntax.
line (out) The read line.

Definition at line 276 of file configuration_file.cpp.

References claw::configuration_file::syntax_description::comment.

00277 {
00278   std::string input_line(line);
00279   std::string::iterator it, last;
00280   bool stop = false;
00281 
00282   line = "";
00283   last = input_line.begin();
00284 
00285   for (it = last; (it!=input_line.end()) && !stop; )
00286     if (*it == syntax.comment)
00287       stop = true;
00288     else if (*it == '\\')
00289       {
00290         line += std::string(last, it);
00291         ++it;
00292 
00293         if ( it == input_line.end() )
00294           {
00295             std::string remaining;
00296             get_line(is, syntax, remaining);
00297             line += remaining;
00298           }
00299         else
00300           {
00301             escape_char(*it, syntax, line);
00302             ++it;
00303           }
00304 
00305         last = it;
00306       }
00307     else
00308       ++it;
00309 
00310   line += std::string(last, it);
00311 } // configuration_file::escape_line()

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.

Parameters:
field The name of the field to get.
Remarks:
The field is searched in the fields declared outside any section.

Definition at line 181 of file configuration_file.cpp.

References m_noname_section.

00182 {
00183   return const_field_iterator( m_noname_section.lower_bound(field) );
00184 } // configuration_file::field_begin()

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.

Parameters:
section The name of the section in which is the field.
field The name of the field to get.

Definition at line 145 of file configuration_file.cpp.

00146 {
00147   file_content::const_iterator it = m_sections.find(section);
00148 
00149   if (it == m_sections.end())
00150     return const_field_iterator();
00151   else
00152     return const_field_iterator( it->second.lower_bound(field) );
00153 } // configuration_file::field_begin()

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.

Parameters:
field The name of the field to get.
Remarks:
The field is searched in the fields declared outside any section.

Definition at line 194 of file configuration_file.cpp.

References m_noname_section.

00195 {
00196   return const_field_iterator( m_noname_section.upper_bound(field) );
00197 } // configuration_file::field_end()

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.

Parameters:
section The name of the section in which is the field.
field The name of the field to get.

Definition at line 163 of file configuration_file.cpp.

00164 {
00165   file_content::const_iterator it = m_sections.find(section);
00166 
00167   if (it == m_sections.end())
00168     return const_field_iterator();
00169   else
00170     return const_field_iterator( it->second.upper_bound(field) );
00171 } // configuration_file::field_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.

Parameters:
is The stream to read the line from.
syntax Description of the file's syntax.
line (out) The read line.

Definition at line 207 of file configuration_file.cpp.

References claw::text::getline(), and claw::text::trim_left().

00208 {
00209   bool result = text::getline(is, line);
00210 
00211   if ( result )
00212     {
00213       text::trim_left(line, " \t");
00214       escape_line(is, syntax, line);
00215     }
00216 
00217   return result;
00218 } // configuration_file::get_line()

bool claw::configuration_file::open ( std::istream &  is,
const syntax_description syntax = syntax_description() 
)

Read the configuration from a stream.

Parameters:
is The stream to read from.
syntax Description of the file's syntax.

Definition at line 78 of file configuration_file.cpp.

References claw::text::trim_right().

00079 {
00080   std::string line;
00081   bool ok = true;
00082   section_content_ptr current_section = &m_noname_section;
00083 
00084   while ( get_line(is, syntax, line) && ok )
00085     {
00086       text::trim_right(line, " \t");
00087 
00088       if ( !line.empty() )
00089         ok = process_line( line, syntax, current_section );
00090     }
00091 
00092   return ok;
00093 } // configuration_file::open()

const std::string & claw::configuration_file::operator() ( const std::string &  field  )  const

Get the value of a field.

Parameters:
field The name of the field to get.
Remarks:
The field is searched in the fields declared outside any section.

Definition at line 127 of file configuration_file.cpp.

References m_noname_section, and s_unknow_field_value.

00128 {
00129   section_content::const_iterator fld = m_noname_section.find(field);
00130 
00131   if ( fld == m_noname_section.end() )
00132     return s_unknow_field_value;
00133   else
00134     return fld->second;
00135 } // 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.

Parameters:
section The name of the section in which is the field.
field The name of the field to get.

Definition at line 102 of file configuration_file.cpp.

00103 {
00104   file_content::const_iterator sect = m_sections.find(section);
00105 
00106   if ( sect == m_sections.end() )
00107     return s_unknow_field_value;
00108   else
00109     {
00110       section_content::const_iterator fld = sect->second.find(field);
00111 
00112       if ( fld == sect->second.end() )
00113         return s_unknow_field_value;
00114       else
00115         return fld->second;
00116     }
00117 } // 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.

Parameters:
line The line to process.
syntax Description of the file's syntax.
section The section we are filling.

Definition at line 228 of file configuration_file.cpp.

References claw::configuration_file::syntax_description::assignment, CLAW_PRECOND, claw::configuration_file::syntax_description::section_name, and claw::text::trim().

00230 {
00231   CLAW_PRECOND( !line.empty() );
00232 
00233   bool result = true;
00234 
00235   if ( (line.size() >= 2)
00236        && (line[0] == syntax.section_name.first)
00237        && ( *(--line.end()) == syntax.section_name.second) )
00238     {
00239       std::string section_name( line.substr(1, line.length()-2) );
00240       text::trim( section_name, " \t" );
00241       section = &m_sections[section_name];
00242     }
00243   else
00244     {
00245       std::string::size_type pos = line.find_first_of(syntax.assignment);
00246 
00247       if (pos != std::string::npos)
00248         {
00249           std::string field( line.substr(0, pos) );
00250           std::string value;
00251 
00252           if ( (pos+1) != line.length() )
00253             {
00254               value = ( line.substr(pos+1) );
00255               text::trim(value, " \t");
00256             }
00257 
00258           text::trim(field, " \t");
00259           section->insert( section_content::value_type(field, value) );
00260         }
00261       else
00262         result = false;
00263     }
00264 
00265   return result;
00266 } // configuration_file::process_line()


Member Data Documentation

The fields set outside a section.

Definition at line 183 of file configuration_file.hpp.

Referenced by field_begin(), field_end(), and operator()().

All the sections and their content.

Definition at line 186 of file configuration_file.hpp.

const std::string claw::configuration_file::s_unknow_field_value [static, private]

String returned when asking for a not filled field.

Definition at line 189 of file configuration_file.hpp.

Referenced by operator()().


The documentation for this class was generated from the following files:

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1