From 09708f470a5637c7b639fdeccb75f5875e949ad9 Mon Sep 17 00:00:00 2001 From: stv Date: Wed, 27 May 2015 20:56:26 +0300 Subject: [PATCH] Bug fixes in binary format storage and other improvements. --- .../BinLDrivers_DocumentStorageDriver.cxx | 31 +++----- src/BinObjMgt/BinObjMgt_Persistent.cxx | 25 ++++--- src/CDF/CDF_Store.cxx | 70 +------------------ src/Storage/Storage.cdl | 10 ++- src/Storage/Storage_File.cdl | 4 +- src/Storage/Storage_File.cxx | 33 +++++++-- src/Storage/Storage_IODevice.cdl | 4 +- src/Storage/Storage_IODevice.cxx | 3 +- src/Storage/Storage_IStream.cdl | 4 +- src/Storage/Storage_IStream.cxx | 16 ++++- src/Storage/Storage_OStream.cdl | 4 +- src/Storage/Storage_OStream.cxx | 23 ++++-- .../XmlLDrivers_DocumentStorageDriver.cxx | 9 +-- 13 files changed, 115 insertions(+), 121 deletions(-) diff --git a/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx b/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx index 911b7921de..a6210cb206 100644 --- a/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx +++ b/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx @@ -100,29 +100,15 @@ void BinLDrivers_DocumentStorageDriver::Write return; } - theDevice->Open( Storage_VSWrite ); + theDevice->Open( Storage_VSAppend ); + Standard_Size aP = theDevice->Tell(); - /* -#if defined(_WIN32) - ofstream anOS ((const wchar_t*) theFileName.ToExtString(), ios::in | ios::binary | ios::ate); -#elif !defined(IRIX) // 10.10.2005 - ofstream anOS (aFileName.ToCString(), ios::in | ios::binary | ios::ate); -#else - ofstream anOS (aFileName.ToCString(), ios::ate); - //ofstream anOS (aFileName.ToCString(), ios::out| ios::binary | ios::ate); -#endif -#ifdef DEB - const Standard_Integer aP = (Standard_Integer) anOS.tellp(); - cout << "POS = " << aP <CanWrite()) { // 2. Write the Table of Contents of Sections - BinLDrivers_VectorOfDocumentSection::Iterator anIterS (mySections); - for (; anIterS.More(); anIterS.Next()) - anIterS.ChangeValue().WriteTOC (theDevice); + BinLDrivers_VectorOfDocumentSection::Iterator anIterS (mySections); + for (; anIterS.More(); anIterS.Next()) + anIterS.ChangeValue().WriteTOC (theDevice); // Shapes Section is the last one, it indicates the end of the table. BinLDrivers_DocumentSection aShapesSection (SHAPESECTION_POS, @@ -172,12 +158,14 @@ void BinLDrivers_DocumentStorageDriver::Write WriteMessage (anErrorStr + "BinLDrivers_DocumentStorageDriver, Problem with the file stream."; #else TCollection_ExtendedString aStr = - anErrorStr + aMethStr + "Problem writing the file "; - WriteMessage (aStr + theFileName); + anErrorStr + aMethStr + "Problem writing the file."; + WriteMessage (aStr); #endif SetIsError(Standard_True); SetStoreStatus(PCDM_SS_WriteFailure); } + + theDevice->Close(); } } @@ -368,7 +356,6 @@ void BinLDrivers_DocumentStorageDriver::WriteInfoSection if (aFileDriver.Open( theDevice, Storage_VSWrite ) != Storage_VSOk) { WriteMessage (TCollection_ExtendedString("Error: Cannot open the device ") + theDevice->Name()); -#endif SetIsError(Standard_True); return; } diff --git a/src/BinObjMgt/BinObjMgt_Persistent.cxx b/src/BinObjMgt/BinObjMgt_Persistent.cxx index fba4784f03..c5e49cf34d 100644 --- a/src/BinObjMgt/BinObjMgt_Persistent.cxx +++ b/src/BinObjMgt/BinObjMgt_Persistent.cxx @@ -111,16 +111,25 @@ Standard_OStream& BinObjMgt_Persistent::Write (Standard_OStream& theOS) void BinObjMgt_Persistent::Write (const Handle(Storage_IODevice)& theDevice) { - if (!theDevice->CanWrite()) - { - return; - } - - Handle(Storage_OStream) aStream = Handle(Storage_OStream)::DownCast(theDevice); - if (!aStream.IsNull()) + Standard_Integer nbWritten = 0; + Standard_Integer *aData = (Standard_Integer*) myData(1); + // update data length + aData[2] = mySize - BP_HEADSIZE; +#if DO_INVERSE + aData[0] = InverseInt (aData[0]); + aData[1] = InverseInt (aData[1]); + aData[2] = InverseInt (aData[2]); +#endif + for (Standard_Integer i=1; theDevice->CanWrite() && nbWritten < mySize && i <= myData.Length(); i++) { - Write (*aStream->Stream()); + Standard_Integer nbToWrite = Min(mySize - nbWritten, BP_PIECESIZE); + theDevice->Write( (char*)myData(i), nbToWrite ); + nbWritten += nbToWrite; } + myIndex = 1; + myOffset = BP_HEADSIZE; + mySize = BP_HEADSIZE; + myIsError = Standard_False; } //======================================================================= diff --git a/src/CDF/CDF_Store.cxx b/src/CDF/CDF_Store.cxx index c4fe046cfd..7b276b7fdf 100644 --- a/src/CDF/CDF_Store.cxx +++ b/src/CDF/CDF_Store.cxx @@ -109,90 +109,24 @@ Standard_ExtString CDF_Store::Name() const { } Standard_Boolean CDF_Store::SetDevice(const Handle(Storage_IODevice)& aDevice) { - /* - TCollection_ExtendedString theFolder(aFolder); - Standard_Integer l = theFolder.Length(); - - // if the last character is the folder separator (which is always the first character) - // it is removed. - // This is correct for Unix systems but not for Windows! VMS and MAC? Thomas Haller, 23.11.01 - if(l > 1) { -#ifndef WNT - if(theFolder.Value(l) == theFolder.Value(1)) theFolder.Trunc(l-1); -#else - if (theFolder.Value(l) == '/' || theFolder.Value(l) == '\\') - theFolder.Trunc(l-1); -#endif - } - - if(theMetaDataDriver->FindFolder(theFolder)) { - */ - myCurrentDocument->SetRequestedDevice(aDevice); - return Standard_True; - /* - } - return Standard_False; - */ + myCurrentDocument->SetRequestedDevice(aDevice); + return Standard_True; } CDF_StoreSetNameStatus CDF_Store::RecheckName () { // return SetName(myCurrentDocument->RequestedName()); return CDF_SSNS_OK; } -/* -CDF_StoreSetNameStatus CDF_Store::SetName(const TCollection_ExtendedString& aName) -{ - TCollection_ExtendedString theName=theMetaDataDriver->SetName(myCurrentDocument,aName); - - if(myCurrentDocument->IsStored ()) { - Handle(CDM_MetaData) E = myCurrentDocument->MetaData(); - if( E->Folder() == myCurrentDocument->RequestedFolder() - && E->Name() == theName) return CDF_SSNS_OK; - } - - if(myCurrentDocument->HasRequestedFolder()) { - if (theMetaDataDriver->Find(myCurrentDocument->RequestedFolder(),theName)) { - if(theMetaDataDriver->MetaData(myCurrentDocument->RequestedFolder(),theName)->IsRetrieved()) - return CDF_SSNS_OpenDocument; - else { - myCurrentDocument->SetRequestedName(theName); - return CDF_SSNS_ReplacingAnExistentDocument; - } - } - } - myCurrentDocument->SetRequestedName(theName); - return CDF_SSNS_OK; -} - -CDF_StoreSetNameStatus CDF_Store::SetName(const Standard_ExtString aName) -{ - TCollection_ExtendedString theName(aName); - return SetName(theName); -} -*/ void CDF_Store::Realize() { Standard_ProgramError_Raise_if(!myList->IsConsistent(),"information are missing"); Handle(CDM_MetaData) m; myText = ""; - myCurrentDocument->RequestedDevice()->Open (Storage_VSWrite); myStatus = myList->Store(m,myText); - myCurrentDocument->RequestedDevice()->Close(); -} -/* -Standard_ExtString CDF_Store::Path() const { - return myPath.ToExtString(); } -*/ Handle(Storage_IODevice) CDF_Store::MetaDataDevice() const { - /* - static TCollection_ExtendedString retv; - retv=""; - if(myCurrentDocument->IsStored()) retv=myCurrentDocument->MetaData()->Path(); - return retv.ToExtString(); - */ return myCurrentDocument->MetaData()->Device(); } diff --git a/src/Storage/Storage.cdl b/src/Storage/Storage.cdl index e09500ebf8..997520686d 100644 --- a/src/Storage/Storage.cdl +++ b/src/Storage/Storage.cdl @@ -86,12 +86,20 @@ is -- - Storage_VSInternalError : an internal error has been detected -- - Storage_VSExtCharParityError : an error -- has occurred while reading 16 bit characte - + + enumeration SeekMode + is + SMBegin, + SMCur, + SMEnd + end; + enumeration OpenMode is VSNone, VSRead, VSWrite, + VSAppend, VSReadWrite end; ---Purpose: diff --git a/src/Storage/Storage_File.cdl b/src/Storage/Storage_File.cdl index 567d241ba2..b45a5fa7bd 100644 --- a/src/Storage/Storage_File.cdl +++ b/src/Storage/Storage_File.cdl @@ -19,6 +19,7 @@ class File from Storage inherits IODevice from Storage uses Position from Storage, AsciiString from TCollection, OpenMode from Storage, + SeekMode from Storage, Error from Storage, FStream from Standard, ExtendedString from TCollection @@ -45,7 +46,8 @@ is Tell (me: mutable) returns Position from Storage is redefined; ---Purpose: returns a position in the device. Return -1 upon error. - Seek (me: mutable; thePos: Position from Storage) returns Boolean from Standard is redefined; + Seek( me: mutable; aPos : Position from Storage; aMode: SeekMode from Storage = Storage_SMBegin ) + returns Boolean from Standard is redefined; Close (me: mutable) returns Boolean from Standard; diff --git a/src/Storage/Storage_File.cxx b/src/Storage/Storage_File.cxx index 17282b80bd..5e8cdcb31e 100644 --- a/src/Storage/Storage_File.cxx +++ b/src/Storage/Storage_File.cxx @@ -79,15 +79,19 @@ Storage_Error Storage_File::Open (const Storage_OpenMode theMode) { if (theMode == Storage_VSRead) { - myStream.open(aFilePath, ios::in | ios::binary ); + myStream.open( aFilePath, ios::in | ios::binary ); } else if (theMode == Storage_VSWrite) { - myStream.open(aFilePath, ios::out | ios::binary ); + myStream.open( aFilePath, ios::out | ios::binary ); + } + else if (theMode == Storage_VSAppend) + { + myStream.open( aFilePath, ios::in | ios::out | ios::binary | ios::ate ); } else if (theMode == Storage_VSReadWrite) { - myStream.open(aFilePath, ios::in | ios::out); + myStream.open( aFilePath, ios::in | ios::out | ios::binary ); } if (!myStream.rdbuf()->is_open()) { @@ -128,12 +132,27 @@ Storage_Position Storage_File::Tell() //function : Seek //purpose : //======================================================================= -Standard_Boolean Storage_File::Seek (const Storage_Position& thePos) +Standard_Boolean Storage_File::Seek (const Storage_Position& thePos, const Storage_SeekMode aMode) { + ios::seekdir dir = ios::beg; + switch ( aMode ) + { + case Storage_SMEnd: + dir = ios::end; + break; + case Storage_SMCur: + dir = ios::cur; + break; + case Storage_SMBegin: + default: + dir = ios::beg; + break; + } + if ( OpenMode() == Storage_VSRead ) - myStream.seekg( thePos ); + myStream.seekg( thePos, dir ); else - myStream.seekp( thePos ); + myStream.seekp( thePos, dir ); return !myStream.fail(); } @@ -166,7 +185,7 @@ Standard_Boolean Storage_File::CanRead() const //======================================================================= Standard_Boolean Storage_File::CanWrite() const { - return myStream.good() && ( OpenMode() == Storage_VSWrite || OpenMode() == Storage_VSReadWrite ); + return myStream.good() && ( OpenMode() == Storage_VSWrite || OpenMode() == Storage_VSAppend || OpenMode() == Storage_VSReadWrite ); } //======================================================================= diff --git a/src/Storage/Storage_IODevice.cdl b/src/Storage/Storage_IODevice.cdl index de50507305..52ad5a42d0 100644 --- a/src/Storage/Storage_IODevice.cdl +++ b/src/Storage/Storage_IODevice.cdl @@ -21,6 +21,7 @@ uses Position from Storage, AsciiString from TCollection, OpenMode from Storage, Error from Storage, + SeekMode from Storage, ExtendedString from TCollection, SequenceOfAsciiString from TColStd, SequenceOfExtendedString from TColStd @@ -48,7 +49,8 @@ is Tell( me : mutable ) returns Position from Storage is deferred; ---Purpose: return position in the device. Return -1 upon error. - Seek( me: mutable; aPos : Position from Storage ) returns Boolean from Standard is deferred; + Seek( me: mutable; aPos : Position from Storage; aMode: SeekMode from Storage = Storage_SMBegin ) + returns Boolean from Standard is deferred; ---Purpose: Set new absolute position within the stream Close(me : mutable) returns Boolean from Standard is deferred; diff --git a/src/Storage/Storage_IODevice.cxx b/src/Storage/Storage_IODevice.cxx index cadc7329a6..515f75a15e 100644 --- a/src/Storage/Storage_IODevice.cxx +++ b/src/Storage/Storage_IODevice.cxx @@ -38,7 +38,8 @@ void Storage_IODevice::ReadLine( Standard_CString& aBuffer, const Standard_Integ { Standard_Character c; Read( &c, sizeof( Standard_Character ) ); - if ( aFin = ( c == anEndSymbol ) ) + aFin = ( c == anEndSymbol ); + if ( aFin ) aBuf[i++] = '\0'; else aBuf[i++] = c; diff --git a/src/Storage/Storage_IStream.cdl b/src/Storage/Storage_IStream.cdl index 215d6d1a87..8f1419a1cf 100644 --- a/src/Storage/Storage_IStream.cdl +++ b/src/Storage/Storage_IStream.cdl @@ -19,6 +19,7 @@ class IStream from Storage inherits IODevice from Storage uses Position from Storage, AsciiString from TCollection, OpenMode from Storage, + SeekMode from Storage, Error from Storage, IStream from Standard, IStreamPtr from Standard, @@ -46,7 +47,8 @@ is Tell (me: mutable) returns Position from Storage is redefined; ---Purpose: returns a position in the device. Return -1 upon error. - Seek (me: mutable; thePos: Position from Storage) returns Boolean from Standard is redefined; + Seek( me: mutable; aPos : Position from Storage; aMode: SeekMode from Storage = Storage_SMBegin ) + returns Boolean from Standard is redefined; Close (me: mutable) returns Boolean from Standard; diff --git a/src/Storage/Storage_IStream.cxx b/src/Storage/Storage_IStream.cxx index 3e1293c967..c91f1df2f0 100644 --- a/src/Storage/Storage_IStream.cxx +++ b/src/Storage/Storage_IStream.cxx @@ -102,9 +102,21 @@ Storage_Position Storage_IStream::Tell() //function : Seek //purpose : //======================================================================= -Standard_Boolean Storage_IStream::Seek (const Storage_Position& thePos) +Standard_Boolean Storage_IStream::Seek (const Storage_Position& thePos, const Storage_SeekMode aMode ) { - myStream->seekg (thePos); + switch ( aMode ) + { + case Storage_SMEnd: + myStream->seekg(thePos, ios::end); + break; + case Storage_SMCur: + myStream->seekg(thePos, ios::cur); + break; + case Storage_SMBegin: + default: + myStream->seekg(thePos, ios::beg); + break; + } return !myStream->fail(); } diff --git a/src/Storage/Storage_OStream.cdl b/src/Storage/Storage_OStream.cdl index 3ebf377940..a3a364ea7a 100644 --- a/src/Storage/Storage_OStream.cdl +++ b/src/Storage/Storage_OStream.cdl @@ -19,6 +19,7 @@ class OStream from Storage inherits IODevice from Storage uses Position from Storage, AsciiString from TCollection, OpenMode from Storage, + SeekMode from Storage, Error from Storage, OStream from Standard, OStreamPtr from Standard, @@ -46,7 +47,8 @@ is Tell (me: mutable) returns Position from Storage is redefined; ---Purpose: returns a position in the device. Return -1 upon error. - Seek (me: mutable; thePos: Position from Storage) returns Boolean from Standard is redefined; + Seek( me: mutable; aPos : Position from Storage; aMode: SeekMode from Storage = Storage_SMBegin ) + returns Boolean from Standard is redefined; Close (me: mutable) returns Boolean from Standard; diff --git a/src/Storage/Storage_OStream.cxx b/src/Storage/Storage_OStream.cxx index 0616d0b730..930760fb9d 100644 --- a/src/Storage/Storage_OStream.cxx +++ b/src/Storage/Storage_OStream.cxx @@ -50,7 +50,7 @@ TCollection_ExtendedString Storage_OStream::Name() const //======================================================================= Storage_Error Storage_OStream::Open (const Storage_OpenMode theMode) { - if (theMode != Storage_VSWrite) + if (theMode != Storage_VSWrite || theMode != Storage_VSAppend) { return Storage_VSOpenError; } @@ -69,7 +69,10 @@ Storage_Error Storage_OStream::Open (const Storage_OpenMode theMode) // clear flags and set the position where the next character is to be inserted myStream->clear(); - myStream->seekp(0, ios::beg); + if ( theMode == Storage_VSWrite ) + myStream->seekp(0, ios::beg); + else + myStream->seekp(0, ios::end); } } else @@ -102,9 +105,21 @@ Storage_Position Storage_OStream::Tell() //function : Seek //purpose : //======================================================================= -Standard_Boolean Storage_OStream::Seek (const Storage_Position& thePos) +Standard_Boolean Storage_OStream::Seek (const Storage_Position& thePos, const Storage_SeekMode aMode ) { - myStream->seekp (thePos); + switch ( aMode ) + { + case Storage_SMEnd: + myStream->seekp(thePos, ios::end); + break; + case Storage_SMCur: + myStream->seekp(thePos, ios::cur); + break; + case Storage_SMBegin: + default: + myStream->seekp(thePos, ios::beg); + break; + } return !myStream->fail(); } diff --git a/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx b/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx index 30e9254a4b..fdea2da6b4 100644 --- a/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx +++ b/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx @@ -115,16 +115,17 @@ void XmlLDrivers_DocumentStorageDriver::Write if (WriteToDomDocument (theDocument, anElement, theDevice) == Standard_False) { // Write DOM_Document into XML file, - //FILE * aFile = OSD_OpenFile(theFileName, "wt"); + theDevice->Open( Storage_VSWrite ); if (theDevice->CanWrite()) { LDOM_XmlWriter aWriter (theDevice); aWriter.SetIndentation(1); aWriter << aDOMDoc; - //fclose(aFile); + theDevice->Close(); ::take_time (0, " +++++ Fin formatting to XML : ", aMessageDriver); - - }else{ + } + else + { SetIsError (Standard_True); TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the device ") + theDevice->Name() + -- 2.39.5