X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcodec.h;h=7acd88f89008e6e50b72333f4437172907dd7b55;hp=8ea99915c2451fc161fc53d16e482ebef77a28a7;hb=d2118ac101602cfe2d62fb7deb6ef3fcb0fe137b;hpb=58384e355b4a78730d69243f1092e47591f2f384 diff --git a/source/codec.h b/source/codec.h index 8ea9991..7acd88f 100644 --- a/source/codec.h +++ b/source/codec.h @@ -17,16 +17,24 @@ public: }; /** -Base class for string codecs. Mostly abstract. Use one of the derived classes -or the function create_codec to create a specific codec. +Base class for string codecs. Use one of the derived classes or the function +create_codec to create a specific codec. For the purposes of this class, an std::wstring is considered to contain Unicode characters and an std::string is considered to be an encoded sequence -of bytes. Codecs are able to determine if an encoded string could be +of bytes. A codec is able to determine if an encoded string could be decoded +with it. */ class StringCodec { public: + enum ErrorMode + { + THROW_ON_ERROR, + IGNORE_ERRORS, + REPLACE_ERRORS + }; + /** Base class for string encoder. Each codec class should contain an Encoder class derived from this. @@ -39,13 +47,13 @@ public: function to put the result into the internal buffer. */ virtual void encode_char(wchar_t) =0; - + /** Encodes a string. */ virtual void encode(const std::wstring &s) { for(std::wstring::const_iterator i=s.begin(); i!=s.end(); ++i) encode_char(*i); } - + /** Brings the encoder back to its initial state. This allows the encoded sequence to be extracted or flushed without loss of integrity. @@ -62,7 +70,7 @@ public: Returns the number of bytes in the output buffer. */ unsigned size() const { return buffer_.size(); } - + /** Clears the encoded sequence. Encoder state is left intact. */ @@ -70,11 +78,14 @@ public: virtual ~Encoder() { } protected: - Encoder() { } + Encoder(ErrorMode em=THROW_ON_ERROR): err_mode_(em) { } void append(char c) { buffer_+=c; } void append(const char *s, unsigned l) { buffer_.append(s, l); } void append(const std::string &s) { buffer_+=s; } + void error(const std::string &); + virtual void append_replacement() { } private: + ErrorMode err_mode_; std::string buffer_; }; @@ -90,8 +101,8 @@ public: { for(std::string::const_iterator i=s.begin(); i!=s.end(); ) decode_char(s, i); } /** - Ensures that all input has been processed. An exception is thrown if - this is not the case. + Ensures that all input has been processed. If this is not the case any + buffers are cleared and an error is triggered. */ virtual void sync() { } @@ -100,15 +111,17 @@ public: void flush() { buffer_.clear(); } virtual ~Decoder() { } protected: - Decoder() { } + Decoder(ErrorMode em): err_mode_(em) { } void append(wchar_t c) { buffer_+=c; } void append(const std::wstring &s) { buffer_+=s; } + void error(const std::string &); private: + ErrorMode err_mode_; std::wstring buffer_; }; - virtual Encoder *create_encoder() const =0; - virtual Decoder *create_decoder() const =0; + virtual Encoder *create_encoder(ErrorMode =THROW_ON_ERROR) const =0; + virtual Decoder *create_decoder(ErrorMode =THROW_ON_ERROR) const =0; virtual bool detect(const std::string &) const; virtual ~StringCodec() { } protected: @@ -116,7 +129,7 @@ protected: }; /** -Convenience function that decodes a string using the given codec. +Convenience function that decodes a string using the given codec. */ template std::wstring decode(const std::string &s)