0022651: Impossible to build OCC as static library due to using Standard_EXPORT inste...
[occt.git] / src / OSD / OSD_OpenFile.hxx
1 // Copyright (c) 2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 //! Auxiulary file to provide Unicode compatibility for file open functionality
15 //! Names of files are encoded as UTF-16 strings
16
17 #ifndef _OSD_OpenFile_HeaderFile
18 #define _OSD_OpenFile_HeaderFile
19
20 #include <Standard_Macro.hxx>
21
22 #if defined(__cplusplus)
23
24 #include <fstream>
25 #include <TCollection_ExtendedString.hxx>
26 #include <NCollection_UtfString.hxx>
27
28 #if defined(_WIN32) && defined(__GLIBCXX__)
29   #include <ext/stdio_filebuf.h> // __gnu_cxx::stdio_filebuf
30 #endif
31
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,
37                                    const char* theMode);
38
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);
43
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);
50
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)
59 {
60 #if defined(_WIN32)
61   #if defined(__GLIBCXX__)
62   // if file buffer is already open, open() should fail according to C++ standard
63   if (theFileBuf.is_open())
64     return false;
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())
72   {
73     theFileBuf.swap (aGccBuf);
74     return true;
75   }
76   return false;
77   #else
78   return theFileBuf.open (theName.ToWideString(), theMode) != 0;
79   #endif
80 #else
81   // conversion to UTF-8 for linux
82   NCollection_Utf8String aString (theName.ToExtString());
83   return theFileBuf.open (aString.ToCString(), theMode) != 0;
84 #endif
85 }
86
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
91 template <typename T>
92 inline void OSD_OpenStream (T& theStream,
93                             const TCollection_ExtendedString& theName,
94                             const std::ios_base::openmode theMode)
95 {
96 #if defined(_WIN32)
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))
102   {
103     theStream.setstate (std::ios_base::failbit);
104   }
105   else
106   {
107     theStream.clear();
108   }
109   #else
110   theStream.open (theName.ToWideString(), theMode);
111   #endif
112 #else
113   // conversion in UTF-8 for linux
114   NCollection_Utf8String aString (theName.ToExtString());
115   theStream.open (aString.ToCString(), theMode);
116 #endif
117 }
118
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,
125                             const char* theName,
126                             const std::ios_base::openmode theMode)
127 {
128 #if defined(_WIN32)
129   // redirect to method taking UTF-16 string
130   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
131   OSD_OpenStream (theStream, aFileNameW, theMode);
132 #else
133   theStream.open (theName, theMode);
134 #endif
135 }
136
137 extern "C" {
138 #endif // __cplusplus
139
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);
145
146 #if defined(__cplusplus)
147 }
148 #endif // __cplusplus
149
150 #endif // _OSD_OpenFile_HeaderFile