File: vtkUTF16TextCodec.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (257 lines) | stat: -rw-r--r-- 6,448 bytes parent folder | download | duplicates (3)
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
/*=========================================================================

Program:   Visualization Toolkit
Module:    vtkUTF16TextCodec.cxx

Copyright (c)
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2010 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/

#include "vtkUTF16TextCodec.h"

#include "vtkObjectFactory.h"
#include "vtkTextCodecFactory.h"

#include <stdexcept>

vtkStandardNewMacro(vtkUTF16TextCodec);

namespace
{
  //////////////////////////////////////////////////////////////////////////////
  // utf16_to_unicode

  vtkTypeUInt32 utf16_to_unicode_next(const bool big_endian, istream& InputStream)
  {
    vtkTypeUInt8 first_byte = InputStream.get();

    if(InputStream.eof())
    {
      throw std::runtime_error("Premature end-of-sequence extracting UTF-16 code unit.");
    }
    vtkTypeUInt8 second_byte = InputStream.get();

    vtkTypeUInt32 returnCode =
      big_endian ? first_byte << 8 | second_byte : second_byte << 8 | first_byte;

    if(returnCode >= 0xd800 && returnCode <= 0xdfff)
    {
      if(InputStream.eof())
      {
        throw std::runtime_error("Premature end-of-sequence extracting UTF-16 trail surrogate first byte.");
      }
      vtkTypeUInt8 third_byte = InputStream.get();

      if(InputStream.eof())
      {
        throw std::runtime_error("Premature end-of-sequence extracting UTF-16 trail surrogate second byte.");
      }
      vtkTypeUInt8 fourth_byte = InputStream.get();

      const vtkTypeUInt32 second_code_unit =
        big_endian ? third_byte << 8 | fourth_byte : fourth_byte << 8 | third_byte;
      if(second_code_unit >= 0xdc00 && second_code_unit <= 0xdfff)
      {
        returnCode = vtkTypeUInt32 (vtkTypeInt32 (returnCode << 10) +
                                    vtkTypeInt32 (second_code_unit) +
                                    (0x10000 - (0xd800 << 10) - 0xdc00));
      }
      else
      {
        throw std::runtime_error("Invalid UTF-16 trail surrogate.");
      }
    }
    return returnCode;
  }


  void utf16_to_unicode(const bool big_endian, istream& InputStream,
                        vtkTextCodec::OutputIterator& output)
  {
    try
    {
      while(!InputStream.eof())
      {
        const vtkTypeUInt32 code_unit = utf16_to_unicode_next(big_endian, InputStream);
        *output++ = code_unit;
      }
    }
    catch(...)
    {
      if (!InputStream.eof())
        throw;
    }
  }


  // iterator to use in testing validity - throws all input away.
  class testIterator : public vtkTextCodec::OutputIterator
  {
  public:
    testIterator& operator++(int) VTK_OVERRIDE {return *this;}
    testIterator& operator*() VTK_OVERRIDE {return *this;}
    testIterator& operator=(const vtkUnicodeString::value_type) VTK_OVERRIDE
      {return *this;}

    testIterator() {}
    ~testIterator() VTK_OVERRIDE {}

  private:
    testIterator(const testIterator&) VTK_DELETE_FUNCTION;
    const testIterator& operator=(const testIterator&) VTK_DELETE_FUNCTION;
  };


} // end anonymous namespace


vtkUTF16TextCodec::vtkUTF16TextCodec() : vtkTextCodec(), _endianExplicitlySet(false),
                                         _bigEndian(true)
{
}

vtkUTF16TextCodec::~vtkUTF16TextCodec()
{
}

const char* vtkUTF16TextCodec::Name()
{
  return "UTF-16";
}

bool vtkUTF16TextCodec::CanHandle(const char* NameString)
{
  if (0 == strcmp(NameString, "UTF-16"))
  {
    _endianExplicitlySet = false;
    return true;
  }
  else if (0 == strcmp(NameString, "UTF-16BE"))
  {
    SetBigEndian(true);
    return true;
  }
  else if (0 == strcmp(NameString, "UTF-16LE"))
  {
    SetBigEndian(false);
    return true;
  }
  else
  {
    return false;
  }
}

void vtkUTF16TextCodec::SetBigEndian(bool state)
{
  _endianExplicitlySet = true;
   _bigEndian = state;
}

void vtkUTF16TextCodec::FindEndianness(istream& InputStream)
{
  _endianExplicitlySet = false;

  try
  {
    istream::char_type c1, c2;
    c1 = InputStream.get() ;
    if (InputStream.fail())
        throw "End of Input reached while reading header." ;

    c2 = InputStream.get() ;
    if (InputStream.fail())
        throw "End of Input reached while reading header." ;

    if(static_cast<unsigned char>(c1) == 0xfe &&
       static_cast<unsigned char>(c2) == 0xff)
    {
      _bigEndian = true;
    }

    else if(static_cast<unsigned char>(c1) == 0xff &&
            static_cast<unsigned char>(c2) == 0xfe)
    {
      _bigEndian = false;
    }

    else
    {
      throw std::runtime_error("Cannot detect UTF-16 endianness.  Try 'UTF-16BE' or 'UTF-16LE' instead.");
    }
  }
  catch (char* cstr)
  {
    throw std::runtime_error(cstr) ;
  }
  catch (...)
  {
    throw std::runtime_error("Cannot detect UTF-16 endianness.  Try 'UTF-16BE' or 'UTF-16LE' instead.");
  }

}


bool vtkUTF16TextCodec::IsValid(istream& InputStream)
{
  bool returnBool = true;
  // get the position of the stream so we can restore it when we are
  // done
  istream::pos_type StreamPos = InputStream.tellg();

  try
  {
    if (!_endianExplicitlySet)
    {
      FindEndianness(InputStream);
    }

    testIterator junk;
    utf16_to_unicode(_bigEndian, InputStream, junk);
  }
  catch(...)
  {
    returnBool = false;
  }

  // reset the stream
  InputStream.clear();
  InputStream.seekg(StreamPos);

  return returnBool;
}

void vtkUTF16TextCodec::ToUnicode(istream& InputStream, vtkTextCodec::OutputIterator& output)
{
  if (!_endianExplicitlySet)
  {
    FindEndianness(InputStream);
  }

  utf16_to_unicode(_bigEndian, InputStream, output);
}


vtkUnicodeString::value_type vtkUTF16TextCodec::NextUnicode(istream& InputStream)
{
  return utf16_to_unicode_next(_bigEndian, InputStream);
}


void vtkUTF16TextCodec::PrintSelf(ostream& os, vtkIndent indent)
{
  os << indent << "vtkUTF16TextCodec (" << this << ") \n";
  indent = indent.GetNextIndent();
  this->Superclass::PrintSelf(os, indent.GetNextIndent());
}