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.
19 #include <OSD_OpenFile.hxx>
21 #include <sys/types.h>
25 //! Auxiliary function converting C++ ios open mode flags to C fopen() flags.
26 static int OSD_OpenFile_iosMode2FileFlags (::std::ios_base::openmode theMode)
29 if (theMode & ::std::ios_base::in)
33 if (theMode & ::std::ios_base::out)
37 if (theMode & ::std::ios_base::app)
41 if (theMode & ::std::ios_base::trunc)
47 if (theMode & ::std::ios_base::binary)
59 // ==============================================
60 // function : OSD_OpenFile
61 // purpose : Opens file
62 // ==============================================
63 int OSD_OpenFileDescriptor (const TCollection_ExtendedString& theName,
64 ::std::ios_base::openmode theMode)
67 const int aFlags = OSD_OpenFile_iosMode2FileFlags (theMode);
69 const errno_t anErrCode = _wsopen_s (&aFileDesc, theName.ToWideString(), aFlags, _SH_DENYNO, _S_IREAD | _S_IWRITE);
75 NCollection_Utf8String aString (theName.ToExtString());
76 aFileDesc = open (aString.ToCString(), aFlags);
81 // ==============================================
82 // function : OSD_OpenFile
83 // purpose : Opens file
84 // ==============================================
85 FILE* OSD_OpenFile(const char* theName,
90 // file name is treated as UTF-8 string and converted to UTF-16 one
91 const TCollection_ExtendedString aFileNameW (theName, Standard_True);
92 const TCollection_ExtendedString aFileModeW (theMode, Standard_True);
93 aFile = ::_wfopen (aFileNameW.ToWideString(),
94 aFileModeW.ToWideString());
96 aFile = ::fopen (theName, theMode);
101 // ==============================================
102 // function : OSD_OpenFile
103 // purpose : Opens file
104 // ==============================================
105 FILE* OSD_OpenFile(const TCollection_ExtendedString& theName,
110 const TCollection_ExtendedString aFileModeW (theMode, Standard_True);
111 aFile = ::_wfopen (theName.ToWideString(),
112 aFileModeW.ToWideString());
114 // conversion in UTF-8 for linux
115 NCollection_Utf8String aString (theName.ToExtString());
116 aFile = ::fopen (aString.ToCString(),theMode);
121 // ==============================================
122 // function : OSD_FileStatCTime
124 // ==============================================
125 Standard_Time OSD_FileStatCTime (const char* theName)
127 Standard_Time aTime = 0;
129 // file name is treated as UTF-8 string and converted to UTF-16 one
130 const TCollection_ExtendedString aFileNameW (theName, Standard_True);
131 struct __stat64 aStat;
132 if (_wstat64 (aFileNameW.ToWideString(), &aStat) == 0)
134 aTime = (Standard_Time )aStat.st_ctime;
138 if (stat (theName, &aStat) == 0)
140 aTime = (Standard_Time )aStat.st_ctime;