1 // Copyright (c) 2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 //! Auxiulary file to provide Unicode compatibility for file open functionality
15 //! Names of files are encoded as UTF-16 strings
17 #ifndef _OSD_OpenFile_HeaderFile
18 #define _OSD_OpenFile_HeaderFile
20 #include <Standard_Macro.hxx>
22 #if defined(__cplusplus)
25 #include <TCollection_ExtendedString.hxx>
26 #include <NCollection_UtfString.hxx>
28 #if defined(_WIN32) && defined(__GLIBCXX__)
29 #include <ext/stdio_filebuf.h> // __gnu_cxx::stdio_filebuf
32 //! Function opens the file.
33 //! @param theName name of file encoded in UTF-16
34 //! @param theMode opening mode
35 //! @return file handle of opened file
36 Standard_EXPORT FILE* OSD_OpenFile (const TCollection_ExtendedString& theName,
39 //! Function retrieves file timestamp.
40 //! @param theName name of file encoded in UTF-8
41 //! @return stat.st_ctime value
42 Standard_EXPORT Standard_Time OSD_FileStatCTime (const char* theName);
44 //! Open file descriptor for specified UTF-16 file path.
45 //! @param theName name of file encoded in UTF-16
46 //! @param theMode opening mode
47 //! @return file descriptor on success or -1 on error
48 Standard_EXPORT int OSD_OpenFileDescriptor (const TCollection_ExtendedString& theName,
49 ::std::ios_base::openmode theMode);
51 //! Function opens the file buffer.
52 //! @param theFileBuf file buffer to open
53 //! @param theName name of file encoded in UTF-16
54 //! @param theMode opening mode
55 //! @return true if success, false otherwise
56 inline bool OSD_OpenStream (::std::filebuf& theFileBuf,
57 const TCollection_ExtendedString& theName,
58 const std::ios_base::openmode theMode)
61 #if defined(__GLIBCXX__)
62 // if file buffer is already open, open() should fail according to C++ standard
63 if (theFileBuf.is_open())
65 // __gnu_cxx::stdio_filebuf is a std::filebuf providing extra constructor taking FILE* or file descriptor;
66 // It does not modify virtual methods or add any fields - so we can safely use swap (or move operator) here.
67 // MinGW does not provide open() methods taking wchar_t* or file descriptor - thus, creating __gnu_cxx::stdio_filebuf
68 // is the only way for opening such files since _wfopen()/_wsopen_s() from C world are available.
69 const int aFileDesc = OSD_OpenFileDescriptor (theName.ToWideString(), theMode);
70 __gnu_cxx::stdio_filebuf<char> aGccBuf (aFileDesc, theMode);
71 if (aGccBuf.is_open())
73 theFileBuf.swap (aGccBuf);
78 return theFileBuf.open (theName.ToWideString(), theMode) != 0;
81 // conversion to UTF-8 for linux
82 NCollection_Utf8String aString (theName.ToExtString());
83 return theFileBuf.open (aString.ToCString(), theMode) != 0;
87 //! Function opens the file stream.
88 //! @param theStream stream to open
89 //! @param theName name of file encoded in UTF-16
90 //! @param theMode opening mode
92 inline void OSD_OpenStream (T& theStream,
93 const TCollection_ExtendedString& theName,
94 const std::ios_base::openmode theMode)
97 #if defined(__GLIBCXX__)
98 // Use hackish code for opening wchar_t* file paths on MinGW,
99 // which considers implementation details of std::filebuf within std::fstream/std::ifstream/std::ofstream.
100 // Should be removed when MinGW will be improved to support wchar_t file paths natively within C++ streams.
101 if (! OSD_OpenStream (*theStream.rdbuf(), theName, theMode))
103 theStream.setstate (std::ios_base::failbit);
110 theStream.open (theName.ToWideString(), theMode);
113 // conversion in UTF-8 for linux
114 NCollection_Utf8String aString (theName.ToExtString());
115 theStream.open (aString.ToCString(), theMode);
119 //! Function opens the file stream.
120 //! @param theStream stream to open
121 //! @param theName name of file encoded in UTF-8
122 //! @param theMode opening mode
123 template <typename T>
124 inline void OSD_OpenStream (T& theStream,
126 const std::ios_base::openmode theMode)
129 // redirect to method taking UTF-16 string
130 const TCollection_ExtendedString aFileNameW (theName, Standard_True);
131 OSD_OpenStream (theStream, aFileNameW, theMode);
133 theStream.open (theName, theMode);
138 #endif // __cplusplus
140 //! Function opens the file.
141 //! @param theName name of file encoded in UTF-8
142 //! @param theMode opening mode
143 //! @return file handle of opened file
144 Standard_EXPORT FILE* OSD_OpenFile (const char* theName, const char* theMode);
146 #if defined(__cplusplus)
148 #endif // __cplusplus
150 #endif // _OSD_OpenFile_HeaderFile