]> git.tdb.fi Git - libs/core.git/blob - source/iso646fi.cpp
Add a function to perform simple character mapping
[libs/core.git] / source / iso646fi.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 "codecutils.h"
9 #include "iso646fi.h"
10
11 using namespace std;
12
13 namespace {
14
15 const unsigned map_size=9;
16
17 const int mapping[map_size*2]=
18 {
19         0xC4, 0x5B,
20         0xC5, 0x5D,
21         0xD6, 0x5C,
22         0xDC, 0x5E,
23         0xE4, 0x7B,
24         0xE5, 0x7D,
25         0xE9, 0x60,
26         0xF6, 0x7C,
27         0xFC, 0x7E
28 };
29
30 } // namespace
31
32 namespace Msp {
33 namespace Codecs {
34
35 void Iso646Fi::Encoder::encode_char(UnicodeChar ch, string &buf)
36 {
37         int tch=transform_mapping_or_direct(mapping, map_size, ch, false);
38         if(tch<0 || tch>0x7F)
39                 error(ch, buf, "Can't express character in ISO-646-FI");
40         else
41                 buf+=tch;
42 }
43
44 void Iso646Fi::Encoder::transliterate(UnicodeChar, string &buf)
45 {
46         buf+='?';
47 }
48
49
50 UnicodeChar Iso646Fi::Decoder::decode_char(const string &str, string::const_iterator &i)
51 {
52         if(i==str.end())
53                 return error("No input");
54
55         unsigned char ch=*i;
56         int tch=(ch<=0x7F ? transform_mapping_or_direct(mapping, map_size, ch, true) : -1);
57
58         UnicodeChar result;
59         if(tch==-1)
60                 result=error("Undefined ISO-646-FI character");
61         else
62                 result=tch;
63
64         ++i;
65         return result;
66 }
67
68 } // namespace Codecs
69 } // namespace Msp