18 #ifndef __itkInternationalizationIOHelpers_h
19 #define __itkInternationalizationIOHelpers_h
27 #include "itkIOConfigure.h"
29 #ifdef ITK_HAVE_UNISTD_H
47 #if defined( ITK_SUPPORTS_WCHAR_T_FILENAME_CSTYLEIO ) \
48 && ( defined( ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS ) || defined( ITK_SUPPORTS_FDSTREAM_HPP ) )
49 #define LOCAL_USE_WIN32_WOPEN 1
53 #define LOCAL_USE_WIN32_WOPEN 0
56 #if ( LOCAL_USE_WIN32_WOPEN && defined( ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS ) ) \
57 || ( !LOCAL_USE_WIN32_WOPEN )
58 #define LOCAL_USE_FDSTREAM 0
61 #define LOCAL_USE_FDSTREAM 1
62 #include "itkfdstream/fdstream.hpp"
70 #if LOCAL_USE_WIN32_WOPEN
76 const int utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(),
77 static_cast< int >( str.length() ), 0, 0);
79 return ( utf16_size != 0 );
90 #if LOCAL_USE_WIN32_WOPEN
92 inline std::wstring Utf8StringToWString(
const std::string & str)
99 const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
100 static_cast< int >( str.length() ), 0, 0);
105 wstr.resize(utf16_size);
106 MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
107 static_cast< int >( str.length() ), &wstr[0], utf16_size);
116 inline int I18nOpen(
const std::string & str,
const int & flags)
118 #if LOCAL_USE_WIN32_WOPEN
121 const std::wstring str_utf16 = Utf8StringToWString(str);
122 return _wopen(str_utf16.c_str(), flags);
124 return open(str.c_str(), flags);
130 inline int I18nOpen(
const std::string & str,
const int & flags,
const int & mode)
132 #if LOCAL_USE_WIN32_WOPEN
135 const std::wstring str_utf16 = Utf8StringToWString(str);
136 return _wopen(str_utf16.c_str(), flags, mode);
138 return open(str.c_str(), flags, mode);
145 #if LOCAL_USE_WIN32_WOPEN
146 return I18nOpen(str, _O_RDONLY | _O_BINARY);
155 #if LOCAL_USE_WIN32_WOPEN
156 if ( !append ) {
return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE); }
157 else {
return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_APPEND | _O_BINARY, _S_IREAD | _S_IWRITE); }
159 if ( !append ) {
return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); }
160 else {
return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE); }
166 inline FILE *
I18nFopen(
const std::string & str,
const std::string & mode)
168 #if LOCAL_USE_WIN32_WOPEN
170 const std::wstring str_utf16 = Utf8StringToWString(str);
171 const std::wstring mode_utf16 = Utf8StringToWString(mode);
172 return _wfopen( str_utf16.c_str(), mode_utf16.c_str() );
174 return fopen( str.c_str(), mode.c_str() );
178 #if LOCAL_USE_FDSTREAM
183 std::ios_base::openmode mode = std::ios_base::out):
194 bool is_open() {
return ( m_fd != -1 ); }
198 if ( m_fd != -1 ) { ::close(m_fd); }
211 std::ios_base::openmode mode = std::ios_base::in):
223 bool is_open() {
return ( m_fd != -1 ); }
227 if ( m_fd != -1 ) { ::close(m_fd); }
235 #elif LOCAL_USE_WIN32_WOPEN
239 I18nOfstream(
const char *str, std::ios_base::openmode mode = std::ios_base::out):
240 std::ofstream(Utf8StringToWString(str).c_str(), mode)
247 I18nIfstream(
const char *str, std::ios_base::openmode mode = std::ios_base::in):
248 std::ifstream(Utf8StringToWString(str).c_str(), mode)
258 #undef LOCAL_USE_WIN32_WOPEN
259 #undef LOCAL_USE_FDSTREAM