Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:37

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,
0037                                    const char* theMode);
0038 
0039 //! Function retrieves file timestamp.
0040 //! @param theName name of file encoded in UTF-8
0041 //! @return stat.st_ctime value
0042 Standard_EXPORT Standard_Time OSD_FileStatCTime (const char* theName);
0043 
0044 //! Open file descriptor for specified UTF-16 file path.
0045 //! @param theName name of file encoded in UTF-16
0046 //! @param theMode opening mode
0047 //! @return file descriptor on success or -1 on error
0048 Standard_EXPORT int OSD_OpenFileDescriptor (const TCollection_ExtendedString& theName,
0049                                            ::std::ios_base::openmode theMode);
0050 
0051 //! Function opens the file buffer.
0052 //! @param theFileBuf file buffer to open
0053 //! @param theName name of file encoded in UTF-16
0054 //! @param theMode opening mode
0055 //! @return true if success, false otherwise
0056 inline bool OSD_OpenStream (::std::filebuf& theFileBuf,
0057                             const TCollection_ExtendedString& theName,
0058                             const std::ios_base::openmode theMode)
0059 {
0060 #if defined(_WIN32)
0061   #if defined(__GLIBCXX__)
0062   // if file buffer is already open, open() should fail according to C++ standard
0063   if (theFileBuf.is_open())
0064     return false;
0065   // __gnu_cxx::stdio_filebuf is a std::filebuf providing extra constructor taking FILE* or file descriptor;
0066   // It does not modify virtual methods or add any fields - so we can safely use swap (or move operator) here.
0067   // MinGW does not provide open() methods taking wchar_t* or file descriptor - thus, creating __gnu_cxx::stdio_filebuf
0068   // is the only way for opening such files 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_Utf8String aString (theName.ToExtString());
0083   return theFileBuf.open (aString.ToCString(), theMode) != 0;
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 std::fstream/std::ifstream/std::ofstream.
0100   // Should be removed when MinGW will be improved to support wchar_t file paths natively within C++ streams.
0101   if (! OSD_OpenStream (*theStream.rdbuf(), theName, theMode))
0102   {
0103     theStream.setstate (std::ios_base::failbit);
0104   }
0105   else
0106   {
0107     theStream.clear();
0108   }
0109   #else
0110   theStream.open (theName.ToWideString(), theMode);
0111   #endif
0112 #else
0113   // conversion in UTF-8 for linux
0114   NCollection_Utf8String aString (theName.ToExtString());
0115   theStream.open (aString.ToCString(), theMode);
0116 #endif
0117 }
0118 
0119 //! Function opens the file stream.
0120 //! @param theStream stream to open
0121 //! @param theName name of file encoded in UTF-8
0122 //! @param theMode opening mode
0123 template <typename T>
0124 inline void OSD_OpenStream (T& theStream,
0125                             const char* theName,
0126                             const std::ios_base::openmode theMode)
0127 {
0128 #if defined(_WIN32)
0129   // redirect to method taking UTF-16 string
0130   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
0131   OSD_OpenStream (theStream, aFileNameW, theMode);
0132 #else
0133   theStream.open (theName, theMode);
0134 #endif
0135 }
0136 
0137 extern "C" {
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