]> git.tdb.fi Git - libs/core.git/blob - source/iso646fi.cpp
More sophisticated error handling
[libs/core.git] / source / iso646fi.cpp
1 #include "iso646fi.h"
2
3 using namespace std;
4
5 namespace Msp {
6
7 void Iso646Fi::Encoder::encode_char(wchar_t c_)
8 {
9         // Win32 has typedef unsigned short wchar_t
10         int c=c_;
11
12         if((c>=0 && c<=0x5A) || c==0x5F || (c>=0x61 && c<=0x7A))
13                 append(c);
14         else if(c==0xC4)
15                 append(0x5B);
16         else if(c==0xC5)
17                 append(0x5D);
18         else if(c==0xD6)
19                 append(0x5C);
20         else if(c==0xDC)
21                 append(0x5E);
22         else if(c==0xE4)
23                 append(0x7B);
24         else if(c==0xE5)
25                 append(0x7D);
26         else if(c==0xE9)
27                 append(0x60);
28         else if(c==0xF6)
29                 append(0x7C);
30         else if(c==0xFC)
31                 append(0x7E);
32         else
33                 error("Can't express character in ISO-646-FI");
34 }
35
36 void Iso646Fi::Decoder::decode_char(const string &str, string::const_iterator &i)
37 {
38         if(i==str.end())
39                 return;
40
41         unsigned char c=*i++;
42         if(c==0x5B)
43                 append(0xC4);
44         else if(c==0x5C)
45                 append(0xD6);
46         else if(c==0x5D)
47                 append(0xC5);
48         else if(c==0x5E)
49                 append(0xDC);
50         else if(c==0x60)
51                 append(0xE9);
52         else if(c==0x7B)
53                 append(0xE4);
54         else if(c==0x7C)
55                 append(0xF6);
56         else if(c==0x7D)
57                 append(0xE5);
58         else if(c==0x7E)
59                 append(0xFC);
60         else if(c<=0x7F)
61                 append(c);
62         else
63                 error("Invalid ISO-646-FI string (undefined character)");
64 }
65
66 }