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