2
2
* Copyright (c) 2002-2004 Vladimir Prus.
3
* Copyright (c) 2010 Monty Taylor
4
5
* Distributed under the Boost Software License, Version 1.0.
5
6
* (See accompanying file LICENSE_1_0.txt or copy at
28
29
namespace program_options
32
typedef std::pair<std::string, std::string> option_result_pair;
33
std::string parse_suffix(const std::string& arg_val);
34
option_result_pair parse_size_suffixes(std::string s);
35
option_result_pair parse_size_arg(std::string s);
37
std::string parse_suffix(const std::string& arg_val)
41
size_t size_suffix_pos= arg_val.find_last_of("kmgKMG");
42
if (size_suffix_pos == arg_val.size()-1)
44
char suffix= arg_val[size_suffix_pos];
45
std::string size_val(arg_val.substr(0, size_suffix_pos));
47
uint64_t base_size= boost::lexical_cast<uint64_t>(size_val);
54
new_size= base_size * 1024;
58
new_size= base_size * 1024 * 1024;
62
new_size= base_size * 1024 * 1024 * 1024;
65
return boost::lexical_cast<std::string>(new_size);
74
option_result_pair parse_size_suffixes(std::string s)
76
size_t equal_pos= s.find("=");
77
if (equal_pos != std::string::npos)
79
std::string arg_key(s.substr(0, equal_pos));
80
std::string arg_val(parse_suffix(s.substr(equal_pos+1)));
82
if (arg_val != s.substr(equal_pos+1))
84
return std::make_pair(arg_key, arg_val);
88
return std::make_pair(std::string(""), std::string(""));
91
option_result_pair parse_size_arg(std::string s)
93
if (s.find("--") == 0)
95
return parse_size_suffixes(s.substr(2));
97
return make_pair(std::string(""), std::string(""));
31
100
class invalid_syntax :
32
101
public boost::program_options::error
36
106
long_not_allowed = 30,
37
107
long_adjacent_not_allowed,
38
108
short_adjacent_not_allowed,
107
177
invalid_syntax::invalid_syntax(const std::string& in_tokens,
108
178
invalid_syntax::kind_t in_kind) :
109
boost::program_options::error(error_message(in_kind).append(" in '").append(in_tokens).append("'"))
110
, m_tokens(in_tokens)
179
boost::program_options::error(error_message(in_kind).append(" in '").append(in_tokens).append("'")),
186
256
if (!s.empty()) {
187
257
// Handle section name
188
if (*s.begin() == '[' && *s.rbegin() == ']') {
258
if (*s.begin() == '[' && *s.rbegin() == ']')
189
260
m_prefix = s.substr(1, s.size()-2);
190
261
if (*m_prefix.rbegin() != '.')
193
else if ((n = s.find('=')) != std::string::npos) {
195
std::string name = m_prefix + boost::trim_copy(s.substr(0, n));
196
std::string option_value = boost::trim_copy(s.substr(n+1));
268
std::string option_value("true");
270
if ((n = s.find('=')) != std::string::npos)
273
name = m_prefix + boost::trim_copy(s.substr(0, n));
274
option_value = boost::trim_copy(parse_suffix(s.substr(n+1)));
279
name = m_prefix + boost::trim_copy(s);
198
282
bool registered = allowed_option(name);
199
283
if (!registered && !m_allow_unregistered)
244
327
// If some element is prefix of 's', then lower_bound will
245
328
// return the next element.
246
329
std::set<std::string>::iterator i = allowed_prefixes.lower_bound(s);
247
if (i != allowed_prefixes.end()) {
330
if (i != allowed_prefixes.end())
248
332
if (i->find(s) == 0)
249
333
bad_prefixes = true;
251
if (i != allowed_prefixes.begin()) {
335
if (i != allowed_prefixes.begin())
253
338
if (s.find(*i) == 0)
254
339
bad_prefixes = true;
365
453
const boost::program_options::option_description& d= *options[i];
367
455
if (d.long_name().empty())
368
boost::throw_exception(
369
boost::program_options::error("long name required for config file"));
456
boost::throw_exception(boost::program_options::error("long name required for config file"));
371
458
allowed_options.insert(d.long_name());
374
461
// Parser return char strings
375
462
boost::program_options::parsed_options result(&desc);
376
std::copy(detail::basic_config_file_iterator<charT>(
377
is, allowed_options, allow_unregistered),
463
std::copy(detail::basic_config_file_iterator<charT>(is,
378
466
detail::basic_config_file_iterator<charT>(),
379
467
std::back_inserter(result.options));
380
468
// Convert char strings into desired type.