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