utf8codec.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 by Marc Boris Duerner
3  * Copyright (C) 2005 by Aloysius Indrayanto
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 #ifndef cxxtools_Utf8Codec_h
30 #define cxxtools_Utf8Codec_h
31 
32 #include <cxxtools/api.h>
33 #include <cxxtools/char.h>
34 #include <cxxtools/textcodec.h>
35 #include <cxxtools/string.h>
36 #include <string>
37 
38 namespace cxxtools
39 {
40 
51  class CXXTOOLS_API Utf8Codec : public TextCodec<Char, char>
52  {
53  public:
62  explicit Utf8Codec(size_t ref = 0);
63 
65  virtual ~Utf8Codec()
66  {}
67 
68  protected:
70  virtual result do_in(MBState& s, const char* fromBegin,
71  const char* fromEnd, const char*& fromNext,
72  Char* toBegin, Char* toEnd, Char*& toNext) const;
73 
75  virtual result do_out(MBState& s, const Char* fromBegin,
76  const Char* fromEnd, const Char*& fromNext,
77  char* toBegin, char* toEnd, char*& toNext) const;
78 
79  // inheritdoc
80  virtual bool do_always_noconv() const throw();
81 
82  // inheritdoc
83  virtual int do_length(MBState& s, const char* fromBegin, const char* fromEnd, size_t max) const;
84 
85  // inheritdoc
86  virtual int do_max_length() const throw();
87 
88  // inheritdoc
89  std::codecvt_base::result do_unshift(cxxtools::MBState&, char*, char*, char*&) const
90  { return std::codecvt_base::noconv; }
91 
92  // inheritdoc
93  int do_encoding() const throw()
94  { return 0; }
95 
96  public:
104  static String decode(const char* data, unsigned size)
105  { return cxxtools::decode<Utf8Codec>(data, size); }
106 
109  static String decode(const std::string& data)
110  { return cxxtools::decode<Utf8Codec>(data); }
111 
114  static std::string encode(const Char* data, unsigned size)
115  { return cxxtools::encode<Utf8Codec>(data, size); }
116 
119  static std::string encode(const String& data)
120  { return cxxtools::encode<Utf8Codec>(data); }
121  };
122 
123 } //namespace cxxtools
124 
125 #endif
126