1
1
#include "unparsed_streambuf.h"
2
#include "util/utf8_streambuf.h"
4
3
#include "diagnostics/xquery_diagnostics.h"
5
4
#include "diagnostics/util_macros.h"
5
#include "util/oseparator.h"
10
12
namespace unparsed{
14
streambuf::pos_type streambuf::seekoff( off_type o, ios_base::seekdir d, ios_base::openmode m )
17
return original()->pubseekoff( o, d, m );
20
streambuf::pos_type streambuf::seekpos( pos_type p, ios_base::openmode m )
23
return original()->pubseekpos( p, m );
26
streambuf::int_type streambuf::pbackfail( int_type c )
28
if ( !traits_type::eq_int_type( c, traits_type::eof() ) &&
30
original()->sputbackc( traits_type::to_char_type( c ) ) ) {
34
return traits_type::eof();
37
streambuf::int_type streambuf::uflow()
39
#ifdef ZORBA_DEBUG_UTF8_STREAMBUF
40
printf( "uflow()\n" );
42
int_type const c = original()->sbumpc();
43
if ( traits_type::eq_int_type( c, traits_type::eof() ) )
44
return traits_type::eof();
45
gbuf_.validate( traits_type::to_char_type( c ) );
49
inline void streambuf::clear() {
53
streamsize streambuf::xsgetn(char_type* to, std::streamsize size )
55
#ifdef ZORBA_DEBUG_UTF8_STREAMBUF
56
printf( "xsgetn()\n" );
58
streamsize return_size = 0;
60
if ( gbuf_.char_len_ ) {
61
streamsize const want = gbuf_.char_len_ - gbuf_.cur_len_;
62
streamsize const get = min( want, size );
63
streamsize const got = original()->sgetn( to, get );
64
for ( streamsize i = 0; i < got; ++i )
65
gbuf_.validate( to[i] );
67
size -= got, return_size += got;
71
if ( streamsize const got = original()->sgetn( to, size ) ) {
72
for ( streamsize i = 0; i < got; ++i )
73
gbuf_.validate( to[i] );
75
size -= got, return_size += got;
82
inline void streambuf::buf_type::clear()
87
void streambuf::buf_type::throw_invalid_utf8( utf8::storage_type *buf, utf8::size_type len ) {
89
oss << hex << setfill('0') << setw(2) << uppercase;
90
oseparator comma( ',' );
92
for ( utf8::size_type i = 0; i < len; ++i )
93
oss << comma << "0x" << (static_cast<unsigned>( buf[i] ) & 0xFF);
96
throw ZORBA_EXCEPTION(
97
zerr::ZXQD0006_INVALID_UTF8_BYTE_SEQUENCE,
98
ERROR_PARAMS( oss.str() )
102
void streambuf::buf_type::validate( utf8::storage_type c, bool bump ) {
103
utf8::size_type char_len_copy = char_len_, cur_len_copy = cur_len_;
105
if ( !char_len_copy ) {
107
// This means we're (hopefully) at the first byte of a UTF-8 byte sequence
108
// comprising a character.
111
char_len_copy = utf8::char_length( c );
114
throw_invalid_utf8 ( &c, 1);
116
catch ( utf8::invalid_byte const& ) {
117
throw_invalid_utf8( &c, 1 );
121
utf8::storage_type *const cur_byte_ptr = utf8_char_ + cur_len_copy;
122
utf8::storage_type const old_byte = *cur_byte_ptr;
125
if ( cur_len_copy++ && !utf8::is_continuation_byte( c ) )
126
throw_invalid_utf8( utf8_char_, cur_len_copy );
129
char_len_ = (cur_len_copy == char_len_copy ? 0 : char_len_copy);
130
cur_len_ = cur_len_copy;
132
*cur_byte_ptr = old_byte;
12
137
streambuf::streambuf(std::streambuf* orig, zstring const& uri, QueryLoc const& loc) :
13
proxy_buf(new utf8::streambuf(orig)),
138
proxy_buf(new xml::streambuf(orig)),