1 // Copyright Vladimir Prus 2002-2004.
\r
2 // Distributed under the Boost Software License, Version 1.0.
\r
3 // (See accompanying file LICENSE_1_0.txt
\r
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
7 #ifndef BOOST_CONFIG_FILE_VP_2003_01_02
\r
8 #define BOOST_CONFIG_FILE_VP_2003_01_02
\r
14 #include <boost/noncopyable.hpp>
\r
15 #include <boost/program_options/config.hpp>
\r
16 #include <boost/program_options/option.hpp>
\r
17 #include <boost/program_options/eof_iterator.hpp>
\r
19 #include <boost/detail/workaround.hpp>
\r
20 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202))
\r
21 #include <boost/program_options/detail/convert.hpp>
\r
24 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
\r
25 #include <istream> // std::getline
\r
28 #include <boost/static_assert.hpp>
\r
29 #include <boost/type_traits/is_same.hpp>
\r
30 #include <boost/shared_ptr.hpp>
\r
34 namespace boost { namespace program_options { namespace detail {
\r
36 /** Standalone parser for config files in ini-line format.
\r
37 The parser is a model of single-pass lvalue iterator, and
\r
38 default constructor creates past-the-end-iterator. The typical usage is:
\r
39 config_file_iterator i(is, ... set of options ...), e;
\r
46 - config file can not contain positional options
\r
47 - '#' is comment character: it is ignored together with
\r
48 the rest of the line.
\r
49 - variable assignments are in the form
\r
51 spaces around '=' are trimmed.
\r
52 - Section names are given in brackets.
\r
54 The actual option name is constructed by combining current section
\r
55 name and specified option name, with dot between. If section_name
\r
56 already contains dot at the end, new dot is not inserted. For example:
\r
61 will result in option "gui.accessibility.visual_bell" with value
\r
62 "yes" been returned.
\r
64 TODO: maybe, we should just accept a pointer to options_description
\r
67 class common_config_file_iterator
\r
68 : public eof_iterator<common_config_file_iterator, option>
\r
71 common_config_file_iterator() { found_eof(); }
\r
72 common_config_file_iterator(
\r
73 const std::set<std::string>& allowed_options,
\r
74 bool allow_unregistered = false);
\r
76 virtual ~common_config_file_iterator() {}
\r
78 public: // Method required by eof_iterator
\r
82 protected: // Stubs for derived classes
\r
84 // Obtains next line from the config file
\r
85 // Note: really, this design is a bit ugly
\r
86 // The most clean thing would be to pass 'line_iterator' to
\r
87 // constructor of this class, but to avoid templating this class
\r
88 // we'd need polymorphic iterator, which does not exist yet.
\r
89 virtual bool getline(std::string&) { return false; }
\r
92 /** Adds another allowed option. If the 'name' ends with
\r
93 '*', then all options with the same prefix are
\r
94 allowed. For example, if 'name' is 'foo*', then 'foo1' and
\r
95 'foo_bar' are allowed. */
\r
96 void add_option(const char* name);
\r
98 // Returns true if 's' is a registered option name.
\r
99 bool allowed_option(const std::string& s) const;
\r
101 // That's probably too much data for iterator, since
\r
102 // it will be copied, but let's not bother for now.
\r
103 std::set<std::string> allowed_options;
\r
104 // Invariant: no element is prefix of other element.
\r
105 std::set<std::string> allowed_prefixes;
\r
106 std::string m_prefix;
\r
107 bool m_allow_unregistered;
\r
110 template<class charT>
\r
111 class basic_config_file_iterator : public common_config_file_iterator {
\r
113 basic_config_file_iterator()
\r
118 /** Creates a config file parser for the specified stream.
\r
120 basic_config_file_iterator(std::basic_istream<charT>& is,
\r
121 const std::set<std::string>& allowed_options,
\r
122 bool allow_unregistered = false);
\r
124 private: // base overrides
\r
126 bool getline(std::string&);
\r
128 private: // internal data
\r
129 shared_ptr<std::basic_istream<charT> > is;
\r
132 typedef basic_config_file_iterator<char> config_file_iterator;
\r
133 typedef basic_config_file_iterator<wchar_t> wconfig_file_iterator;
\r
136 struct null_deleter
\r
138 void operator()(void const *) const {}
\r
142 template<class charT>
\r
143 basic_config_file_iterator<charT>::
\r
144 basic_config_file_iterator(std::basic_istream<charT>& is,
\r
145 const std::set<std::string>& allowed_options,
\r
146 bool allow_unregistered)
\r
147 : common_config_file_iterator(allowed_options, allow_unregistered)
\r
149 this->is.reset(&is, null_deleter());
\r
153 // Specializing this function for wchar_t causes problems on
\r
154 // borland and vc7, as well as on metrowerks. On the first two
\r
155 // I don't know a workaround, so make use of 'to_internal' to
\r
156 // avoid specialization.
\r
157 template<class charT>
\r
159 basic_config_file_iterator<charT>::getline(std::string& s)
\r
161 std::basic_string<charT> in;
\r
162 if (std::getline(*is, in)) {
\r
163 s = to_internal(in);
\r
170 // Specialization is needed to workaround getline bug on Comeau.
\r
171 #if BOOST_WORKAROUND(__COMO_VERSION__, BOOST_TESTED_AT(4303)) || \
\r
172 (defined(__sgi) && BOOST_WORKAROUND(_COMPILER_VERSION, BOOST_TESTED_AT(741)))
\r
175 basic_config_file_iterator<wchar_t>::getline(std::string& s);
\r