From 982aed825f20bb465a92118724b61c8a2092f7ff Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:12:43 +0000 Subject: [PATCH] + implement string2bool converters --- include/mapnik/util/conversions.hpp | 3 ++ src/conversions.cpp | 44 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/include/mapnik/util/conversions.hpp b/include/mapnik/util/conversions.hpp index 7fcddea73..522633d4a 100644 --- a/include/mapnik/util/conversions.hpp +++ b/include/mapnik/util/conversions.hpp @@ -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); diff --git a/src/conversions.cpp b/src/conversions.cpp index 63c95c74f..843599537 100644 --- a/src/conversions.cpp +++ b/src/conversions.cpp @@ -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 +{ + 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); }