1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <FWOSDriver_Driver.ixx>
19 #include <TCollection_ExtendedString.hxx>
20 #include <OSD_Path.hxx>
21 #include <OSD_Directory.hxx>
22 #include <OSD_Protection.hxx>
23 #include <OSD_SingleProtection.hxx>
24 #include <OSD_File.hxx>
25 #include <OSD_FileNode.hxx>
27 #include <TCollection_ExtendedString.hxx>
33 //==============================================================================
36 //==============================================================================
37 static void PutSlash (TCollection_ExtendedString& anXSTRING) {
45 //==============================================================================
46 //function : FWOSDriver_Driver
48 //==============================================================================
49 FWOSDriver_Driver::FWOSDriver_Driver() {}
51 //==============================================================================
54 //==============================================================================
55 Standard_Boolean FWOSDriver_Driver::Find(const TCollection_ExtendedString& aFolder,
56 const TCollection_ExtendedString& aName,
57 const TCollection_ExtendedString& aVersion)
60 OSD_Path thePath=UTL::Path(aFolder);
61 OSD_Directory theDirectory(thePath);
62 if(theDirectory.Exists()) {
63 TCollection_ExtendedString f(aFolder);
66 OSD_Path p2 = UTL::Path(f);
68 return theFile.Exists();
70 return Standard_False;
73 //==============================================================================
74 //function : HasReadPermission
76 //==============================================================================
77 Standard_Boolean FWOSDriver_Driver::HasReadPermission(const TCollection_ExtendedString& aFolder,
78 const TCollection_ExtendedString& aName,
79 const TCollection_ExtendedString& aVersion)
81 OSD_SingleProtection theProtection=OSD_File(UTL::Path(Concatenate(aFolder,aName))).Protection().User();
82 switch (theProtection) {
95 return Standard_False;
99 //==============================================================================
100 //function : MetaData
102 //==============================================================================
103 Handle(CDM_MetaData) FWOSDriver_Driver::MetaData(const TCollection_ExtendedString& aFolder,
104 const TCollection_ExtendedString& aName,
105 const TCollection_ExtendedString& aVersion)
107 TCollection_ExtendedString p = Concatenate(aFolder,aName);
108 return CDM_MetaData::LookUp(aFolder,aName,p,p,UTL::IsReadOnly(p));
111 //==============================================================================
112 //function : CreateMetaData
114 //==============================================================================
115 Handle(CDM_MetaData) FWOSDriver_Driver::CreateMetaData(const Handle(CDM_Document)& aDocument,
116 const TCollection_ExtendedString& aFileName)
118 return CDM_MetaData::LookUp(aDocument->RequestedFolder(),aDocument->RequestedName(),
119 Concatenate(aDocument->RequestedFolder(),aDocument->RequestedName()),
120 aFileName,UTL::IsReadOnly(aFileName));
123 //==============================================================================
124 //function : BuildFileName
126 //==============================================================================
127 TCollection_ExtendedString FWOSDriver_Driver::BuildFileName(const Handle(CDM_Document)& aDocument)
130 TCollection_ExtendedString retstr = TCollection_ExtendedString(aDocument->RequestedFolder());
132 retstr += aDocument->RequestedName();
136 //==============================================================================
137 //function : FindFolder
139 //==============================================================================
140 Standard_Boolean FWOSDriver_Driver::FindFolder(const TCollection_ExtendedString& aFolder)
143 OSD_Path thePath=UTL::Path(aFolder);
144 OSD_Directory theDirectory(thePath);
145 return theDirectory.Exists();
148 //==============================================================================
149 //function : Concatenate
151 //==============================================================================
152 TCollection_ExtendedString FWOSDriver_Driver::Concatenate(const TCollection_ExtendedString& aFolder,
153 const TCollection_ExtendedString& aName)
155 TCollection_ExtendedString ff(aFolder);
163 //==============================================================================
164 //function : DefaultFolder
166 //==============================================================================
167 TCollection_ExtendedString FWOSDriver_Driver::DefaultFolder()
169 TCollection_ExtendedString theDefaultFolder;
170 if (theDefaultFolder.Length() == 0) {
173 TCollection_ExtendedString hd=UTL::xgetenv("HOMEDRIVE");
174 if(hd.Length() != NULL) {
176 theDefaultFolder+=UTL::xgetenv("HOMEPATH");
179 theDefaultFolder=UTL::xgetenv("TEMP");
180 if(theDefaultFolder.Length()==0)
181 Standard_Failure::Raise("cannot determine default folder; HOMEDRIVE and TEMP are undefined");
184 TCollection_ExtendedString home=UTL::xgetenv("HOME");
185 if(home.Length() !=0)
186 theDefaultFolder = home;
188 theDefaultFolder= TCollection_ExtendedString("/tmp");
191 return theDefaultFolder;
194 //==============================================================================
195 //function : BuildMetaData
197 //==============================================================================
198 Handle(CDM_MetaData) FWOSDriver_Driver::BuildMetaData(const TCollection_ExtendedString& aFileName)
201 OSD_Path p = UTL::Path(aFileName);
203 TCollection_ExtendedString f = UTL::Trek(p);
204 TCollection_ExtendedString n = UTL::Name(p);
206 n += UTL::Extension(p);
208 return CDM_MetaData::LookUp(f,n,aFileName,aFileName,UTL::IsReadOnly(aFileName));
211 //==============================================================================
214 //==============================================================================
215 TCollection_ExtendedString FWOSDriver_Driver::SetName(const Handle(CDM_Document)& aDocument,
216 const TCollection_ExtendedString& aName)
219 TCollection_ExtendedString xn(aName), n(aName);
222 //windows is not case sensitive
223 Standard_ExtCharacter echar;
224 //make the extension lower case
225 for(int i = 1; i <= xn.Length(); i++)
228 echar = towlower(echar);
229 xn.SetValue(i, echar);
233 TCollection_ExtendedString e (aDocument->FileExtension());
234 TCollection_ExtendedString xe(e);
235 if (e.Length() > 0) {
237 //windows is not case sensitive
238 Standard_ExtCharacter echar;
239 //make the extension lower case
240 for(int i = 1; i <= xe.Length(); i++)
243 echar = towlower(echar);
244 xe.SetValue(i, echar);
249 Standard_Integer ln = xn.Length();
250 Standard_Integer le = xe.Length();
251 Standard_Boolean ExtensionIsAlreadyThere = Standard_False;
253 Standard_Integer ind=xn.SearchFromEnd(xe);
254 ExtensionIsAlreadyThere = ind+le-1==ln;
256 if(!ExtensionIsAlreadyThere) n+=e;