Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkInternationalizationIOHelpers.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkInternationalizationIOHelpers.h,v $
00005   Language:  C++
00006   Date:      $Date: 2010-01-13 20:16:01 $
00007   Version:   $Revision: 1.3 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkInternationalizationIOHelpers_h
00018 #define __itkInternationalizationIOHelpers_h
00019 
00020 // This header provides some helper functions to deal with unicode filenames
00021 // It is mainly directed towards being able to use utf-8 encoded filenames
00022 // on windows.
00023 // This should help better dealing with internationalization (a.k.a i18n)
00024 
00025 #include "itkConfigure.h"
00026 #include "itkMacro.h"
00027 
00028 #ifdef ITK_HAVE_UNISTD_H
00029 # include <unistd.h> // for unlink
00030 #else
00031 # include <io.h>
00032 #endif
00033 
00034 #include <stdio.h> // Borland needs this (cstdio does not work easy)
00035 #include <fcntl.h>
00036 #include <iostream>
00037 #include <string>
00038 #include <sys/stat.h>
00039 
00040 // Find out how to handle unicode filenames on windows:
00041 // * VS>=8.0 has _wopen and _wfopen and can open a (i/o)fstream using a wide string
00042 // * cygwin has NO _wopen an NO _wfopen. If you really need unicode
00043 //   filenames on cygwin, just use cygwin >= 1.7 for now, it works with utf8
00044 //   natively. Alternatively, we could try and use pure win32 functions such as
00045 //   CreateFileW and convert the win32 file handle using _open_osfhandle and _fdopen
00046 // * VS6.0 has _wopen and _wfopen but cannot open a (i/o)fstream using a wide string
00047 //   nor can it compile fdstream => disable unicode filename support
00048 // * Borland c++, VS7.x and MinGW have _wopen and _wfopen but cannot open a
00049 //   (i/o)fstream using a wide string. They can however compile fdstream
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> // required by winnls.h
00055 # include <winnls.h> // for MultiByteToWideChar
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 // Check if the string is correctly encoded
00076 #if LOCAL_USE_WIN32_WOPEN
00077 inline bool IsStringEncodingValid(const std::string & str)
00078 {
00079   // Check if the string is really encoded in utf-8 using windows API
00080   // MultiByteToWideChar returns 0 if there was a problem during conversion
00081   // when given the MB_ERR_INVALID_CHARS flag
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 // Convert a utf8 encoded std::string to a utf16 encoded wstring on windows
00095 inline std::wstring Utf8StringToWString( const std::string & str )
00096 {
00097   // We do not set the MB_ERR_INVALID_CHARS to do an approximate conversion when non
00098   // utf8 characters are found. An alternative would be to throw an exception
00099 
00100   // First get the size
00101   const int utf16_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(),
00102                                              static_cast<int>(str.length()), 0, 0);
00103 
00104   // Now do the conversion
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 // Get a file descriptor from a filename (using utf8 to wstring
00116 // on windows if requested) without specifying any specific permissions
00117 inline int I18nOpen( const std::string & str, const int & flags )
00118 {
00119 #if LOCAL_USE_WIN32_WOPEN
00120   // cygwin has NO _wopen but mingw has
00121   // If you really need unicode filenames on cygwin, just use cygwin >= 1.7
00122   // Convert to utf16
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 // Get a file descriptor from a filename (using utf8 to wstring
00131 // on windows if requested)
00132 inline int I18nOpen( const std::string & str, const int & flags, const int & mode )
00133 {
00134 #if LOCAL_USE_WIN32_WOPEN
00135   // cygwin has NO _wopen but mingw has
00136   // If you really need unicode filenames on cygwin, just use cygwin >= 1.7
00137   // Convert to utf16
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 // Reading wrapper around I18nOpen to avoid explicitely specifying the flags
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 // Writting wrapper around I18nOpen to avoid explicitely specifying the flags
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 // Get a FILE * pointer from a filename (using utf8 to wstring
00170 // on windows if requested)
00171 inline FILE * I18nFopen( const std::string & str, const std::string & mode )
00172 {
00173 #if LOCAL_USE_WIN32_WOPEN
00174   // cygwin has NO _wfopen but mingw has
00175   // If you really need unicode filenames on cygwin, just use cygwin >= 1.7
00176   // Convert to utf16
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 } // end namespace
00265 } // end namespace
00266 
00267 
00268 #undef LOCAL_USE_WIN32_WOPEN
00269 #undef LOCAL_USE_FDSTREAM
00270 
00271 #endif  /* __itkInternationalizationIOHelpers_h */
00272 

Generated at Mon Jul 12 2010 18:50:34 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000