ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkInternationalizationIOHelpers.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkInternationalizationIOHelpers_h
19 #define __itkInternationalizationIOHelpers_h
20 #include "ITKIOImageBaseExport.h"
21 
22 // This header provides some helper functions to deal with unicode filenames
23 // It is mainly directed towards being able to use utf-8 encoded filenames
24 // on windows.
25 // This should help better dealing with internationalization (a.k.a i18n)
26 
27 #include "itkMacro.h"
28 #include "itkIOConfigure.h"
29 
30 #ifdef ITK_HAVE_UNISTD_H
31 #include <unistd.h> // for unlink
32 #else
33 #include <io.h>
34 #endif
35 
36 #include <cstdio>
37 #include <fcntl.h>
38 #include <iostream>
39 #include <string>
40 #include <sys/stat.h>
41 
42 // Find out how to handle unicode filenames on windows:
43 // * VS>=8.0 has _wopen and _wfopen and can open a (i/o)fstream using a wide
44 // string
45 // * VS7.x and MinGW have _wopen and _wfopen but cannot open a
46 // (i/o)fstream using a wide string. They can however compile fdstream
47 
48 #if defined( ITK_SUPPORTS_WCHAR_T_FILENAME_CSTYLEIO ) \
49  && ( defined( ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS ) || defined( ITK_SUPPORTS_FDSTREAM_HPP ) )
50 #define LOCAL_USE_WIN32_WOPEN 1
51 #include <windows.h> // required by winnls.h
52 #include <winnls.h> // for MultiByteToWideChar
53 #else
54 #define LOCAL_USE_WIN32_WOPEN 0
55 #endif
56 
57 #if ( LOCAL_USE_WIN32_WOPEN && defined( ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS ) ) \
58  || ( !LOCAL_USE_WIN32_WOPEN )
59 #define LOCAL_USE_FDSTREAM 0
60 #include <fstream>
61 #else
62 #define LOCAL_USE_FDSTREAM 1
63 #include "itkfdstream/fdstream.hpp"
64 #endif
65 
66 namespace itk
67 {
68 namespace i18n
69 {
70 // Check if the string is correctly encoded
71 #if LOCAL_USE_WIN32_WOPEN
72 inline bool IsStringEncodingValid(const std::string & str)
73 {
74  // Check if the string is really encoded in utf-8 using windows API
75  // MultiByteToWideChar returns 0 if there was a problem during conversion
76  // when given the MB_ERR_INVALID_CHARS flag
77  const int utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(),
78  static_cast< int >( str.length() ), 0, 0);
79 
80  return ( utf16_size != 0 );
81 }
82 
83 #else
84 inline bool IsStringEncodingValid( const std::string & itkNotUsed(str) )
85 {
86  return true;
87 }
88 
89 #endif
90 
91 #if LOCAL_USE_WIN32_WOPEN
92 // Convert a utf8 encoded std::string to a utf16 encoded wstring on windows
93 inline std::wstring Utf8StringToWString(const std::string & str)
94 {
95  // We do not set the MB_ERR_INVALID_CHARS to do an approximate conversion when
96  // non
97  // utf8 characters are found. An alternative would be to throw an exception
98 
99  // First get the size
100  const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
101  static_cast< int >( str.length() ), 0, 0);
102 
103  // Now do the conversion
104  std::wstring wstr;
105 
106  wstr.resize(utf16_size);
107  MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
108  static_cast< int >( str.length() ), &wstr[0], utf16_size);
109 
110  return wstr;
111 }
112 
113 #endif
114 
115 // Get a file descriptor from a filename (using utf8 to wstring
116 // on windows if requested) without specifying any specific permissions
117 inline int I18nOpen(const std::string & str, const int & flags)
118 {
119 #if LOCAL_USE_WIN32_WOPEN
120  // mingw has _wopen
121  // Convert to utf16
122  const std::wstring str_utf16 = Utf8StringToWString(str);
123  return _wopen(str_utf16.c_str(), flags);
124 #else
125  return open(str.c_str(), flags);
126 #endif
127 }
128 
129 // Get a file descriptor from a filename (using utf8 to wstring
130 // on windows if requested)
131 inline int I18nOpen(const std::string & str, const int & flags, const int & mode)
132 {
133 #if LOCAL_USE_WIN32_WOPEN
134  // mingw has _wopen
135  // Convert to utf16
136  const std::wstring str_utf16 = Utf8StringToWString(str);
137  return _wopen(str_utf16.c_str(), flags, mode);
138 #else
139  return open(str.c_str(), flags, mode);
140 #endif
141 }
142 
143 // Reading wrapper around I18nOpen to avoid explicitely specifying the flags
144 inline int I18nOpenForReading(const std::string & str)
145 {
146 #if LOCAL_USE_WIN32_WOPEN
147  return I18nOpen(str, _O_RDONLY | _O_BINARY);
148 #else
149  return I18nOpen(str, O_RDONLY);
150 #endif
151 }
152 
153 // Writing wrapper around I18nOpen to avoid explicitely specifying the flags
154 inline int I18nOpenForWriting(const std::string & str, const bool append = false)
155 {
156 #if LOCAL_USE_WIN32_WOPEN
157  if ( !append ) { return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE); }
158  else { return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_APPEND | _O_BINARY, _S_IREAD | _S_IWRITE); }
159 #else
160  if ( !append ) { return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); }
161  else { return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE); }
162 #endif
163 }
164 
165 // Get a FILE * pointer from a filename (using utf8 to wstring
166 // on windows if requested)
167 inline FILE * I18nFopen(const std::string & str, const std::string & mode)
168 {
169 #if LOCAL_USE_WIN32_WOPEN
170  // Convert to utf16
171  const std::wstring str_utf16 = Utf8StringToWString(str);
172  const std::wstring mode_utf16 = Utf8StringToWString(mode);
173  return _wfopen( str_utf16.c_str(), mode_utf16.c_str() );
174 #else
175  return fopen( str.c_str(), mode.c_str() );
176 #endif
177 }
178 
179 #if LOCAL_USE_FDSTREAM
180 class I18nOfstream:public std::ostream
181 {
182 public:
183  I18nOfstream(const char *str,
184  std::ios_base::openmode mode = std::ios_base::out):
185  std::ostream(0),
186  m_fd( I18nOpenForWriting(str, ( mode & std::ios::app ) ? true:false) ),
187  m_buf(m_fd)
188  {
190  this->rdbuf(&m_buf);
191  }
192 
193  ~I18nOfstream() { this->close(); }
194 
195  bool is_open() { return ( m_fd != -1 ); }
196 
197  void close()
198  {
199  if ( m_fd != -1 ) { ::close(m_fd); }
200  m_fd = -1;
201  }
202 
203 private:
204  int m_fd;
205  itk::fdoutbuf m_buf;
206 };
207 
208 class I18nIfstream:public std::istream
209 {
210 public:
211  I18nIfstream(const char *str,
212  std::ios_base::openmode mode = std::ios_base::in):
213  std::istream(0),
214  m_fd( I18nOpenForReading(str) ),
215  m_buf(m_fd)
216  {
218  (void) mode;
219  this->rdbuf(&m_buf);
220  }
221 
222  ~I18nIfstream() { this->close(); }
223 
224  bool is_open() { return ( m_fd != -1 ); }
225 
226  void close()
227  {
228  if ( m_fd != -1 ) { ::close(m_fd); }
229  m_fd = -1;
230  }
231 
232 private:
233  int m_fd;
234  itk::fdinbuf m_buf;
235 };
236 #elif LOCAL_USE_WIN32_WOPEN
237 class I18nOfstream:public std::ofstream
238 {
239 public:
240  I18nOfstream(const char *str, std::ios_base::openmode mode = std::ios_base::out):
241  std::ofstream(Utf8StringToWString(str).c_str(), mode)
242  {}
243 };
244 
245 class I18nIfstream:public std::ifstream
246 {
247 public:
248  I18nIfstream(const char *str, std::ios_base::openmode mode = std::ios_base::in):
249  std::ifstream(Utf8StringToWString(str).c_str(), mode)
250  {}
251 };
252 #else
253 typedef std::ofstream I18nOfstream;
254 typedef std::ifstream I18nIfstream;
255 #endif
256 } // end namespace
257 } // end namespace
258 
259 #undef LOCAL_USE_WIN32_WOPEN
260 #undef LOCAL_USE_FDSTREAM
261 
262 #endif /* __itkInternationalizationIOHelpers_h */
int I18nOpenForWriting(const std::string &str, const bool append=false)
bool IsStringEncodingValid(const std::string &)
int I18nOpen(const std::string &str, const int &flags)
int I18nOpenForReading(const std::string &str)
FILE * I18nFopen(const std::string &str, const std::string &mode)