1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2017 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
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>
21 const Standard_CString MAGICNUMBER = "CMPFILE";
23 IMPLEMENT_STANDARD_RTTIEXT(FSD_CmpFile, FSD_File)
25 //=======================================================================
26 //function : FSD_CmpFile
28 //=======================================================================
30 FSD_CmpFile::FSD_CmpFile()
33 //=======================================================================
34 //function : IsGoodFileType
35 //purpose : INFO SECTION
37 //=======================================================================
39 Storage_Error FSD_CmpFile::IsGoodFileType(const TCollection_AsciiString& aName)
44 s = f.Open(aName, Storage_VSRead);
46 if (s == Storage_VSOk) {
47 TCollection_AsciiString l;
48 Standard_Size len = strlen(FSD_CmpFile::MagicNumber());
54 if (strncmp(FSD_CmpFile::MagicNumber(), l.ToCString(), len) != 0) {
55 s = Storage_VSFormatError;
62 //=======================================================================
65 //=======================================================================
66 Storage_Error FSD_CmpFile::Open(const TCollection_AsciiString& aName, const Storage_OpenMode aMode)
68 Storage_Error result = Storage_VSOk;
71 if (OpenMode() == Storage_VSNone) {
72 std::ios_base::openmode anOpenMode = std::ios_base::openmode(0);
81 // std::ios::nocreate is not portable
82 #if !defined(IRIX) && !defined(DECOSF1)
83 anOpenMode = std::ios::in | std::ios::binary;
85 anOpenMode = std::ios::in;
91 #if !defined(IRIX) && !defined(DECOSF1)
92 anOpenMode = std::ios::out | std::ios::binary;
94 anOpenMode = std::ios::out;
98 case Storage_VSReadWrite:
100 #if !defined(IRIX) && !defined(DECOSF1)
101 anOpenMode = std::ios::in | std::ios::out | std::ios::binary;
103 anOpenMode = std::ios::in | std::ios::out;
110 OSD_OpenStream(myStream, aName, anOpenMode);
112 if (myStream.fail()) {
113 result = Storage_VSOpenError;
116 myStream.precision(17);
117 myStream.imbue(std::locale::classic()); // use always C locale
122 result = Storage_VSAlreadyOpen;
127 //=======================================================================
128 //function : MagicNumber
129 //purpose : ------------------ PROTECTED
130 //=======================================================================
132 Standard_CString FSD_CmpFile::MagicNumber()
137 //=======================================================================
138 //function : ReadLine
139 //purpose : read from the current position to the end of line.
140 //=======================================================================
142 void FSD_CmpFile::ReadLine(TCollection_AsciiString& buffer)
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--)
154 //=======================================================================
155 //function : WriteExtendedLine
156 //purpose : write from the current position to the end of line.
157 //=======================================================================
159 void FSD_CmpFile::WriteExtendedLine(const TCollection_ExtendedString& buffer)
162 Standard_ExtString extBuffer;
163 Standard_Integer i, c, d;
165 extBuffer = buffer.ToExtString();
167 for (i = 0; i < buffer.Length(); i++) {
168 c = (extBuffer[i] & 0x0000FF00) >> 8;
169 d = extBuffer[i] & 0x000000FF;
171 myStream << (char)c << (char)d;
174 myStream << (char)0 << "\n";
176 Standard_ExtString extBuffer;
179 extBuffer = buffer.ToExtString();
180 PutInteger(buffer.Length());
181 for (i = 0; i < buffer.Length(); i++) {
182 PutExtCharacter(extBuffer[i]);
188 //=======================================================================
189 //function : ReadExtendedLine
191 //=======================================================================
193 void FSD_CmpFile::ReadExtendedLine(TCollection_ExtendedString& buffer)
195 Standard_ExtCharacter c;
200 for (i = 0; i < buffer.Length(); i++) {
208 //=======================================================================
209 //function : ReadString
210 //purpose : read from the first none space character position to the end of line.
211 //=======================================================================
213 void FSD_CmpFile::ReadString(TCollection_AsciiString& buffer)
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--)
225 //=======================================================================
228 //=======================================================================
230 void FSD_CmpFile::Destroy()
232 if (OpenMode() != Storage_VSNone) {
237 //=======================================================================
238 //function : BeginWriteInfoSection
239 //purpose : -------------------------- INFO : WRITE
240 //=======================================================================
242 Storage_Error FSD_CmpFile::BeginWriteInfoSection()
244 myStream << FSD_CmpFile::MagicNumber() << '\n';
245 myStream << "BEGIN_INFO_SECTION\n";
246 if (myStream.bad()) throw Storage_StreamWriteError();
251 //=======================================================================
252 //function : BeginReadInfoSection
254 //=======================================================================
256 Storage_Error FSD_CmpFile::BeginReadInfoSection()
259 TCollection_AsciiString l;
260 Standard_Size len = strlen(FSD_CmpFile::MagicNumber());
264 if (strncmp(FSD_CmpFile::MagicNumber(), l.ToCString(), len) != 0) {
265 s = Storage_VSFormatError;
268 s = FindTag("BEGIN_INFO_SECTION");
274 //=======================================================================
275 //function : WritePersistentObjectHeader
277 //=======================================================================
279 void FSD_CmpFile::WritePersistentObjectHeader(const Standard_Integer aRef,
280 const Standard_Integer aType)
282 myStream << "\n#" << aRef << "%" << aType << " ";
283 if (myStream.bad()) throw Storage_StreamWriteError();
286 //=======================================================================
287 //function : BeginWritePersistentObjectData
289 //=======================================================================
291 void FSD_CmpFile::BeginWritePersistentObjectData()
293 if (myStream.bad()) throw Storage_StreamWriteError();
296 //=======================================================================
297 //function : BeginWriteObjectData
299 //=======================================================================
301 void FSD_CmpFile::BeginWriteObjectData()
303 if (myStream.bad()) throw Storage_StreamWriteError();
306 //=======================================================================
307 //function : EndWriteObjectData
309 //=======================================================================
311 void FSD_CmpFile::EndWriteObjectData()
313 if (myStream.bad()) throw Storage_StreamWriteError();
316 //=======================================================================
317 //function : EndWritePersistentObjectData
319 //=======================================================================
321 void FSD_CmpFile::EndWritePersistentObjectData()
323 if (myStream.bad()) throw Storage_StreamWriteError();
326 //=======================================================================
327 //function : ReadPersistentObjectHeader
329 //=======================================================================
331 void FSD_CmpFile::ReadPersistentObjectHeader(Standard_Integer& aRef,
332 Standard_Integer& aType)
339 if (IsEnd() || (c != ' ') || (c == '\r') || (c == '\n')) {
340 throw Storage_StreamFormatError();
345 if (!(myStream >> aRef)) throw Storage_StreamTypeMismatchError();
350 if (IsEnd() || (c != ' ') || (c == '\r') || (c == '\n')) {
351 throw Storage_StreamFormatError();
356 if (!(myStream >> aType)) throw Storage_StreamTypeMismatchError();
357 // std::cout << "REF:" << aRef << " TYPE:"<< aType << std::endl;
360 //=======================================================================
361 //function : BeginReadPersistentObjectData
363 //=======================================================================
365 void FSD_CmpFile::BeginReadPersistentObjectData()
367 //std::cout << "BeginReadPersistentObjectData" << std::endl;
370 //=======================================================================
371 //function : BeginReadObjectData
373 //=======================================================================
375 void FSD_CmpFile::BeginReadObjectData()
377 // std::cout << "BeginReadObjectData" << std::endl;
380 //=======================================================================
381 //function : EndReadObjectData
383 //=======================================================================
385 void FSD_CmpFile::EndReadObjectData()
387 // std::cout << "EndReadObjectData" << std::endl;
390 //=======================================================================
391 //function : EndReadPersistentObjectData
393 //=======================================================================
395 void FSD_CmpFile::EndReadPersistentObjectData()
400 while (c != '\n' && (c != '\r')) {
401 if (IsEnd() || (c != ' ')) {
402 throw Storage_StreamFormatError();
409 // std::cout << "EndReadPersistentObjectData" << std::endl;