1 #ifndef MSP_STRINGS_CODEC_H_
2 #define MSP_STRINGS_CODEC_H_
10 An exception thrown for all kinds of problems encountered while encoding or
13 class CodecError: public Exception
16 CodecError(const std::string &w_): Exception(w_) { }
20 Base class for string codecs. Use one of the derived classes or the function
21 create_codec to create a specific codec.
23 For the purposes of this class, an std::wstring is considered to contain
24 Unicode characters and an std::string is considered to be an encoded sequence
25 of bytes. A codec is able to determine if an encoded string could be decoded
39 Base class for string encoder. Each codec class should contain an Encoder
40 class derived from this.
46 Encodes a single character. Derived classes should use the append
47 function to put the result into the internal buffer.
49 virtual void encode_char(wchar_t) =0;
54 virtual void encode(const std::wstring &s)
55 { for(std::wstring::const_iterator i=s.begin(); i!=s.end(); ++i) encode_char(*i); }
58 Brings the encoder back to its initial state. This allows the encoded
59 sequence to be extracted or flushed without loss of integrity.
61 virtual void sync() { }
64 Returns a reference to the encoded sequence. Call sync() first to make
65 sure it's a valid encoded string by itself.
67 const std::string &get() const { return buffer_; }
70 Returns the number of bytes in the output buffer.
72 unsigned size() const { return buffer_.size(); }
75 Clears the encoded sequence. Encoder state is left intact.
77 void flush() { buffer_.clear(); }
79 virtual ~Encoder() { }
81 Encoder(ErrorMode em=THROW_ON_ERROR): err_mode_(em) { }
82 void append(char c) { buffer_+=c; }
83 void append(const char *s, unsigned l) { buffer_.append(s, l); }
84 void append(const std::string &s) { buffer_+=s; }
85 void error(const std::string &);
86 virtual void append_replacement() { }
93 Base class for string decoder. Each codec class should contain an Decoder
94 class derived from this.
99 virtual void decode_char(const std::string &, std::string::const_iterator &) =0;
100 virtual void decode(const std::string &s)
101 { for(std::string::const_iterator i=s.begin(); i!=s.end(); ) decode_char(s, i); }
104 Ensures that all input has been processed. If this is not the case any
105 buffers are cleared and an error is triggered.
107 virtual void sync() { }
109 const std::wstring &get() const { return buffer_; }
110 unsigned size() const { return buffer_.size(); }
111 void flush() { buffer_.clear(); }
112 virtual ~Decoder() { }
114 Decoder(ErrorMode em): err_mode_(em) { }
115 void append(wchar_t c) { buffer_+=c; }
116 void append(const std::wstring &s) { buffer_+=s; }
117 void error(const std::string &);
120 std::wstring buffer_;
123 virtual Encoder *create_encoder(ErrorMode =THROW_ON_ERROR) const =0;
124 virtual Decoder *create_decoder(ErrorMode =THROW_ON_ERROR) const =0;
125 virtual bool detect(const std::string &) const;
126 virtual ~StringCodec() { }
132 Convenience function that decodes a string using the given codec.
135 std::wstring decode(const std::string &s)
137 typename C::Decoder dec;
144 std::string encode(const std::wstring &s)
146 typename C::Encoder enc;
152 template<class F, class T>
153 std::string transcode(const std::string &s)
155 typename F::Decoder from;
156 typename T::Encoder to;
159 to.encode(from.get());
164 StringCodec *create_codec(const std::string &);