18 #ifndef itkInternationalizationIOHelpers_h
19 #define itkInternationalizationIOHelpers_h
20 #include "ITKIOImageBaseExport.h"
28 #include "itkIOConfigure.h"
30 #ifdef ITK_HAVE_UNISTD_H
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
54 #define LOCAL_USE_WIN32_WOPEN 0
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
62 #define LOCAL_USE_FDSTREAM 1
63 #include "itkfdstream/fdstream.hpp"
71 #if LOCAL_USE_WIN32_WOPEN
77 const int utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(),
78 static_cast< int >( str.length() ), 0, 0);
80 return ( utf16_size != 0 );
91 #if LOCAL_USE_WIN32_WOPEN
93 inline std::wstring Utf8StringToWString(
const std::string & str)
100 const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
101 static_cast< int >( str.length() ), 0, 0);
106 wstr.resize(utf16_size);
107 MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
108 static_cast< int >( str.length() ), &wstr[0], utf16_size);
117 inline int I18nOpen(
const std::string & str,
const int & flags)
119 #if LOCAL_USE_WIN32_WOPEN
122 const std::wstring str_utf16 = Utf8StringToWString(str);
123 return _wopen(str_utf16.c_str(), flags);
125 return open(str.c_str(), flags);
131 inline int I18nOpen(
const std::string & str,
const int & flags,
const int & mode)
133 #if LOCAL_USE_WIN32_WOPEN
136 const std::wstring str_utf16 = Utf8StringToWString(str);
137 return _wopen(str_utf16.c_str(), flags, mode);
139 return open(str.c_str(), flags, mode);
146 #if LOCAL_USE_WIN32_WOPEN
147 return I18nOpen(str, _O_RDONLY | _O_BINARY);
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); }
160 if ( !append ) {
return I18nOpen(str, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); }
161 else {
return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR); }
163 if ( !append ) {
return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); }
164 else {
return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE); }
170 inline FILE *
I18nFopen(
const std::string & str,
const std::string & mode)
172 #if LOCAL_USE_WIN32_WOPEN
174 const std::wstring str_utf16 = Utf8StringToWString(str);
175 const std::wstring mode_utf16 = Utf8StringToWString(mode);
176 return _wfopen( str_utf16.c_str(), mode_utf16.c_str() );
178 return fopen( str.c_str(), mode.c_str() );
182 #if LOCAL_USE_FDSTREAM
187 std::ios_base::openmode mode = std::ios_base::out):
198 bool is_open() {
return ( m_fd != -1 ); }
202 if ( m_fd != -1 ) { ::close(m_fd); }
215 std::ios_base::openmode mode = std::ios_base::in):
227 bool is_open() {
return ( m_fd != -1 ); }
231 if ( m_fd != -1 ) { ::close(m_fd); }
239 #elif LOCAL_USE_WIN32_WOPEN
243 I18nOfstream(
const char *str, std::ios_base::openmode mode = std::ios_base::out):
244 std::ofstream(Utf8StringToWString(str).c_str(), mode)
251 I18nIfstream(
const char *str, std::ios_base::openmode mode = std::ios_base::in):
252 std::ifstream(Utf8StringToWString(str).c_str(), mode)
262 #undef LOCAL_USE_WIN32_WOPEN
263 #undef LOCAL_USE_FDSTREAM
int I18nOpenForWriting(const std::string &str, const bool append=false)
std::ifstream I18nIfstream
bool IsStringEncodingValid(const std::string &)
std::ofstream I18nOfstream
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)