2012-03-01 18:36:13 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-04-05 16:59:36 -07:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/util/conversions.hpp>
|
2013-01-08 14:17:31 -08:00
|
|
|
#include <mapnik/value_types.hpp>
|
2012-04-05 16:59:36 -07:00
|
|
|
|
2013-01-19 10:12:32 -08:00
|
|
|
#include <cstring>
|
2014-04-25 21:46:40 -07:00
|
|
|
#include <algorithm>
|
2013-01-19 10:12:32 -08:00
|
|
|
|
2012-03-01 18:36:13 +00:00
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
|
|
|
2013-03-10 12:56:31 -07:00
|
|
|
#if _MSC_VER
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2012-03-13 07:56:11 -07:00
|
|
|
#define BOOST_SPIRIT_AUTO(domain_, name, expr) \
|
2014-07-07 18:23:15 +01:00
|
|
|
using name##_expr_type = boost::proto::result_of:: \
|
|
|
|
deep_copy<BOOST_TYPEOF(expr)>::type ; \
|
2012-03-13 07:56:11 -07:00
|
|
|
BOOST_SPIRIT_ASSERT_MATCH( \
|
|
|
|
boost::spirit::domain_::domain, name##_expr_type); \
|
|
|
|
BOOST_AUTO(name, boost::proto::deep_copy(expr)); \
|
2012-03-13 08:05:26 -07:00
|
|
|
|
2014-01-27 13:19:21 -08:00
|
|
|
// karma is used by default
|
2013-01-21 10:25:27 -08:00
|
|
|
#define MAPNIK_KARMA_TO_STRING
|
2012-03-13 08:05:26 -07:00
|
|
|
|
2013-01-19 10:12:32 -08:00
|
|
|
#ifdef MAPNIK_KARMA_TO_STRING
|
2014-01-27 13:19:21 -08:00
|
|
|
#include <boost/spirit/include/karma.hpp>
|
2013-01-04 09:27:39 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mapnik {
|
|
|
|
|
|
|
|
namespace util {
|
2012-03-01 18:36:13 +00:00
|
|
|
|
|
|
|
using namespace boost::spirit;
|
|
|
|
|
2014-01-26 14:00:58 -08:00
|
|
|
BOOST_SPIRIT_AUTO(qi, INTEGER, qi::int_type())
|
2013-01-04 09:27:39 -08:00
|
|
|
#ifdef BIGINT
|
2014-01-26 14:00:58 -08:00
|
|
|
BOOST_SPIRIT_AUTO(qi, LONGLONG, qi::long_long_type())
|
2013-01-04 09:27:39 -08:00
|
|
|
#endif
|
2014-01-26 14:00:58 -08:00
|
|
|
BOOST_SPIRIT_AUTO(qi, FLOAT, qi::float_type())
|
|
|
|
BOOST_SPIRIT_AUTO(qi, DOUBLE, qi::double_type())
|
2012-03-01 18:36:13 +00:00
|
|
|
|
2013-02-26 12:07:36 -05:00
|
|
|
bool string2int(const char * iter, const char * end, int & result)
|
2013-01-15 14:12:43 +00:00
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,INTEGER,space,result);
|
2012-03-01 18:36:13 +00:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string2int(std::string const& value, int & result)
|
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
2012-03-01 18:36:13 +00:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 14:00:58 -08:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,INTEGER,space,result);
|
2012-03-01 18:36:13 +00:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:27:39 -08:00
|
|
|
#ifdef BIGINT
|
2013-02-26 12:07:36 -05:00
|
|
|
bool string2int(const char * iter, const char * end, mapnik::value_integer & result)
|
2012-12-03 13:12:09 +00:00
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,LONGLONG,space,result);
|
2012-12-03 13:12:09 +00:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:27:39 -08:00
|
|
|
bool string2int(std::string const& value, mapnik::value_integer & result)
|
2012-12-03 13:12:09 +00:00
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
2012-12-03 13:12:09 +00:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 14:00:58 -08:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,LONGLONG,space,result);
|
2012-12-03 13:12:09 +00:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
2013-01-04 09:27:39 -08:00
|
|
|
#endif
|
2012-12-03 13:12:09 +00:00
|
|
|
|
2012-03-01 18:36:13 +00:00
|
|
|
bool string2double(std::string const& value, double & result)
|
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
2012-03-01 18:36:13 +00:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 14:00:58 -08:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,DOUBLE,space,result);
|
2012-03-01 18:36:13 +00:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:07:36 -05:00
|
|
|
bool string2double(const char * iter, const char * end, double & result)
|
2012-03-01 18:36:13 +00:00
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,DOUBLE,space,result);
|
2012-03-01 18:36:13 +00:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string2float(std::string const& value, float & result)
|
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
2012-03-01 18:36:13 +00:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 14:00:58 -08:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,FLOAT,space,result);
|
2012-03-01 18:36:13 +00:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:07:36 -05:00
|
|
|
bool string2float(const char * iter, const char * end, float & result)
|
2012-03-01 18:36:13 +00:00
|
|
|
{
|
2014-01-26 14:00:58 -08:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,FLOAT,space,result);
|
2012-03-01 18:36:13 +00:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
2013-02-27 10:08:04 -05:00
|
|
|
// double conversion - here we use sprintf over karma to work
|
|
|
|
// around https://github.com/mapnik/mapnik/issues/1741
|
|
|
|
bool to_string(std::string & s, double val)
|
|
|
|
{
|
|
|
|
s.resize(s.capacity());
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%g", val));
|
|
|
|
if (n2 <= s.size())
|
|
|
|
{
|
|
|
|
s.resize(n2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s.resize(n2);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-19 10:12:32 -08:00
|
|
|
#ifdef MAPNIK_KARMA_TO_STRING
|
2013-01-04 09:27:39 -08:00
|
|
|
|
|
|
|
bool to_string(std::string & str, int value)
|
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
|
|
|
|
2013-01-10 20:57:02 -08:00
|
|
|
#ifdef BIGINT
|
|
|
|
bool to_string(std::string & str, mapnik::value_integer value)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
2013-01-10 20:57:02 -08:00
|
|
|
#endif
|
2013-01-04 09:27:39 -08:00
|
|
|
|
2013-01-10 20:57:02 -08:00
|
|
|
bool to_string(std::string & str, unsigned value)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
|
|
|
|
2013-01-10 20:57:02 -08:00
|
|
|
bool to_string(std::string & str, bool value)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-01-19 10:12:32 -08:00
|
|
|
bool to_string(std::string & s, int val)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
2013-01-19 10:12:32 -08:00
|
|
|
s.resize(s.capacity());
|
|
|
|
while (true)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
2013-01-19 10:12:32 -08:00
|
|
|
size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%d", val));
|
|
|
|
if (n2 <= s.size())
|
|
|
|
{
|
|
|
|
s.resize(n2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s.resize(n2);
|
2013-01-04 09:27:39 -08:00
|
|
|
}
|
2013-01-19 10:12:32 -08:00
|
|
|
return true;
|
2013-01-04 09:27:39 -08:00
|
|
|
}
|
|
|
|
|
2013-01-10 20:57:02 -08:00
|
|
|
#ifdef BIGINT
|
2013-01-19 10:12:32 -08:00
|
|
|
bool to_string(std::string & s, mapnik::value_integer val)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
2013-01-19 10:12:32 -08:00
|
|
|
s.resize(s.capacity());
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lld", val));
|
|
|
|
if (n2 <= s.size())
|
|
|
|
{
|
|
|
|
s.resize(n2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s.resize(n2);
|
|
|
|
}
|
|
|
|
return true;
|
2013-01-04 09:27:39 -08:00
|
|
|
}
|
2013-01-19 10:12:32 -08:00
|
|
|
#endif
|
2013-01-04 09:27:39 -08:00
|
|
|
|
2013-01-19 10:12:32 -08:00
|
|
|
bool to_string(std::string & s, unsigned val)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
2013-01-19 10:12:32 -08:00
|
|
|
s.resize(s.capacity());
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%u", val));
|
|
|
|
if (n2 <= s.size())
|
|
|
|
{
|
|
|
|
s.resize(n2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s.resize(n2);
|
|
|
|
}
|
|
|
|
return true;
|
2012-03-01 18:36:13 +00:00
|
|
|
}
|
2013-01-04 09:27:39 -08:00
|
|
|
|
2013-01-19 10:12:32 -08:00
|
|
|
bool to_string(std::string & s, bool val)
|
2013-01-04 09:27:39 -08:00
|
|
|
{
|
2013-01-19 10:12:32 -08:00
|
|
|
if (val) s = "true";
|
|
|
|
else s = "false";
|
|
|
|
return true;
|
2013-01-04 09:27:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // end namespace util
|
|
|
|
|
2012-03-01 18:36:13 +00:00
|
|
|
}
|