2008-01-03 12:41:00 +01:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2008-01-03 12:41:00 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2014-11-20 15:25:50 +01:00
|
|
|
* Copyright (C) 2014 Artem Pavlenko
|
2008-01-03 12:41:00 +01:00
|
|
|
*
|
|
|
|
* 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-08 02:45:01 +02:00
|
|
|
// mapnik
|
2008-01-03 12:41:00 +01:00
|
|
|
#include <mapnik/unicode.hpp>
|
2013-08-14 00:52:04 +02:00
|
|
|
#include <mapnik/value_types.hpp>
|
2008-01-03 12:41:00 +01:00
|
|
|
|
2012-04-08 02:45:01 +02:00
|
|
|
// stl
|
|
|
|
#include <cstdlib>
|
2008-01-03 12:41:00 +01:00
|
|
|
#include <string>
|
|
|
|
|
2014-09-30 02:14:40 +02:00
|
|
|
// icu
|
|
|
|
#include <unicode/ucnv.h>
|
|
|
|
|
2008-01-03 12:41:00 +01:00
|
|
|
namespace mapnik {
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
transcoder::transcoder (std::string const& encoding)
|
2014-12-02 20:21:36 +01:00
|
|
|
: conv_(0)
|
2009-12-16 21:02:06 +01:00
|
|
|
{
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
conv_ = ucnv_open(encoding.c_str(),&err);
|
2014-12-02 20:21:36 +01:00
|
|
|
if (!U_SUCCESS(err))
|
|
|
|
{
|
|
|
|
// NOT: conv_ should be null on error so no need to call ucnv_close
|
|
|
|
throw std::runtime_error(std::string("could not create converter for ") + encoding);
|
|
|
|
}
|
2009-12-16 21:02:06 +01:00
|
|
|
}
|
2008-01-03 12:41:00 +01:00
|
|
|
|
2014-05-06 19:06:47 +02:00
|
|
|
mapnik::value_unicode_string transcoder::transcode(const char* data, std::int32_t length) const
|
2009-12-16 21:02:06 +01:00
|
|
|
{
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-08-14 00:52:04 +02:00
|
|
|
mapnik::value_unicode_string ustr(data,length,conv_,err);
|
2009-12-16 21:02:06 +01:00
|
|
|
if (ustr.isBogus())
|
2008-01-03 12:41:00 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
ustr.remove();
|
2008-01-03 12:41:00 +01:00
|
|
|
}
|
2009-12-16 21:02:06 +01:00
|
|
|
return ustr;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2008-01-03 12:41:00 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
transcoder::~transcoder()
|
|
|
|
{
|
|
|
|
if (conv_) ucnv_close(conv_);
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2008-01-03 12:41:00 +01:00
|
|
|
}
|