]> git.tdb.fi Git - libs/core.git/blob - source/utf8.h
94569eef9542f551ff8f821c5b6d079aa3f36325
[libs/core.git] / source / utf8.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_STRINGS_UTF8_H_
9 #define MSP_STRINGS_UTF8_H_
10
11 #include "codec.h"
12
13 namespace Msp {
14
15 class Utf8: public StringCodec
16 {
17 public:
18         class Encoder: public StringCodec::Encoder
19         {
20         public:
21                 Encoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Encoder(em) { }
22                 void encode_char(wchar_t);
23         private:
24                 void append_replacement() { append("\357\277\275"); }
25         };
26
27         class Decoder: public StringCodec::Decoder
28         {
29         public:
30                 Decoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Decoder(em), bytes(0), code(0) { }
31                 void     decode_char(const std::string &, std::string::const_iterator &);
32                 void     sync();
33                 void     reset();
34         private:
35                 unsigned bytes;
36                 unsigned code;
37         };
38
39         Encoder *create_encoder(ErrorMode em=THROW_ON_ERROR) const { return new Encoder(em); }
40         Decoder *create_decoder(ErrorMode em=THROW_ON_ERROR) const { return new Decoder(em); }
41 };
42
43 } // namespace Msp
44
45 #endif