X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstringcodec%2Fcodec.h;h=eec1e4bddb092a80a03574aa6207c30df53150ea;hb=3e3868860d4f4ca1bbe55fbf271c804307cea3c9;hp=e04e909fb7a1fc5c4750db7e13f33f2d44a96f92;hpb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd;p=libs%2Fcore.git diff --git a/source/stringcodec/codec.h b/source/stringcodec/codec.h index e04e909..eec1e4b 100644 --- a/source/stringcodec/codec.h +++ b/source/stringcodec/codec.h @@ -1,22 +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_ +#ifndef MSP_STRINGCODEC_CODEC_H_ +#define MSP_STRINGCODEC_CODEC_H_ #include #include namespace Msp { -namespace Codecs { +namespace StringCodec { -typedef int UnicodeChar; +typedef int unichar; -typedef std::basic_string ustring; +typedef std::basic_string ustring; enum ErrorMode { @@ -65,7 +58,7 @@ public: /** Encodes a single unicode character. If the character can't be represented in this encoding, error() should be called. */ - virtual void encode_char(UnicodeChar ch, std::string &buf) = 0; + virtual void encode_char(unichar ch, std::string &buf) = 0; /** Encodes a unicode string. This is equivalent to calling encode_char for each character in the string with the same buffer. */ @@ -86,11 +79,11 @@ public: THROW_ON_ERROR: throws CodecError(msg) IGNORE_ERRORS: does nothing TRANSLITERATE: calls transliterate(ch, buf) */ - void error(UnicodeChar ch, std::string &buf, const std::string &msg); + void error(unichar ch, std::string &buf, const std::string &msg); /** Attempts to produce an alternative encoding for a unicode character. Typically this includes dropping accent marks or romanizing letters. */ - virtual void transliterate(UnicodeChar ch, std::string &buf) = 0; + virtual void transliterate(unichar ch, std::string &buf) = 0; }; /** @@ -112,7 +105,7 @@ public: state change sequence was decoded but no character followed it. If invalid input is encountered, the error() function should be called and the iterator advanced only if it doesn't throw. */ - virtual UnicodeChar decode_char(const std::string &str, std::string::const_iterator &i) = 0; + virtual unichar decode_char(const std::string &str, std::string::const_iterator &i) = 0; /** Decodes a string. */ virtual void decode(const std::string &str, ustring &buf); @@ -129,7 +122,7 @@ public: THROW_ON_ERROR: throws CodecError(msg) IGNORE_ERRORS: returns -1 TRANSLITERATE: return 0xFFFE */ - UnicodeChar error(const std::string &msg); + unichar error(const std::string &msg); }; protected: @@ -161,9 +154,7 @@ template ustring decode(const std::string &s) { typename C::Decoder dec; - ustring result; - dec.decode(s, result); - return result; + return dec.decode(s); } /** Convenience function that encodes a string. */ @@ -171,24 +162,14 @@ template std::string encode(const ustring &s) { typename C::Encoder enc; - std::string result; - enc.encode(s, result); - enc.sync(result); - return result; + return enc.encode(s); } /** Convenience function that transcodes a string from one codec to another. */ template std::string transcode(const std::string &s) { - typename F::Decoder from; - typename T::Encoder to; - ustring temp; - from.decode(s, temp); - std::string result; - to.encode(temp, result); - to.sync(result); - return result; + return encode(decode(s)); } /** Creates a codec for an encoding by name. The caller is responsible for @@ -199,7 +180,7 @@ Codec *create_codec(const std::string &); The codec must be deleted when it's no longer needed. */ Codec *detect_codec(const std::string &); -} // namespace Codecs +} // namespace StringCodec } // namespace Msp #endif