More header guards.
[libsub.git] / src / iso6937.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <string>
21 #include <boost/optional.hpp>
22 #include <boost/locale.hpp>
23 #include "iso6937_tables.h"
24 #include "iso6937.h"
25
26 using std::string;
27 using std::cout;
28 using std::wcout;
29 using std::wstring;
30 using std::map;
31 using boost::optional;
32 using boost::locale::conv::utf_to_utf;
33 using namespace sub;
34
35 wstring
36 sub::iso6937_to_utf16 (string s)
37 {
38         if (iso6937::diacriticals.empty ()) {
39                 make_iso6937_tables ();
40         }
41         
42         wstring o;
43
44         boost::optional<unsigned char> diacritical;
45
46         int i = 0;
47         while (s[i] != '\0') {
48                 unsigned char const u = static_cast<unsigned char> (s[i]);
49                 if (u >= 0xc1 && u <= 0xcf) {
50                         diacritical = u;
51                 } else if (diacritical) {
52                         o += (*iso6937::diacriticals[diacritical.get()])[u];
53                         diacritical.reset ();
54                 } else {
55                         o += iso6937::main[u];
56                 }
57
58                 ++i;
59         }
60
61         return o;
62 }
63
64 static optional<char>
65 find (map<char, wchar_t> const & m, wchar_t c)
66 {
67         for (map<char, wchar_t>::const_iterator i = m.begin(); i != m.end(); ++i) {
68                 if (i->second == c) {
69                         return i->first;
70                 }
71         }
72
73         return optional<char> ();
74 }
75
76 string
77 sub::utf16_to_iso6937 (wstring s)
78 {
79         if (iso6937::diacriticals.empty ()) {
80                 make_iso6937_tables ();
81         }
82         
83         /* XXX: slow */
84
85         string o;
86         for (size_t i = 0; i < s.size(); ++i) {
87                 optional<char> c = find (iso6937::main, s[i]);
88                 if (c) {
89                         o += c.get ();
90                 } else {
91                         for (map<char, map<char, wchar_t> *>::const_iterator j = iso6937::diacriticals.begin(); j != iso6937::diacriticals.end(); ++j) {
92                                 c = find (*(j->second), s[i]);
93                                 if (c) {
94                                         o += j->first;
95                                         o += c.get ();
96                                         break;
97                                 }
98                         }
99                 }
100         }
101
102         return o;
103 }
104