]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/windows1252.cpp
Move files around to prepare for assimilation into core
[libs/core.git] / source / stringcodec / windows1252.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 "windows1252.h"
9
10 using namespace std;
11
12 namespace {
13
14 unsigned short table[32]=
15 {
16         0x20AC, 0,      0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
17         0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0,      0x017D, 0,
18         0,      0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
19         0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0,      0x017E, 0x0178
20 };
21
22 }
23
24
25 namespace Msp {
26 namespace Codecs {
27
28 void Windows1252::Encoder::encode_char(UnicodeChar ch, string &buf)
29 {
30         if((ch>=0 && ch<=0x7F) || (ch>=0xA0 && ch<=0xFF))
31                 buf += ch;
32         else
33         {
34                 for(unsigned i=0; i<32; ++i)
35                         if(table[i]==ch)
36                         {
37                                 buf += ch;
38                                 return;
39                         }
40
41                 error(ch, buf, "Can't express character in Windows-1252");
42         }
43 }
44
45 void Windows1252::Encoder::transliterate(UnicodeChar, string &buf)
46 {
47         buf += '?';
48 }
49
50
51 UnicodeChar Windows1252::Decoder::decode_char(const string &str, string::const_iterator &i)
52 {
53         if(i==str.end())
54                 return error("No input");
55
56         int ch = static_cast<unsigned char>(*i);
57         UnicodeChar result;
58         if(ch>=0x80 && ch<=0x9F)
59         {
60                 result = table[ch-0x80];
61                 if(result==0)
62                         result = error("Undefined Windows-1252 character");
63         }
64         else
65                 result = ch;
66
67         ++i;
68         return result;
69 }
70
71 } // namespace Codecs
72 } // namespace Msp