]> git.tdb.fi Git - libs/core.git/blobdiff - source/windows1252.cpp
Rework the codec API completely to remove the internal buffering
[libs/core.git] / source / windows1252.cpp
index db9d417685b9c9314a616ac69854a9ea32ff9562..d809c3ff9bad7616a0e053f600edd72f2a207866 100644 (file)
@@ -22,44 +22,50 @@ unsigned short table[32]=
 }
 
 namespace Msp {
 }
 
 namespace Msp {
+namespace Codecs {
 
 
-void Windows1252::Encoder::encode_char(wchar_t c_)
+void Windows1252::Encoder::encode_char(UnicodeChar ch, string &buf)
 {
 {
-       int c=c_;
-       if((c>=0 && c<=0x7F) || (c>=0xA0 && c<=0xFF))
-               append(c);
+       if((ch>=0 && ch<=0x7F) || (ch>=0xA0 && ch<=0xFF))
+               buf+=ch;
        else
        {
        else
        {
-               unsigned i;
-               for(i=0; i<32; ++i)
-                       if(table[i]==c)
+               for(unsigned i=0; i<32; ++i)
+                       if(table[i]==ch)
                        {
                        {
-                               append(c);
-                               break;
+                               buf+=ch;
+                               return;
                        }
                        }
-               
-               if(i==32)
-                       error("Can't express character in Windows-1252");
+
+               error(ch, buf, "Can't express character in Windows-1252");
        }
 }
 
        }
 }
 
-void Windows1252::Decoder::decode_char(const string &str, string::const_iterator &i)
+void Windows1252::Encoder::transliterate(UnicodeChar, string &buf)
+{
+       buf+='?';
+}
+
+
+UnicodeChar Windows1252::Decoder::decode_char(const string &str, string::const_iterator &i)
 {
        if(i==str.end())
 {
        if(i==str.end())
-               return;
-       else
+               return error("No input");
+
+       int ch=static_cast<unsigned char>(*i);
+       UnicodeChar result;
+       if(ch>=0x80 && ch<=0x9F)
        {
        {
-               int c=static_cast<unsigned char>(*i++);
-               if(c>=0x80 && c<=0x9F)
-               {
-                       c-=0x80;
-                       if(table[c]==0)
-                               error("Invalid Windows-1252 string (undefined character)");
-                       append(table[c]);
-               }
-               else
-                       append(c);
+               result=table[ch-0x80];
+               if(result==0)
+                       result=error("Undefined Windows-1252 character");
        }
        }
+       else
+               result=ch;
+
+       ++i;
+       return result;
 }
 
 }
 
+} // namespace Codecs
 } // namespace Msp
 } // namespace Msp