+ implement string2bool converters
This commit is contained in:
parent
f1d142a3c1
commit
982aed825f
2 changed files with 45 additions and 2 deletions
|
@ -32,6 +32,9 @@
|
|||
|
||||
namespace mapnik { namespace util {
|
||||
|
||||
MAPNIK_DECL bool string2bool(const char * value, bool & result);
|
||||
MAPNIK_DECL bool string2bool(std::string const& value, bool & result);
|
||||
|
||||
MAPNIK_DECL bool string2int(const char * value, int & result);
|
||||
MAPNIK_DECL bool string2int(std::string const& value, int & result);
|
||||
|
||||
|
|
|
@ -60,13 +60,53 @@ BOOST_SPIRIT_AUTO(qi, LONGLONG, qi::long_long)
|
|||
BOOST_SPIRIT_AUTO(qi, FLOAT, qi::float_)
|
||||
BOOST_SPIRIT_AUTO(qi, DOUBLE, qi::double_)
|
||||
|
||||
|
||||
|
||||
struct bool_symbols : qi::symbols<char,bool>
|
||||
{
|
||||
bool_symbols()
|
||||
{
|
||||
add("true",true)
|
||||
("false",false)
|
||||
("yes",true)
|
||||
("no",false)
|
||||
("on",true)
|
||||
("off",false)
|
||||
("1",true)
|
||||
("0",false);
|
||||
}
|
||||
};
|
||||
|
||||
bool string2bool(const char * value, bool & result)
|
||||
{
|
||||
using boost::spirit::qi::no_case;
|
||||
size_t length = strlen(value);
|
||||
if (length < 1 || value == NULL)
|
||||
return false;
|
||||
const char *iter = value;
|
||||
const char *end = value + length
|
||||
bool r = qi::phrase_parse(iter,end, no_case[bool_symbols()] ,ascii::space,result);
|
||||
return r && (iter == end);
|
||||
}
|
||||
|
||||
bool string2bool(std::string const& value, bool & result)
|
||||
{
|
||||
using boost::spirit::qi::no_case;
|
||||
if (value.empty())
|
||||
return false;
|
||||
std::string::const_iterator str_beg = value.begin();
|
||||
std::string::const_iterator str_end = value.end();
|
||||
bool r = qi::phrase_parse(str_beg,str_end,no_case[bool_symbols()],ascii::space,result);
|
||||
return r && (str_beg == str_end);
|
||||
}
|
||||
|
||||
bool string2int(const char * value, int & result)
|
||||
{
|
||||
size_t length = strlen(value);
|
||||
if (length < 1 || value == NULL)
|
||||
return false;
|
||||
const char *iter = value;
|
||||
const char *end = value + length;
|
||||
const char *iter = value;
|
||||
const char *end = value + length;
|
||||
bool r = qi::phrase_parse(iter,end,INTEGER,ascii::space,result);
|
||||
return r && (iter == end);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue