0022972: Eliminate macro definitions that has compiler-provided analogs (WNT and...
[occt.git] / src / CDF / CDF_FWOSDriver.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
42cf5bc1 14
15#include <CDF_FWOSDriver.hxx>
16#include <CDM_Document.hxx>
17#include <CDM_MetaData.hxx>
7fd59977 18#include <OSD_Directory.hxx>
7fd59977 19#include <OSD_File.hxx>
20#include <OSD_FileNode.hxx>
42cf5bc1 21#include <OSD_Path.hxx>
22#include <OSD_Protection.hxx>
23#include <OSD_SingleProtection.hxx>
24#include <Standard_Type.hxx>
7fd59977 25#include <TCollection_ExtendedString.hxx>
42cf5bc1 26#include <UTL.hxx>
27
57c28b61 28#ifdef _MSC_VER
7fd59977 29#include <tchar.h>
57c28b61 30#endif // _MSC_VER
7fd59977 31
32
858aac03 33//==============================================================================
34//function : PutSlash
35//purpose :
36//==============================================================================
7fd59977 37static void PutSlash (TCollection_ExtendedString& anXSTRING) {
57c28b61 38#ifdef _WIN32
7fd59977 39 anXSTRING+="\\";
40#else
41 anXSTRING+="/";
57c28b61 42#endif // _WIN32
7fd59977 43}
44
858aac03 45//==============================================================================
b6c0b841 46//function : CDF_FWOSDriver
858aac03 47//purpose :
48//==============================================================================
b6c0b841 49CDF_FWOSDriver::CDF_FWOSDriver() {}
7fd59977 50
858aac03 51//==============================================================================
52//function : Find
53//purpose :
54//==============================================================================
b6c0b841 55Standard_Boolean CDF_FWOSDriver::Find(const TCollection_ExtendedString& aFolder,
858aac03 56 const TCollection_ExtendedString& aName,
35e08fe8 57 const TCollection_ExtendedString& /*aVersion*/)
858aac03 58{
7fd59977 59
60 OSD_Path thePath=UTL::Path(aFolder);
61 OSD_Directory theDirectory(thePath);
62 if(theDirectory.Exists()) {
63 TCollection_ExtendedString f(aFolder);
64 PutSlash(f);
65 f+=aName;
66 OSD_Path p2 = UTL::Path(f);
67 OSD_File theFile(p2);
68 return theFile.Exists();
69 }
70 return Standard_False;
71}
72
858aac03 73//==============================================================================
74//function : HasReadPermission
75//purpose :
76//==============================================================================
b6c0b841 77Standard_Boolean CDF_FWOSDriver::HasReadPermission(const TCollection_ExtendedString& aFolder,
858aac03 78 const TCollection_ExtendedString& aName,
35e08fe8 79 const TCollection_ExtendedString& /*aVersion*/)
858aac03 80{
7fd59977 81 OSD_SingleProtection theProtection=OSD_File(UTL::Path(Concatenate(aFolder,aName))).Protection().User();
82 switch (theProtection) {
83 case OSD_None:
84 case OSD_R:
85 case OSD_RW:
86 case OSD_RX:
87 case OSD_WX:
88 case OSD_RWX:
89 case OSD_RD:
90 case OSD_RWD:
91 case OSD_RXD:
92 case OSD_RWXD:
93 return Standard_True;
94 default:
95 return Standard_False;
96 }
7fd59977 97}
98
858aac03 99//==============================================================================
100//function : MetaData
101//purpose :
102//==============================================================================
b6c0b841 103Handle(CDM_MetaData) CDF_FWOSDriver::MetaData(const TCollection_ExtendedString& aFolder,
858aac03 104 const TCollection_ExtendedString& aName,
35e08fe8 105 const TCollection_ExtendedString& /*aVersion*/)
858aac03 106{
7fd59977 107 TCollection_ExtendedString p = Concatenate(aFolder,aName);
108 return CDM_MetaData::LookUp(aFolder,aName,p,p,UTL::IsReadOnly(p));
109}
110
858aac03 111//==============================================================================
112//function : CreateMetaData
113//purpose :
114//==============================================================================
b6c0b841 115Handle(CDM_MetaData) CDF_FWOSDriver::CreateMetaData(const Handle(CDM_Document)& aDocument,
858aac03 116 const TCollection_ExtendedString& aFileName)
117{
118 return CDM_MetaData::LookUp(aDocument->RequestedFolder(),aDocument->RequestedName(),
119 Concatenate(aDocument->RequestedFolder(),aDocument->RequestedName()),
120 aFileName,UTL::IsReadOnly(aFileName));
7fd59977 121}
858aac03 122
123//==============================================================================
124//function : BuildFileName
125//purpose :
126//==============================================================================
b6c0b841 127TCollection_ExtendedString CDF_FWOSDriver::BuildFileName(const Handle(CDM_Document)& aDocument)
858aac03 128{
7fd59977 129
130 TCollection_ExtendedString retstr = TCollection_ExtendedString(aDocument->RequestedFolder());
131 PutSlash(retstr);
132 retstr += aDocument->RequestedName();
133 return retstr;
134}
858aac03 135
136//==============================================================================
137//function : FindFolder
138//purpose :
139//==============================================================================
b6c0b841 140Standard_Boolean CDF_FWOSDriver::FindFolder(const TCollection_ExtendedString& aFolder)
858aac03 141{
7fd59977 142
143 OSD_Path thePath=UTL::Path(aFolder);
144 OSD_Directory theDirectory(thePath);
145 return theDirectory.Exists();
146}
147
858aac03 148//==============================================================================
149//function : Concatenate
150//purpose :
151//==============================================================================
b6c0b841 152TCollection_ExtendedString CDF_FWOSDriver::Concatenate(const TCollection_ExtendedString& aFolder,
858aac03 153 const TCollection_ExtendedString& aName)
154{
7fd59977 155 TCollection_ExtendedString ff(aFolder);
156 ff = "";
157 ff += aFolder;
158 PutSlash(ff);
159 ff+=aName;
160 return ff;
161}
162
858aac03 163//==============================================================================
164//function : DefaultFolder
165//purpose :
166//==============================================================================
b6c0b841 167TCollection_ExtendedString CDF_FWOSDriver::DefaultFolder()
858aac03 168{
7fd59977 169 TCollection_ExtendedString theDefaultFolder;
170 if (theDefaultFolder.Length() == 0) {
171
57c28b61 172#ifdef _WIN32
7fd59977 173 TCollection_ExtendedString hd=UTL::xgetenv("HOMEDRIVE");
174 if(hd.Length() != NULL) {
175 theDefaultFolder=hd;
176 theDefaultFolder+=UTL::xgetenv("HOMEPATH");
177 }
178 else {
179 theDefaultFolder=UTL::xgetenv("TEMP");
858aac03 180 if(theDefaultFolder.Length()==0)
181 Standard_Failure::Raise("cannot determine default folder; HOMEDRIVE and TEMP are undefined");
7fd59977 182 }
183#else
184 TCollection_ExtendedString home=UTL::xgetenv("HOME");
185 if(home.Length() !=0)
186 theDefaultFolder = home;
187 else
188 theDefaultFolder= TCollection_ExtendedString("/tmp");
189#endif
190 }
191 return theDefaultFolder;
192}
193
858aac03 194//==============================================================================
195//function : BuildMetaData
196//purpose :
197//==============================================================================
b6c0b841 198Handle(CDM_MetaData) CDF_FWOSDriver::BuildMetaData(const TCollection_ExtendedString& aFileName)
858aac03 199{
7fd59977 200
201 OSD_Path p = UTL::Path(aFileName);
202
203 TCollection_ExtendedString f = UTL::Trek(p);
204 TCollection_ExtendedString n = UTL::Name(p);
205 n +=".";
206 n += UTL::Extension(p);
207
208 return CDM_MetaData::LookUp(f,n,aFileName,aFileName,UTL::IsReadOnly(aFileName));
209}
210
858aac03 211//==============================================================================
212//function : SetName
213//purpose :
214//==============================================================================
b6c0b841 215TCollection_ExtendedString CDF_FWOSDriver::SetName(const Handle(CDM_Document)& aDocument,
858aac03 216 const TCollection_ExtendedString& aName)
217{
7fd59977 218
858aac03 219 TCollection_ExtendedString xn(aName), n(aName);
220
57c28b61 221#ifdef _WIN32
858aac03 222 //windows is not case sensitive
858aac03 223 //make the extension lower case
224 for(int i = 1; i <= xn.Length(); i++)
225 {
51740958 226 Standard_ExtCharacter echar = xn.Value(i);
858aac03 227 echar = towlower(echar);
228 xn.SetValue(i, echar);
229 }
230#endif
7fd59977 231
232 TCollection_ExtendedString e (aDocument->FileExtension());
858aac03 233 TCollection_ExtendedString xe(e);
7fd59977 234 if (e.Length() > 0) {
57c28b61 235#ifdef _WIN32
858aac03 236 //windows is not case sensitive
858aac03 237 //make the extension lower case
238 for(int i = 1; i <= xe.Length(); i++)
239 {
51740958 240 Standard_ExtCharacter echar = xe.Value(i);
858aac03 241 echar = towlower(echar);
242 xe.SetValue(i, echar);
243 }
244#endif
245 xe.Insert(1, '.');
7fd59977 246 e.Insert(1, '.');
858aac03 247 Standard_Integer ln = xn.Length();
248 Standard_Integer le = xe.Length();
7fd59977 249 Standard_Boolean ExtensionIsAlreadyThere = Standard_False;
250 if(ln>=le) {
858aac03 251 Standard_Integer ind=xn.SearchFromEnd(xe);
7fd59977 252 ExtensionIsAlreadyThere = ind+le-1==ln;
253 }
254 if(!ExtensionIsAlreadyThere) n+=e;
255 }
256 return n;
257}