CharEncoding.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/core/encoding/CharEncoding.h
24 
25  \brief A class for handling character enconding/decoding.
26 
27  \author Matheus Cavassan Zaglia
28  \author Gilberto Ribeiro de Queiroz
29 */
30 
31 #ifndef __TERRALIB_CORE_ENCODING_CHARENCODING_H__
32 #define __TERRALIB_CORE_ENCODING_CHARENCODING_H__
33 
34 // TerraLib
35 #include "../Config.h"
36 
37 // STL
38 #include <vector>
39 #include <string>
40 
41 namespace te
42 {
43  namespace core
44  {
45  /*!
46  \enum EncodingType
47 
48  \brief Supported character encodings.
49  */
50  enum class EncodingType
51  {
52  UTF8, /*!< UTF-8 encoding. */
53  CP1250, /*!< CP1250 encoding. */
54  CP1251, /*!< CP1251 encoding. */
55  CP1252, /*!< CP1252 encoding. */
56  CP1253, /*!< CP1253 encoding. */
57  CP1254, /*!< CP1254 encoding. */
58  CP1257, /*!< CP1257 encoding. */
59  LATIN1 /*!< Latin1 encoding (ISO8859-1). */
60  };
61 
62  /*!
63  \class CharEncoding
64 
65  \brief A class for handling character enconding/decoding.
66  */
68  {
69  public:
70  /*!
71  \brief Convert a string in UTF-8 to the current locale encoding.
72 
73  \param src UTF-8 string.
74 
75  \exception te::Exception if the system locale cannot be retrieved.
76 
77  \return String encoded according to new character encoding.
78  */
79  static std::string fromUTF8(const std::string& src);
80 
81  /*!
82  \brief Convert a string in UTF-8 to another character encoding.
83 
84  \param src UTF-8 string.
85  \param to The target character encoding.
86 
87  \return String encoded according to new character encoding.
88  */
89  static std::string fromUTF8(const std::string& src, EncodingType to);
90 
91  /*!
92  \brief Convert a string from a current locale encoding to UTF-8.
93 
94  \param src String to be encoded in UTF-8.
95 
96  \exception te::Exception if the system locale cannot be retrieved.
97 
98  \return String encoded in UTF-8.
99  */
100  static std::string toUTF8(const std::string& src);
101 
102  /*!
103  \brief Convert a string from a given character encoding to UTF-8.
104 
105  \param src String to be encoded in UTF-8.
106 
107  \return String encoded in UTF-8.
108  */
109  static std::string toUTF8(const std::string& src, EncodingType from);
110 
111  /*!
112  \brief Convert a string from one character encoding to another one.
113 
114  \param src String encoded according to "from" encoding type.
115  \param from Current string encoding.
116  \param to New encoding for the string.
117 
118  \return String in a new encoding.
119  */
120  static std::string convert(const std::string& src, EncodingType from,
121  EncodingType to);
122  /*!
123  \brief Decomposes a UTF-8 encoded string and extracts its ASCII
124  characters.
125 
126  \note Non-ascii characters will be skipped.
127 
128  \param src UTF-8 encoded string.
129 
130  \exception te::Exception if the given string cannot be decomposed.
131 
132  \return ASCII String.
133  */
134  static std::string toASCII(const std::string& src);
135 
136  /*!
137  \brief Retrive a string from a given character encoding type enum.
138 
139  \param et Encoding Type to be retrieved as string.
140 
141  \return Encoding Type as string.
142  */
143  static std::string getEncodingName(EncodingType et);
144 
145  /*!
146  \brief Retrive an EncodingType from a given character encoding name.
147 
148  \param name Encoding name to be retrieved as enum.
149 
150  \return Encoding as enum.
151  */
152  static te::core::EncodingType getEncodingType(const std::string& name);
153 
154  /*!
155  \brief Retrive a vector of string with all available encoding types name.
156 
157  \return A vector of encoding type as string.
158  */
159  static std::vector<std::string> getEncodingList();
160 
161  private:
162 // Not instatiable
163  CharEncoding();
164  ~CharEncoding();
165 // No copy allowed
166  CharEncoding(CharEncoding const&);
167  CharEncoding& operator=(CharEncoding const&);
168  };
169  } // end namespace core
170 } // end namespace te
171 
172 #endif //__TERRALIB_CORE_ENCODING_CHARENCODING_H__
A class for handling character enconding/decoding.
Definition: CharEncoding.h:67
EncodingType
Supported character encodings.
Definition: CharEncoding.h:50
#define TECOREEXPORT
Definition: Config.h:40
URI C++ Library.
std::string TECOMMONEXPORT convert(const path &v)
URI path to string.