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)) || (!LOCAL_USE_WIN32_WOPEN)
58 # define LOCAL_USE_FDSTREAM 0
61 # define LOCAL_USE_FDSTREAM 1
62 # include "itkfdstream/fdstream.hxx"
70 #if LOCAL_USE_WIN32_WOPEN
77 const int utf16_size =
78 MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), static_cast<int>(str.length()),
nullptr, 0);
80 return (utf16_size != 0);
92 #if LOCAL_USE_WIN32_WOPEN
95 Utf8StringToWString(
const std::string & str)
102 const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()),
nullptr, 0);
107 wstr.resize(utf16_size);
108 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), &wstr[0], utf16_size);
120 #if LOCAL_USE_WIN32_WOPEN
123 const std::wstring str_utf16 = Utf8StringToWString(str);
124 return _wopen(str_utf16.c_str(), flags);
126 return open(str.c_str(), flags);
133 I18nOpen(
const std::string & str,
const int flags,
const int mode)
135 #if LOCAL_USE_WIN32_WOPEN
138 const std::wstring str_utf16 = Utf8StringToWString(str);
139 return _wopen(str_utf16.c_str(), flags, mode);
141 return open(str.c_str(), flags, mode);
149 #if LOCAL_USE_WIN32_WOPEN
150 return I18nOpen(str, _O_RDONLY | _O_BINARY);
160 #if LOCAL_USE_WIN32_WOPEN
163 return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE);
167 return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_APPEND | _O_BINARY, _S_IREAD | _S_IWRITE);
172 return I18nOpen(str, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
175 return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
180 return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
184 return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE);
192 I18nFopen(
const std::string & str,
const std::string & mode)
194 #if LOCAL_USE_WIN32_WOPEN
196 const std::wstring str_utf16 = Utf8StringToWString(str);
197 const std::wstring mode_utf16 = Utf8StringToWString(mode);
198 return _wfopen(str_utf16.c_str(), mode_utf16.c_str());
200 return fopen(str.c_str(), mode.c_str());
204 #if LOCAL_USE_FDSTREAM
208 I18nOfstream(
const char * str, std::ios_base::openmode mode = std::ios_base::out)
243 I18nIfstream(
const char * str, std::ios_base::openmode itkNotused(mode) = std::ios_base::in)
274 #elif LOCAL_USE_WIN32_WOPEN
278 I18nOfstream(
const char * str, std::ios_base::openmode mode = std::ios_base::out)
279 : std::ofstream(Utf8StringToWString(str).c_str(), mode)
286 I18nIfstream(
const char * str, std::ios_base::openmode mode = std::ios_base::in)
287 : std::ifstream(Utf8StringToWString(str).c_str(), mode)
297 #undef LOCAL_USE_WIN32_WOPEN
298 #undef LOCAL_USE_FDSTREAM