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
32 Base class for string encoder. Each codec class should contain an Encoder
33 class derived from this.
39 Encodes a single character. Derived classes should use the append
40 function to put the result into the internal buffer.
42 virtual void encode_char(wchar_t) =0;
47 virtual void encode(const std::wstring &s)
48 { for(std::wstring::const_iterator i=s.begin(); i!=s.end(); ++i) encode_char(*i); }
51 Brings the encoder back to its initial state. This allows the encoded
52 sequence to be extracted or flushed without loss of integrity.
54 virtual void sync() { }
57 Returns a reference to the encoded sequence. Call sync() first to make
58 sure it's a valid encoded string by itself.
60 const std::string &get() const { return buffer_; }
63 Returns the number of bytes in the output buffer.
65 unsigned size() const { return buffer_.size(); }
68 Clears the encoded sequence. Encoder state is left intact.
70 void flush() { buffer_.clear(); }
72 virtual ~Encoder() { }
75 void append(char c) { buffer_+=c; }
76 void append(const char *s, unsigned l) { buffer_.append(s, l); }
77 void append(const std::string &s) { buffer_+=s; }
83 Base class for string decoder. Each codec class should contain an Decoder
84 class derived from this.
89 virtual void decode_char(const std::string &, std::string::const_iterator &) =0;
90 virtual void decode(const std::string &s)
91 { for(std::string::const_iterator i=s.begin(); i!=s.end(); ) decode_char(s, i); }
94 Ensures that all input has been processed. If this is not the case any
95 buffers are cleared and an exception is thrown,
97 virtual void sync() { }
100 Resets the decoder, clearing a possibly erroneus state. Does not flush
103 virtual void reset() { }
105 const std::wstring &get() const { return buffer_; }
106 unsigned size() const { return buffer_.size(); }
107 void flush() { buffer_.clear(); }
108 virtual ~Decoder() { }
111 void append(wchar_t c) { buffer_+=c; }
112 void append(const std::wstring &s) { buffer_+=s; }
114 std::wstring buffer_;
117 virtual Encoder *create_encoder() const =0;
118 virtual Decoder *create_decoder() const =0;
119 virtual bool detect(const std::string &) const;
120 virtual ~StringCodec() { }
126 Convenience function that decodes a string using the given codec.
129 std::wstring decode(const std::string &s)
131 typename C::Decoder dec;
138 std::string encode(const std::wstring &s)
140 typename C::Encoder enc;
146 template<class F, class T>
147 std::string transcode(const std::string &s)
149 typename F::Decoder from;
150 typename T::Encoder to;
153 to.encode(from.get());
158 StringCodec *create_codec(const std::string &);