Chris@16
|
1 // Copyright Vladimir Prus 2002-2004.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt
|
Chris@16
|
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6
|
Chris@16
|
7 #ifndef BOOST_CONFIG_FILE_VP_2003_01_02
|
Chris@16
|
8 #define BOOST_CONFIG_FILE_VP_2003_01_02
|
Chris@16
|
9
|
Chris@16
|
10 #include <iosfwd>
|
Chris@16
|
11 #include <string>
|
Chris@16
|
12 #include <set>
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/noncopyable.hpp>
|
Chris@16
|
15 #include <boost/program_options/config.hpp>
|
Chris@16
|
16 #include <boost/program_options/option.hpp>
|
Chris@16
|
17 #include <boost/program_options/eof_iterator.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/detail/workaround.hpp>
|
Chris@16
|
20 #include <boost/program_options/detail/convert.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
|
Chris@16
|
23 #include <istream> // std::getline
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 #include <boost/static_assert.hpp>
|
Chris@16
|
27 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
28 #include <boost/shared_ptr.hpp>
|
Chris@16
|
29
|
Chris@16
|
30
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost { namespace program_options { namespace detail {
|
Chris@16
|
33
|
Chris@16
|
34 /** Standalone parser for config files in ini-line format.
|
Chris@16
|
35 The parser is a model of single-pass lvalue iterator, and
|
Chris@16
|
36 default constructor creates past-the-end-iterator. The typical usage is:
|
Chris@16
|
37 config_file_iterator i(is, ... set of options ...), e;
|
Chris@16
|
38 for(; i !=e; ++i) {
|
Chris@16
|
39 *i;
|
Chris@16
|
40 }
|
Chris@16
|
41
|
Chris@16
|
42 Syntax conventions:
|
Chris@16
|
43
|
Chris@16
|
44 - config file can not contain positional options
|
Chris@16
|
45 - '#' is comment character: it is ignored together with
|
Chris@16
|
46 the rest of the line.
|
Chris@16
|
47 - variable assignments are in the form
|
Chris@16
|
48 name '=' value.
|
Chris@16
|
49 spaces around '=' are trimmed.
|
Chris@16
|
50 - Section names are given in brackets.
|
Chris@16
|
51
|
Chris@16
|
52 The actual option name is constructed by combining current section
|
Chris@16
|
53 name and specified option name, with dot between. If section_name
|
Chris@16
|
54 already contains dot at the end, new dot is not inserted. For example:
|
Chris@16
|
55 @verbatim
|
Chris@16
|
56 [gui.accessibility]
|
Chris@16
|
57 visual_bell=yes
|
Chris@16
|
58 @endverbatim
|
Chris@16
|
59 will result in option "gui.accessibility.visual_bell" with value
|
Chris@16
|
60 "yes" been returned.
|
Chris@16
|
61
|
Chris@16
|
62 TODO: maybe, we should just accept a pointer to options_description
|
Chris@16
|
63 class.
|
Chris@16
|
64 */
|
Chris@16
|
65 class common_config_file_iterator
|
Chris@16
|
66 : public eof_iterator<common_config_file_iterator, option>
|
Chris@16
|
67 {
|
Chris@16
|
68 public:
|
Chris@16
|
69 common_config_file_iterator() { found_eof(); }
|
Chris@16
|
70 common_config_file_iterator(
|
Chris@16
|
71 const std::set<std::string>& allowed_options,
|
Chris@16
|
72 bool allow_unregistered = false);
|
Chris@16
|
73
|
Chris@16
|
74 virtual ~common_config_file_iterator() {}
|
Chris@16
|
75
|
Chris@16
|
76 public: // Method required by eof_iterator
|
Chris@16
|
77
|
Chris@16
|
78 void get();
|
Chris@16
|
79
|
Chris@16
|
80 protected: // Stubs for derived classes
|
Chris@16
|
81
|
Chris@16
|
82 // Obtains next line from the config file
|
Chris@16
|
83 // Note: really, this design is a bit ugly
|
Chris@16
|
84 // The most clean thing would be to pass 'line_iterator' to
|
Chris@16
|
85 // constructor of this class, but to avoid templating this class
|
Chris@16
|
86 // we'd need polymorphic iterator, which does not exist yet.
|
Chris@16
|
87 virtual bool getline(std::string&) { return false; }
|
Chris@16
|
88
|
Chris@16
|
89 private:
|
Chris@16
|
90 /** Adds another allowed option. If the 'name' ends with
|
Chris@16
|
91 '*', then all options with the same prefix are
|
Chris@16
|
92 allowed. For example, if 'name' is 'foo*', then 'foo1' and
|
Chris@16
|
93 'foo_bar' are allowed. */
|
Chris@16
|
94 void add_option(const char* name);
|
Chris@16
|
95
|
Chris@16
|
96 // Returns true if 's' is a registered option name.
|
Chris@16
|
97 bool allowed_option(const std::string& s) const;
|
Chris@16
|
98
|
Chris@16
|
99 // That's probably too much data for iterator, since
|
Chris@16
|
100 // it will be copied, but let's not bother for now.
|
Chris@16
|
101 std::set<std::string> allowed_options;
|
Chris@16
|
102 // Invariant: no element is prefix of other element.
|
Chris@16
|
103 std::set<std::string> allowed_prefixes;
|
Chris@16
|
104 std::string m_prefix;
|
Chris@16
|
105 bool m_allow_unregistered;
|
Chris@16
|
106 };
|
Chris@16
|
107
|
Chris@16
|
108 template<class charT>
|
Chris@16
|
109 class basic_config_file_iterator : public common_config_file_iterator {
|
Chris@16
|
110 public:
|
Chris@16
|
111 basic_config_file_iterator()
|
Chris@16
|
112 {
|
Chris@16
|
113 found_eof();
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 /** Creates a config file parser for the specified stream.
|
Chris@16
|
117 */
|
Chris@16
|
118 basic_config_file_iterator(std::basic_istream<charT>& is,
|
Chris@16
|
119 const std::set<std::string>& allowed_options,
|
Chris@16
|
120 bool allow_unregistered = false);
|
Chris@16
|
121
|
Chris@16
|
122 private: // base overrides
|
Chris@16
|
123
|
Chris@16
|
124 bool getline(std::string&);
|
Chris@16
|
125
|
Chris@16
|
126 private: // internal data
|
Chris@16
|
127 shared_ptr<std::basic_istream<charT> > is;
|
Chris@16
|
128 };
|
Chris@16
|
129
|
Chris@16
|
130 typedef basic_config_file_iterator<char> config_file_iterator;
|
Chris@16
|
131 typedef basic_config_file_iterator<wchar_t> wconfig_file_iterator;
|
Chris@16
|
132
|
Chris@16
|
133
|
Chris@16
|
134 struct null_deleter
|
Chris@16
|
135 {
|
Chris@16
|
136 void operator()(void const *) const {}
|
Chris@16
|
137 };
|
Chris@16
|
138
|
Chris@16
|
139
|
Chris@16
|
140 template<class charT>
|
Chris@16
|
141 basic_config_file_iterator<charT>::
|
Chris@16
|
142 basic_config_file_iterator(std::basic_istream<charT>& is,
|
Chris@16
|
143 const std::set<std::string>& allowed_options,
|
Chris@16
|
144 bool allow_unregistered)
|
Chris@16
|
145 : common_config_file_iterator(allowed_options, allow_unregistered)
|
Chris@16
|
146 {
|
Chris@16
|
147 this->is.reset(&is, null_deleter());
|
Chris@16
|
148 get();
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 // Specializing this function for wchar_t causes problems on
|
Chris@16
|
152 // borland and vc7, as well as on metrowerks. On the first two
|
Chris@16
|
153 // I don't know a workaround, so make use of 'to_internal' to
|
Chris@16
|
154 // avoid specialization.
|
Chris@16
|
155 template<class charT>
|
Chris@16
|
156 bool
|
Chris@16
|
157 basic_config_file_iterator<charT>::getline(std::string& s)
|
Chris@16
|
158 {
|
Chris@16
|
159 std::basic_string<charT> in;
|
Chris@16
|
160 if (std::getline(*is, in)) {
|
Chris@16
|
161 s = to_internal(in);
|
Chris@16
|
162 return true;
|
Chris@16
|
163 } else {
|
Chris@16
|
164 return false;
|
Chris@16
|
165 }
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 // Specialization is needed to workaround getline bug on Comeau.
|
Chris@16
|
169 #if BOOST_WORKAROUND(__COMO_VERSION__, BOOST_TESTED_AT(4303)) || \
|
Chris@16
|
170 (defined(__sgi) && BOOST_WORKAROUND(_COMPILER_VERSION, BOOST_TESTED_AT(741)))
|
Chris@16
|
171 template<>
|
Chris@16
|
172 bool
|
Chris@16
|
173 basic_config_file_iterator<wchar_t>::getline(std::string& s);
|
Chris@16
|
174 #endif
|
Chris@16
|
175
|
Chris@16
|
176
|
Chris@16
|
177
|
Chris@16
|
178 }}}
|
Chris@16
|
179
|
Chris@16
|
180 #endif
|