0026377: Passing Handle objects as arguments to functions as non-const reference...
[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 #include <OSD_OpenFile.hxx>
15 #include <TCollection_ExtendedString.hxx>
16 #include <NCollection_UtfString.hxx>
17
18 // ==============================================
19 // function : OSD_OpenFile
20 // purpose : Opens file
21 // ==============================================
22 FILE* OSD_OpenFile(const char* theName,
23                    const char* theMode)
24 {
25   FILE* aFile = 0;
26 #ifdef _WIN32
27   // file name is treated as UTF-8 string and converted to UTF-16 one
28   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
29   const TCollection_ExtendedString aFileModeW (theMode, Standard_True);
30   aFile = ::_wfopen ((const wchar_t* )aFileNameW.ToExtString(),
31                      (const wchar_t* )aFileModeW.ToExtString());
32 #else
33   aFile = ::fopen (theName, theMode);
34 #endif
35   return aFile;
36 }
37
38 // ==============================================
39 // function : OSD_OpenFile
40 // purpose : Opens file
41 // ==============================================
42 FILE* OSD_OpenFile(const TCollection_ExtendedString& theName,
43                    const char* theMode)
44 {
45   FILE* aFile = 0;
46 #ifdef _WIN32
47   const TCollection_ExtendedString aFileModeW (theMode, Standard_True);
48   aFile = ::_wfopen ((const wchar_t* )theName.ToExtString(),
49                      (const wchar_t* )aFileModeW.ToExtString());
50 #else
51   // conversion in UTF-8 for linux
52   NCollection_Utf8String aString((const Standard_Utf16Char*)theName.ToExtString());
53   aFile = ::fopen (aString.ToCString(),theMode);
54 #endif
55   return aFile;
56 }
57
58 // ==============================================
59 // function : OSD_OpenFileBuf
60 // purpose : Opens file buffer
61 // ==============================================
62 void OSD_OpenFileBuf(std::filebuf& theBuff,
63                      const char* theName,
64                      const std::ios_base::openmode theMode)
65 {
66 #ifdef _WIN32
67   // file name is treated as UTF-8 string and converted to UTF-16 one
68   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
69   theBuff.open ((const wchar_t* )aFileNameW.ToExtString(), theMode);
70 #else
71   theBuff.open (theName, theMode);
72 #endif
73 }
74
75 // ==============================================
76 // function : OSD_OpenFileBuf
77 // purpose : Opens file buffer
78 // ==============================================
79 void OSD_OpenFileBuf(std::filebuf& theBuff,
80                      const TCollection_ExtendedString& theName,
81                      const std::ios_base::openmode theMode)
82 {
83 #ifdef _WIN32
84   theBuff.open ((const wchar_t* )theName.ToExtString(), theMode);
85 #else
86   // conversion in UTF-8 for linux
87   NCollection_Utf8String aString((const Standard_Utf16Char*)theName.ToExtString());
88   theBuff.open (aString.ToCString(),theMode);
89 #endif
90 }
91
92 // ==============================================
93 // function : OSD_OpenStream
94 // purpose : Opens file stream
95 // ==============================================
96 void OSD_OpenStream(std::ofstream& theStream,
97                     const char* theName,
98                     const std::ios_base::openmode theMode)
99 {
100 #ifdef _WIN32
101   // file name is treated as UTF-8 string and converted to UTF-16 one
102   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
103   theStream.open ((const wchar_t* )aFileNameW.ToExtString(), theMode);
104 #else
105   theStream.open (theName, theMode);
106 #endif
107 }
108
109 // ==============================================
110 // function : OSD_OpenStream
111 // purpose : Opens file stream
112 // ==============================================
113 void OSD_OpenStream(std::ofstream& theStream,
114                     const TCollection_ExtendedString& theName,
115                     const std::ios_base::openmode theMode)
116 {
117 #ifdef _WIN32
118   theStream.open ((const wchar_t* )theName.ToExtString(), theMode);
119 #else
120   // conversion in UTF-8 for linux
121   NCollection_Utf8String aString((const Standard_Utf16Char*)theName.ToExtString());
122   theStream.open (aString.ToCString(),theMode);
123 #endif
124 }
125
126 // ==============================================
127 // function : OSD_OpenStream
128 // purpose  : Opens file stream
129 // ==============================================
130 void OSD_OpenStream (std::ifstream&                theStream,
131                      const char*                   theName,
132                      const std::ios_base::openmode theMode)
133 {
134 #ifdef _WIN32
135   // file name is treated as UTF-8 string and converted to UTF-16 one
136   const TCollection_ExtendedString aFileNameW (theName, Standard_True);
137   theStream.open ((const wchar_t*)aFileNameW.ToExtString(), theMode);
138 #else
139   theStream.open (theName, theMode);
140 #endif
141 }
142
143 // ==============================================
144 // function : OSD_OpenStream
145 // purpose  : Opens file stream
146 // ==============================================
147 void OSD_OpenStream (std::ifstream&                    theStream,
148                      const TCollection_ExtendedString& theName,
149                      const std::ios_base::openmode     theMode)
150 {
151 #ifdef _WIN32
152   theStream.open ((const wchar_t*)theName.ToExtString(), theMode);
153 #else
154   // conversion in UTF-8 for linux
155   NCollection_Utf8String aString ((const Standard_Utf16Char*)theName.ToExtString());
156   theStream.open (aString.ToCString(), theMode);
157 #endif
158 }