0030611: Coding Rules - eliminate GCC compiler warnings -Wcatch-value
[occt.git] / src / PCDM / PCDM_ReadWriter.cxx
1 // Created on: 1997-12-09
2 // Created by: Jean-Louis Frenkel
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <CDM_Document.hxx>
19 #include <PCDM.hxx>
20 #include <PCDM_DOMHeaderParser.hxx>
21 #include <PCDM_ReadWriter.hxx>
22 #include <PCDM_ReadWriter_1.hxx>
23 #include <Standard_ErrorHandler.hxx>
24 #include <Standard_Type.hxx>
25 #include <Storage_BaseDriver.hxx>
26 #include <Storage_Data.hxx>
27 #include <Storage_HeaderData.hxx>
28 #include <Storage_TypeData.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TColStd_HSequenceOfAsciiString.hxx>
32 #include <UTL.hxx>
33
34 IMPLEMENT_STANDARD_RTTIEXT(PCDM_ReadWriter,Standard_Transient)
35
36 #define FILE_FORMAT "FILE_FORMAT: "
37
38 static TCollection_ExtendedString TryXmlDriverType
39                                 (const TCollection_AsciiString& theFileName);
40
41 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream);
42
43 //=======================================================================
44 //function : Open
45 //purpose  : 
46 //=======================================================================
47
48 void PCDM_ReadWriter::Open (Storage_BaseDriver&                 aDriver,
49                             const TCollection_ExtendedString&   aFileName,
50                             const Storage_OpenMode              aMode)
51 {
52   Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
53   if(error != Storage_VSOk) {
54     Standard_SStream aMsg; aMsg << "could not open the file: ";
55     aMsg << aFileName;
56     switch (error) {
57     case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
58     case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
59     default:
60       break;
61     }
62     aMsg << (char)0;
63     throw Standard_Failure(aMsg.str().c_str());
64   }
65 }
66
67 //=======================================================================
68 //function : Reader
69 //purpose  : 
70 //=======================================================================
71
72 //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
73
74 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
75                                            (const TCollection_ExtendedString&)
76 {
77   static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
78   return theReader;
79 }
80
81 //=======================================================================
82 //function : Writer
83 //purpose  : 
84 //=======================================================================
85
86 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
87 {
88   static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
89   return theWriter;
90 }
91  
92 //=======================================================================
93 //function : WriteFileFormat
94 //purpose  : 
95 //=======================================================================
96
97 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
98                                        const Handle(CDM_Document)& aDocument)
99
100   TCollection_AsciiString ligne(FILE_FORMAT);
101   ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
102
103   aData->AddToUserInfo(ligne);
104 }
105
106 //=======================================================================
107 //function : FileFormat
108 //purpose  : 
109 //=======================================================================
110
111 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
112                                 (const TCollection_ExtendedString& aFileName)
113 {
114   TCollection_ExtendedString theFormat;
115   
116   PCDM_BaseDriverPointer theFileDriver;
117
118   // conversion to UTF-8 is done inside
119   TCollection_AsciiString theFileName (aFileName);
120   if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
121     return ::TryXmlDriverType (theFileName);
122
123   static Standard_Boolean theFileIsOpen;
124   theFileIsOpen=Standard_False;
125
126   try {
127     OCC_CATCH_SIGNALS
128     
129     Open(*theFileDriver,aFileName,Storage_VSRead);
130     theFileIsOpen=Standard_True;
131     Storage_HeaderData hd;
132     hd.Read (*theFileDriver);
133     const TColStd_SequenceOfAsciiString &refUserInfo = hd.UserInfo();
134     Standard_Boolean found=Standard_False;
135     for (Standard_Integer i =1; !found && i<=  refUserInfo.Length() ; i++) {
136       if(refUserInfo(i).Search(FILE_FORMAT) != -1) {
137         found=Standard_True;
138         theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
139                                              Standard_True);
140       }
141     }
142     if (!found)
143     {
144       Storage_TypeData td;
145       td.Read (*theFileDriver);
146       theFormat = td.Types()->Value(1);
147     }
148   }
149   catch (Standard_Failure const&) {}
150
151   
152   if(theFileIsOpen)theFileDriver->Close();
153
154   delete theFileDriver;
155
156   return theFormat;
157 }
158
159 //=======================================================================
160 //function : FileFormat
161 //purpose  : 
162 //=======================================================================
163
164 TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
165 {
166   TCollection_ExtendedString aFormat;
167
168   Storage_BaseDriver* aFileDriver = 0L;
169   if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_XmlFile)
170   {
171     return ::TryXmlDriverType (theIStream);
172   }
173   if (!aFileDriver)
174   {
175     // type is not recognized, return empty string
176     return aFormat;
177   }
178
179   aFileDriver->ReadCompleteInfo (theIStream, theData);
180
181   for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
182   {
183     const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
184
185     if(aLine.Search (FILE_FORMAT) != -1)
186     {
187       aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
188     }
189   }
190
191   return aFormat;
192 }
193
194 //=======================================================================
195 //function : ::TryXmlDriverType
196 //purpose  : called from FileFormat()
197 //=======================================================================
198
199 static TCollection_ExtendedString TryXmlDriverType
200                                 (const TCollection_AsciiString& theFileName)
201 {
202   TCollection_ExtendedString theFormat;
203   PCDM_DOMHeaderParser       aParser;
204   const char                 * aDocumentElementName = "document";
205   aParser.SetStartElementName (Standard_CString(aDocumentElementName));
206
207   // Parse the file; if there is no error or an error appears before retrieval
208   // of the DocumentElement, the XML format cannot be defined
209   if (aParser.parse (theFileName.ToCString()))
210   {
211     LDOM_Element anElement = aParser.GetElement();
212     if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
213       theFormat = anElement.getAttribute ("format");
214   }
215   return theFormat;
216 }
217
218 //=======================================================================
219 //function : ::TryXmlDriverType
220 //purpose  : called from FileFormat()
221 //=======================================================================
222
223 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
224 {
225   TCollection_ExtendedString theFormat;
226   PCDM_DOMHeaderParser       aParser;
227   const char                 * aDocumentElementName = "document";
228   aParser.SetStartElementName (Standard_CString(aDocumentElementName));
229
230   if (theIStream.good())
231   {
232     // Parse the file; if there is no error or an error appears before retrieval
233     // of the DocumentElement, the XML format cannot be defined
234     if (aParser.parse (theIStream, Standard_True))
235     {
236       LDOM_Element anElement = aParser.GetElement();
237       if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
238         theFormat = anElement.getAttribute ("format");
239     }
240   }
241
242   return theFormat;
243 }