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