2
* Copyright 2006-2008 The FLWOR Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
20
#include "diagnostics/assert.h"
21
#include "util/unicode_util.h"
22
#include "util/utf8_util.h"
24
#include "transcoder.h"
28
///////////////////////////////////////////////////////////////////////////////
30
transcoder::transcoder( std::ostream& output_stream, bool in_utf16 ) :
37
#endif /* ZORBA_NO_ICU */
42
void transcoder::write_utf16( char const *s, std::streamsize len ) {
43
unicode::char_type *u_s;
44
unicode::size_type u_len;
45
if ( !unicode::to_string( s, len, &u_s, &u_len ) )
46
throw std::runtime_error( "unicode::to_string() failed" );
48
char const *const byte = reinterpret_cast<char const*>( u_s );
49
for ( int i = 0; i < u_len * (int)sizeof( unicode::char_type ); ++i )
55
void transcoder::write_utf16_char( char ch ) {
56
if ( utf8::is_start_byte( ch ) ) {
57
if ( utf8_char_len_ > 1 )
58
throw std::runtime_error( "incomplete UTF-8 character" );
59
utf8_char_len_ = utf8::char_length( ch );
60
} else if ( utf8::is_continuation_byte( ch ) ) {
62
throw std::runtime_error( "invalid UTF-8 byte" );
65
utf8_buf_[ utf8_buf_len_++ ] = ch;
67
if ( utf8_buf_len_ == utf8_char_len_ ) {
68
unicode::char_type u_ch;
69
if ( !unicode::to_char( utf8_buf_, &u_ch ) )
70
throw std::runtime_error( "unicode::to_char() failed" );
72
char const *const byte = reinterpret_cast<char const*>( &u_ch );
73
for ( int i = 0; i < (int)sizeof( unicode::char_type ); ++i )
81
#endif /* ZORBA_NO_ICU */
83
///////////////////////////////////////////////////////////////////////////////
86
/* vim:set et sw=2 ts=2: */