Repository navigation
Expand file tree
/
Copy pathxml_source.cpp
More file actions
279 lines (251 loc) · 6.12 KB
/
Copy pathxml_source.cpp
File metadata and controls
279 lines (251 loc) · 6.12 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
*
* Copyright (c) 2016-2019
* Viktor Gubin
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#include "stdafx.hpp"
#include "xml_source.hpp"
#include "strings.hpp"
namespace io {
namespace xml {
const std::size_t source::READ_BUFF_INITIAL_SIZE = memory_traits::page_size(); // 4k in most cases
const std::size_t source::READ_BUFF_MAXIMAL_SIZE = 0x1000000; // 16 m
static constexpr unsigned int ASCII_CP_CODE = 20127;
static constexpr unsigned int ISO_LATIN1_CP_CODE = 28591;
static constexpr unsigned int WINDOWS_LATIN1_CP_CODE = 1252;
static constexpr unsigned int UTF8_CP_CODE = 65001;
static constexpr char NL = '\n';
static constexpr char CR = '\r';
static bool is_utf16(const uint8_t* bom)
{
return utf_16le_bom::is(bom) || utf_16be_bom::is(bom);
}
static bool is_utf32(const uint8_t* bom)
{
return utf_32be_bom::is(bom) || utf_32le_bom::is(bom);
}
static s_read_channel open_convert_channel(std::error_code& ec,io::byte_buffer& rb, const uint8_t* pos, const charset& ch, const s_read_channel &src) noexcept
{
byte_buffer new_rb;
if( is_utf16(pos) )
pos += 2;
else if( is_utf32(pos) )
pos += 4;
new_rb = byte_buffer::allocate( ec, rb.capacity() );
s_code_cnvtr cnv = code_cnvtr::open(
ec,
ch,
code_pages::UTF_8,
cnvrt_control::failure_on_failing_chars
);
if(ec)
return s_read_channel();
cnv->convert(ec, pos, rb.size(), new_rb);
rb.swap(new_rb);
return conv_read_channel::open(ec, src, cnv );
}
// source
s_source source::open(std::error_code& ec, const s_read_channel& src, byte_buffer&& rb) noexcept
{
uint8_t *pos = const_cast<uint8_t*>( rb.position().get() );
s_charset_detector chdet = charset_detector::create(ec);
if(ec)
return s_source();
charset_detect_status chdetstat = chdet->detect(ec, pos, rb.size() );
if( ec )
return s_source();
static const double CONFIDENT = 0.5F;
if( !chdetstat && (chdetstat.confidence() < CONFIDENT) ) {
ec = make_error_code(converrc::not_supported);
return s_source();
}
charset ch = chdetstat.character_set();
s_read_channel text_channel;
switch( static_cast<unsigned int>(ch.code() ) ) {
// UTF-8 or latin1
case ASCII_CP_CODE:
case ISO_LATIN1_CP_CODE:
case WINDOWS_LATIN1_CP_CODE:
case UTF8_CP_CODE:
text_channel = src;
if( utf8_bom::is(pos) )
rb.shift( utf8_bom::len() );
break;
// Create converter
default:
text_channel = open_convert_channel(ec, rb, pos, ch, src );
if(ec)
return s_source();
}
source *sc = nobadalloc<source>::construct(ec, std::move(text_channel), std::move(rb) );
return (nullptr == sc) ? s_source(): s_source(sc);
}
s_source source::create(std::error_code& ec,s_read_channel&& src) noexcept
{
byte_buffer buff = byte_buffer::allocate(ec,READ_BUFF_INITIAL_SIZE);
if(ec)
return s_source();
// charge buffer to detect character set
uint8_t *pos = const_cast<uint8_t*>(buff.position().get());
size_t read = src->read(ec, pos, buff.capacity() );
if(ec)
return s_source();
buff.move(read);
buff.flip();
return open(ec, src, std::move(buff));
}
source::source(s_read_channel&& src, byte_buffer&& rb) noexcept:
object(),
last_( error::ok ),
pos_(nullptr),
end_(nullptr),
row_(1),
col_(1),
src_(src),
// 1page is minimum
rb_( std::move(rb) ),
mb_state_( 0 )
{
pos_ = rb_.position().cdata();
end_ = rb_.last().cdata();
}
source::~source() noexcept
{}
error source::read_more() noexcept
{
rb_.clear();
if( rb_.capacity() < READ_BUFF_MAXIMAL_SIZE ) {
if( io_unlikely( !rb_.exp_grow() ) )
return error::out_of_memory;
}
std::error_code ec;
size_t read = src_->read(ec, const_cast<uint8_t*>(rb_.position().get()), rb_.capacity() );
if( ec )
return error::io_error;
rb_.move(read);
rb_.flip();
return error::ok;
}
error source::charge() noexcept
{
error ec = read_more();
if( io_likely( ec == error::ok && !rb_.empty()) ) {
pos_ = rb_.position().cdata();
end_ = rb_.last().cdata();
}
else
pos_ = end_;
return ec;
}
// normalize line endings according W3C XML spec
inline char source::normalize_line_endings(const char ch)
{
switch( ch ) {
case CR:
// according xml standard \r\n combination should be interpret as single \n
if( io_likely( NL == *pos_ ) ) {
++pos_;
++row_;
col_ = 1;
return NL;
}
++col_;
break;
case NL:
++row_;
col_ = 1;
break;
default:
++col_;
}
return ch;
}
inline bool source::fetch() noexcept
{
if( end_ == (pos_+1) )
last_ = charge();
return pos_ != end_ || error::ok == last_;
}
char source::next() noexcept
{
constexpr const char EOF_CH = std::char_traits<char>::to_char_type( std::char_traits<char>::eof() );
if( io_unlikely( !fetch() ) )
return EOF_CH;
char ret;
// check for a multi-byte tail byte
if( io_unlikely(0 != mb_state_) ) {
ret = *pos_++;
--mb_state_;
} else {
unsigned int len = utf8::mblen( pos_ );
ret = *pos_++;
switch( len ) {
case io_likely(1):
ret = normalize_line_endings( ret );
break;
case 2: case 3: case 4:
mb_state_ = static_cast<uint8_t>(len - 1);
++col_;
break;
default:
last_ = error::illegal_chars;
ret = EOF_CH;
break;
}
}
return ret;
}
void source::read_until_char(byte_buffer& to,const char lookup,const char illegal) noexcept
{
char c;
char stops[3] = {lookup, illegal, EOF};
do {
c = next();
if( !to.put(c) ) {
if( io_likely( to.exp_grow() ) ) {
to.put(c);
}
else {
last_ = error::out_of_memory;
break;
}
}
}
while( nullptr == io_memchr(stops, static_cast<int>(c), 3) );
if( lookup != c ) {
if(EOF == c)
last_ = error::illegal_markup;
to.clear();
}
}
void source::read_until_double_char(byte_buffer& to, const char ch) noexcept
{
const uint16_t pattern = pack_word(static_cast<uint16_t>(ch), ch);
char c;
uint16_t i = 0;
do {
c = next();
if( io_unlikely( cheq(c,EOF) ) )
break;
if( !to.put(c) ) {
if( io_likely( to.exp_grow() ) ) {
to.put(c);
}
else {
last_ = error::out_of_memory;
break;
}
}
i = pack_word(i,c);
}
while( i != pattern);
if( error::ok != last_ || cheq(c,EOF) )
to.clear();
}
} // namespace xml
} // namesapce io