Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/OSD_OpenFile.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (c) 2014 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 //! Auxiulary file to provide Unicode compatibility for file open functionality
0015 //! Names of files are encoded as UTF-16 strings
0016 
0017 #ifndef _OSD_OpenFile_HeaderFile
0018 #define _OSD_OpenFile_HeaderFile
0019 
0020 #include <Standard_Macro.hxx>
0021 
0022 #if defined(__cplusplus)
0023 
0024   #include <fstream>
0025   #include <TCollection_ExtendedString.hxx>
0026   #include <NCollection_UtfString.hxx>
0027 
0028   #if defined(_WIN32) && defined(__GLIBCXX__)
0029     #include <ext/stdio_filebuf.h> // __gnu_cxx::stdio_filebuf
0030   #endif
0031 
0032 //! Function opens the file.
0033 //! @param theName name of file encoded in UTF-16
0034 //! @param theMode opening mode
0035 //! @return file handle of opened file
0036 Standard_EXPORT FILE* OSD_OpenFile(const TCollection_ExtendedString& theName, const char* theMode);
0037 
0038 //! Function retrieves file timestamp.
0039 //! @param theName name of file encoded in UTF-8
0040 //! @return stat.st_ctime value
0041 Standard_EXPORT std::time_t OSD_FileStatCTime(const char* theName);
0042 
0043 //! Open file descriptor for specified UTF-16 file path.
0044 //! @param theName name of file encoded in UTF-16
0045 //! @param theMode opening mode
0046 //! @return file descriptor on success or -1 on error
0047 Standard_EXPORT int OSD_OpenFileDescriptor(const TCollection_ExtendedString& theName,
0048                                            ::std::ios_base::openmode         theMode);
0049 
0050 //! Function opens the file buffer.
0051 //! @param theFileBuf file buffer to open
0052 //! @param theName name of file encoded in UTF-16
0053 //! @param theMode opening mode
0054 //! @return true if success, false otherwise
0055 inline bool OSD_OpenStream(::std::filebuf&                   theFileBuf,
0056                            const TCollection_ExtendedString& theName,
0057                            const std::ios_base::openmode     theMode)
0058 {
0059   #if defined(_WIN32)
0060     #if defined(__GLIBCXX__)
0061   // if file buffer is already open, open() should fail according to C++ standard
0062   if (theFileBuf.is_open())
0063     return false;
0064   // __gnu_cxx::stdio_filebuf is a std::filebuf providing extra constructor taking FILE* or file
0065   // descriptor; It does not modify virtual methods or add any fields - so we can safely use swap
0066   // (or move operator) here. MinGW does not provide open() methods taking wchar_t* or file
0067   // descriptor - thus, creating __gnu_cxx::stdio_filebuf is the only way for opening such files
0068   // since _wfopen()/_wsopen_s() from C world are available.
0069   const int aFileDesc = OSD_OpenFileDescriptor(theName.ToWideString(), theMode);
0070   __gnu_cxx::stdio_filebuf<char> aGccBuf(aFileDesc, theMode);
0071   if (aGccBuf.is_open())
0072   {
0073     theFileBuf.swap(aGccBuf);
0074     return true;
0075   }
0076   return false;
0077     #else
0078   return theFileBuf.open(theName.ToWideString(), theMode) != 0;
0079     #endif
0080   #else
0081   // conversion to UTF-8 for linux
0082   NCollection_UtfString<char> aString(theName.ToExtString());
0083   return theFileBuf.open(aString.ToCString(), theMode) != nullptr;
0084   #endif
0085 }
0086 
0087 //! Function opens the file stream.
0088 //! @param theStream stream to open
0089 //! @param theName name of file encoded in UTF-16
0090 //! @param theMode opening mode
0091 template <typename T>
0092 inline void OSD_OpenStream(T&                                theStream,
0093                            const TCollection_ExtendedString& theName,
0094                            const std::ios_base::openmode     theMode)
0095 {
0096   #if defined(_WIN32)
0097     #if defined(__GLIBCXX__)
0098   // Use hackish code for opening wchar_t* file paths on MinGW,
0099   // which considers implementation details of std::filebuf within
0100   // std::fstream/std::ifstream/std::ofstream. Should be removed when MinGW will be improved to
0101   // support wchar_t file paths natively within C++ streams.
0102   if (!OSD_OpenStream(*theStream.rdbuf(), theName, theMode))
0103   {
0104     theStream.setstate(std::ios_base::failbit);
0105   }
0106   else
0107   {
0108     theStream.clear();
0109   }
0110     #else
0111   theStream.open(theName.ToWideString(), theMode);
0112     #endif
0113   #else
0114   // conversion in UTF-8 for linux
0115   NCollection_UtfString<char> aString(theName.ToExtString());
0116   theStream.open(aString.ToCString(), theMode);
0117   #endif
0118 }
0119 
0120 //! Function opens the file stream.
0121 //! @param theStream stream to open
0122 //! @param theName name of file encoded in UTF-8
0123 //! @param theMode opening mode
0124 template <typename T>
0125 inline void OSD_OpenStream(T& theStream, const char* theName, const std::ios_base::openmode theMode)
0126 {
0127   #if defined(_WIN32)
0128   // redirect to method taking UTF-16 string
0129   const TCollection_ExtendedString aFileNameW(theName, true);
0130   OSD_OpenStream(theStream, aFileNameW, theMode);
0131   #else
0132   theStream.open(theName, theMode);
0133   #endif
0134 }
0135 
0136 extern "C"
0137 {
0138 #endif // __cplusplus
0139 
0140   //! Function opens the file.
0141   //! @param theName name of file encoded in UTF-8
0142   //! @param theMode opening mode
0143   //! @return file handle of opened file
0144   Standard_EXPORT FILE* OSD_OpenFile(const char* theName, const char* theMode);
0145 
0146 #if defined(__cplusplus)
0147 }
0148 #endif // __cplusplus
0149 
0150 #endif // _OSD_OpenFile_HeaderFile