2012-03-01 19:36:13 +01: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-06 01:59:36 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/util/conversions.hpp>
|
2013-01-08 23:17:31 +01:00
|
|
|
#include <mapnik/value_types.hpp>
|
2012-04-06 01:59:36 +02:00
|
|
|
|
2013-01-19 19:12:32 +01:00
|
|
|
#include <cstring>
|
2014-04-26 06:46:40 +02:00
|
|
|
#include <algorithm>
|
2013-01-19 19:12:32 +01:00
|
|
|
|
2014-01-27 22:19:21 +01:00
|
|
|
// karma is used by default
|
2013-01-21 19:25:27 +01:00
|
|
|
#define MAPNIK_KARMA_TO_STRING
|
2012-03-13 16:05:26 +01:00
|
|
|
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
|
|
|
#include <boost/spirit/include/qi.hpp>
|
2013-01-19 19:12:32 +01:00
|
|
|
#ifdef MAPNIK_KARMA_TO_STRING
|
2014-01-27 22:19:21 +01:00
|
|
|
#include <boost/spirit/include/karma.hpp>
|
2013-01-04 18:27:39 +01:00
|
|
|
#endif
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
#if _MSC_VER
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2013-01-04 18:27:39 +01:00
|
|
|
|
|
|
|
namespace mapnik {
|
|
|
|
|
|
|
|
namespace util {
|
2012-03-01 19:36:13 +01:00
|
|
|
|
|
|
|
using namespace boost::spirit;
|
|
|
|
|
2014-09-23 19:49:31 +02:00
|
|
|
auto INTEGER = qi::int_type();
|
2013-01-04 18:27:39 +01:00
|
|
|
#ifdef BIGINT
|
2014-09-23 19:49:31 +02:00
|
|
|
auto LONGLONG = qi::long_long_type();
|
2013-01-04 18:27:39 +01:00
|
|
|
#endif
|
2014-09-23 19:49:31 +02:00
|
|
|
auto FLOAT = qi::float_type();
|
|
|
|
auto DOUBLE = qi::double_type();
|
2012-03-01 19:36:13 +01:00
|
|
|
|
2013-02-26 18:07:36 +01:00
|
|
|
bool string2int(const char * iter, const char * end, int & result)
|
2013-01-15 15:12:43 +01:00
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,INTEGER,space,result);
|
2012-03-01 19:36:13 +01:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string2int(std::string const& value, int & result)
|
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
2012-03-01 19:36:13 +01:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 23:00:58 +01:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,INTEGER,space,result);
|
2012-03-01 19:36:13 +01:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
|
|
|
|
2013-01-04 18:27:39 +01:00
|
|
|
#ifdef BIGINT
|
2013-02-26 18:07:36 +01:00
|
|
|
bool string2int(const char * iter, const char * end, mapnik::value_integer & result)
|
2012-12-03 14:12:09 +01:00
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,LONGLONG,space,result);
|
2012-12-03 14:12:09 +01:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
2013-01-04 18:27:39 +01:00
|
|
|
bool string2int(std::string const& value, mapnik::value_integer & result)
|
2012-12-03 14:12:09 +01:00
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
2012-12-03 14:12:09 +01:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 23:00:58 +01:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,LONGLONG,space,result);
|
2012-12-03 14:12:09 +01:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
2013-01-04 18:27:39 +01:00
|
|
|
#endif
|
2012-12-03 14:12:09 +01:00
|
|
|
|
2012-03-01 19:36:13 +01:00
|
|
|
bool string2double(std::string const& value, double & result)
|
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
2012-03-01 19:36:13 +01:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 23:00:58 +01:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,DOUBLE,space,result);
|
2012-03-01 19:36:13 +01:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
|
|
|
|
2013-02-26 18:07:36 +01:00
|
|
|
bool string2double(const char * iter, const char * end, double & result)
|
2012-03-01 19:36:13 +01:00
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,DOUBLE,space,result);
|
2012-03-01 19:36:13 +01:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string2float(std::string const& value, float & result)
|
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
2012-03-01 19:36:13 +01:00
|
|
|
std::string::const_iterator str_beg = value.begin();
|
|
|
|
std::string::const_iterator str_end = value.end();
|
2014-01-26 23:00:58 +01:00
|
|
|
bool r = qi::phrase_parse(str_beg,str_end,FLOAT,space,result);
|
2012-03-01 19:36:13 +01:00
|
|
|
return r && (str_beg == str_end);
|
|
|
|
}
|
|
|
|
|
2013-02-26 18:07:36 +01:00
|
|
|
bool string2float(const char * iter, const char * end, float & result)
|
2012-03-01 19:36:13 +01:00
|
|
|
{
|
2014-01-26 23:00:58 +01:00
|
|
|
ascii::space_type space;
|
|
|
|
bool r = qi::phrase_parse(iter,end,FLOAT,space,result);
|
2012-03-01 19:36:13 +01:00
|
|
|
return r && (iter == end);
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:08:04 +01: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 19:12:32 +01:00
|
|
|
#ifdef MAPNIK_KARMA_TO_STRING
|
2013-01-04 18:27:39 +01: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-11 05:57:02 +01:00
|
|
|
#ifdef BIGINT
|
|
|
|
bool to_string(std::string & str, mapnik::value_integer value)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
2013-01-11 05:57:02 +01:00
|
|
|
#endif
|
2013-01-04 18:27:39 +01:00
|
|
|
|
2013-01-11 05:57:02 +01:00
|
|
|
bool to_string(std::string & str, unsigned value)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
|
|
|
|
2013-01-11 05:57:02 +01:00
|
|
|
bool to_string(std::string & str, bool value)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
std::back_insert_iterator<std::string> sink(str);
|
|
|
|
return karma::generate(sink, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-01-19 19:12:32 +01:00
|
|
|
bool to_string(std::string & s, int val)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
2013-01-19 19:12:32 +01:00
|
|
|
s.resize(s.capacity());
|
|
|
|
while (true)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
2013-01-19 19:12:32 +01: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 18:27:39 +01:00
|
|
|
}
|
2013-01-19 19:12:32 +01:00
|
|
|
return true;
|
2013-01-04 18:27:39 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 05:57:02 +01:00
|
|
|
#ifdef BIGINT
|
2013-01-19 19:12:32 +01:00
|
|
|
bool to_string(std::string & s, mapnik::value_integer val)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
2013-01-19 19:12:32 +01: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 18:27:39 +01:00
|
|
|
}
|
2013-01-19 19:12:32 +01:00
|
|
|
#endif
|
2013-01-04 18:27:39 +01:00
|
|
|
|
2013-01-19 19:12:32 +01:00
|
|
|
bool to_string(std::string & s, unsigned val)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
2013-01-19 19:12:32 +01: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 19:36:13 +01:00
|
|
|
}
|
2013-01-04 18:27:39 +01:00
|
|
|
|
2013-01-19 19:12:32 +01:00
|
|
|
bool to_string(std::string & s, bool val)
|
2013-01-04 18:27:39 +01:00
|
|
|
{
|
2013-01-19 19:12:32 +01:00
|
|
|
if (val) s = "true";
|
|
|
|
else s = "false";
|
|
|
|
return true;
|
2013-01-04 18:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // end namespace util
|
|
|
|
|
2012-03-01 19:36:13 +01:00
|
|
|
}
|