Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes

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.

{
  // nothing to do
} // 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.

{
  open(is, syntax);
} // 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.

{
  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.

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.

{
  std::string input_line(line);
  std::string::iterator it, last;
  bool stop = false;

  line = "";
  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.

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.

{
  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.

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.

{
  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.

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.

{
  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.

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.

{
  return const_field_iterator( m_noname_section.upper_bound(field) );
} // 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().

{
  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::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().

{
  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 &  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.

{
  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()()

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.

{
  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()()

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().

{
  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()


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: