0030959: OSD_Parallel_TBB: number of execution threads is strictly limited by the...
[occt.git] / src / OSD / OSD_OpenFile.hxx
CommitLineData
94708556 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>
fb0b0531 26#include <NCollection_UtfString.hxx>
94708556 27
fc8918ad 28#if defined(_WIN32) && defined(__GLIBCXX__)
29 #include <ext/stdio_filebuf.h> // __gnu_cxx::stdio_filebuf
30#endif
31
fb0b0531 32//! Function opens the file.
94708556 33//! @param theName name of file encoded in UTF-16
34//! @param theMode opening mode
fb0b0531 35//! @return file handle of opened file
68df8478 36Standard_EXPORT FILE* OSD_OpenFile (const TCollection_ExtendedString& theName,
fb0b0531 37 const char* theMode);
94708556 38
fb0b0531 39//! Function retrieves file timestamp.
b40693b0 40//! @param theName name of file encoded in UTF-8
fb0b0531 41//! @return stat.st_ctime value
68df8478 42Standard_EXPORT Standard_Time OSD_FileStatCTime (const char* theName);
b40693b0 43
fc8918ad 44//! Open file descriptor for specified UTF-16 file path.
45//! @param theName name of file encoded in UTF-16
94708556 46//! @param theMode opening mode
fc8918ad 47//! @return file descriptor on success or -1 on error
68df8478 48Standard_EXPORT int OSD_OpenFileDescriptor (const TCollection_ExtendedString& theName,
fc8918ad 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
56inline bool OSD_OpenStream (::std::filebuf& theFileBuf,
57 const TCollection_ExtendedString& theName,
fb0b0531 58 const std::ios_base::openmode theMode)
59{
fc8918ad 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
fb0b0531 80#else
fc8918ad 81 // conversion to UTF-8 for linux
82 NCollection_Utf8String aString (theName.ToExtString());
83 return theFileBuf.open (aString.ToCString(), theMode) != 0;
fb0b0531 84#endif
85}
94708556 86
fb0b0531 87//! Function opens the file stream.
88//! @param theStream stream to open
94708556 89//! @param theName name of file encoded in UTF-16
90//! @param theMode opening mode
fb0b0531 91template <typename T>
92inline void OSD_OpenStream (T& theStream,
93 const TCollection_ExtendedString& theName,
94 const std::ios_base::openmode theMode)
95{
fc8918ad 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
fb0b0531 110 theStream.open (theName.ToWideString(), theMode);
fc8918ad 111 #endif
fb0b0531 112#else
113 // conversion in UTF-8 for linux
114 NCollection_Utf8String aString (theName.ToExtString());
115 theStream.open (aString.ToCString(), theMode);
116#endif
117}
ce0594b8 118
fc8918ad 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
123template <typename T>
124inline 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
94708556 137extern "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
68df8478 144Standard_EXPORT FILE* OSD_OpenFile (const char* theName, const char* theMode);
94708556 145
146#if defined(__cplusplus)
147}
148#endif // __cplusplus
149
150#endif // _OSD_OpenFile_HeaderFile