]> git.tdb.fi Git - libs/core.git/blob - source/codec.cpp
Rework the codec API completely to remove the internal buffering
[libs/core.git] / source / codec.cpp
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #include "ascii.h"
9 #include "codec.h"
10 #include "iso2022jp.h"
11 #include "iso646fi.h"
12 #include "iso88591.h"
13 #include "jisx0201.h"
14 #include "jisx0208.h"
15 #include "utf8.h"
16 #include "windows1252.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace Codecs {
22
23 bool Codec::detect(const string &str) const
24 {
25         Decoder *dec=create_decoder();
26         bool result=true;
27         try
28         {
29                 for(string::const_iterator i=str.begin(); i!=str.end(); )
30                         dec->decode_char(str, i);
31         }
32         catch(const CodecError &)
33         {
34                 result=false;
35         }
36
37         delete dec;
38
39         return result;
40 }
41
42 void Codec::Encoder::encode(const ustring &str, string &buf)
43 {
44         for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i)
45                 encode_char(*i, buf);
46 }
47
48 void Codec::Encoder::error(UnicodeChar ch, string &buf, const string &msg)
49 {
50         switch(err_mode)
51         {
52         case TRANSLITERATE:
53                 transliterate(ch, buf);
54         case IGNORE_ERRORS:
55                 break;
56         default:
57                 throw CodecError(msg);
58         }
59 }
60
61
62 void Codec::Decoder::decode(const string &str, ustring &buf)
63 {
64         for(string::const_iterator i=str.begin(); i!=str.end();)
65         {
66                 UnicodeChar c=decode_char(str, i);
67                 if(c!=-1)
68                         buf+=c;
69         }
70 }
71
72 UnicodeChar Codec::Decoder::error(const string &msg)
73 {
74         switch(err_mode)
75         {
76         case TRANSLITERATE:
77                 return 0xFFFE;
78         case IGNORE_ERRORS:
79                 return -1;
80         default:
81                 throw CodecError(msg);
82         }
83 }
84
85 Codec *create_codec(const string &n)
86 {
87         string name;
88         for(string::const_iterator i=n.begin(); i!=n.end(); ++i)
89         {
90                 if(isupper(*i))
91                         name+=tolower(*i);
92                 else if(islower(*i) || isdigit(*i))
93                         name+=*i;
94         }
95
96         if(name=="ascii") return new Ascii;
97         if(name=="iso2022jp") return new Iso2022Jp;
98         if(name=="iso646fi") return new Iso646Fi;
99         if(name=="iso88591" || name=="latin1") return new Iso88591;
100         if(name=="jisx0201") return new JisX0201;
101         if(name=="jisx0208") return new JisX0208;
102         if(name=="utf8") return new Utf8;
103         if(name=="windows1252") return new Windows1252;
104         throw InvalidParameterValue("Unknown string codec");
105 }
106
107 } // namespace Codecs
108 } // namespace Msp