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_IREAD | S_IWRITE); }
161 else {
return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE); }
167 inline FILE *
I18nFopen(
const std::string & str,
const std::string & mode)
169 #if LOCAL_USE_WIN32_WOPEN
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() );
175 return fopen( str.c_str(), mode.c_str() );
179 #if LOCAL_USE_FDSTREAM
184 std::ios_base::openmode mode = std::ios_base::out):
195 bool is_open() {
return ( m_fd != -1 ); }
199 if ( m_fd != -1 ) { ::close(m_fd); }
212 std::ios_base::openmode mode = std::ios_base::in):
224 bool is_open() {
return ( m_fd != -1 ); }
228 if ( m_fd != -1 ) { ::close(m_fd); }
236 #elif LOCAL_USE_WIN32_WOPEN
240 I18nOfstream(
const char *str, std::ios_base::openmode mode = std::ios_base::out):
241 std::ofstream(Utf8StringToWString(str).c_str(), mode)
248 I18nIfstream(
const char *str, std::ios_base::openmode mode = std::ios_base::in):
249 std::ifstream(Utf8StringToWString(str).c_str(), mode)
259 #undef LOCAL_USE_WIN32_WOPEN
260 #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)