]> git.tdb.fi Git - libs/core.git/blob - source/windows1252.cpp
db9d417685b9c9314a616ac69854a9ea32ff9562
[libs/core.git] / source / 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 namespace Msp {
25
26 void Windows1252::Encoder::encode_char(wchar_t c_)
27 {
28         int c=c_;
29         if((c>=0 && c<=0x7F) || (c>=0xA0 && c<=0xFF))
30                 append(c);
31         else
32         {
33                 unsigned i;
34                 for(i=0; i<32; ++i)
35                         if(table[i]==c)
36                         {
37                                 append(c);
38                                 break;
39                         }
40                 
41                 if(i==32)
42                         error("Can't express character in Windows-1252");
43         }
44 }
45
46 void Windows1252::Decoder::decode_char(const string &str, string::const_iterator &i)
47 {
48         if(i==str.end())
49                 return;
50         else
51         {
52                 int c=static_cast<unsigned char>(*i++);
53                 if(c>=0x80 && c<=0x9F)
54                 {
55                         c-=0x80;
56                         if(table[c]==0)
57                                 error("Invalid Windows-1252 string (undefined character)");
58                         append(table[c]);
59                 }
60                 else
61                         append(c);
62         }
63 }
64
65 } // namespace Msp