X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstringcodec%2Fcodec.h;h=00f03b0cc4b4c4f2f4fcd133f2d3fa67e9fac5fb;hb=94ee3a1040f67d9de2e92fc34049642b08d65b3e;hp=cb8006696e52c17042e304cc66efc1ef5d644815;hpb=c5cb2162baeeb7c750595e07ba1cbfcb03702f77;p=libs%2Fcore.git diff --git a/source/stringcodec/codec.h b/source/stringcodec/codec.h index cb80066..00f03b0 100644 --- a/source/stringcodec/codec.h +++ b/source/stringcodec/codec.h @@ -2,15 +2,12 @@ #define MSP_STRINGCODEC_CODEC_H_ #include -#include +#include "except.h" +#include "ustring.h" namespace Msp { namespace StringCodec { -typedef int unichar; - -typedef std::basic_string ustring; - enum ErrorMode { THROW_ON_ERROR, @@ -18,15 +15,6 @@ enum ErrorMode TRANSLITERATE }; -/** -An exception thrown for all kinds of problems encountered while encoding or -decoding strings. -*/ -class CodecError: public Exception -{ -public: - CodecError(const std::string &w_): Exception(w_) { } -}; /** Base class for string codecs. Use one of the derived classes or the function @@ -76,10 +64,17 @@ public: protected: /** Handles an error depending on the error mode. - THROW_ON_ERROR: throws CodecError(msg) + THROW_ON_ERROR: throws err IGNORE_ERRORS: does nothing TRANSLITERATE: calls transliterate(ch, buf) */ - void error(unichar ch, std::string &buf, const std::string &msg); + template + void error(unichar ch, std::string &buf, const E &err) + { + if(err_mode==TRANSLITERATE) + transliterate(ch, buf); + else if(err_mode!=IGNORE_ERRORS) + throw err; + } /** Attempts to produce an alternative encoding for a unicode character. Typically this includes dropping accent marks or romanizing letters. */ @@ -119,10 +114,19 @@ public: /** Handles an error depending on the error mode. The return value is suitable for returning from decode_char. - THROW_ON_ERROR: throws CodecError(msg) + THROW_ON_ERROR: throws err IGNORE_ERRORS: returns -1 TRANSLITERATE: return 0xFFFD */ - unichar error(const std::string &msg); + template + unichar error(const E &err) + { + if(err_mode==TRANSLITERATE) + return 0xFFFD; + else if(err_mode==IGNORE_ERRORS) + return -1; + else + throw err; + } }; protected: