12d6e7a0ff098e6abfe7312c6c504bb29855b6af
[occt.git] / src / OSD / OSD_OpenFile.cxx
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 #ifdef _WIN32
15   #include <windows.h>
16 #endif
17
18 #include <OSD_OpenFile.hxx>
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22
23 // ==============================================
24 // function : OSD_OpenFile
25 // purpose : Opens file
26 // ==============================================
27 FILE* OSD_OpenFile(const char* theName,
28                    const char* theMode)
29 {
30   FILE* aFile = 0;
31 #if defined(_WIN32)
32   // file name is treated as UTF-8 string and converted to UTF-16 one
33   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
34   const TCollection_ExtendedString aFileModeW (theMode, Standard_True);
35   aFile = ::_wfopen (aFileNameW.ToWideString(),
36                      aFileModeW.ToWideString());
37 #else
38   aFile = ::fopen (theName, theMode);
39 #endif
40   return aFile;
41 }
42
43 // ==============================================
44 // function : OSD_OpenFile
45 // purpose : Opens file
46 // ==============================================
47 FILE* OSD_OpenFile(const TCollection_ExtendedString& theName,
48                    const char* theMode)
49 {
50   FILE* aFile = 0;
51 #if defined(_WIN32)
52   const TCollection_ExtendedString aFileModeW (theMode, Standard_True);
53   aFile = ::_wfopen (theName.ToWideString(),
54                      aFileModeW.ToWideString());
55 #else
56   // conversion in UTF-8 for linux
57   NCollection_Utf8String aString (theName.ToExtString());
58   aFile = ::fopen (aString.ToCString(),theMode);
59 #endif
60   return aFile;
61 }
62
63 // ==============================================
64 // function : OSD_FileStatCTime
65 // purpose :
66 // ==============================================
67 Standard_Time OSD_FileStatCTime (const char* theName)
68 {
69   Standard_Time aTime = 0;
70 #if defined(_WIN32)
71   // file name is treated as UTF-8 string and converted to UTF-16 one
72   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
73   struct __stat64 aStat;
74   if (_wstat64 (aFileNameW.ToWideString(), &aStat) == 0)
75   {
76     aTime = (Standard_Time )aStat.st_ctime;
77   }
78 #else
79   struct stat aStat;
80   if (stat (theName, &aStat) == 0)
81   {
82     aTime = (Standard_Time )aStat.st_ctime;
83   }
84 #endif
85   return aTime;
86 }