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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
|
// Copyright 2018 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See https://github.com/danielaparker/jsoncons for latest version
#ifndef JSONCONS_SOURCE_HPP
#define JSONCONS_SOURCE_HPP
#include <stdexcept>
#include <string>
#include <vector>
#include <istream>
#include <memory> // std::addressof
#include <cstring> // std::memcpy
#include <exception>
#include <iterator>
#include <type_traits> // std::enable_if
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/byte_string.hpp> // jsoncons::byte_traits
#include <jsoncons/more_type_traits.hpp>
namespace jsoncons {
template <class CharT>
class basic_null_istream : public std::basic_istream<CharT>
{
class null_buffer : public std::basic_streambuf<CharT>
{
null_buffer(const null_buffer&) = delete;
null_buffer& operator=(const null_buffer&) = delete;
public:
using typename std::basic_streambuf<CharT>::int_type;
using typename std::basic_streambuf<CharT>::traits_type;
null_buffer() = default;
null_buffer(null_buffer&&) = default;
null_buffer& operator=(null_buffer&&) = default;
int_type overflow( int_type ch = typename std::basic_streambuf<CharT>::traits_type::eof() ) override
{
return ch;
}
} nb_;
public:
basic_null_istream()
: std::basic_istream<CharT>(&nb_)
{
}
basic_null_istream(const null_buffer&) = delete;
basic_null_istream& operator=(const null_buffer&) = delete;
basic_null_istream(basic_null_istream&&) noexcept
: std::basic_istream<CharT>(&nb_)
{
}
basic_null_istream& operator=(basic_null_istream&&) noexcept
{
return *this;
}
};
template <class CharT>
struct char_result
{
CharT value;
bool eof;
};
// text sources
template <class CharT>
class stream_source
{
static constexpr std::size_t default_max_buffer_size = 16384;
public:
using value_type = CharT;
private:
using char_type = typename std::conditional<sizeof(CharT) == sizeof(char),char,CharT>::type;
basic_null_istream<char_type> null_is_;
std::basic_istream<char_type>* stream_ptr_;
std::basic_streambuf<char_type>* sbuf_;
std::size_t position_;
std::vector<value_type> buffer_;
const value_type* buffer_data_;
std::size_t buffer_length_;
// Noncopyable
stream_source(const stream_source&) = delete;
stream_source& operator=(const stream_source&) = delete;
public:
stream_source()
: stream_ptr_(&null_is_), sbuf_(null_is_.rdbuf()), position_(0),
buffer_(1), buffer_data_(buffer_.data()), buffer_length_(0)
{
}
stream_source(std::basic_istream<char_type>& is, std::size_t buf_size = default_max_buffer_size)
: stream_ptr_(std::addressof(is)), sbuf_(is.rdbuf()), position_(0),
buffer_(buf_size), buffer_data_(buffer_.data()), buffer_length_(0)
{
}
stream_source(stream_source&& other) noexcept
: stream_ptr_(&null_is_), sbuf_(null_is_.rdbuf()), position_(0),
buffer_(), buffer_data_(buffer_.data()), buffer_length_(0)
{
if (other.stream_ptr_ != &other.null_is_)
{
stream_ptr_ = other.stream_ptr_;
sbuf_ = other.sbuf_;
position_ = other.position_;
buffer_ = std::move(other.buffer_);
buffer_data_ = buffer_.data() + (other.buffer_data_ - other.buffer_.data());
buffer_length_ = other.buffer_length_;
other = stream_source();
}
}
~stream_source()
{
}
stream_source& operator=(stream_source&& other) noexcept
{
if (other.stream_ptr_ != &other.null_is_)
{
stream_ptr_ = other.stream_ptr_;
sbuf_ = other.sbuf_;
position_ = other.position_;
buffer_ = std::move(other.buffer_);
buffer_data_ = buffer_.data() + (other.buffer_data_ - other.buffer_.data());
buffer_length_ = other.buffer_length_;
other = stream_source();
}
else
{
stream_ptr_ = &null_is_;
sbuf_ = null_is_.rdbuf();
position_ = 0;
buffer_data_ = buffer_.data();
buffer_length_ = 0;
}
return *this;
}
bool eof() const
{
return buffer_length_ == 0 && stream_ptr_->eof();
}
bool is_error() const
{
return stream_ptr_->bad();
}
std::size_t position() const
{
return position_;
}
void ignore(std::size_t length)
{
std::size_t len = 0;
if (buffer_length_ > 0)
{
len = (std::min)(buffer_length_, length);
position_ += len;
buffer_data_ += len;
buffer_length_ -= len;
}
while (length - len > 0)
{
fill_buffer();
if (buffer_length_ == 0)
{
break;
}
std::size_t len2 = (std::min)(buffer_length_, length-len);
position_ += len2;
buffer_data_ += len2;
buffer_length_ -= len2;
len += len2;
}
}
char_result<value_type> peek()
{
if (buffer_length_ == 0)
{
fill_buffer();
}
if (buffer_length_ > 0)
{
value_type c = *buffer_data_;
return char_result<value_type>{c, false};
}
else
{
return char_result<value_type>{0, true};
}
}
span<const value_type> read_buffer()
{
if (buffer_length_ == 0)
{
fill_buffer();
}
const value_type* data = buffer_data_;
std::size_t length = buffer_length_;
buffer_data_ += buffer_length_;
position_ += buffer_length_;
buffer_length_ = 0;
return span<const value_type>(data, length);
}
std::size_t read(value_type* p, std::size_t length)
{
std::size_t len = 0;
if (buffer_length_ > 0)
{
len = (std::min)(buffer_length_, length);
std::memcpy(p, buffer_data_, len*sizeof(value_type));
buffer_data_ += len;
buffer_length_ -= len;
position_ += len;
}
if (length - len == 0)
{
return len;
}
else if (length - len < buffer_.size())
{
fill_buffer();
if (buffer_length_ > 0)
{
std::size_t len2 = (std::min)(buffer_length_, length-len);
std::memcpy(p+len, buffer_data_, len2*sizeof(value_type));
buffer_data_ += len2;
buffer_length_ -= len2;
position_ += len2;
len += len2;
}
return len;
}
else
{
if (stream_ptr_->eof())
{
buffer_length_ = 0;
return 0;
}
JSONCONS_TRY
{
std::streamsize count = sbuf_->sgetn(reinterpret_cast<char_type*>(p+len), length-len);
std::size_t len2 = static_cast<std::size_t>(count);
if (len2 < length-len)
{
stream_ptr_->clear(stream_ptr_->rdstate() | std::ios::eofbit);
}
len += len2;
position_ += len2;
return len;
}
JSONCONS_CATCH(const std::exception&)
{
stream_ptr_->clear(stream_ptr_->rdstate() | std::ios::badbit | std::ios::eofbit);
return 0;
}
}
}
private:
void fill_buffer()
{
if (stream_ptr_->eof())
{
buffer_length_ = 0;
return;
}
buffer_data_ = buffer_.data();
JSONCONS_TRY
{
std::streamsize count = sbuf_->sgetn(reinterpret_cast<char_type*>(buffer_.data()), buffer_.size());
buffer_length_ = static_cast<std::size_t>(count);
if (buffer_length_ < buffer_.size())
{
stream_ptr_->clear(stream_ptr_->rdstate() | std::ios::eofbit);
}
}
JSONCONS_CATCH(const std::exception&)
{
stream_ptr_->clear(stream_ptr_->rdstate() | std::ios::badbit | std::ios::eofbit);
buffer_length_ = 0;
}
}
};
// string_source
template <class CharT>
class string_source
{
public:
using value_type = CharT;
using string_view_type = jsoncons::basic_string_view<value_type>;
private:
const value_type* data_;
const value_type* current_;
const value_type* end_;
// Noncopyable
string_source(const string_source&) = delete;
string_source& operator=(const string_source&) = delete;
public:
string_source()
: data_(nullptr), current_(nullptr), end_(nullptr)
{
}
template <class Sourceable>
string_source(const Sourceable& s,
typename std::enable_if<type_traits::is_sequence_of<Sourceable,value_type>::value>::type* = 0)
: data_(s.data()), current_(s.data()), end_(s.data()+s.size())
{
}
string_source(const value_type* data)
: data_(data), current_(data), end_(data+std::char_traits<value_type>::length(data))
{
}
string_source(string_source&& val) = default;
string_source& operator=(string_source&& val) = default;
bool eof() const
{
return current_ == end_;
}
bool is_error() const
{
return false;
}
std::size_t position() const
{
return (current_ - data_)/sizeof(value_type);
}
void ignore(std::size_t count)
{
std::size_t len;
if ((std::size_t)(end_ - current_) < count)
{
len = end_ - current_;
}
else
{
len = count;
}
current_ += len;
}
char_result<value_type> peek()
{
return current_ < end_ ? char_result<value_type>{*current_, false} : char_result<value_type>{0, true};
}
span<const value_type> read_buffer()
{
const value_type* data = current_;
std::size_t length = end_ - current_;
current_ = end_;
return span<const value_type>(data, length);
}
std::size_t read(value_type* p, std::size_t length)
{
std::size_t len;
if ((std::size_t)(end_ - current_) < length)
{
len = end_ - current_;
}
else
{
len = length;
}
std::memcpy(p, current_, len*sizeof(value_type));
current_ += len;
return len;
}
};
// iterator source
template <class IteratorT>
class iterator_source
{
public:
using value_type = typename std::iterator_traits<IteratorT>::value_type;
private:
static constexpr std::size_t default_max_buffer_size = 16384;
IteratorT current_;
IteratorT end_;
std::size_t position_;
std::vector<value_type> buffer_;
std::size_t buffer_length_;
using difference_type = typename std::iterator_traits<IteratorT>::difference_type;
using iterator_category = typename std::iterator_traits<IteratorT>::iterator_category;
// Noncopyable
iterator_source(const iterator_source&) = delete;
iterator_source& operator=(const iterator_source&) = delete;
public:
iterator_source(const IteratorT& first, const IteratorT& last, std::size_t buf_size = default_max_buffer_size)
: current_(first), end_(last), position_(0), buffer_(buf_size), buffer_length_(0)
{
}
iterator_source(iterator_source&& other) = default;
iterator_source& operator=(iterator_source&& other) = default;
bool eof() const
{
return !(current_ != end_);
}
bool is_error() const
{
return false;
}
std::size_t position() const
{
return position_;
}
void ignore(std::size_t count)
{
while (count-- > 0 && current_ != end_)
{
++position_;
++current_;
}
}
char_result<value_type> peek()
{
return current_ != end_ ? char_result<value_type>{*current_, false} : char_result<value_type>{0, true};
}
span<const value_type> read_buffer()
{
if (buffer_length_ == 0)
{
buffer_length_ = read(buffer_.data(), buffer_.size());
}
std::size_t length = buffer_length_;
buffer_length_ = 0;
return span<const value_type>(buffer_.data(), length);
}
template <class Category = iterator_category>
typename std::enable_if<std::is_same<Category,std::random_access_iterator_tag>::value, std::size_t>::type
read(value_type* data, std::size_t length)
{
std::size_t count = (std::min)(length, static_cast<std::size_t>(std::distance(current_, end_)));
JSONCONS_COPY(current_, current_ + count, data);
current_ += count;
position_ += count;
return count;
}
template <class Category = iterator_category>
typename std::enable_if<!std::is_same<Category,std::random_access_iterator_tag>::value, std::size_t>::type
read(value_type* data, std::size_t length)
{
value_type* p = data;
value_type* pend = data + length;
while (p < pend && current_ != end_)
{
*p = static_cast<value_type>(*current_);
++p;
++current_;
}
position_ += (p - data);
return p - data;
}
};
// binary sources
using binary_stream_source = stream_source<uint8_t>;
class bytes_source
{
public:
typedef uint8_t value_type;
private:
const value_type* data_;
const value_type* current_;
const value_type* end_;
// Noncopyable
bytes_source(const bytes_source&) = delete;
bytes_source& operator=(const bytes_source&) = delete;
public:
bytes_source()
: data_(nullptr), current_(nullptr), end_(nullptr)
{
}
template <class Sourceable>
bytes_source(const Sourceable& source,
typename std::enable_if<type_traits::is_byte_sequence<Sourceable>::value,int>::type = 0)
: data_(reinterpret_cast<const value_type*>(source.data())),
current_(data_),
end_(data_+source.size())
{
}
bytes_source(bytes_source&&) = default;
bytes_source& operator=(bytes_source&&) = default;
bool eof() const
{
return current_ == end_;
}
bool is_error() const
{
return false;
}
std::size_t position() const
{
return current_ - data_;
}
void ignore(std::size_t count)
{
std::size_t len;
if ((std::size_t)(end_ - current_) < count)
{
len = end_ - current_;
}
else
{
len = count;
}
current_ += len;
}
char_result<value_type> peek()
{
return current_ < end_ ? char_result<value_type>{*current_, false} : char_result<value_type>{0, true};
}
span<const value_type> read_buffer()
{
const value_type* data = current_;
std::size_t length = end_ - current_;
current_ = end_;
return span<const value_type>(data, length);
}
std::size_t read(value_type* p, std::size_t length)
{
std::size_t len;
if ((std::size_t)(end_ - current_) < length)
{
len = end_ - current_;
}
else
{
len = length;
}
std::memcpy(p, current_, len*sizeof(value_type));
current_ += len;
return len;
}
};
// binary_iterator source
template <class IteratorT>
class binary_iterator_source
{
public:
using value_type = uint8_t;
private:
static constexpr std::size_t default_max_buffer_size = 16384;
IteratorT current_;
IteratorT end_;
std::size_t position_;
std::vector<value_type> buffer_;
std::size_t buffer_length_;
using difference_type = typename std::iterator_traits<IteratorT>::difference_type;
using iterator_category = typename std::iterator_traits<IteratorT>::iterator_category;
// Noncopyable
binary_iterator_source(const binary_iterator_source&) = delete;
binary_iterator_source& operator=(const binary_iterator_source&) = delete;
public:
binary_iterator_source(const IteratorT& first, const IteratorT& last, std::size_t buf_size = default_max_buffer_size)
: current_(first), end_(last), position_(0), buffer_(buf_size), buffer_length_(0)
{
}
binary_iterator_source(binary_iterator_source&& other) = default;
binary_iterator_source& operator=(binary_iterator_source&& other) = default;
bool eof() const
{
return !(current_ != end_);
}
bool is_error() const
{
return false;
}
std::size_t position() const
{
return position_;
}
void ignore(std::size_t count)
{
while (count-- > 0 && current_ != end_)
{
++position_;
++current_;
}
}
char_result<value_type> peek()
{
return current_ != end_ ? char_result<value_type>{static_cast<value_type>(*current_), false} : char_result<value_type>{0, true};
}
span<const value_type> read_buffer()
{
if (buffer_length_ == 0)
{
buffer_length_ = read(buffer_.data(), buffer_.size());
}
std::size_t length = buffer_length_;
buffer_length_ = 0;
return span<const value_type>(buffer_.data(), length);
}
template <class Category = iterator_category>
typename std::enable_if<std::is_same<Category,std::random_access_iterator_tag>::value, std::size_t>::type
read(value_type* data, std::size_t length)
{
std::size_t count = (std::min)(length, static_cast<std::size_t>(std::distance(current_, end_)));
JSONCONS_COPY(current_, current_ + count, data);
current_ += count;
position_ += count;
return count;
}
template <class Category = iterator_category>
typename std::enable_if<!std::is_same<Category,std::random_access_iterator_tag>::value, std::size_t>::type
read(value_type* data, std::size_t length)
{
value_type* p = data;
value_type* pend = data + length;
while (p < pend && current_ != end_)
{
*p = static_cast<value_type>(*current_);
++p;
++current_;
}
position_ += (p - data);
return p - data;
}
};
template <class Source>
struct source_reader
{
using value_type = typename Source::value_type;
static constexpr std::size_t max_buffer_length = 16384;
template <class Container>
static
typename std::enable_if<std::is_convertible<value_type,typename Container::value_type>::value &&
type_traits::has_reserve<Container>::value &&
type_traits::has_data_exact<value_type*,Container>::value
, std::size_t>::type
read(Source& source, Container& v, std::size_t length)
{
std::size_t unread = length;
std::size_t n = (std::min)(max_buffer_length, unread);
while (n > 0 && !source.eof())
{
std::size_t offset = v.size();
v.resize(v.size()+n);
std::size_t actual = source.read(v.data()+offset, n);
unread -= actual;
n = (std::min)(max_buffer_length, unread);
}
return length - unread;
}
template <class Container>
static
typename std::enable_if<std::is_convertible<value_type,typename Container::value_type>::value &&
type_traits::has_reserve<Container>::value &&
!type_traits::has_data_exact<value_type*, Container>::value
, std::size_t>::type
read(Source& source, Container& v, std::size_t length)
{
std::size_t unread = length;
std::size_t n = (std::min)(max_buffer_length, unread);
while (n > 0 && !source.eof())
{
v.reserve(v.size()+n);
std::size_t actual = 0;
while (actual < n)
{
typename Source::value_type c;
if (source.read(&c,1) != 1)
{
break;
}
v.push_back(c);
++actual;
}
unread -= actual;
n = (std::min)(max_buffer_length, unread);
}
return length - unread;
}
};
template <class Source>
constexpr std::size_t source_reader<Source>::max_buffer_length;
#if !defined(JSONCONS_NO_DEPRECATED)
using bin_stream_source = binary_stream_source;
#endif
} // namespace jsoncons
#endif
|