Commit | Line | Data |
---|---|---|
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 | ||
7c65581d | 14 | #ifdef _WIN32 |
15 | #include <windows.h> | |
fc8918ad | 16 | #include <share.h> |
7c65581d | 17 | #endif |
18 | ||
94708556 | 19 | #include <OSD_OpenFile.hxx> |
94708556 | 20 | |
ce0594b8 | 21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
fc8918ad | 23 | #include <fcntl.h> |
24 | ||
25 | //! Auxiliary function converting C++ ios open mode flags to C fopen() flags. | |
26 | static int OSD_OpenFile_iosMode2FileFlags (::std::ios_base::openmode theMode) | |
27 | { | |
28 | int aFlags = 0; | |
29 | if (theMode & ::std::ios_base::in) | |
30 | { | |
31 | aFlags |= O_RDONLY; | |
32 | } | |
33 | if (theMode & ::std::ios_base::out) | |
34 | { | |
35 | aFlags |= O_WRONLY; | |
36 | aFlags |= O_CREAT; | |
718d07fe BB |
37 | aFlags |= O_TRUNC; |
38 | ||
fc8918ad | 39 | if (theMode & ::std::ios_base::app) |
40 | { | |
41 | aFlags |= O_APPEND; | |
42 | } | |
fc8918ad | 43 | } |
44 | #ifdef _WIN32 | |
45 | if (theMode & ::std::ios_base::binary) | |
46 | { | |
47 | aFlags |= O_BINARY; | |
48 | } | |
49 | else | |
50 | { | |
51 | aFlags |= O_TEXT; | |
52 | } | |
53 | #endif | |
54 | return aFlags; | |
55 | } | |
56 | ||
57 | // ============================================== | |
58 | // function : OSD_OpenFile | |
59 | // purpose : Opens file | |
60 | // ============================================== | |
61 | int OSD_OpenFileDescriptor (const TCollection_ExtendedString& theName, | |
62 | ::std::ios_base::openmode theMode) | |
63 | { | |
64 | int aFileDesc = -1; | |
65 | const int aFlags = OSD_OpenFile_iosMode2FileFlags (theMode); | |
66 | #if defined(_WIN32) | |
67 | const errno_t anErrCode = _wsopen_s (&aFileDesc, theName.ToWideString(), aFlags, _SH_DENYNO, _S_IREAD | _S_IWRITE); | |
68 | if (anErrCode != 0) | |
69 | { | |
70 | return -1; | |
71 | } | |
72 | #else | |
73 | NCollection_Utf8String aString (theName.ToExtString()); | |
74 | aFileDesc = open (aString.ToCString(), aFlags); | |
75 | #endif | |
76 | return aFileDesc; | |
77 | } | |
ce0594b8 | 78 | |
94708556 | 79 | // ============================================== |
80 | // function : OSD_OpenFile | |
81 | // purpose : Opens file | |
82 | // ============================================== | |
83 | FILE* OSD_OpenFile(const char* theName, | |
84 | const char* theMode) | |
85 | { | |
86 | FILE* aFile = 0; | |
7c65581d | 87 | #if defined(_WIN32) |
94708556 | 88 | // file name is treated as UTF-8 string and converted to UTF-16 one |
89 | const TCollection_ExtendedString aFileNameW (theName, Standard_True); | |
90 | const TCollection_ExtendedString aFileModeW (theMode, Standard_True); | |
fb0b0531 | 91 | aFile = ::_wfopen (aFileNameW.ToWideString(), |
92 | aFileModeW.ToWideString()); | |
94708556 | 93 | #else |
94 | aFile = ::fopen (theName, theMode); | |
95 | #endif | |
96 | return aFile; | |
97 | } | |
98 | ||
99 | // ============================================== | |
100 | // function : OSD_OpenFile | |
101 | // purpose : Opens file | |
102 | // ============================================== | |
103 | FILE* OSD_OpenFile(const TCollection_ExtendedString& theName, | |
104 | const char* theMode) | |
105 | { | |
106 | FILE* aFile = 0; | |
7c65581d | 107 | #if defined(_WIN32) |
94708556 | 108 | const TCollection_ExtendedString aFileModeW (theMode, Standard_True); |
fb0b0531 | 109 | aFile = ::_wfopen (theName.ToWideString(), |
110 | aFileModeW.ToWideString()); | |
94708556 | 111 | #else |
112 | // conversion in UTF-8 for linux | |
fb0b0531 | 113 | NCollection_Utf8String aString (theName.ToExtString()); |
94708556 | 114 | aFile = ::fopen (aString.ToCString(),theMode); |
115 | #endif | |
116 | return aFile; | |
117 | } | |
118 | ||
ce0594b8 | 119 | // ============================================== |
120 | // function : OSD_FileStatCTime | |
121 | // purpose : | |
122 | // ============================================== | |
123 | Standard_Time OSD_FileStatCTime (const char* theName) | |
124 | { | |
125 | Standard_Time aTime = 0; | |
126 | #if defined(_WIN32) | |
127 | // file name is treated as UTF-8 string and converted to UTF-16 one | |
128 | const TCollection_ExtendedString aFileNameW (theName, Standard_True); | |
129 | struct __stat64 aStat; | |
fb0b0531 | 130 | if (_wstat64 (aFileNameW.ToWideString(), &aStat) == 0) |
ce0594b8 | 131 | { |
132 | aTime = (Standard_Time )aStat.st_ctime; | |
133 | } | |
134 | #else | |
135 | struct stat aStat; | |
136 | if (stat (theName, &aStat) == 0) | |
137 | { | |
138 | aTime = (Standard_Time )aStat.st_ctime; | |
139 | } | |
140 | #endif | |
141 | return aTime; | |
142 | } |