izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
a60-svg-codecvt.h
Go to the documentation of this file.
1// alpha60 codecvt utf8 -*- mode: C++ -*-
2
3// alpha60
4// bittorrent x scrape x data + analytics
5
6// Copyright (c) 2016-2024, Benjamin De Kosnik <b.dekosnik@gmail.com>
7
8// This file is part of the alpha60 library. This library is free
9// software; you can redistribute it and/or modify it under the terms
10// of the GNU General Public License as published by the Free Software
11// Foundation; either version 3, or (at your option) any later
12// version.
13
14// This library is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// General Public License for more details.
18
19#ifndef a60_SVG_CODECVT_H
20#define a60_SVG_CODECVT_H 1
21
22#include <ext/codecvt_specializations.h>
23
24
25namespace svg {
26
27/// Convert from ISO-8859-1 encoding to UTF-8, required for XML.
28string
30{
31 using namespace std;
32
33 using result = codecvt_base::result;
34 using int_type = char;
35 using ext_type = char;
36 using state_type = __gnu_cxx::encoding_state;
37 using ccutf8_codecvt = codecvt<int_type, ext_type, state_type>;
38
39 string out;
40 const int_type* internal_ptr;
41 ext_type* external_ptr;
42 ext_type* buf = new int_type[in.size() * 3];
43
44 // Use iconv on linux, ie `iconv --list`
45
46 // Internal encoding is ISO 8859 and external is UTF-8.
47 state_type state("ISO-8859-16", "UTF-8", 0, 0);
48
49 ccutf8_codecvt* cvtf8 = new ccutf8_codecvt;
50 locale loc(locale::classic(), cvtf8);
51 const ccutf8_codecvt& cvt = use_facet<ccutf8_codecvt>(loc);
52 result r = cvt.out(state, in.c_str(), in.c_str() + in.size(), internal_ptr,
53 buf, buf + in.size() * 3, external_ptr);
54 if (r == codecvt_base::ok)
55 out = string(buf, external_ptr);
56 else
57 {
58 if (r == codecvt_base::partial)
59 {
60 ext_type* n_ptr = external_ptr;
61 while (n_ptr > external_ptr)
62 {
63 external_ptr = n_ptr;
64 r = cvt.in(state, in.c_str(), in.c_str() + in.size(),
65 internal_ptr, buf, buf + in.size() * 3, n_ptr);
66 }
67
68 if (r == codecvt_base::ok)
69 out = string(buf, n_ptr);
70 else
71 {
72 std::cerr << "convert_to_utf8: partial at "
73 << ulong(n_ptr - in.c_str()) << " "
74 << in << std::endl;
75 out = in;
76 }
77 }
78 if (r == codecvt_base::error)
79 {
80 std::cerr << "convert_to_utf8: error " << in << std::endl;
81 out = in;
82 }
83 }
84
85 // Any escape characters may have to be wrapped for HTML with &#[0][0][0];
86 //delete cvtf8;
87 delete [] buf;
88
89 return out;
90}
91
92
93string
96
97} // namespace svg
98#endif
unsigned long ulong
Definition a60-svg.h:58
string convert_to_utf8(string in)
string convert_8859_to_utf8(string in)
Convert from ISO-8859-1 encoding to UTF-8, required for XML.