]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/program_options/detail/config_file.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
218660202235323d062fff80035dc72a819ca1e2
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / program_options / detail / config_file.hpp
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
5 \r
6 \r
7 #ifndef BOOST_CONFIG_FILE_VP_2003_01_02\r
8 #define BOOST_CONFIG_FILE_VP_2003_01_02\r
9 \r
10 #include <iosfwd>\r
11 #include <string>\r
12 #include <set>\r
13 \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
18 \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
22 #endif\r
23 \r
24 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))\r
25 #include <istream> // std::getline\r
26 #endif\r
27 \r
28 #include <boost/static_assert.hpp>\r
29 #include <boost/type_traits/is_same.hpp>\r
30 #include <boost/shared_ptr.hpp>\r
31 \r
32 \r
33 \r
34 namespace boost { namespace program_options { namespace detail {\r
35 \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
40         for(; i !=e; ++i) {\r
41             *i;\r
42         }\r
43         \r
44         Syntax conventions:\r
45 \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
50           name '=' value.\r
51           spaces around '=' are trimmed.\r
52         - Section names are given in brackets. \r
53 \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
57          @verbatim\r
58          [gui.accessibility]\r
59          visual_bell=yes\r
60          @endverbatim\r
61          will result in option "gui.accessibility.visual_bell" with value\r
62          "yes" been returned.\r
63 \r
64          TODO: maybe, we should just accept a pointer to options_description\r
65          class.\r
66      */    \r
67     class common_config_file_iterator \r
68         : public eof_iterator<common_config_file_iterator, option>\r
69     {\r
70     public:\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
75 \r
76         virtual ~common_config_file_iterator() {}\r
77 \r
78     public: // Method required by eof_iterator\r
79         \r
80         void get();\r
81         \r
82     protected: // Stubs for derived classes\r
83 \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
90         \r
91     private:\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
97 \r
98         // Returns true if 's' is a registered option name.\r
99         bool allowed_option(const std::string& s) const; \r
100 \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
108     };\r
109 \r
110     template<class charT>\r
111     class basic_config_file_iterator : public common_config_file_iterator {\r
112     public:\r
113         basic_config_file_iterator()\r
114         {\r
115             found_eof();\r
116         }\r
117 \r
118         /** Creates a config file parser for the specified stream.            \r
119         */\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
123 \r
124     private: // base overrides\r
125 \r
126         bool getline(std::string&);\r
127 \r
128     private: // internal data\r
129         shared_ptr<std::basic_istream<charT> > is;\r
130     };\r
131     \r
132     typedef basic_config_file_iterator<char> config_file_iterator;\r
133     typedef basic_config_file_iterator<wchar_t> wconfig_file_iterator;\r
134 \r
135 \r
136     struct null_deleter\r
137     {\r
138         void operator()(void const *) const {}\r
139     };\r
140 \r
141 \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
148     {\r
149         this->is.reset(&is, null_deleter());                 \r
150         get();\r
151     }\r
152 \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
158     bool\r
159     basic_config_file_iterator<charT>::getline(std::string& s)\r
160     {\r
161         std::basic_string<charT> in;\r
162         if (std::getline(*is, in)) {\r
163             s = to_internal(in);\r
164             return true;\r
165         } else {\r
166             return false;\r
167         }\r
168     }\r
169 \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
173     template<>\r
174     bool\r
175     basic_config_file_iterator<wchar_t>::getline(std::string& s);\r
176 #endif\r
177 \r
178     \r
179 \r
180 }}}\r
181 \r
182 #endif\r