drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
config_file.h
1/*
2 * Copyright (C) 2002-2004 Vladimir Prus.
3 * Copyright (C) 2010 Monty Taylor
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE_1_0.txt or copy at
7 * http://www.boost.org/LICENSE_1_0.txt)
8 */
9
10#pragma once
11
12#include <boost/program_options.hpp>
13#include <boost/program_options/eof_iterator.hpp>
14#include <boost/type_traits/is_same.hpp>
15#include <memory>
16#include <boost/algorithm/string.hpp>
17
18#include <boost/noncopyable.hpp>
19
20#include <iosfwd>
21#include <vector>
22#include <utility>
23#include <set>
24
25namespace drizzled {
26namespace program_options {
27
28typedef std::pair<std::string, std::string> option_result_pair;
29std::string parse_suffix(const std::string& arg_val);
30option_result_pair parse_size_suffixes(std::string s);
31option_result_pair parse_size_arg(std::string s);
32
33std::string parse_suffix(const std::string& arg_val)
34{
35 try
36 {
37 size_t size_suffix_pos= arg_val.find_last_of("kmgKMG");
38 if (size_suffix_pos == arg_val.size()-1)
39 {
40 char suffix= arg_val[size_suffix_pos];
41 std::string size_val(arg_val.substr(0, size_suffix_pos));
42
43 uint64_t base_size= boost::lexical_cast<uint64_t>(size_val);
44 uint64_t new_size= 0;
45
46 switch (suffix)
47 {
48 case 'K':
49 case 'k':
50 new_size= base_size * 1024;
51 break;
52 case 'M':
53 case 'm':
54 new_size= base_size * 1024 * 1024;
55 break;
56 case 'G':
57 case 'g':
58 new_size= base_size * 1024 * 1024 * 1024;
59 break;
60 }
61 return boost::lexical_cast<std::string>(new_size);
62 }
63 }
64 catch (std::exception&)
65 { }
66
67 return arg_val;
68}
69
70option_result_pair parse_size_suffixes(std::string s)
71{
72 size_t equal_pos= s.find("=");
73 if (equal_pos != std::string::npos)
74 {
75 std::string arg_key(s.substr(0, equal_pos));
76 std::string arg_val(parse_suffix(s.substr(equal_pos+1)));
77
78 if (arg_val != s.substr(equal_pos+1))
79 {
80 return std::make_pair(arg_key, arg_val);
81 }
82 }
83
84 return std::make_pair(std::string(""), std::string(""));
85}
86
87option_result_pair parse_size_arg(std::string s)
88{
89 if (s.find("--") == 0)
90 {
91 return parse_size_suffixes(s.substr(2));
92 }
93 return make_pair(std::string(""), std::string(""));
94}
95
96class invalid_syntax :
97 public boost::program_options::error
98{
99public:
100 enum kind_t
101 {
102 long_not_allowed = 30,
103 long_adjacent_not_allowed,
104 short_adjacent_not_allowed,
105 empty_adjacent_parameter,
106 missing_parameter,
107 extra_parameter,
108 unrecognized_line
109 };
110
111 invalid_syntax(const std::string& in_tokens, kind_t in_kind);
112
113
114 ~invalid_syntax() noexcept {}
115
116 kind_t kind() const
117 {
118 return m_kind;
119 }
120
121
122 const std::string& tokens() const
123 {
124 return m_tokens;
125 }
126
127
128protected:
130 static std::string error_message(kind_t kind)
131 {
132 // Initially, store the message in 'const char*' variable, to avoid
133 // conversion to string in all cases.
134 const char* msg;
135 switch(kind)
136 {
137 case long_not_allowed:
138 msg = "long options are not allowed";
139 break;
140 case long_adjacent_not_allowed:
141 msg = "parameters adjacent to long options not allowed";
142 break;
143 case short_adjacent_not_allowed:
144 msg = "parameters adjust to short options are not allowed";
145 break;
146 case empty_adjacent_parameter:
147 msg = "adjacent parameter is empty";
148 break;
149 case missing_parameter:
150 msg = "required parameter is missing";
151 break;
152 case extra_parameter:
153 msg = "extra parameter";
154 break;
155 case unrecognized_line:
156 msg = "unrecognized line";
157 break;
158 default:
159 msg = "unknown error";
160 }
161 return msg;
162 }
163
164private:
165 // TODO: copy ctor might throw
166 std::string m_tokens;
167
168 kind_t m_kind;
169};
170
171invalid_syntax::invalid_syntax(const std::string& in_tokens,
172 invalid_syntax::kind_t in_kind) :
173 boost::program_options::error(error_message(in_kind).append(" in '").append(in_tokens).append("'")),
174 m_tokens(in_tokens),
175 m_kind(in_kind)
176{ }
177
178namespace detail
179{
180
210class common_config_file_iterator :
211 public boost::eof_iterator<common_config_file_iterator,
212 boost::program_options::option>
213{
214public:
215 common_config_file_iterator()
216 {
217 found_eof();
218 }
219
220 common_config_file_iterator(const std::set<std::string>& in_allowed_options,
221 bool allow_unregistered) :
222 allowed_options(in_allowed_options),
223 m_allow_unregistered(allow_unregistered)
224 {
225 for(std::set<std::string>::const_iterator i = allowed_options.begin();
226 i != allowed_options.end();
227 ++i)
228 {
229 add_option(i->c_str());
230 }
231 }
232
233 virtual ~common_config_file_iterator() {}
234
235public: // Method required by eof_iterator
236
237 void get()
238 {
239 std::string s;
240 std::string::size_type n;
241 bool found = false;
242
243 while(this->getline(s)) {
244
245 // strip '#' comments and whitespace
246 if ((n = s.find('#')) != std::string::npos)
247 s = s.substr(0, n);
248 boost::trim(s);
249
250 if (!s.empty()) {
251 // Handle section name
252 if (*s.begin() == '[' && *s.rbegin() == ']')
253 {
254 m_prefix = s.substr(1, s.size()-2);
255 if (*m_prefix.rbegin() != '.')
256 m_prefix += '.';
257 }
258 else
259 {
260
261 std::string name;
262 std::string option_value("true");
263
264 if ((n = s.find('=')) != std::string::npos)
265 {
266
267 name = m_prefix + boost::trim_copy(s.substr(0, n));
268 option_value = boost::trim_copy(parse_suffix(s.substr(n+1)));
269
270 }
271 else
272 {
273 name = m_prefix + boost::trim_copy(s);
274 }
275
276 bool registered = allowed_option(name);
277 if (!registered && !m_allow_unregistered)
278 boost::throw_exception(boost::program_options::unknown_option(name));
279
280 found = true;
281 this->value().string_key = name;
282 this->value().value.clear();
283 this->value().value.push_back(option_value);
284 this->value().unregistered = !registered;
285 this->value().original_tokens.clear();
286 this->value().original_tokens.push_back(name);
287 this->value().original_tokens.push_back(option_value);
288 break;
289
290 }
291 }
292 }
293 if (!found)
294 found_eof();
295 }
296
297protected: // Stubs for derived classes
298
299 // Obtains next line from the config file
300 // Note: really, this design is a bit ugly
301 // The most clean thing would be to pass 'line_iterator' to
302 // constructor of this class, but to avoid templating this class
303 // we'd need polymorphic iterator, which does not exist yet.
304 virtual bool getline(std::string&) { return false; }
305
306private:
311 void add_option(const char* name)
312 {
313 std::string s(name);
314 assert(!s.empty());
315 if (*s.rbegin() == '*')
316 {
317 s.resize(s.size()-1);
318 bool bad_prefixes(false);
319 // If 's' is a prefix of one of allowed suffix, then
320 // lower_bound will return that element.
321 // If some element is prefix of 's', then lower_bound will
322 // return the next element.
323 std::set<std::string>::iterator i = allowed_prefixes.lower_bound(s);
324 if (i != allowed_prefixes.end())
325 {
326 if (i->find(s) == 0)
327 bad_prefixes = true;
328 }
329 if (i != allowed_prefixes.begin())
330 {
331 --i;
332 if (s.find(*i) == 0)
333 bad_prefixes = true;
334 }
335 if (bad_prefixes)
336 boost::throw_exception(boost::program_options::error("bad prefixes"));
337 allowed_prefixes.insert(s);
338 }
339 }
340
341
342 // Returns true if 's' is a registered option name.
343 bool allowed_option(const std::string& s) const
344 {
345 std::set<std::string>::const_iterator i = allowed_options.find(s);
346 if (i != allowed_options.end())
347 return true;
348 // If s is "pa" where "p" is allowed prefix then
349 // lower_bound should find the element after "p".
350 // This depends on 'allowed_prefixes' invariant.
351 i = allowed_prefixes.lower_bound(s);
352 if (i != allowed_prefixes.begin() && s.find(*--i) == 0)
353 return true;
354 return false;
355 }
356
357
358 // That's probably too much data for iterator, since
359 // it will be copied, but let's not bother for now.
360 std::set<std::string> allowed_options;
361 // Invariant: no element is prefix of other element.
362 std::set<std::string> allowed_prefixes;
363 std::string m_prefix;
364 bool m_allow_unregistered;
365};
366
367template<class charT>
368class basic_config_file_iterator :
369 public common_config_file_iterator
370{
371public:
372
373 basic_config_file_iterator()
374 {
375 found_eof();
376 }
377
379 basic_config_file_iterator(std::basic_istream<charT>& is,
380 const std::set<std::string>& allowed_options,
381 bool allow_unregistered = false);
382
383private: // base overrides
384
385 bool getline(std::string&);
386
387private: // internal data
388 std::shared_ptr<std::basic_istream<charT> > is;
389};
390
391typedef basic_config_file_iterator<char> config_file_iterator;
392typedef basic_config_file_iterator<wchar_t> wconfig_file_iterator;
393
395{
396 void operator()(void const *) const {}
397};
398
399
400template<class charT>
401basic_config_file_iterator<charT>::
402basic_config_file_iterator(std::basic_istream<charT>& in_is,
403 const std::set<std::string>& in_allowed_options,
404 bool in_allow_unregistered) :
405 common_config_file_iterator(in_allowed_options, in_allow_unregistered)
406{
407 this->is.reset(&in_is, null_deleter());
408 get();
409}
410
411
412// Specializing this function for wchar_t causes problems on
413// borland and vc7, as well as on metrowerks. On the first two
414// I don't know a workaround, so make use of 'to_internal' to
415// avoid specialization.
416template<class charT>
417bool
418basic_config_file_iterator<charT>::getline(std::string& s)
419{
420 if (std::getline(*is, s))
421 {
422 return true;
423 }
424 else
425 {
426 return false;
427 }
428}
429
430} /* namespace detail */
431
436template<class charT>
437boost::program_options::basic_parsed_options<charT>
438parse_config_file(std::basic_istream<charT>& is,
439 const boost::program_options::options_description& desc,
440 bool allow_unregistered = false)
441{
442 std::set<std::string> allowed_options;
443
444 const std::vector<boost::shared_ptr<boost::program_options::option_description> >& options = desc.options();
445 for (unsigned i = 0; i < options.size(); ++i)
446 {
447 const boost::program_options::option_description& d= *options[i];
448
449 if (d.long_name().empty())
450 boost::throw_exception(boost::program_options::error("long name required for config file"));
451
452 allowed_options.insert(d.long_name());
453 }
454
455 // Parser return char strings
456 boost::program_options::parsed_options result(&desc);
458 allowed_options,
459 allow_unregistered),
461 std::back_inserter(result.options));
462 // Convert char strings into desired type.
463 return boost::program_options::basic_parsed_options<charT>(result);
464}
465
471template<class charT>
472boost::program_options::basic_parsed_options<charT>
473parse_config_file(const char* filename,
474 const boost::program_options::options_description& desc,
475 bool allow_unregistered = false)
476{
477 // Parser return char strings
478 std::basic_ifstream< charT > strm(filename);
479 if (!strm)
480 {
481 boost::throw_exception("Couldn't open file");
482 }
483 return parse_config_file(strm, desc, allow_unregistered);
484}
485
486} /* namespace program_options */
487} /* namespace drizzled */
basic_config_file_iterator(std::basic_istream< charT > &is, const std::set< std::string > &allowed_options, bool allow_unregistered=false)
static std::string error_message(kind_t kind)
TODO: Rename this file - func.h is stupid.