0031972: Application Framework, FSD_CmpFile - exception on reading file in old persis...
[occt.git] / src / FSD / FSD_CmpFile.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2017 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <FSD_CmpFile.hxx>
16 #include <OSD_OpenFile.hxx>
17 #include <Storage_StreamFormatError.hxx>
18 #include <Storage_StreamTypeMismatchError.hxx>
19 #include <Storage_StreamWriteError.hxx>
20
21 const Standard_CString MAGICNUMBER = "CMPFILE";
22
23 IMPLEMENT_STANDARD_RTTIEXT(FSD_CmpFile, FSD_File)
24
25 //=======================================================================
26 //function : FSD_CmpFile
27 //purpose  : 
28 //=======================================================================
29
30 FSD_CmpFile::FSD_CmpFile()
31 {}
32
33 //=======================================================================
34 //function : IsGoodFileType
35 //purpose  : INFO SECTION
36 //           write
37 //=======================================================================
38
39 Storage_Error FSD_CmpFile::IsGoodFileType(const TCollection_AsciiString& aName)
40 {
41   FSD_CmpFile      f;
42   Storage_Error s;
43
44   s = f.Open(aName, Storage_VSRead);
45
46   if (s == Storage_VSOk) {
47     TCollection_AsciiString l;
48     Standard_Size len = strlen(FSD_CmpFile::MagicNumber());
49
50     f.ReadChar(l, len);
51
52     f.Close();
53
54     if (strncmp(FSD_CmpFile::MagicNumber(), l.ToCString(), len) != 0) {
55       s = Storage_VSFormatError;
56     }
57   }
58
59   return s;
60 }
61
62 //=======================================================================
63 //function : Open
64 //purpose  : 
65 //=======================================================================
66 Storage_Error FSD_CmpFile::Open(const TCollection_AsciiString& aName, const Storage_OpenMode aMode)
67 {
68   Storage_Error result = Storage_VSOk;
69   SetName(aName);
70
71   if (OpenMode() == Storage_VSNone) {
72     std::ios_base::openmode anOpenMode = std::ios_base::openmode(0);
73     switch (aMode)
74     {
75     case Storage_VSNone:
76     {
77       break;
78     }
79     case Storage_VSRead:
80     {
81       // std::ios::nocreate is not portable
82 #if !defined(IRIX) && !defined(DECOSF1)
83       anOpenMode = std::ios::in | std::ios::binary;
84 #else
85       anOpenMode = std::ios::in;
86 #endif
87       break;
88     }
89     case Storage_VSWrite:
90     {
91 #if !defined(IRIX) && !defined(DECOSF1)
92       anOpenMode = std::ios::out | std::ios::binary;
93 #else
94       anOpenMode = std::ios::out;
95 #endif
96       break;
97     }
98     case Storage_VSReadWrite:
99     {
100 #if !defined(IRIX) && !defined(DECOSF1)
101       anOpenMode = std::ios::in | std::ios::out | std::ios::binary;
102 #else
103       anOpenMode = std::ios::in | std::ios::out;
104 #endif
105       break;
106     }
107     }
108     if (anOpenMode != 0)
109     {
110       OSD_OpenStream(myStream, aName, anOpenMode);
111     }
112     if (myStream.fail()) {
113       result = Storage_VSOpenError;
114     }
115     else {
116       myStream.precision(17);
117       myStream.imbue(std::locale::classic()); // use always C locale
118       SetOpenMode(aMode);
119     }
120     }
121   else {
122     result = Storage_VSAlreadyOpen;
123   }
124   return result;
125     }
126
127 //=======================================================================
128 //function : MagicNumber
129 //purpose  : ------------------ PROTECTED
130 //=======================================================================
131
132 Standard_CString FSD_CmpFile::MagicNumber()
133 {
134   return MAGICNUMBER;
135 }
136
137 //=======================================================================
138 //function : ReadLine
139 //purpose  : read from the current position to the end of line.
140 //=======================================================================
141
142 void FSD_CmpFile::ReadLine(TCollection_AsciiString& buffer)
143 {
144   buffer.Clear();
145   TCollection_AsciiString aBuf('\0');
146   FSD_File::ReadLine(aBuf);
147   for (Standard_Integer lv = aBuf.Length(); lv >= 1 && (aBuf.Value(lv) == '\r' || (aBuf.Value(lv) == '\n')); lv--)
148   {
149     aBuf.Trunc (lv - 1);
150   }
151   buffer = aBuf;
152 }
153
154 //=======================================================================
155 //function : WriteExtendedLine
156 //purpose  : write from the current position to the end of line.
157 //=======================================================================
158
159 void FSD_CmpFile::WriteExtendedLine(const TCollection_ExtendedString& buffer)
160 {
161 #if 0
162   Standard_ExtString extBuffer;
163   Standard_Integer   i, c, d;
164
165   extBuffer = buffer.ToExtString();
166
167   for (i = 0; i < buffer.Length(); i++) {
168     c = (extBuffer[i] & 0x0000FF00) >> 8;
169     d = extBuffer[i] & 0x000000FF;
170
171     myStream << (char)c << (char)d;
172   }
173
174   myStream << (char)0 << "\n";
175 #endif
176   Standard_ExtString extBuffer;
177   Standard_Integer   i;
178
179   extBuffer = buffer.ToExtString();
180   PutInteger(buffer.Length());
181   for (i = 0; i < buffer.Length(); i++) {
182     PutExtCharacter(extBuffer[i]);
183   }
184
185   myStream << "\n";
186 }
187
188 //=======================================================================
189 //function : ReadExtendedLine
190 //purpose  : 
191 //=======================================================================
192
193 void FSD_CmpFile::ReadExtendedLine(TCollection_ExtendedString& buffer)
194 {
195   Standard_ExtCharacter c;
196   Standard_Integer i;
197
198   GetInteger(i);
199
200   for (i = 0; i < buffer.Length(); i++) {
201     GetExtCharacter(c);
202     buffer += c;
203   }
204
205   FlushEndOfLine();
206 }
207
208 //=======================================================================
209 //function : ReadString
210 //purpose  : read from the first none space character position to the end of line.
211 //=======================================================================
212
213 void FSD_CmpFile::ReadString(TCollection_AsciiString& buffer)
214 {
215   buffer.Clear();
216   TCollection_AsciiString aBuf('\0');
217   FSD_File::ReadString(aBuf);
218   for (Standard_Integer lv = aBuf.Length(); lv >= 1 && (aBuf.Value(lv) == '\r' || (aBuf.Value(lv) == '\n')); lv--)
219   {
220     aBuf.Trunc (lv - 1);
221   }
222   buffer = aBuf;
223 }
224
225 //=======================================================================
226 //function : Destroy
227 //purpose  : 
228 //=======================================================================
229
230 void FSD_CmpFile::Destroy()
231 {
232   if (OpenMode() != Storage_VSNone) {
233     Close();
234   }
235 }
236
237 //=======================================================================
238 //function : BeginWriteInfoSection
239 //purpose  : -------------------------- INFO : WRITE
240 //=======================================================================
241
242 Storage_Error FSD_CmpFile::BeginWriteInfoSection()
243 {
244   myStream << FSD_CmpFile::MagicNumber() << '\n';
245   myStream << "BEGIN_INFO_SECTION\n";
246   if (myStream.bad()) throw Storage_StreamWriteError();
247
248   return Storage_VSOk;
249 }
250
251 //=======================================================================
252 //function : BeginReadInfoSection
253 //purpose  : 
254 //=======================================================================
255
256 Storage_Error FSD_CmpFile::BeginReadInfoSection()
257 {
258   Storage_Error s;
259   TCollection_AsciiString l;
260   Standard_Size        len = strlen(FSD_CmpFile::MagicNumber());
261
262   ReadChar(l, len);
263
264   if (strncmp(FSD_CmpFile::MagicNumber(), l.ToCString(), len) != 0) {
265     s = Storage_VSFormatError;
266   }
267   else {
268     s = FindTag("BEGIN_INFO_SECTION");
269   }
270
271   return s;
272 }
273
274 //=======================================================================
275 //function : WritePersistentObjectHeader
276 //purpose  : 
277 //=======================================================================
278
279 void FSD_CmpFile::WritePersistentObjectHeader(const Standard_Integer aRef,
280                                               const Standard_Integer aType)
281 {
282   myStream << "\n#" << aRef << "%" << aType << " ";
283   if (myStream.bad()) throw Storage_StreamWriteError();
284 }
285
286 //=======================================================================
287 //function : BeginWritePersistentObjectData
288 //purpose  : 
289 //=======================================================================
290
291 void FSD_CmpFile::BeginWritePersistentObjectData()
292 {
293   if (myStream.bad()) throw Storage_StreamWriteError();
294 }
295
296 //=======================================================================
297 //function : BeginWriteObjectData
298 //purpose  : 
299 //=======================================================================
300
301 void FSD_CmpFile::BeginWriteObjectData()
302 {
303   if (myStream.bad()) throw Storage_StreamWriteError();
304 }
305
306 //=======================================================================
307 //function : EndWriteObjectData
308 //purpose  : 
309 //=======================================================================
310
311 void FSD_CmpFile::EndWriteObjectData()
312 {
313   if (myStream.bad()) throw Storage_StreamWriteError();
314 }
315
316 //=======================================================================
317 //function : EndWritePersistentObjectData
318 //purpose  : 
319 //=======================================================================
320
321 void FSD_CmpFile::EndWritePersistentObjectData()
322 {
323   if (myStream.bad()) throw Storage_StreamWriteError();
324 }
325
326 //=======================================================================
327 //function : ReadPersistentObjectHeader
328 //purpose  : 
329 //=======================================================================
330
331 void FSD_CmpFile::ReadPersistentObjectHeader(Standard_Integer& aRef,
332                                              Standard_Integer& aType)
333 {
334   char c = '\0';
335
336   myStream.get(c);
337
338   while (c != '#') {
339     if (IsEnd() || (c != ' ') || (c == '\r') || (c == '\n')) {
340       throw Storage_StreamFormatError();
341     }
342     myStream.get(c);
343   }
344
345   if (!(myStream >> aRef)) throw Storage_StreamTypeMismatchError();
346
347   myStream.get(c);
348
349   while (c != '%') {
350     if (IsEnd() || (c != ' ') || (c == '\r') || (c == '\n')) {
351       throw Storage_StreamFormatError();
352     }
353     myStream.get(c);
354   }
355
356   if (!(myStream >> aType)) throw Storage_StreamTypeMismatchError();
357   //  std::cout << "REF:" << aRef << " TYPE:"<< aType << std::endl;
358 }
359
360 //=======================================================================
361 //function : BeginReadPersistentObjectData
362 //purpose  : 
363 //=======================================================================
364
365 void FSD_CmpFile::BeginReadPersistentObjectData()
366 {
367   //std::cout << "BeginReadPersistentObjectData" << std::endl;
368 }
369
370 //=======================================================================
371 //function : BeginReadObjectData
372 //purpose  : 
373 //=======================================================================
374
375 void FSD_CmpFile::BeginReadObjectData()
376 {
377   //  std::cout << "BeginReadObjectData" << std::endl;
378 }
379
380 //=======================================================================
381 //function : EndReadObjectData
382 //purpose  : 
383 //=======================================================================
384
385 void FSD_CmpFile::EndReadObjectData()
386 {
387   //  std::cout << "EndReadObjectData" << std::endl;
388 }
389
390 //=======================================================================
391 //function : EndReadPersistentObjectData
392 //purpose  : 
393 //=======================================================================
394
395 void FSD_CmpFile::EndReadPersistentObjectData()
396 {
397   char c = '\0';
398
399   myStream.get(c);
400   while (c != '\n' && (c != '\r')) {
401     if (IsEnd() || (c != ' ')) {
402       throw Storage_StreamFormatError();
403     }
404     myStream.get(c);
405   }
406   if (c == '\r') {
407     myStream.get(c);
408   }
409   //  std::cout << "EndReadPersistentObjectData" << std::endl;
410 }