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
95Utf8StringToWString(
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);
133I18nOpen(
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);
176 return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
181 return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
185 return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE);
193I18nFopen(
const std::string & str,
const std::string & mode)
195#if LOCAL_USE_WIN32_WOPEN
197 const std::wstring str_utf16 = Utf8StringToWString(str);
198 const std::wstring mode_utf16 = Utf8StringToWString(mode);
199 return _wfopen(str_utf16.c_str(), mode_utf16.c_str());
201 return fopen(str.c_str(), mode.c_str());
205#if LOCAL_USE_FDSTREAM
209 I18nOfstream(
const char * str, std::ios_base::openmode mode = std::ios_base::out)
218 ~I18nOfstream() { this->close(); }
244 I18nIfstream(
const char * str, std::ios_base::openmode itkNotused(mode) = std::ios_base::in)
253 ~I18nIfstream() { this->close(); }
275#elif LOCAL_USE_WIN32_WOPEN
279 I18nOfstream(
const char * str, std::ios_base::openmode mode = std::ios_base::out)
280 :
std::ofstream(Utf8StringToWString(str).c_str(), mode)
287 I18nIfstream(
const char * str, std::ios_base::openmode mode = std::ios_base::in)
288 :
std::ifstream(Utf8StringToWString(str).c_str(), mode)
298#undef LOCAL_USE_WIN32_WOPEN
299#undef LOCAL_USE_FDSTREAM
int I18nOpen(const std::string &str, const int flags)
int I18nOpenForReading(const std::string &str)
bool IsStringEncodingValid(const std::string &)
std::ofstream I18nOfstream
std::ifstream I18nIfstream
int I18nOpenForWriting(const std::string &str, const bool append=false)
FILE * I18nFopen(const std::string &str, const std::string &mode)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....