Integration of OCCT 6.5.0 from SVN
[occt.git] / src / PCDM / PCDM_ReadWriter_1.cxx
CommitLineData
7fd59977 1// File: PCDM_ReadWriter_1.cxx
2// Created: Tue Dec 9 08:22:39 1997
3// Author: Jean-Louis Frenkel
4// <rmi@frilox.paris1.matra-dtv.fr>
5
6
7#include <PCDM_ReadWriter_1.ixx>
8#include <UTL.hxx>
9#include <Storage_Data.hxx>
10#include <TCollection_AsciiString.hxx>
11#include <TCollection_ExtendedString.hxx>
12#include <CDM_Document.hxx>
13#include <CDM_ReferenceIterator.hxx>
14#include <CDM_MetaData.hxx>
15#include <TColStd_SequenceOfExtendedString.hxx>
16#include <Storage_Schema.hxx>
17#include <Storage_HeaderData.hxx>
18#include <TColStd_SequenceOfAsciiString.hxx>
19#include <PCDM_Reference.hxx>
20#include <Standard_ErrorHandler.hxx>
21#include <OSD_Path.hxx>
22#include <PCDM_BaseDriverPointer.hxx>
23#include <PCDM.hxx>
24#include <PCDM_TypeOfFileDriver.hxx>
25
26#define START_REF "START_REF"
27#define END_REF "END_REF"
28#define START_EXT "START_EXT"
29#define END_EXT "END_EXT"
30#define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
31#define REFERENCE_COUNTER "REFERENCE_COUNTER: "
32
33//=======================================================================
34//function : PCDM_ReadWriter_1
35//purpose :
36//=======================================================================
37
38PCDM_ReadWriter_1::PCDM_ReadWriter_1() {}
39static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
40
41 Standard_Integer i, j, len ;
42
43 len = aString.Length() ;
44#ifdef WNT
45 // Case of network path, such as \\MACHINE\dir
46 for (i = j = 2 ; j <= len ; i++,j++) {
47#else
48 for (i = j = 1 ; j <= len ; i++,j++) {
49#endif
50 Standard_Character c = aString.Value(j) ;
51 aString.SetValue(i,c) ;
52 if (c == '/')
53 while(j < len && aString.Value(j+1) == '/') j++ ;
54 }
55 len = i-1 ;
56 if (aString.Value(len) == '/') len-- ;
57 aString.Trunc(len) ;
58 return len ;
59}
60static TCollection_AsciiString AbsolutePath(
61 const TCollection_AsciiString& aDirPath,
62 const TCollection_AsciiString& aRelFilePath)
63{
64 TCollection_AsciiString EmptyString = "" ;
65#ifdef WNT
66 if (aRelFilePath.Search(":") == 2 ||
67 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
68#else
69 if(aRelFilePath.Search("/") == 1)
70#endif
71 return aRelFilePath ;
72
73 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
74 Standard_Integer i,len ;
75
76#ifdef WNT
77 if(DirPath.Search(":") != 2 &&
78 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
79#else
80 if (DirPath.Search("/") != 1 )
81#endif
82 return EmptyString ;
83
84#ifdef WNT
85 DirPath.ChangeAll('\\','/') ;
86 RelFilePath.ChangeAll('\\','/') ;
87#endif
88
89 RemoveExtraSeparator(DirPath) ;
90 len = RemoveExtraSeparator(RelFilePath) ;
91
92 while (RelFilePath.Search("../") == 1) {
93 if (len == 3)
94 return EmptyString ;
95 RelFilePath = RelFilePath.SubString(4,len) ;
96 len -= 3 ;
97 if (DirPath.IsEmpty())
98 return EmptyString ;
99 i = DirPath.SearchFromEnd("/") ;
100 if (i < 0)
101 return EmptyString ;
102 DirPath.Trunc(i-1) ;
103 }
104 TCollection_AsciiString retx;
105 retx= DirPath;
106 retx+= "/";
107 retx+=RelFilePath ;
108 return retx;
109}
110
111static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
112 TCollection_AsciiString theCFile=UTL::CString(aFileName);
113 TCollection_AsciiString theDirectory;
114 Standard_Integer i=theCFile.SearchFromEnd("/");
115#ifdef WNT
116// if(i==-1) i=theCFile.SearchFromEnd("\\");
117 if(theCFile.SearchFromEnd("\\") > i)
118 i=theCFile.SearchFromEnd("\\");
119#endif
120 if(i!=-1) theDirectory=theCFile.SubString(1,i);
121 return theDirectory;
122}
123//=======================================================================
124//function : Version
125//purpose :
126//=======================================================================
127
128TCollection_AsciiString PCDM_ReadWriter_1::Version() const {
129 return "PCDM_ReadWriter_1";
130}
131//=======================================================================
132//function : WriteReferenceCounter
133//purpose :
134//=======================================================================
135
136void PCDM_ReadWriter_1::WriteReferenceCounter(const Handle(Storage_Data)& aData, const Handle(CDM_Document)& aDocument) const {
137 TCollection_AsciiString ligne(REFERENCE_COUNTER);
138 ligne+=aDocument->ReferenceCounter();
139 aData->AddToUserInfo(ligne);
140}
141//=======================================================================
142//function : WriteReferences
143//purpose :
144//=======================================================================
145
146void PCDM_ReadWriter_1::WriteReferences(const Handle(Storage_Data)& aData, const Handle(CDM_Document)& aDocument,const TCollection_ExtendedString& theReferencerFileName) const {
147
148 Standard_Integer theNumber = aDocument->ToReferencesNumber();
149 if(theNumber > 0) {
150
151 aData->AddToUserInfo(START_REF);
152
153 CDM_ReferenceIterator it(aDocument);
154
155 TCollection_ExtendedString ligne;
156
157 TCollection_AsciiString theAbsoluteDirectory=GetDirFromFile(theReferencerFileName);
158
159 for (;it.More();it.Next()) {
160 ligne = TCollection_ExtendedString(it.ReferenceIdentifier());
161 ligne += " ";
162 ligne += TCollection_ExtendedString(it.Document()->Modifications());
163 ligne += " ";
164
165 TCollection_AsciiString thePath=UTL::CString(it.Document()->MetaData()->FileName());
166 TCollection_AsciiString theRelativePath;
167 if(!theAbsoluteDirectory.IsEmpty()) {
168 theRelativePath=OSD_Path::RelativePath(theAbsoluteDirectory,thePath);
169 if(!theRelativePath.IsEmpty()) thePath=theRelativePath;
170 }
171 ligne += UTL::ExtendedString(thePath);
172 UTL::AddToUserInfo(aData,ligne);
173 }
174 aData->AddToUserInfo(END_REF);
175 }
176}
177
178//=======================================================================
179//function : WriteExtensions
180//purpose :
181//=======================================================================
182
183void PCDM_ReadWriter_1::WriteExtensions(const Handle(Storage_Data)& aData, const Handle(CDM_Document)& aDocument) const {
184
185 TColStd_SequenceOfExtendedString theExtensions;
186 aDocument->Extensions(theExtensions);
187 Standard_Integer theNumber = theExtensions.Length();
188 if(theNumber > 0) {
189
190 aData->AddToUserInfo(START_EXT);
191 for (Standard_Integer i=1; i<=theNumber; i++) {
192 UTL::AddToUserInfo(aData,theExtensions(i));
193 }
194 aData->AddToUserInfo(END_EXT);
195 }
196}
197//=======================================================================
198//function : WriteVersion
199//purpose :
200//=======================================================================
201
202void PCDM_ReadWriter_1::WriteVersion(const Handle(Storage_Data)& aData, const Handle(CDM_Document)& aDocument) const {
203 TCollection_AsciiString ligne(MODIFICATION_COUNTER);
204 ligne+=aDocument->Modifications();
205 aData->AddToUserInfo(ligne);
206}
207//=======================================================================
208//function : ReadReferenceCounter
209//purpose :
210//=======================================================================
211
212Standard_Integer PCDM_ReadWriter_1::ReadReferenceCounter(const TCollection_ExtendedString& aFileName, const Handle(CDM_MessageDriver)& theMsgDriver) const {
213
214 static Standard_Integer theReferencesCounter ;
215 theReferencesCounter=0;
216 static Standard_Integer i ;
217
218 PCDM_BaseDriverPointer theFileDriver;
219 if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(aFileName)), theFileDriver) == PCDM_TOFD_Unknown) return theReferencesCounter;
220
221 static Standard_Boolean theFileIsOpen ;
222 theFileIsOpen=Standard_False;
223
224 try {
225 OCC_CATCH_SIGNALS
226 PCDM_ReadWriter::Open(*theFileDriver,aFileName,Storage_VSRead);
227 theFileIsOpen=Standard_True;
228
229 Handle(Storage_Schema) s = new Storage_Schema;
230 Handle(Storage_HeaderData) hd = s->ReadHeaderSection(*theFileDriver);
231 const TColStd_SequenceOfAsciiString &refUserInfo = hd->UserInfo();
232
233 for ( i =1; i<= refUserInfo.Length() ; i++) {
234 if(refUserInfo(i).Search(REFERENCE_COUNTER) != -1) {
235 try { OCC_CATCH_SIGNALS theReferencesCounter=refUserInfo(i).Token(" ",2).IntegerValue();}
236 catch (Standard_Failure) {
237// cout << "warning: could not read the reference counter in " << aFileName << endl;
238 TCollection_ExtendedString aMsg("Warning: ");
239 aMsg = aMsg.Cat("could not read the reference counter in ").Cat(aFileName).Cat("\0");
240 if(!theMsgDriver.IsNull())
241 theMsgDriver->Write(aMsg.ToExtString());
242 }
243 }
244 }
245
246 }
247 catch (Standard_Failure) {}
248
249 if(theFileIsOpen) theFileDriver->Close();
250
251 delete theFileDriver;
252 return theReferencesCounter;
253}
254
255//=======================================================================
256//function : ReadReferences
257//purpose :
258//=======================================================================
259
260void PCDM_ReadWriter_1::ReadReferences(const TCollection_ExtendedString& aFileName, PCDM_SequenceOfReference& theReferences, const Handle(CDM_MessageDriver)& theMsgDriver) const {
261
262 TColStd_SequenceOfExtendedString ReadReferences;
263
264 ReadUserInfo(aFileName,START_REF,END_REF,ReadReferences, theMsgDriver);
265
266 Standard_Integer theReferenceIdentifier;
267 TCollection_ExtendedString theFileName;
268 Standard_Integer theDocumentVersion;
269
270 TCollection_AsciiString theAbsoluteDirectory=GetDirFromFile(aFileName);
271
272 for (Standard_Integer i=1; i<=ReadReferences.Length(); i++) {
273 Standard_Integer pos=ReadReferences(i).Search(" ");
274 if(pos != -1) {
275 TCollection_ExtendedString theRest=ReadReferences(i).Split(pos);
276 theReferenceIdentifier=UTL::IntegerValue(ReadReferences(i));
277
278 Standard_Integer pos2=theRest.Search(" ");
279
280 theFileName=theRest.Split(pos2);
281 theDocumentVersion=UTL::IntegerValue(theRest);
282
283 TCollection_AsciiString thePath=UTL::CString(theFileName);
284 TCollection_AsciiString theAbsolutePath;
285 if(!theAbsoluteDirectory.IsEmpty()) {
286 theAbsolutePath=AbsolutePath(theAbsoluteDirectory,thePath);
287 if(!theAbsolutePath.IsEmpty()) thePath=theAbsolutePath;
288 }
289 if(!theMsgDriver.IsNull()) {
290// cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
291 TCollection_ExtendedString aMsg("Warning: ");
292 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(theReferenceIdentifier).Cat("; File:").Cat(thePath).Cat(", version:").Cat(theDocumentVersion).Cat("\0");
293 theMsgDriver->Write(aMsg.ToExtString());
294 }
295 theReferences.Append(PCDM_Reference (theReferenceIdentifier,UTL::ExtendedString(thePath),theDocumentVersion));
296
297 }
298 }
299
300}
301
302//=======================================================================
303//function : ReadExtensions
304//purpose :
305//=======================================================================
306
307void PCDM_ReadWriter_1::ReadExtensions(const TCollection_ExtendedString& aFileName, TColStd_SequenceOfExtendedString& theExtensions, const Handle(CDM_MessageDriver)& theMsgDriver) const {
308
309 ReadUserInfo(aFileName,START_EXT,END_EXT,theExtensions, theMsgDriver);
310}
311
312
313//=======================================================================
314//function : ReadUserInfo
315//purpose :
316//=======================================================================
317
318void PCDM_ReadWriter_1::ReadUserInfo(const TCollection_ExtendedString& aFileName,
319 const TCollection_AsciiString& Start,
320 const TCollection_AsciiString& End,
321 TColStd_SequenceOfExtendedString& theUserInfo,
322 const Handle(CDM_MessageDriver)&) {
323
324 static Standard_Integer i ;
325 PCDM_BaseDriverPointer theFileDriver;
326 if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(aFileName)), theFileDriver) == PCDM_TOFD_Unknown) return;
327
328 PCDM_ReadWriter::Open(*theFileDriver,aFileName,Storage_VSRead);
329 Handle(Storage_Schema) s = new Storage_Schema;
330 Handle(Storage_HeaderData) hd = s->ReadHeaderSection(*theFileDriver);
331 const TColStd_SequenceOfAsciiString &refUserInfo = hd->UserInfo();
332
333 Standard_Integer debut=0,fin=0;
334
335 for ( i =1; i<= refUserInfo.Length() ; i++) {
336 TCollection_ExtendedString theLine=refUserInfo(i);
337 if(refUserInfo(i)== Start) debut=i;
338 if(refUserInfo(i)== End) fin=i;
339 }
340 if(debut != 0) {
341 for (i=debut+1 ; i<fin; i++) {
342 theUserInfo.Append(UTL::ExtendedString(refUserInfo(i)));
343 }
344 }
345 theFileDriver->Close();
346 delete theFileDriver;
347}
348
349//=======================================================================
350//function : ReadDocumentVersion
351//purpose :
352//=======================================================================
353
354Standard_Integer PCDM_ReadWriter_1::ReadDocumentVersion(const TCollection_ExtendedString& aFileName, const Handle(CDM_MessageDriver)& theMsgDriver) const {
355
356 static Standard_Integer theVersion ;
357 theVersion=-1;
358
359 PCDM_BaseDriverPointer theFileDriver;
360 if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(aFileName)), theFileDriver) == PCDM_TOFD_Unknown) return theVersion;
361
362 static Standard_Boolean theFileIsOpen ;
363 theFileIsOpen =Standard_False;
364
365 try {
366 OCC_CATCH_SIGNALS
367 PCDM_ReadWriter::Open(*theFileDriver,aFileName,Storage_VSRead);
368 theFileIsOpen=Standard_True;
369 Handle(Storage_Schema) s = new Storage_Schema;
370 Handle(Storage_HeaderData) hd = s->ReadHeaderSection(*theFileDriver);
371 const TColStd_SequenceOfAsciiString &refUserInfo = hd->UserInfo();
372
373 static Standard_Integer i ;
374 for ( i =1; i<= refUserInfo.Length() ; i++) {
375 if(refUserInfo(i).Search(MODIFICATION_COUNTER) != -1) {
376 try { OCC_CATCH_SIGNALS theVersion=refUserInfo(i).Token(" ",2).IntegerValue();}
377 catch (Standard_Failure) {
378// cout << "warning: could not read the version in " << aFileName << endl;
379 TCollection_ExtendedString aMsg("Warning: ");
380 aMsg = aMsg.Cat("could not read the version in ").Cat(aFileName).Cat("\0");
381 if(!theMsgDriver.IsNull())
382 theMsgDriver->Write(aMsg.ToExtString());
383 }
384
385 }
386 }
387 }
388
389 catch (Standard_Failure) {}
390
391 if(theFileIsOpen) theFileDriver->Close();
392 delete theFileDriver;
393 return theVersion;
394}