00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkInternationalizationIOHelpers_h
00018 #define __itkInternationalizationIOHelpers_h
00019
00020
00021
00022
00023
00024
00025 #include "itkConfigure.h"
00026 #include "itkMacro.h"
00027
00028 #ifdef ITK_HAVE_UNISTD_H
00029 # include <unistd.h>
00030 #else
00031 # include <io.h>
00032 #endif
00033
00034 #include <stdio.h>
00035 #include <fcntl.h>
00036 #include <iostream>
00037 #include <string>
00038 #include <sys/stat.h>
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #if defined(ITK_SUPPORTS_WCHAR_T_FILENAME_CSTYLEIO) \
00052 && ( defined(ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS) || defined(ITK_SUPPORTS_FDSTREAM_HPP) )
00053 # define LOCAL_USE_WIN32_WOPEN 1
00054 # include <windows.h>
00055 # include <winnls.h>
00056 #else
00057 # define LOCAL_USE_WIN32_WOPEN 0
00058 #endif
00059
00060 #if (LOCAL_USE_WIN32_WOPEN && defined(ITK_SUPPORTS_WCHAR_T_FILENAME_IOSTREAMS_CONSTRUCTORS)) \
00061 || (!LOCAL_USE_WIN32_WOPEN)
00062 # define LOCAL_USE_FDSTREAM 0
00063 # include <fstream>
00064 #else
00065 # define LOCAL_USE_FDSTREAM 1
00066 # include "fdstream.hpp"
00067 #endif
00068
00069
00070 namespace itk
00071 {
00072 namespace i18n
00073 {
00074
00075
00076 #if LOCAL_USE_WIN32_WOPEN
00077 inline bool IsStringEncodingValid(const std::string & str)
00078 {
00079
00080
00081
00082 const int utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(),
00083 static_cast<int>(str.length()), 0, 0);
00084 return (utf16_size != 0);
00085 }
00086 #else
00087 inline bool IsStringEncodingValid(const std::string & itkNotUsed( str ) )
00088 {
00089 return true;
00090 }
00091 #endif
00092
00093 #if LOCAL_USE_WIN32_WOPEN
00094
00095 inline std::wstring Utf8StringToWString( const std::string & str )
00096 {
00097
00098
00099
00100
00101 const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
00102 static_cast<int>(str.length()), 0, 0);
00103
00104
00105 std::wstring wstr;
00106 wstr.resize(utf16_size);
00107 MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
00108 static_cast<int>(str.length()), &wstr[0], utf16_size);
00109
00110 return wstr;
00111 }
00112
00113 #endif
00114
00115
00116
00117 inline int I18nOpen( const std::string & str, const int & flags )
00118 {
00119 #if LOCAL_USE_WIN32_WOPEN
00120
00121
00122
00123 const std::wstring str_utf16 = Utf8StringToWString( str );
00124 return _wopen(str_utf16.c_str(), flags);
00125 #else
00126 return open(str.c_str(), flags);
00127 #endif
00128 }
00129
00130
00131
00132 inline int I18nOpen( const std::string & str, const int & flags, const int & mode )
00133 {
00134 #if LOCAL_USE_WIN32_WOPEN
00135
00136
00137
00138 const std::wstring str_utf16 = Utf8StringToWString( str );
00139 return _wopen(str_utf16.c_str(), flags, mode);
00140 #else
00141 return open(str.c_str(), flags, mode);
00142 #endif
00143 }
00144
00145
00146 inline int I18nOpenForReading( const std::string & str )
00147 {
00148 #if LOCAL_USE_WIN32_WOPEN
00149 return I18nOpen(str, _O_RDONLY | _O_BINARY );
00150 #else
00151
00152 return I18nOpen(str, O_RDONLY );
00153 #endif
00154 }
00155
00156
00157 inline int I18nOpenForWritting( const std::string & str, const bool append = false )
00158 {
00159 #if LOCAL_USE_WIN32_WOPEN
00160 if (!append) return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE );
00161 else return I18nOpen(str, _O_WRONLY | _O_CREAT | _O_APPEND | _O_BINARY, _S_IREAD | _S_IWRITE );
00162 #else
00163
00164 if (!append) return I18nOpen(str, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );
00165 else return I18nOpen(str, O_WRONLY | O_CREAT | O_APPEND, S_IREAD | S_IWRITE );
00166 #endif
00167 }
00168
00169
00170
00171 inline FILE * I18nFopen( const std::string & str, const std::string & mode )
00172 {
00173 #if LOCAL_USE_WIN32_WOPEN
00174
00175
00176
00177 const std::wstring str_utf16 = Utf8StringToWString( str );
00178 const std::wstring mode_utf16 = Utf8StringToWString( mode );
00179 return _wfopen(str_utf16.c_str(), mode_utf16.c_str());
00180 #else
00181 return fopen(str.c_str(), mode.c_str());
00182 #endif
00183 }
00184
00185 #if LOCAL_USE_FDSTREAM
00186 class I18nOfstream : public std::ostream
00187 {
00188 public:
00189 I18nOfstream( const char * str,
00190 std::ios_base::openmode mode = std::ios_base::out )
00191 : std::ostream(0)
00192 , m_fd( I18nOpenForWritting( str, (mode & std::ios::app)?true:false ) )
00193 , m_buf( m_fd )
00194 {
00196 this->rdbuf(&m_buf);
00197 }
00198
00199 ~I18nOfstream() { this->close(); }
00200
00201 bool is_open() { return (m_fd!=-1); }
00202
00203 void close()
00204 {
00205 if ( m_fd!=-1 ) ::close( m_fd );
00206 m_fd = -1;
00207 }
00208
00209 private:
00210 int m_fd;
00211 itk::fdoutbuf m_buf;
00212 };
00213
00214 class I18nIfstream : public std::istream
00215 {
00216 public:
00217 I18nIfstream( const char * str,
00218 std::ios_base::openmode mode = std::ios_base::in )
00219 : std::istream(0)
00220 , m_fd( I18nOpenforreading( str ) )
00221 , m_buf( m_fd )
00222 {
00224 this->rdbuf(&m_buf);
00225 }
00226
00227 ~I18nIfstream() { this->close(); }
00228
00229 bool is_open() { return (m_fd!=-1); }
00230
00231 void close()
00232 {
00233 if ( m_fd!=-1 ) ::close( m_fd );
00234 m_fd = -1;
00235 }
00236
00237 private:
00238 int m_fd;
00239 itk::fdinbuf m_buf;
00240 };
00241 #elif LOCAL_USE_WIN32_WOPEN
00242 class I18nOfstream : public std::ofstream
00243 {
00244 public:
00245 I18nOfstream( const char * str, std::ios_base::openmode mode = std::ios_base::out )
00246 : std::ofstream( Utf8StringToWString(str).c_str(), mode )
00247 {
00248 }
00249 };
00250
00251 class I18nIfstream : public std::ifstream
00252 {
00253 public:
00254 I18nIfstream( const char * str, std::ios_base::openmode mode = std::ios_base::in )
00255 : std::ifstream( Utf8StringToWString(str).c_str(), mode )
00256 {
00257 }
00258 };
00259 #else
00260 typedef std::ofstream I18nOfstream;
00261 typedef std::ifstream I18nIfstream;
00262 #endif
00263
00264 }
00265 }
00266
00267
00268 #undef LOCAL_USE_WIN32_WOPEN
00269 #undef LOCAL_USE_FDSTREAM
00270
00271 #endif
00272