]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/windows1252.cpp
Additional adjustments for Poller
[libs/core.git] / source / stringcodec / windows1252.cpp
1 #include "windows1252.h"
2
3 using namespace std;
4
5 namespace {
6
7 unsigned short table[32]=
8 {
9         0x20AC, 0,      0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
10         0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0,      0x017D, 0,
11         0,      0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
12         0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0,      0x017E, 0x0178
13 };
14
15 }
16
17
18 namespace Msp {
19 namespace StringCodec {
20
21 void Windows1252::Encoder::encode_char(unichar ch, string &buf)
22 {
23         if((ch>=0 && ch<=0x7F) || (ch>=0xA0 && ch<=0xFF))
24                 buf += ch;
25         else
26         {
27                 for(unsigned i=0; i<32; ++i)
28                         if(table[i]==ch)
29                         {
30                                 buf += ch;
31                                 return;
32                         }
33
34                 error(ch, buf, invalid_character(ch, "Windows-1252"));
35         }
36 }
37
38 void Windows1252::Encoder::transliterate(unichar, string &buf)
39 {
40         buf += '?';
41 }
42
43
44 unichar Windows1252::Decoder::decode_char(const string &str, string::const_iterator &i)
45 {
46         if(i==str.end())
47                 return -1;
48
49         int ch = static_cast<unsigned char>(*i);
50         unichar result;
51         if(ch>=0x80 && ch<=0x9F)
52         {
53                 result = table[ch-0x80];
54                 if(result==0)
55                         result = error(invalid_sequence(i, i+1, "undefined Windows-1252 character"));
56         }
57         else
58                 result = ch;
59
60         ++i;
61         return result;
62 }
63
64 } // namespace StringCodec
65 } // namespace Msp