]> git.tdb.fi Git - libs/core.git/blobdiff - source/codec.cpp
Rework the codec API completely to remove the internal buffering
[libs/core.git] / source / codec.cpp
index efc981492f18a08d766eb0c39932548adecfd8c5..c38d82889502c8804bcd56d0b1af0b8a481bde30 100644 (file)
@@ -1,23 +1,26 @@
+/* $Id$
+
+This file is part of libmspstrings
+Copyright © 2006-2007 Mikko Rasa
+Distributed under the LGPL
+*/
+
 #include "ascii.h"
 #include "codec.h"
 #include "iso2022jp.h"
 #include "iso646fi.h"
+#include "iso88591.h"
 #include "jisx0201.h"
 #include "jisx0208.h"
-#include "latin1.h"
 #include "utf8.h"
+#include "windows1252.h"
 
 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;
@@ -36,11 +39,50 @@ bool StringCodec::detect(const string &str) const
        return result;
 }
 
-/**
-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)
+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)
+       {
+       case TRANSLITERATE:
+               transliterate(ch, buf);
+       case IGNORE_ERRORS:
+               break;
+       default:
+               throw CodecError(msg);
+       }
+}
+
+
+void Codec::Decoder::decode(const string &str, ustring &buf)
+{
+       for(string::const_iterator i=str.begin(); i!=str.end();)
+       {
+               UnicodeChar c=decode_char(str, i);
+               if(c!=-1)
+                       buf+=c;
+       }
+}
+
+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)
@@ -54,11 +96,13 @@ StringCodec *create_codec(const string &n)
        if(name=="ascii") return new Ascii;
        if(name=="iso2022jp") return new Iso2022Jp;
        if(name=="iso646fi") return new Iso646Fi;
+       if(name=="iso88591" || name=="latin1") return new Iso88591;
        if(name=="jisx0201") return new JisX0201;
        if(name=="jisx0208") return new JisX0208;
-       if(name=="latin1") return new Latin1;
        if(name=="utf8") return new Utf8;
+       if(name=="windows1252") return new Windows1252;
        throw InvalidParameterValue("Unknown string codec");
 }
 
+} // namespace Codecs
 } // namespace Msp