+ implement string2bool converters

This commit is contained in:
artemp 2013-01-15 14:12:43 +00:00
parent f1d142a3c1
commit 982aed825f
2 changed files with 45 additions and 2 deletions

View file

@ -32,6 +32,9 @@
namespace mapnik { namespace util { 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(const char * value, int & result);
MAPNIK_DECL bool string2int(std::string const& value, int & result); MAPNIK_DECL bool string2int(std::string const& value, int & result);

View file

@ -60,13 +60,53 @@ BOOST_SPIRIT_AUTO(qi, LONGLONG, qi::long_long)
BOOST_SPIRIT_AUTO(qi, FLOAT, qi::float_) BOOST_SPIRIT_AUTO(qi, FLOAT, qi::float_)
BOOST_SPIRIT_AUTO(qi, DOUBLE, qi::double_) 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) bool string2int(const char * value, int & result)
{ {
size_t length = strlen(value); size_t length = strlen(value);
if (length < 1 || value == NULL) if (length < 1 || value == NULL)
return false; return false;
const char *iter = value; const char *iter = value;
const char *end = value + length; const char *end = value + length;
bool r = qi::phrase_parse(iter,end,INTEGER,ascii::space,result); bool r = qi::phrase_parse(iter,end,INTEGER,ascii::space,result);
return r && (iter == end); return r && (iter == end);
} }