claw 1.9.0
 
Loading...
Searching...
No Matches
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@stuff-o-matic.com
24*/
30#ifndef __CLAW_CONFIGURATION_FILE_HPP__
31#define __CLAW_CONFIGURATION_FILE_HPP__
32
33#include <claw/functional.hpp>
34#include <claw/iterator.hpp>
35
36#include <iostream>
37#include <map>
38#include <string>
39
40namespace 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(const std::string& key,
61 const std::string& value) const;
62 std::string make_section_name(const std::string& name) const;
63
64 public:
66 char comment;
67
70
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, file_content::const_iterator,
92
95 const section_content::key_type, section_content::const_iterator,
98
103 class const_field_iterator
104 {
105 private:
107 typedef section_content::const_iterator wrapped_iterator_type;
108
109 public:
110 typedef std::string value_type;
111 typedef const value_type& reference;
112 typedef const value_type* pointer;
113 typedef wrapped_iterator_type::difference_type difference_type;
114
115 typedef wrapped_iterator_type::iterator_category iterator_category;
116
117 public:
118 const_field_iterator()
119 {}
120 const_field_iterator(wrapped_iterator_type it)
121 : m_iterator(it)
122 {}
123
124 bool operator==(const const_field_iterator& that) const
125 {
126 return m_iterator == that.m_iterator;
127 }
128
129 bool operator!=(const const_field_iterator& that) const
130 {
131 return m_iterator != that.m_iterator;
132 }
133
134 const_field_iterator& operator++()
135 {
136 ++m_iterator;
137 return *this;
138 }
139
140 const_field_iterator operator++(int)
141 {
142 const_field_iterator tmp(*this);
143 ++m_iterator;
144 return tmp;
145 }
146
147 const_field_iterator& operator--()
148 {
149 --m_iterator;
150 return *this;
151 }
152
153 const_field_iterator operator--(int)
154 {
155 const_field_iterator tmp(*this);
156 --m_iterator;
157 return tmp;
158 }
159
160 reference operator*() const
161 {
162 return m_iterator->second;
163 }
164
165 pointer operator->() const
166 {
167 return &m_iterator->second;
168 }
169
170 private:
172 wrapped_iterator_type m_iterator;
173
174 }; // class const_field_iterator
175
176 public:
178 configuration_file(std::istream& is, const syntax_description& syntax
180
181 bool open(std::istream& is,
182 const syntax_description& syntax = syntax_description());
183 void save(std::ostream& os,
184 const syntax_description& syntax = syntax_description());
185
186 const std::string& operator()(const std::string& section,
187 const std::string& field) const;
188
189 const std::string& operator()(const std::string& field) const;
190
191 bool has_field(const std::string& section, const std::string& field) const;
192 bool has_field(const std::string& field) const;
193
194 void set_value(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(const std::string& section, const std::string& field,
199 const std::string& val);
200 void add_value(const std::string& field, const std::string& val);
201
202 void clear_section(const std::string& section);
203
204 const_field_iterator field_begin(const std::string& section,
205 const std::string& field) const;
206 const_field_iterator field_end(const std::string& section,
207 const std::string& field) const;
208
209 const_field_iterator field_begin(const std::string& field) const;
210 const_field_iterator field_end(const std::string& field) const;
211
214
215 const_section_iterator section_begin(const std::string& section) const;
216 const_section_iterator section_end(const std::string& section) const;
217
220
221 private:
222 bool get_line(std::istream& is, const syntax_description& syntax,
223 std::string& line) const;
224 bool process_line(const std::string& line,
225 const syntax_description& syntax,
226 section_content_ptr& section);
227
228 void escape_line(std::istream& is, const syntax_description& syntax,
229 std::string& line) const;
230
231 void escape_char(char escaped, const syntax_description& syntax,
232 std::string& str) const;
233
234 void save_section_content(const section_content& c, std::ostream& os,
235 const syntax_description& syntax) const;
236
237 private:
239 section_content m_noname_section;
240
242 file_content m_sections;
243
245 static const std::string s_unknow_field_value;
246
247 }; // class configuration_file
248}
249
250#endif // __CLAW_CONFIGURATION_FILE_HPP__
This class is an iterator on the values set for a same field name.
const_section_iterator section_begin() const
Get an iterator on the field names of a section.
void add_value(const std::string &section, const std::string &field, const std::string &val)
Add a value to a field.
const_file_iterator file_begin() const
Get an iterator on the first named section.
void set_value(const std::string &section, const std::string &field, const std::string &val)
Set the value of a field.
void clear_section(const std::string &section)
Remove a section and its fields.
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.
void save(std::ostream &os, const syntax_description &syntax=syntax_description())
Write the configuration in a stream.
const_section_iterator section_end() const
Get an iterator past the last field name of a section.
claw::wrapped_iterator< constfile_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.
bool has_field(const std::string &section, const std::string &field) const
Tell if a field exists.
claw::wrapped_iterator< constsection_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.
configuration_file()
Default 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_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_file_iterator file_end() const
Get an iterator just past the last named section.
Fuction object to get the first element of a std::pair.
This class defines an iterator resulting of the appliance of a function to an effective iterator.
Definition iterator.hpp:450
Some function object classes.
Some special kind of iterators. As an example: iterator on the keys of a map.
This is the main namespace.
This class tells us how to parse the input file.
std::string make_comment(const std::string &value) const
Create a comment 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::string make_section_name(const std::string &name) const
Create a section name from a string.
std::pair< char, char > paired_symbol
Two symbols making a pair (like () or []).
paired_symbol section_name
Pair of symbols around a section name.
char comment
Symbol used to comment the rest of the line.
char assignment
Symbol used to assign a value to a field.