Claw  1.7.3
configuration_file.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_CONFIGURATION_FILE_HPP__
31 #define __CLAW_CONFIGURATION_FILE_HPP__
32 
33 #include <claw/iterator.hpp>
34 #include <claw/functional.hpp>
35 
36 #include <iostream>
37 #include <map>
38 #include <string>
39 
40 namespace claw
41 {
47  {
48  public:
51  {
52  public:
54  typedef std::pair<char, char> paired_symbol;
55 
56  public:
58 
59  std::string make_comment( const std::string& value ) const;
60  std::string make_assignment
61  ( const std::string& key, const std::string& value ) const;
62  std::string make_section_name( const std::string& name ) const;
63 
64  public:
66  char comment;
67 
69  char assignment;
70 
72  paired_symbol section_name;
73 
74  }; // struct syntax_descritpion
75 
76  private:
78  typedef std::multimap<std::string, std::string> section_content;
79 
81  typedef std::map<std::string, section_content> file_content;
82 
84  typedef section_content* section_content_ptr;
85 
86  public:
89  < const file_content::key_type,
90  file_content::const_iterator,
92  >::iterator_type const_file_iterator;
93 
96  < const section_content::key_type,
97  section_content::const_iterator,
99  >::iterator_type const_section_iterator;
100 
105  {
106  private:
108  typedef section_content::const_iterator wrapped_iterator_type;
109 
110  public:
111  typedef std::string value_type;
112  typedef const value_type& reference;
113  typedef const value_type* pointer;
114  typedef wrapped_iterator_type::difference_type difference_type;
115 
116  typedef wrapped_iterator_type::iterator_category iterator_category;
117 
118  public:
120  const_field_iterator( wrapped_iterator_type it ) : m_iterator(it) {}
121 
122  bool operator==( const const_field_iterator& that ) const
123  {
124  return m_iterator == that.m_iterator;
125  } // operator==()
126 
127  bool operator!=( const const_field_iterator& that ) const
128  {
129  return m_iterator != that.m_iterator;
130  } // operator!=()
131 
132  const_field_iterator& operator++()
133  {
134  ++m_iterator;
135  return *this;
136  } // operator++()
137 
138  const_field_iterator operator++(int)
139  {
140  const_field_iterator tmp(*this);
141  ++m_iterator;
142  return tmp;
143  } // operator++() [post]
144 
145  const_field_iterator& operator--()
146  {
147  --m_iterator;
148  return *this;
149  } // operator--()
150 
151  const_field_iterator operator--(int)
152  {
153  const_field_iterator tmp(*this);
154  --m_iterator;
155  return tmp;
156  } // operator--() [post]
157 
158  reference operator*() const
159  {
160  return m_iterator->second;
161  } // operator*()
162 
163  pointer operator->() const
164  {
165  return &m_iterator->second;
166  } // operator->()
167 
168  private:
170  wrapped_iterator_type m_iterator;
171 
172  }; // class const_field_iterator
173 
174  public:
177  (std::istream& is, const syntax_description& syntax = syntax_description());
178 
179  bool open
180  (std::istream& is, const syntax_description& syntax = syntax_description());
181  void save
182  (std::ostream& os, const syntax_description& syntax = syntax_description());
183 
184  const std::string&
185  operator()( const std::string& section, const std::string& field ) const;
186 
187  const std::string& operator()( const std::string& field ) const;
188 
189  bool has_field
190  ( const std::string& section, const std::string& field ) const;
191  bool has_field( const std::string& field ) const;
192 
193  void set_value
194  ( const std::string& section, const std::string& field,
195  const std::string& val );
196  void set_value( const std::string& field, const std::string& val );
197 
198  void add_value
199  ( const std::string& section, const std::string& field,
200  const std::string& val );
201  void add_value( const std::string& field, const std::string& val );
202 
203  void clear_section( const std::string& section );
204 
206  field_begin( const std::string& section, const std::string& field ) const;
208  field_end( const std::string& section, const std::string& field ) const;
209 
210  const_field_iterator field_begin( const std::string& field ) const;
211  const_field_iterator field_end( const std::string& field ) const;
212 
213  const_section_iterator section_begin() const;
214  const_section_iterator section_end() const;
215 
216  const_section_iterator section_begin( const std::string& section ) const;
217  const_section_iterator section_end( const std::string& section ) const;
218 
219  const_file_iterator file_begin() const;
220  const_file_iterator file_end() const;
221 
222  private:
223  bool get_line( std::istream& is, const syntax_description& syntax,
224  std::string& line ) const;
225  bool
226  process_line( const std::string& line, const syntax_description& syntax,
227  section_content_ptr& section );
228 
229  void escape_line( std::istream& is, const syntax_description& syntax,
230  std::string& line ) const;
231 
232  void escape_char
233  ( char escaped, const syntax_description& syntax, std::string& str ) const;
234 
235  void save_section_content
236  ( const section_content& c, std::ostream& os,
237  const syntax_description& syntax ) const;
238 
239  private:
241  section_content m_noname_section;
242 
244  file_content m_sections;
245 
247  static const std::string s_unknow_field_value;
248 
249  }; // class configuration_file
250 } // namespace claw
251 
252 #endif // __CLAW_CONFIGURATION_FILE_HPP__
std::string make_section_name(const std::string &name) const
Create a section name 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::pair< char, char > paired_symbol
Two symbols making a pair (like () or []).
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_file_iterator file_begin() const
Get an iterator on the first named section.
std::string make_comment(const std::string &value) const
Create a comment from a string.
char assignment
Symbol used to assign a value to a field.
This class defines an iterator resulting of the appliance of a function to an effective iterator...
Definition: iterator.hpp:361
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:92
A class to get the content of a configuration file.
bool has_field(const std::string &section, const std::string &field) const
Tell if a field exists.
const_file_iterator file_end() const
Get an iterator just past the last named section.
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.
const_section_iterator section_end() const
Get an iterator past the last field name of a section.
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_section_iterator section_begin() const
Get an iterator on the field names of a section.
paired_symbol section_name
Pair of symbols around a section name.
bool open(std::istream &is, const syntax_description &syntax=syntax_description())
Read the configuration from a stream.
void add_value(const std::string &section, const std::string &field, const std::string &val)
Add a value to a field.
void save(std::ostream &os, const syntax_description &syntax=syntax_description())
Write the configuration in a stream.
void clear_section(const std::string &section)
Remove a section and its fields.
configuration_file()
Default constructor.
Some special kind of iterators. As an example: iterator on the keys of a map.
This class tells us how to parse the input file.
void set_value(const std::string &section, const std::string &field, const std::string &val)
Set the value of a field.
This is the main namespace.
Definition: algorithm.hpp:33
const std::string & operator()(const std::string &section, const std::string &field) const
Get the value of a field.
char comment
Symbol used to comment the rest of the line.
This class is an iterator on the values set for a same field name.
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.
Some function object classes.