This file is indexed.

/usr/include/InsightToolkit/Common/itkInternationalizationIOHelpers.h is in libinsighttoolkit3-dev 3.20.1+git20120521-6build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkInternationalizationIOHelpers.h
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/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 notices for more information.

=========================================================================*/
#ifndef __itkInternationalizationIOHelpers_h
#define __itkInternationalizationIOHelpers_h

// This header provides some helper functions to deal with unicode filenames
// It is mainly directed towards being able to use utf-8 encoded filenames
// on windows.
// This should help better dealing with internationalization (a.k.a i18n)

#include "itkConfigure.h"
#include "itkMacro.h"

#ifdef ITK_HAVE_UNISTD_H
# include <unistd.h> // for unlink
#else
# include <io.h>
#endif

#include <stdio.h> // Borland needs this (cstdio does not work easy)
#include <fcntl.h>
#include <iostream>
#include <string>
#include <sys/stat.h>

// Find out how to handle unicode filenames on windows:
// * VS>=8.0 has _wopen and _wfopen and can open a (i/o)fstream using a wide string
// * cygwin has NO _wopen an NO _wfopen. If you really need unicode
//   filenames on cygwin, just use cygwin >= 1.7 for now, it works with utf8
//   natively. Alternatively, we could try and use pure win32 functions such as
//   CreateFileW and convert the win32 file handle using _open_osfhandle and _fdopen
// * VS6.0 has _wopen and _wfopen but cannot open a (i/o)fstream using a wide string
//   nor can it compile fdstream => disable unicode filename support
// * Borland c++, VS7.x and MinGW have _wopen and _wfopen but cannot open a
//   (i/o)fstream using a wide string. They can however compile fdstream

#if defined(ITK_SUPPORTS_WCHAR_T_FILENAME_CSTYLEIO) \
   && ( defined(ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS) || defined(ITK_SUPPORTS_FDSTREAM_HPP) )
# define LOCAL_USE_WIN32_WOPEN 1
# include <windows.h> // required by winnls.h
# include <winnls.h> // for MultiByteToWideChar
#else
# define LOCAL_USE_WIN32_WOPEN 0
#endif

#if (LOCAL_USE_WIN32_WOPEN && defined(ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS)) \
   || (!LOCAL_USE_WIN32_WOPEN)
# define LOCAL_USE_FDSTREAM 0
# include <fstream>
#else
# define LOCAL_USE_FDSTREAM 1
# include "fdstream.hpp"
#endif


namespace itk
{
namespace i18n
{

// Check if the string is correctly encoded
#if LOCAL_USE_WIN32_WOPEN
inline bool IsStringEncodingValid(const std::string & str)
{
  // Check if the string is really encoded in utf-8 using windows API
  // MultiByteToWideChar returns 0 if there was a problem during conversion
  // when given the MB_ERR_INVALID_CHARS flag
  const int utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(),
                                             static_cast<int>(str.length()), 0, 0);
  return (utf16_size != 0);
}
#else
inline bool IsStringEncodingValid(const std::string & itkNotUsed( str ) )
{
  return true;
}
#endif

#if LOCAL_USE_WIN32_WOPEN
// Convert a utf8 encoded std::string to a utf16 encoded wstring on windows
inline std::wstring Utf8StringToWString( const std::string & str )
{
  // We do not set the MB_ERR_INVALID_CHARS to do an approximate conversion when non
  // utf8 characters are found. An alternative would be to throw an exception

  // First get the size
  const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
                                             static_cast<int>(str.length()), 0, 0);

  // Now do the conversion
  std::wstring wstr;
  wstr.resize(utf16_size);
  MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
                      static_cast<int>(str.length()), &wstr[0], utf16_size);
  
  return wstr;
}

#endif

// Get a file descriptor from a filename (using utf8 to wstring
// on windows if requested) without specifying any specific permissions
inline int I18nOpen( const std::string & str, const int & flags )
{
#if LOCAL_USE_WIN32_WOPEN
  // cygwin has NO _wopen but mingw has
  // If you really need unicode filenames on cygwin, just use cygwin >= 1.7
  // Convert to utf16
  const std::wstring str_utf16 = Utf8StringToWString( str );
  return _wopen(str_utf16.c_str(), flags);
#else
  return open(str.c_str(), flags);
#endif
}

