]> git.tdb.fi Git - libs/core.git/blobdiff - source/codec.h
Add glob and regex thingies
[libs/core.git] / source / codec.h
index d5ba595b3b3ee765caa574479c766c92f27281c8..b194c3f46b56b4be503ff63e2741f8243f43c3fa 100644 (file)
@@ -1,8 +1,15 @@
+/* $Id$
+
+This file is part of libmspstrings
+Copyright © 2006-2007 Mikko Rasa
+Distributed under the LGPL
+*/
+
 #ifndef MSP_STRINGS_CODEC_H_
 #define MSP_STRINGS_CODEC_H_
 
 #include <string>
-#include <msp/error.h>
+#include <msp/core/error.h>
 
 namespace Msp {
 
@@ -28,6 +35,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 +85,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_;
        };
 
@@ -91,8 +108,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() { }
 
@@ -101,15 +118,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: