libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNCharsetConverter.h
Go to the documentation of this file.
1 /* Copyright 2012-2014 CoReNum, INSA-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNCharsetConverter.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNCharsetConverter_HEADER
23 #define CRNCharsetConverter_HEADER
24 
25 #include <iconv.h>
26 #include <CRNException.h>
27 
28 namespace crn
29 {
30  class StringUTF8;
31 
42  {
43  public:
44  enum class Status { OK, BUFFER, INVALID, INCOMPLETE };
45 
46  class Exception: public crn::Exception
47  {
48  public:
50  explicit Exception() noexcept;
52  explicit Exception(const crn::StringUTF8 &msg) noexcept;
54  explicit Exception(const char *msg) noexcept;
55  };
56 
58  {
59  public:
61  explicit ExceptionInvalidCharacter() noexcept;
63  explicit ExceptionInvalidCharacter(const crn::StringUTF8 &msg) noexcept;
65  explicit ExceptionInvalidCharacter(const char *msg) noexcept;
66  };
67 
69  {
70  public:
72  explicit ExceptionIncompleteCode() throw();
74  explicit ExceptionIncompleteCode(const crn::StringUTF8 &msg) noexcept;
76  explicit ExceptionIncompleteCode(const char *msg) noexcept;
77  };
78 
80  CharsetConverter(const std::string &to_code, bool translit = true, bool throw_exceptions = true);
81  CharsetConverter(const CharsetConverter&) = delete;
87 
89  void Reset(const std::string &to_code, bool translit = true);
91  std::string FromUTF8(const crn::StringUTF8 &str, Status *stat = nullptr) const;
93  crn::StringUTF8 ToUTF8(const std::string &str, Status *stat = nullptr) const;
94 
96  const std::string& GetCharset() const { return current_code; }
97 
98  private:
100  std::string fromUTF8(const crn::StringUTF8 &str, size_t buff, Status *stat) const;
102  crn::StringUTF8 toUTF8(const std::string &str, size_t buff, Status *stat) const;
103 
104  mutable iconv_t toutf, fromutf;
105  bool silent;
106  std::string current_code;
107  bool throws;
108  };
109 }
110 
112 #endif
113 
CharsetConverter & operator=(const CharsetConverter &)=delete
std::string FromUTF8(const crn::StringUTF8 &str, Status *stat=nullptr) const
Converts from unicode to the selected charset.
void Reset(const std::string &to_code, bool translit=true)
Changes the charset to convert.
CharsetConverter(const std::string &to_code, bool translit=true, bool throw_exceptions=true)
Constructor.
const std::string & GetCharset() const
Returns the used charset.
ExceptionInvalidCharacter() noexcept
Default constructor.
Character set converter.
crn::StringUTF8 ToUTF8(const std::string &str, Status *stat=nullptr) const
Converts to unicode.
A character string class.
Definition: CRNStringUTF8.h:49
Base class for exceptions.
Definition: CRNException.h:39
Exception() noexcept
Default constructor.