configuration_file.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_CONFIGURATION_FILE_HPP__
#define __CLAW_CONFIGURATION_FILE_HPP__
#include <claw/iterator.hpp>
#include <claw/functional.hpp>
#include <iostream>
#include <map>
#include <string>
namespace claw
{
class configuration_file
{
public:
struct syntax_description
{
public:
typedef std::pair<char, char> paired_symbol;
public:
syntax_description();
std::string make_comment( const std::string& value ) const;
std::string make_assignment
( const std::string& key, const std::string& value ) const;
std::string make_section_name( const std::string& name ) const;
public:
char comment;
char assignment;
paired_symbol section_name;
}; // struct syntax_descritpion
private:
typedef std::multimap<std::string, std::string> section_content;
typedef std::map<std::string, section_content> file_content;
typedef section_content* section_content_ptr;
public:
typedef claw::wrapped_iterator
< const file_content::key_type,
file_content::const_iterator,
const_pair_first<file_content::value_type>
>::iterator_type const_file_iterator;
typedef claw::wrapped_iterator
< const section_content::key_type,
section_content::const_iterator,
const_pair_first<section_content::value_type>
>::iterator_type const_section_iterator;
class const_field_iterator
{
private:
typedef section_content::const_iterator wrapped_iterator_type;
public:
typedef std::string value_type;
typedef const value_type& reference;
typedef const value_type* pointer;
typedef wrapped_iterator_type::difference_type difference_type;
typedef wrapped_iterator_type::iterator_category iterator_category;
public:
const_field_iterator() {}
const_field_iterator( wrapped_iterator_type it ) : m_iterator(it) {}
bool operator==( const const_field_iterator& that ) const
{
return m_iterator == that.m_iterator;
} // operator==()
bool operator!=( const const_field_iterator& that ) const
{
return m_iterator != that.m_iterator;
} // operator!=()
const_field_iterator& operator++()
{
++m_iterator;
return *this;
} // operator++()
const_field_iterator operator++(int)
{
const_field_iterator tmp(*this);
++m_iterator;
return tmp;
} // operator++() [post]
const_field_iterator& operator--()
{
--m_iterator;
return *this;
} // operator--()
const_field_iterator operator--(int)
{
const_field_iterator tmp(*this);
--m_iterator;
return tmp;
} // operator--() [post]
reference operator*() const
{
return m_iterator->second;
} // operator*()
pointer operator->() const
{
return &m_iterator->second;
} // operator->()
private:
wrapped_iterator_type m_iterator;
}; // class const_field_iterator
public:
configuration_file();
configuration_file
(std::istream& is, const syntax_description& syntax = syntax_description());
bool open
(std::istream& is, const syntax_description& syntax = syntax_description());
void save
(std::ostream& os, const syntax_description& syntax = syntax_description());
const std::string&
operator()( const std::string& section, const std::string& field ) const;
const std::string& operator()( const std::string& field ) const;
bool has_field
( const std::string& section, const std::string& field ) const;
bool has_field( const std::string& field ) const;
void set_value
( const std::string& section, const std::string& field,
const std::string& val );
void set_value( const std::string& field, const std::string& val );
void add_value
( const std::string& section, const std::string& field,
const std::string& val );
void add_value( const std::string& field, const std::string& val );
void clear_section( const std::string& section );
const_field_iterator
field_begin( const std::string& section, const std::string& field ) const;
const_field_iterator
field_end( const std::string& section, const std::string& field ) const;
const_field_iterator field_begin( const std::string& field ) const;
const_field_iterator field_end( const std::string& field ) const;
const_section_iterator section_begin() const;
const_section_iterator section_end() const;
const_section_iterator section_begin( const std::string& section ) const;
const_section_iterator section_end( const std::string& section ) const;
const_file_iterator file_begin() const;
const_file_iterator file_end() const;
private:
bool get_line( std::istream& is, const syntax_description& syntax,
std::string& line ) const;
bool
process_line( const std::string& line, const syntax_description& syntax,
section_content_ptr& section );
void escape_line( std::istream& is, const syntax_description& syntax,
std::string& line ) const;
void escape_char
( char escaped, const syntax_description& syntax, std::string& str ) const;
void save_section_content
( const section_content& c, std::ostream& os,
const syntax_description& syntax ) const;
private:
section_content m_noname_section;
file_content m_sections;
static const std::string s_unknow_field_value;
}; // class configuration_file
} // namespace claw
#endif // __CLAW_CONFIGURATION_FILE_HPP__
claw::configuration_file::section_end
const_section_iterator section_end() const
Get an iterator past the last field name of a section.
Definition: configuration_file.cpp:377
claw::configuration_file::file_end
const_file_iterator file_end() const
Get an iterator just past the last named section.
Definition: configuration_file.cpp:429
claw::configuration_file::syntax_description::make_section_name
std::string make_section_name(const std::string &name) const
Create a section name from a string.
Definition: configuration_file.cpp:73
claw::configuration_file::syntax_description::paired_symbol
std::pair< char, char > paired_symbol
Two symbols making a pair (like () or []).
Definition: configuration_file.hpp:54
claw::configuration_file::field_begin
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.
Definition: configuration_file.cpp:304
claw::configuration_file::syntax_description::make_comment
std::string make_comment(const std::string &value) const
Create a comment from a string.
Definition: configuration_file.cpp:50
claw::configuration_file::syntax_description::make_assignment
std::string make_assignment(const std::string &key, const std::string &value) const
Make an assignment of a value to a key.
Definition: configuration_file.cpp:62
claw::configuration_file::syntax_description::assignment
char assignment
Symbol used to assign a value to a field.
Definition: configuration_file.hpp:69
claw::wrapped_iterator
This class defines an iterator resulting of the appliance of a function to an effective iterator...
Definition: iterator.hpp:361
claw::const_pair_first
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:92
claw::configuration_file
A class to get the content of a configuration file.
Definition: configuration_file.hpp:46
claw::configuration_file::syntax_description::syntax_description
syntax_description()
Default constructor.
Definition: configuration_file.cpp:38
claw::configuration_file::const_section_iterator
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.
Definition: configuration_file.hpp:99
claw::configuration_file::file_begin
const_file_iterator file_begin() const
Get an iterator on the first named section.
Definition: configuration_file.cpp:419
claw::configuration_file::syntax_description::section_name
paired_symbol section_name
Pair of symbols around a section name.
Definition: configuration_file.hpp:72
claw::configuration_file::open
bool open(std::istream &is, const syntax_description &syntax=syntax_description())
Read the configuration from a stream.
Definition: configuration_file.cpp:113
claw::configuration_file::add_value
void add_value(const std::string &section, const std::string &field, const std::string &val)
Add a value to a field.
Definition: configuration_file.cpp:267
claw::configuration_file::operator()
const std::string & operator()(const std::string &section, const std::string &field) const
Get the value of a field.
Definition: configuration_file.cpp:161
claw::configuration_file::save
void save(std::ostream &os, const syntax_description &syntax=syntax_description())
Write the configuration in a stream.
Definition: configuration_file.cpp:137
claw::configuration_file::clear_section
void clear_section(const std::string &section)
Remove a section and its fields.
Definition: configuration_file.cpp:291
claw::configuration_file::configuration_file
configuration_file()
Default constructor.
Definition: configuration_file.cpp:89
iterator.hpp
Some special kind of iterators. As an example: iterator on the keys of a map.
claw::configuration_file::section_begin
const_section_iterator section_begin() const
Get an iterator on the field names of a section.
Definition: configuration_file.cpp:365
claw::configuration_file::has_field
bool has_field(const std::string &section, const std::string &field) const
Tell if a field exists.
Definition: configuration_file.cpp:203
claw::configuration_file::syntax_description
This class tells us how to parse the input file.
Definition: configuration_file.hpp:50
claw::configuration_file::set_value
void set_value(const std::string &section, const std::string &field, const std::string &val)
Set the value of a field.
Definition: configuration_file.cpp:230
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::configuration_file::field_end
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.
Definition: configuration_file.cpp:322
claw::configuration_file::syntax_description::comment
char comment
Symbol used to comment the rest of the line.
Definition: configuration_file.hpp:66
claw::configuration_file::const_field_iterator
This class is an iterator on the values set for a same field name.
Definition: configuration_file.hpp:104
claw::configuration_file::const_file_iterator
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.
Definition: configuration_file.hpp:92
functional.hpp
Some function object classes.