]> git.tdb.fi Git - libs/core.git/blob - source/utf8.h
More sophisticated error handling
[libs/core.git] / source / utf8.h
1 #ifndef MSP_STRINGS_UTF8_H_
2 #define MSP_STRINGS_UTF8_H_
3
4 #include "codec.h"
5
6 namespace Msp {
7
8 class Utf8: public StringCodec
9 {
10 public:
11         class Encoder: public StringCodec::Encoder
12         {
13         public:
14                 Encoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Encoder(em) { }
15                 void encode_char(wchar_t);
16         private:
17                 void append_replacement() { append("\357\277\275"); }
18         };
19
20         class Decoder: public StringCodec::Decoder
21         {
22         public:
23                 Decoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Decoder(em), bytes(0), code(0) { }
24                 void     decode_char(const std::string &, std::string::const_iterator &);
25                 void     sync();
26                 void     reset();
27         private:
28                 unsigned bytes;
29                 unsigned code;
30         };
31
32         Encoder *create_encoder(ErrorMode em=THROW_ON_ERROR) const { return new Encoder(em); }
33         Decoder *create_decoder(ErrorMode em=THROW_ON_ERROR) const { return new Decoder(em); }
34 };
35
36 } // namespace Msp
37
38 #endif