Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-14 09:14:19

0001 // Created on: 1996-11-29
0002 // Created by: Christophe LEYNADIER
0003 // Copyright (c) 1996-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _FSD_BinaryFile_HeaderFile
0018 #define _FSD_BinaryFile_HeaderFile
0019 
0020 #include <FSD_BStream.hxx>
0021 #include <FSD_FileHeader.hxx>
0022 #include <Storage_BaseDriver.hxx>
0023 #include <Storage_Error.hxx>
0024 #include <Storage_OpenMode.hxx>
0025 #include <Storage_Data.hxx>
0026 #include <Storage_Position.hxx>
0027 #include <Standard_Integer.hxx>
0028 #include <TCollection_AsciiString.hxx>
0029 #include <NCollection_Sequence.hxx>
0030 #include <TCollection_ExtendedString.hxx>
0031 
0032 class TCollection_AsciiString;
0033 class TCollection_ExtendedString;
0034 class Storage_HeaderData;
0035 
0036 // Macro that tells if bytes must be reversed when read/write
0037 // data to/from a binary file. It is needed to provide binary file compatibility
0038 // between little and big endian platforms.
0039 #ifndef OCCT_BINARY_FILE_DO_INVERSE
0040   #if defined(SOLARIS) || defined(IRIX)
0041     // Do inverse on big endian platform
0042     #define OCCT_BINARY_FILE_DO_INVERSE 1
0043   #else
0044     #define OCCT_BINARY_FILE_DO_INVERSE 0
0045   #endif
0046 #endif
0047 
0048 class FSD_BinaryFile : public Storage_BaseDriver
0049 {
0050 public:
0051   DEFINE_STANDARD_RTTIEXT(FSD_BinaryFile, Storage_BaseDriver)
0052 
0053 public:
0054   Standard_EXPORT FSD_BinaryFile();
0055 
0056   Standard_EXPORT Storage_Error Open(const TCollection_AsciiString& aName,
0057                                      const Storage_OpenMode         aMode) override;
0058 
0059   Standard_EXPORT bool IsEnd() override;
0060 
0061   //! return position in the file. Return -1 upon error.
0062   Standard_EXPORT Storage_Position Tell() override;
0063 
0064   Standard_EXPORT static Storage_Error IsGoodFileType(const TCollection_AsciiString& aName);
0065 
0066   Standard_EXPORT Storage_Error BeginWriteInfoSection() override;
0067 
0068   Standard_EXPORT static int WriteInfo(
0069     Standard_OStream&                                    theOStream,
0070     const int                                            nbObj,
0071     const TCollection_AsciiString&                       dbVersion,
0072     const TCollection_AsciiString&                       date,
0073     const TCollection_AsciiString&                       schemaName,
0074     const TCollection_AsciiString&                       schemaVersion,
0075     const TCollection_ExtendedString&                    appName,
0076     const TCollection_AsciiString&                       appVersion,
0077     const TCollection_ExtendedString&                    objectType,
0078     const NCollection_Sequence<TCollection_AsciiString>& userInfo,
0079     const bool                                           theOnlyCount = false);
0080 
0081   Standard_EXPORT void WriteInfo(
0082     const int                                            nbObj,
0083     const TCollection_AsciiString&                       dbVersion,
0084     const TCollection_AsciiString&                       date,
0085     const TCollection_AsciiString&                       schemaName,
0086     const TCollection_AsciiString&                       schemaVersion,
0087     const TCollection_ExtendedString&                    appName,
0088     const TCollection_AsciiString&                       appVersion,
0089     const TCollection_ExtendedString&                    objectType,
0090     const NCollection_Sequence<TCollection_AsciiString>& userInfo) override;
0091 
0092   Standard_EXPORT Storage_Error EndWriteInfoSection() override;
0093 
0094   Standard_EXPORT Storage_Error EndWriteInfoSection(Standard_OStream& theOStream);
0095 
0096   Standard_EXPORT Storage_Error BeginReadInfoSection() override;
0097 
0098   Standard_EXPORT void ReadInfo(int&                                           nbObj,
0099                                 TCollection_AsciiString&                       dbVersion,
0100                                 TCollection_AsciiString&                       date,
0101                                 TCollection_AsciiString&                       schemaName,
0102                                 TCollection_AsciiString&                       schemaVersion,
0103                                 TCollection_ExtendedString&                    appName,
0104                                 TCollection_AsciiString&                       appVersion,
0105                                 TCollection_ExtendedString&                    objectType,
0106                                 NCollection_Sequence<TCollection_AsciiString>& userInfo) override;
0107 
0108   Standard_EXPORT void ReadCompleteInfo(Standard_IStream&          theIStream,
0109                                         occ::handle<Storage_Data>& theData) override;
0110 
0111   Standard_EXPORT Storage_Error EndReadInfoSection() override;
0112 
0113   Standard_EXPORT Storage_Error BeginWriteCommentSection() override;
0114 
0115   Standard_EXPORT Storage_Error BeginWriteCommentSection(Standard_OStream& theOStream);
0116 
0117   Standard_EXPORT void WriteComment(
0118     const NCollection_Sequence<TCollection_ExtendedString>& userComments) override;
0119 
0120   Standard_EXPORT static int WriteComment(
0121     Standard_OStream&                                       theOStream,
0122     const NCollection_Sequence<TCollection_ExtendedString>& theComments,
0123     const bool                                              theOnlyCount = false);
0124 
0125   Standard_EXPORT Storage_Error EndWriteCommentSection() override;
0126 
0127   Standard_EXPORT Storage_Error EndWriteCommentSection(Standard_OStream& theOStream);
0128 
0129   Standard_EXPORT Storage_Error BeginReadCommentSection() override;
0130 
0131   Standard_EXPORT void ReadComment(
0132     NCollection_Sequence<TCollection_ExtendedString>& userComments) override;
0133 
0134   Standard_EXPORT static void ReadComment(
0135     Standard_IStream&                                 theIStream,
0136     NCollection_Sequence<TCollection_ExtendedString>& userComments);
0137 
0138   Standard_EXPORT Storage_Error EndReadCommentSection() override;
0139 
0140   Standard_EXPORT Storage_Error BeginWriteTypeSection() override;
0141 
0142   Standard_EXPORT void SetTypeSectionSize(const int aSize) override;
0143 
0144   Standard_EXPORT void WriteTypeInformations(const int                      typeNum,
0145                                              const TCollection_AsciiString& typeName) override;
0146 
0147   Standard_EXPORT Storage_Error EndWriteTypeSection() override;
0148 
0149   Standard_EXPORT Storage_Error BeginReadTypeSection() override;
0150 
0151   Standard_EXPORT int TypeSectionSize() override;
0152 
0153   Standard_EXPORT static int TypeSectionSize(Standard_IStream& theIStream);
0154 
0155   Standard_EXPORT void ReadTypeInformations(int&                     typeNum,
0156                                             TCollection_AsciiString& typeName) override;
0157 
0158   Standard_EXPORT static void ReadTypeInformations(Standard_IStream&        theIStream,
0159                                                    int&                     typeNum,
0160                                                    TCollection_AsciiString& typeName);
0161 
0162   Standard_EXPORT Storage_Error EndReadTypeSection() override;
0163 
0164   Standard_EXPORT Storage_Error BeginWriteRootSection() override;
0165 
0166   Standard_EXPORT void SetRootSectionSize(const int aSize) override;
0167 
0168   Standard_EXPORT void WriteRoot(const TCollection_AsciiString& rootName,
0169                                  const int                      aRef,
0170                                  const TCollection_AsciiString& aType) override;
0171 
0172   Standard_EXPORT Storage_Error EndWriteRootSection() override;
0173 
0174   Standard_EXPORT Storage_Error BeginReadRootSection() override;
0175 
0176   Standard_EXPORT int RootSectionSize() override;
0177 
0178   Standard_EXPORT static int RootSectionSize(Standard_IStream& theIStream);
0179 
0180   Standard_EXPORT void ReadRoot(TCollection_AsciiString& rootName,
0181                                 int&                     aRef,
0182                                 TCollection_AsciiString& aType) override;
0183 
0184   Standard_EXPORT static void ReadRoot(Standard_IStream&        theIStream,
0185                                        TCollection_AsciiString& rootName,
0186                                        int&                     aRef,
0187                                        TCollection_AsciiString& aType);
0188 
0189   Standard_EXPORT Storage_Error EndReadRootSection() override;
0190 
0191   Standard_EXPORT Storage_Error BeginWriteRefSection() override;
0192 
0193   Standard_EXPORT void SetRefSectionSize(const int aSize) override;
0194 
0195   Standard_EXPORT void WriteReferenceType(const int reference, const int typeNum) override;
0196 
0197   Standard_EXPORT Storage_Error EndWriteRefSection() override;
0198 
0199   Standard_EXPORT Storage_Error BeginReadRefSection() override;
0200 
0201   Standard_EXPORT int RefSectionSize() override;
0202 
0203   Standard_EXPORT static int RefSectionSize(Standard_IStream& theIStream);
0204 
0205   Standard_EXPORT void ReadReferenceType(int& reference, int& typeNum) override;
0206 
0207   Standard_EXPORT static void ReadReferenceType(Standard_IStream& theIStream,
0208                                                 int&              reference,
0209                                                 int&              typeNum);
0210 
0211   Standard_EXPORT Storage_Error EndReadRefSection() override;
0212 
0213   Standard_EXPORT Storage_Error BeginWriteDataSection() override;
0214 
0215   Standard_EXPORT void WritePersistentObjectHeader(const int aRef, const int aType) override;
0216 
0217   Standard_EXPORT void BeginWritePersistentObjectData() override;
0218 
0219   Standard_EXPORT void BeginWriteObjectData() override;
0220 
0221   Standard_EXPORT void EndWriteObjectData() override;
0222 
0223   Standard_EXPORT void EndWritePersistentObjectData() override;
0224 
0225   Standard_EXPORT Storage_Error EndWriteDataSection() override;
0226 
0227   Standard_EXPORT Storage_Error BeginReadDataSection() override;
0228 
0229   Standard_EXPORT void ReadPersistentObjectHeader(int& aRef, int& aType) override;
0230 
0231   Standard_EXPORT void BeginReadPersistentObjectData() override;
0232 
0233   Standard_EXPORT void BeginReadObjectData() override;
0234 
0235   Standard_EXPORT void EndReadObjectData() override;
0236 
0237   Standard_EXPORT void EndReadPersistentObjectData() override;
0238 
0239   Standard_EXPORT Storage_Error EndReadDataSection() override;
0240 
0241   Standard_EXPORT void SkipObject() override;
0242 
0243   Standard_EXPORT Storage_BaseDriver& PutReference(const int aValue) override;
0244 
0245   Standard_EXPORT Storage_BaseDriver& PutCharacter(const char aValue) override;
0246 
0247   Standard_EXPORT Storage_BaseDriver& PutExtCharacter(const char16_t aValue) override;
0248 
0249   Standard_EXPORT static int PutInteger(Standard_OStream& theOStream,
0250                                         const int         aValue,
0251                                         const bool        theOnlyCount = false);
0252 
0253   Standard_EXPORT Storage_BaseDriver& PutInteger(const int aValue) override;
0254 
0255   Standard_EXPORT Storage_BaseDriver& PutBoolean(const bool aValue) override;
0256 
0257   Standard_EXPORT Storage_BaseDriver& PutReal(const double aValue) override;
0258 
0259   Standard_EXPORT Storage_BaseDriver& PutShortReal(const float aValue) override;
0260 
0261   Standard_EXPORT Storage_BaseDriver& GetReference(int& aValue) override;
0262 
0263   Standard_EXPORT Storage_BaseDriver& GetCharacter(char& aValue) override;
0264 
0265   Standard_EXPORT static void GetReference(Standard_IStream& theIStream, int& aValue);
0266 
0267   Standard_EXPORT Storage_BaseDriver& GetExtCharacter(char16_t& aValue) override;
0268 
0269   Standard_EXPORT Storage_BaseDriver& GetInteger(int& aValue) override;
0270 
0271   Standard_EXPORT static void GetInteger(Standard_IStream& theIStream, int& aValue);
0272 
0273   Standard_EXPORT Storage_BaseDriver& GetBoolean(bool& aValue) override;
0274 
0275   Standard_EXPORT Storage_BaseDriver& GetReal(double& aValue) override;
0276 
0277   Standard_EXPORT Storage_BaseDriver& GetShortReal(float& aValue) override;
0278 
0279   Standard_EXPORT Storage_Error Close() override;
0280 
0281   Standard_EXPORT void Destroy();
0282 
0283   ~FSD_BinaryFile() override { Destroy(); }
0284 
0285 public:
0286   //!@name Own methods
0287 
0288   /// Inverse bytes in integer value
0289   static int InverseInt(const int theValue)
0290   {
0291     return (0 | ((theValue & 0x000000ff) << 24) | ((theValue & 0x0000ff00) << 8)
0292             | ((theValue & 0x00ff0000) >> 8) | ((theValue >> 24) & 0x000000ff));
0293   }
0294 
0295   /// Inverse bytes in extended character value
0296   static char16_t InverseExtChar(const char16_t theValue)
0297   {
0298     return (0 | ((theValue & 0x00ff) << 8) | ((theValue & 0xff00) >> 8));
0299   }
0300 
0301   /// Inverse bytes in real value
0302   Standard_EXPORT static double InverseReal(const double theValue);
0303 
0304   /// Inverse bytes in short real value
0305   Standard_EXPORT static float InverseShortReal(const float theValue);
0306 
0307   /// Inverse bytes in size value
0308   Standard_EXPORT static size_t InverseSize(const size_t theValue);
0309   /// Inverse bytes in 64bit unsigned int value
0310   Standard_EXPORT static uint64_t InverseUint64(const uint64_t theValue);
0311 
0312   Standard_EXPORT static void ReadHeader(Standard_IStream& theIStream,
0313                                          FSD_FileHeader&   theFileHeader);
0314 
0315   Standard_EXPORT static void ReadHeaderData(Standard_IStream&                      theIStream,
0316                                              const occ::handle<Storage_HeaderData>& theHeaderData);
0317 
0318   Standard_EXPORT static void ReadString(Standard_IStream&        theIStream,
0319                                          TCollection_AsciiString& buffer);
0320 
0321   Standard_EXPORT static void ReadExtendedString(Standard_IStream&           theIStream,
0322                                                  TCollection_ExtendedString& buffer);
0323 
0324   Standard_EXPORT static int WriteHeader(Standard_OStream&     theOStream,
0325                                          const FSD_FileHeader& theHeader,
0326                                          const bool            theOnlyCount = false);
0327 
0328   Standard_EXPORT static const char* MagicNumber();
0329 
0330 protected:
0331   //! read <rsize> character from the current position.
0332   Standard_EXPORT void ReadChar(TCollection_AsciiString& buffer, const size_t rsize);
0333 
0334   //! read string from the current position.
0335   Standard_EXPORT void ReadString(TCollection_AsciiString& buffer);
0336 
0337   //! write string at the current position.
0338   Standard_EXPORT void WriteString(const TCollection_AsciiString& buffer);
0339 
0340   //! write string at the current position.
0341   Standard_EXPORT static int WriteString(Standard_OStream&              theOStream,
0342                                          const TCollection_AsciiString& theString,
0343                                          const bool                     theOnlyCount = false);
0344 
0345   //! read string from the current position.
0346   Standard_EXPORT void ReadExtendedString(TCollection_ExtendedString& buffer);
0347 
0348   //! write string at the current position.
0349   Standard_EXPORT void WriteExtendedString(const TCollection_ExtendedString& buffer);
0350 
0351   //! write string at the current position.
0352   Standard_EXPORT static int WriteExtendedString(Standard_OStream&                 theOStream,
0353                                                  const TCollection_ExtendedString& theString,
0354                                                  const bool theOnlyCount = false);
0355 
0356 private:
0357   void WriteHeader();
0358 
0359   void ReadHeader();
0360 
0361 private:
0362   FSD_BStream    myStream;
0363   FSD_FileHeader myHeader{};
0364 };
0365 
0366 #endif // _FSD_BinaryFile_HeaderFile