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. Mostly abstract. Use one of the derived classes
21 or the function 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. Codecs are able to determine if an encoded string could be
31 Base class for string encoder. Each codec class should contain an Encoder
32 class derived from this.
38 Encodes a single character. Derived classes should use the append
39 function to put the result into the internal buffer.
41 virtual void encode_char(wchar_t) =0;
46 virtual void encode(const std::wstring &s)
47 { for(std::wstring::const_iterator i=s.begin(); i!=s.end(); ++i) encode_char(*i); }
50 Brings the encoder back to its initial state. This allows the encoded
51 sequence to be extracted or flushed without loss of integrity.
53 virtual void sync() { }
56 Returns a reference to the encoded sequence. Call sync() first to make
57 sure it's a valid encoded string by itself.
59 const std::string &get() const { return buffer_; }
62 Returns the number of bytes in the output buffer.
64 unsigned size() const { return buffer_.size(); }
67 Clears the encoded sequence. Encoder state is left intact.
69 void flush() { buffer_.clear(); }
71 virtual ~Encoder() { }
74 void append(char c) { buffer_+=c; }
75 void append(const char *s, unsigned l) { buffer_.append(s, l); }
76 void append(const std::string &s) { buffer_+=s; }
82 Base class for string decoder. Each codec class should contain an Decoder
83 class derived from this.
88 virtual void decode_char(const std::string &, std::string::const_iterator &) =0;
89 virtual void decode(const std::string &s)
90 { for(std::string::const_iterator i=s.begin(); i!=s.end(); ) decode_char(s, i); }
93 Ensures that all input has been processed. An exception is thrown if
96 virtual void sync() { }
98 const std::wstring &get() const { return buffer_; }
99 unsigned size() const { return buffer_.size(); }
100 void flush() { buffer_.clear(); }
101 virtual ~Decoder() { }
104 void append(wchar_t c) { buffer_+=c; }
105 void append(const std::wstring &s) { buffer_+=s; }
107 std::wstring buffer_;
110 virtual Encoder *create_encoder() const =0;
111 virtual Decoder *create_decoder() const =0;
112 virtual bool detect(const std::string &) const;
113 virtual ~StringCodec() { }
119 Convenience function that decodes a string using the given codec.
122 std::wstring decode(const std::string &s)
124 typename C::Decoder dec;
131 std::string encode(const std::wstring &s)
133 typename C::Encoder enc;
139 template<class F, class T>
140 std::string transcode(const std::string &s)
142 typename F::Decoder from;
143 typename T::Encoder to;
146 to.encode(from.get());
151 StringCodec *create_codec(const std::string &);