X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcodec.h;h=7acd88f89008e6e50b72333f4437172907dd7b55;hp=5fb29db29b305577582e6ed56c58f3bda61d823c;hb=d2118ac101602cfe2d62fb7deb6ef3fcb0fe137b;hpb=dbda1bb7f44f289c9f1c5ba9741970ac264d8e5d diff --git a/source/codec.h b/source/codec.h index 5fb29db..7acd88f 100644 --- a/source/codec.h +++ b/source/codec.h @@ -28,6 +28,13 @@ 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. @@ -71,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_; }; @@ -92,30 +102,26 @@ public: /** Ensures that all input has been processed. If this is not the case any - buffers are cleared and an exception is thrown, + buffers are cleared and an error is triggered. */ virtual void sync() { } - /** - Resets the decoder, clearing a possibly erroneus state. Does not flush - the internal buffer. - */ - virtual void reset() { } - const std::wstring &get() const { return buffer_; } unsigned size() const { return buffer_.size(); } 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: