X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcodec.cpp;h=c38d82889502c8804bcd56d0b1af0b8a481bde30;hp=96d18c7c922ac51aaa14bbec8138b0b8090a98bd;hb=f47bc86e6ce900c5323e593db003c93110538268;hpb=79d472ad3fde75de2eba2487579b047d35e56978 diff --git a/source/codec.cpp b/source/codec.cpp index 96d18c7..c38d828 100644 --- a/source/codec.cpp +++ b/source/codec.cpp @@ -18,14 +18,9 @@ Distributed under the LGPL using namespace std; namespace Msp { +namespace Codecs { -/** -Determines whether the given string can be successfully decoded with this -codec. Note that this function returning true does not guarantee that the -string was actually encoded with this codec. In particular, many 8-bit -encodings are indistinguishable. -*/ -bool StringCodec::detect(const string &str) const +bool Codec::detect(const string &str) const { Decoder *dec=create_decoder(); bool result=true; @@ -33,7 +28,6 @@ bool StringCodec::detect(const string &str) const { for(string::const_iterator i=str.begin(); i!=str.end(); ) dec->decode_char(str, i); - dec->sync(); } catch(const CodecError &) { @@ -45,31 +39,50 @@ bool StringCodec::detect(const string &str) const return result; } -void StringCodec::Encoder::error(const string &msg) +void Codec::Encoder::encode(const ustring &str, string &buf) +{ + for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i) + encode_char(*i, buf); +} + +void Codec::Encoder::error(UnicodeChar ch, string &buf, const string &msg) { - switch(err_mode_) + switch(err_mode) { - case IGNORE_ERRORS: break; - case REPLACE_ERRORS: append_replacement(); break; - default: throw CodecError(msg); + case TRANSLITERATE: + transliterate(ch, buf); + case IGNORE_ERRORS: + break; + default: + throw CodecError(msg); } } -void StringCodec::Decoder::error(const string &msg) + +void Codec::Decoder::decode(const string &str, ustring &buf) { - switch(err_mode_) + for(string::const_iterator i=str.begin(); i!=str.end();) { - case IGNORE_ERRORS: break; - case REPLACE_ERRORS: append(0xFFFD); break; - default: throw CodecError(msg); + UnicodeChar c=decode_char(str, i); + if(c!=-1) + buf+=c; } } -/** -Creates a codec for the given encoding. The caller is responsible for deleting -the codec when it's no longer needed. -*/ -StringCodec *create_codec(const string &n) +UnicodeChar Codec::Decoder::error(const string &msg) +{ + switch(err_mode) + { + case TRANSLITERATE: + return 0xFFFE; + case IGNORE_ERRORS: + return -1; + default: + throw CodecError(msg); + } +} + +Codec *create_codec(const string &n) { string name; for(string::const_iterator i=n.begin(); i!=n.end(); ++i) @@ -91,4 +104,5 @@ StringCodec *create_codec(const string &n) throw InvalidParameterValue("Unknown string codec"); } +} // namespace Codecs } // namespace Msp