CharEncoding.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled
4  applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/core/encoding/CharEncoding.cpp
23 
24  \brief A class for handling character enconding/decoding.
25 
26  \author Matheus Cavassan Zaglia
27  \author Gilberto Ribeiro de Queiroz
28 */
29 
30 // TerraLib
31 #include "CharEncoding.h"
32 #include "../logger/Logger.h"
33 #include "../Exception.h"
34 #include "../translator/Translator.h"
35 
36 // Boost
37 #include <boost/assign/list_of.hpp>
38 #include <boost/format.hpp>
39 #include <boost/locale.hpp>
40 
41 const std::map<te::core::EncodingType, std::string> EncodingString =
42  boost::assign::map_list_of(te::core::EncodingType::UTF8, "UTF-8")
50 
51 std::string te::core::CharEncoding::toUTF8(const std::string &src,
52  EncodingType from)
53 {
54  return boost::locale::conv::to_utf<char>(src, EncodingString.at(from));
55 }
56 
57 std::string te::core::CharEncoding::toUTF8(const std::string &src)
58 {
59  boost::locale::generator g;
60  g.locale_cache_enabled(true);
61  try
62  {
63  std::locale loc = g(boost::locale::util::get_system_locale(true));
64  return boost::locale::conv::to_utf<char>(src, loc);
65  }
66  catch(std::exception &e)
67  {
68  boost::format err_msg(TE_TR(
69  "Could not get the system locale in order to convert fromUTF8 to the "
70  "system encoding.\n"
71  "%1%"));
72  TE_LOG_ERROR((err_msg % e.what()).str());
73  throw Exception() << ErrorDescription((err_msg % e.what()).str());
74  }
75 }
76 
77 std::string te::core::CharEncoding::fromUTF8(const std::string &src)
78 {
79  boost::locale::generator g;
80  g.locale_cache_enabled(true);
81  try
82  {
83  std::locale loc = g(boost::locale::util::get_system_locale(false));
84 
85  std::string teste = boost::locale::conv::from_utf<char>(src, loc);
86 
87  return boost::locale::conv::from_utf<char>(src, loc);
88  }
89  catch(std::exception &e)
90  {
91  boost::format err_msg(TE_TR(
92  "Could not get the system locale in order to convert fromUTF8 to the "
93  "system encoding.\n"
94  "%1%"));
95  TE_LOG_ERROR((err_msg % e.what()).str());
96  throw Exception() << ErrorDescription((err_msg % e.what()).str());
97  }
98 }
99 
100 std::string te::core::CharEncoding::fromUTF8(const std::string &src,
101  EncodingType to)
102 {
103  return boost::locale::conv::from_utf<char>(src, EncodingString.at(to));
104 }
105 
106 std::string te::core::CharEncoding::convert(const std::string &src,
107  EncodingType from, EncodingType to)
108 {
109  return boost::locale::conv::between(src, EncodingString.at(to),
110  EncodingString.at(from));
111 }
112 
113 std::string te::core::CharEncoding::toASCII(const std::string &src)
114 {
115  std::string decomposed;
116  boost::locale::generator g;
117 
118  try
119  {
120  g.locale_cache_enabled(true);
121  std::locale loc = g.generate("");
122  decomposed = boost::locale::normalize(src, boost::locale::norm_nfd, loc);
123  }
124  catch(std::exception&)
125  {
126  boost::format err_msg(TE_TR(
127  "Could not decompose the given string to extract the ASCII "
128  "characters."));
129  TE_LOG_ERROR(err_msg.str());
130  throw Exception() << ErrorDescription(err_msg.str());
131  }
132 
133  std::string ascii_str;
134 
135  for(size_t i = 0; decomposed.size() > i; ++i)
136  if(decomposed[i] >= 0 && decomposed[i] <= 127) ascii_str += decomposed[i];
137 
138  return ascii_str;
139 }
140 
142 {
143  return EncodingString.at(et);
144 }
145 
147  const std::string &name)
148 {
149  std::map<EncodingType, std::string>::const_iterator it;
150  for(it = EncodingString.begin(); it != EncodingString.end(); ++it)
151  {
152  if(it->second == name) return it->first;
153  }
154 
155  boost::format err_msg(TE_TR("There is no encoding type with the name: %1%."));
156  throw Exception() << te::ErrorDescription((err_msg % name).str());
157 }
158 
159 std::vector<std::string> te::core::CharEncoding::getEncodingList()
160 {
161  std::vector<std::string> vec;
162 
163  std::map<EncodingType, std::string>::const_iterator it;
164 
165  for(it = EncodingString.begin(); it != EncodingString.end(); ++it)
166  vec.push_back(it->second);
167 
168  return vec;
169 }
const std::map< te::core::EncodingType, std::string > EncodingString
static std::string toASCII(const std::string &src)
Decomposes a UTF-8 encoded string and extracts its ASCII characters.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
EncodingType
Supported character encodings.
Definition: CharEncoding.h:50
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
A class for handling character enconding/decoding.
static te::core::EncodingType getEncodingType(const std::string &name)
Retrive an EncodingType from a given character encoding name.
static std::string convert(const std::string &src, EncodingType from, EncodingType to)
Convert a string from one character encoding to another one.
static std::string toUTF8(const std::string &src)
Convert a string from a current locale encoding to UTF-8.
#define TE_LOG_ERROR(message)
Use this tag in order to log a message to the TerraLib default logger with the ERROR level...
Definition: Logger.h:337
Base exception class for TerraLib Core Runtime Library.
static std::vector< std::string > getEncodingList()
Retrive a vector of string with all available encoding types name.