~zorba-coders/zorba/bug-1032166

« back to all changes in this revision

Viewing changes to src/zorbatypes/transcoder.cpp

  • Committer: Tarmac
  • Author(s): Paul J. Lucas
  • Date: 2012-07-25 00:15:29 UTC
  • mfrom: (10944.1.9 pjl-misc)
  • Revision ID: tarmac-20120725001529-5935lj54a3mzwxp5
Removed zorbatypes/transcoder.h & .cpp. Approved: Matthias Brantner, Paul J. Lucas

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2006-2008 The FLWOR Foundation.
3
 
 *
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
7
 
 *
8
 
 * http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
#include "stdafx.h"
17
 
 
18
 
#include <stdexcept>
19
 
 
20
 
#include "diagnostics/assert.h"
21
 
#include "util/unicode_util.h"
22
 
#include "util/utf8_util.h"
23
 
 
24
 
#include "transcoder.h"
25
 
 
26
 
namespace zorba {
27
 
 
28
 
///////////////////////////////////////////////////////////////////////////////
29
 
 
30
 
transcoder::transcoder( std::ostream& output_stream, bool in_utf16 ) :
31
 
  os( output_stream ),
32
 
  utf16( in_utf16 )
33
 
{
34
 
#ifndef ZORBA_NO_ICU
35
 
  utf8_buf_len_ = 0;
36
 
  utf8_char_len_ = 1;
37
 
#endif /* ZORBA_NO_ICU */
38
 
}
39
 
 
40
 
#ifndef ZORBA_NO_ICU
41
 
 
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" );
47
 
 
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 )
50
 
    os << byte[i];
51
 
 
52
 
  delete[] u_s;
53
 
}
54
 
 
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 ) ) {
61
 
    if ( !utf8_buf_len_ )
62
 
      throw std::runtime_error( "invalid UTF-8 byte" );
63
 
  }
64
 
 
65
 
  utf8_buf_[ utf8_buf_len_++ ] = ch;
66
 
 
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" );
71
 
 
72
 
    char const *const byte = reinterpret_cast<char const*>( &u_ch );
73
 
    for ( int i = 0; i < (int)sizeof( unicode::char_type ); ++i )
74
 
      os << byte[i];
75
 
 
76
 
    utf8_buf_len_ = 0;
77
 
    utf8_char_len_ = 1;
78
 
  }
79
 
}
80
 
 
81
 
#endif /* ZORBA_NO_ICU */
82
 
 
83
 
///////////////////////////////////////////////////////////////////////////////
84
 
 
85
 
} // namespace zorba
86
 
/* vim:set et sw=2 ts=2: */