summaryrefslogtreecommitdiff
path: root/src/iso6937.cc
blob: d6c1970c9d41aebfe5d72f1c5e937cb83631f827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <string>
#include <boost/optional.hpp>
#include "iso6937_tables.h"
#include "iso6937.h"

using std::string;
using std::cout;
using std::wstring;
using namespace sub;

wstring
sub::iso6937_to_utf16 (char const * s)
{
	if (iso6937::grave.empty ()) {
		make_iso6937_tables ();
	}
	
	wstring o;

	boost::optional<unsigned char> diacritical;

	while (*s != '\0') {
		unsigned char const u = static_cast<unsigned char> (*s);
		if (u >= 0xc1 && u <= 0xcf) {
			diacritical = u;
		} else if (diacritical) {
			switch (diacritical.get ()) {
			case 0xC1:
				o += iso6937::grave[u];
				break;
			case 0xC2:
				o += iso6937::acute[u];
				break;
			case 0xC3:
				o += iso6937::circumflex[u];
				break;
			case 0xC4:
				o += iso6937::tilde[u];
				break;
			case 0xC5:
				o += iso6937::macron[u];
				break;
			case 0xC6:
				o += iso6937::breve[u];
				break;
			case 0xC7:
				o += iso6937::dot[u];
				break;
			case 0xC8:
				o += iso6937::diaeresis[u];
				break;
			case 0xCA:
				o += iso6937::ring[u];
				break;
			case 0xCB:
				o += iso6937::cedilla[u];
				break;
			case 0xCD:
				o += iso6937::double_acute[u];
				break;
			case 0xCE:
				o += iso6937::ogonek[u];
				break;
			case 0xCF:
				o += iso6937::caron[u];
				break;
			}

			diacritical.reset ();
		} else {
			o += iso6937::main[u];
		}

		++s;
	}

	return o;
}