1 // Copyright Vladimir Prus 2002-2004.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_CONFIG_FILE_VP_2003_01_02
8 #define BOOST_CONFIG_FILE_VP_2003_01_02
14 #include <boost/noncopyable.hpp>
15 #include <boost/program_options/config.hpp>
16 #include <boost/program_options/option.hpp>
17 #include <boost/program_options/eof_iterator.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/program_options/detail/convert.hpp>
22 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
23 #include <istream> // std::getline
26 #include <boost/static_assert.hpp>
27 #include <boost/type_traits/is_same.hpp>
28 #include <boost/shared_ptr.hpp>
31 # pragma warning(push)
32 # pragma warning(disable: 4251) // class XYZ needs to have dll-interface to be used by clients of class XYZ
37 namespace boost { namespace program_options { namespace detail {
39 /** Standalone parser for config files in ini-line format.
40 The parser is a model of single-pass lvalue iterator, and
41 default constructor creates past-the-end-iterator. The typical usage is:
42 config_file_iterator i(is, ... set of options ...), e;
49 - config file can not contain positional options
50 - '#' is comment character: it is ignored together with
52 - variable assignments are in the form
54 spaces around '=' are trimmed.
55 - Section names are given in brackets.
57 The actual option name is constructed by combining current section
58 name and specified option name, with dot between. If section_name
59 already contains dot at the end, new dot is not inserted. For example:
64 will result in option "gui.accessibility.visual_bell" with value
67 TODO: maybe, we should just accept a pointer to options_description
70 class BOOST_PROGRAM_OPTIONS_DECL common_config_file_iterator
71 : public eof_iterator<common_config_file_iterator, option>
74 common_config_file_iterator() { found_eof(); }
75 common_config_file_iterator(
76 const std::set<std::string>& allowed_options,
77 bool allow_unregistered = false);
79 virtual ~common_config_file_iterator() {}
81 public: // Method required by eof_iterator
85 #if BOOST_WORKAROUND(_MSC_VER, <= 1900)
87 void advance(difference_type) {}
90 protected: // Stubs for derived classes
92 // Obtains next line from the config file
93 // Note: really, this design is a bit ugly
94 // The most clean thing would be to pass 'line_iterator' to
95 // constructor of this class, but to avoid templating this class
96 // we'd need polymorphic iterator, which does not exist yet.
97 virtual bool getline(std::string&) { return false; }
100 /** Adds another allowed option. If the 'name' ends with
101 '*', then all options with the same prefix are
102 allowed. For example, if 'name' is 'foo*', then 'foo1' and
103 'foo_bar' are allowed. */
104 void add_option(const char* name);
106 // Returns true if 's' is a registered option name.
107 bool allowed_option(const std::string& s) const;
109 // That's probably too much data for iterator, since
110 // it will be copied, but let's not bother for now.
111 std::set<std::string> allowed_options;
112 // Invariant: no element is prefix of other element.
113 std::set<std::string> allowed_prefixes;
114 std::string m_prefix;
115 bool m_allow_unregistered;
118 template<class charT>
119 class basic_config_file_iterator : public common_config_file_iterator {
121 basic_config_file_iterator()
126 /** Creates a config file parser for the specified stream.
128 basic_config_file_iterator(std::basic_istream<charT>& is,
129 const std::set<std::string>& allowed_options,
130 bool allow_unregistered = false);
132 private: // base overrides
134 bool getline(std::string&);
136 private: // internal data
137 shared_ptr<std::basic_istream<charT> > is;
140 typedef basic_config_file_iterator<char> config_file_iterator;
141 typedef basic_config_file_iterator<wchar_t> wconfig_file_iterator;
146 void operator()(void const *) const {}
150 template<class charT>
151 basic_config_file_iterator<charT>::
152 basic_config_file_iterator(std::basic_istream<charT>& is,
153 const std::set<std::string>& allowed_options,
154 bool allow_unregistered)
155 : common_config_file_iterator(allowed_options, allow_unregistered)
157 this->is.reset(&is, null_deleter());
161 // Specializing this function for wchar_t causes problems on
162 // borland and vc7, as well as on metrowerks. On the first two
163 // I don't know a workaround, so make use of 'to_internal' to
164 // avoid specialization.
165 template<class charT>
167 basic_config_file_iterator<charT>::getline(std::string& s)
169 std::basic_string<charT> in;
170 if (std::getline(*is, in)) {
178 // Specialization is needed to workaround getline bug on Comeau.
179 #if BOOST_WORKAROUND(__COMO_VERSION__, BOOST_TESTED_AT(4303)) || \
180 (defined(__sgi) && BOOST_WORKAROUND(_COMPILER_VERSION, BOOST_TESTED_AT(741)))
183 basic_config_file_iterator<wchar_t>::getline(std::string& s);
191 # pragma warning(pop)