// Get a file descriptor from a filename (using utf8 to wstring
// on windows if requested)
inline int I18nOpen( const std::string & str, const int & flags, const int & mode )
{
#if LOCAL_USE_WIN32_WOPEN
  // cygwin has NO _wopen but mingw has
  // If you really need unicode filenames on cygwin, just use cygwin >= 1.7
  // Convert to utf16
  const std::wstring str_utf16 = Utf8StringToWString( str );
  return _wopen(str_utf16.c_str(), flags, mode);
#else
  return open(str.c_str(), flags, mode);
#endif
}

// Reading wrapper around I18nOpen to avoid explicitely specifying the flags
inline int I18nOpenForReading( const std::string & str )
{
#if LOCAL_USE_WIN32_WOPEN
  return I18nOpen(str, _O_RDONLY | _O_BINARY );
#else
  ///\todo check if cygwin has and needs the O_BINARY flag
  return I18nOpen(str, O_RDONLY );
#endif
}

// Writting wrapper around I18nOpen to avoid explicitely specifying the flags
inline int I18nOpenForWritting( const std::string & str, const bool append = false )
{
#if LOCAL_USE_WIN32_WOPEN
  if (!append) return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE );
  else return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_APPEND | _O_BINARY, _S_IREAD | _S_IWRITE );
#else
  ///\todo check if cygwin has and needs the O_BINARY flag
  if (!append) return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );
  else return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE );
#endif
}

// Get a FILE * pointer from a filename (using utf8 to wstring
// on windows if requested)
inline FILE * I18nFopen( const std::string & str, const std::string & mode )
{
#if LOCAL_USE_WIN32_WOPEN
  // cygwin has NO _wfopen but mingw has
  // If you really need unicode filenames on cygwin, just use cygwin >= 1.7
  // Convert to utf16
  const std::wstring str_utf16 = Utf8StringToWString( str );
  const std::wstring mode_utf16 = Utf8StringToWString( mode );
  return _wfopen(str_utf16.c_str(), mode_utf16.c_str());
#else
  return fopen(str.c_str(), mode.c_str());
#endif
}

#if LOCAL_USE_FDSTREAM
class I18nOfstream : public std::ostream 
{
public:
  I18nOfstream( const char * str, 
    std::ios_base::openmode mode = std::ios_base::out )
    : std::ostream(0)
    , m_fd( I18nOpenForWritting( str, (mode & std::ios::app)?true:false ) )
    , m_buf( m_fd )
    {
    ///\todo better handle mode flag
    this->rdbuf(&m_buf);
    }

  ~I18nOfstream() { this->close(); }

  bool is_open() { return (m_fd!=-1); }

  void close()
    {
    if ( m_fd!=-1 ) ::close( m_fd );
    m_fd = -1;
    }

private:
  int m_fd;
  itk::fdoutbuf m_buf;
};

class I18nIfstream : public std::istream 
{
public:
  I18nIfstream( const char * str, 
    std::ios_base::openmode mode = std::ios_base::in )
    : std::istream(0)
    , m_fd( I18nOpenforreading( str ) )
    , m_buf( m_fd )
    {
    ///\todo better handle mode flag
    this->rdbuf(&m_buf);
    }

  ~I18nIfstream() { this->close(); }

  bool is_open() { return (m_fd!=-1); }

  void close()
    {
    if ( m_fd!=-1 ) ::close( m_fd );
    m_fd = -1;
    }

private:
  int m_fd;
  itk::fdinbuf m_buf;
};
#elif LOCAL_USE_WIN32_WOPEN
class I18nOfstream : public std::ofstream
{
public:
  I18nOfstream( const char * str, std::ios_base::openmode mode = std::ios_base::out )
    : std::ofstream( Utf8StringToWString(str).c_str(), mode )
    {
    }
};

class I18nIfstream : public std::ifstream
{
public:
  I18nIfstream( const char * str, std::ios_base::openmode mode = std::ios_base::in )
    : std::ifstream( Utf8StringToWString(str).c_str(), mode )
    {
    }
};
#else
typedef std::ofstream I18nOfstream;
typedef std::ifstream I18nIfstream;
#endif

} // end namespace
} // end namespace


#undef LOCAL_USE_WIN32_WOPEN
#undef LOCAL_USE_FDSTREAM

#endif  /* __itkInternationalizationIOHelpers_h */