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