root/OpenSceneGraph/trunk/src/osgPlugins/osga/OSGA_Archive.h @ 13041

Revision 13041, 8.7 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osg/Notify>
15#include <osgDB/Archive>
16#include <osgDB/FileNameUtils>
17
18#include <OpenThreads/ScopedLock>
19#include <OpenThreads/ReentrantMutex>
20
21#define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex)
22
23class OSGA_Archive : public osgDB::Archive
24{
25    public:
26        OSGA_Archive();
27        virtual ~OSGA_Archive();
28
29        virtual const char* libraryName() const { return "osga"; }
30
31        virtual const char* className() const { return "Archive"; }
32
33        virtual bool acceptsExtension(const std::string& extension) const
34        {
35            return osgDB::equalCaseInsensitive(extension,"osga");
36        }
37
38        /** open the archive.*/
39        virtual bool open(const std::string& filename, ArchiveStatus status, unsigned int indexBlockSizeHint=4096);
40
41        /** open the archive for reading.*/
42        virtual bool open(std::istream& fin);
43
44        /** close the archive.*/
45        virtual void close();
46
47        /** Get the file name which represents the archived file.*/
48        virtual std::string getArchiveFileName() const { return _archiveFileName; }
49
50        /** Get the file name which represents the master file recorded in the Archive.*/
51        virtual std::string getMasterFileName() const;
52
53        /** return true if file exists in archive.*/
54        virtual bool fileExists(const std::string& filename) const;
55
56        /** return type of file. */
57        virtual osgDB::FileType getFileType(const std::string& filename) const;
58
59        /** Get the full list of file names available in the archive.*/
60        virtual bool getFileNames(FileNameList& fileNameList) const;
61
62
63        /** Read an osg::Object of specified file name from the Archive.*/
64        virtual ReadResult readObject(const std::string& fileName,const Options* options=NULL) const;
65
66        /** Read an osg::Image of specified file name from the Archive.*/
67        virtual ReadResult readImage(const std::string& fileName,const Options* options=NULL) const;
68
69        /** Read an osg::HeightField of specified file name from the Archive.*/
70        virtual ReadResult readHeightField(const std::string& fileName,const Options* options=NULL) const;
71
72        /** Read an osg::Node of specified file name from the Archive.*/
73        virtual ReadResult readNode(const std::string& fileName,const Options* options=NULL) const;
74
75        /** Read an osg::Shader of specified file name from the Archive.*/
76        virtual ReadResult readShader(const std::string& fileName,const Options* options=NULL) const;
77
78        /** Write an osg::Object with specified file name to the Archive.*/
79        virtual WriteResult writeObject(const osg::Object& obj,const std::string& fileName,const Options* options=NULL) const;
80
81        /** Write an osg::Image with specified file name to the Archive.*/
82        virtual WriteResult writeImage(const osg::Image& image,const std::string& fileName,const Options* options=NULL) const;
83
84        /** Write an osg::HeightField with specified file name to the Archive.*/
85        virtual WriteResult writeHeightField(const osg::HeightField& heightField,const std::string& fileName,const Options* options=NULL) const;
86
87        /** Write an osg::Node with specified file name to the Archive.*/
88        virtual WriteResult writeNode(const osg::Node& node,const std::string& fileName,const Options* options=NULL) const;
89
90        /** Write an osg::Shader with specified file name to the Archive.*/
91        virtual WriteResult writeShader(const osg::Shader& shader,const std::string& fileName,const Options* options=NULL) const;
92
93        #if defined(_MSC_VER)
94        typedef __int64 pos_type;
95        typedef __int64 size_type;
96        #else
97        typedef unsigned long long pos_type;
98        typedef unsigned long long size_type;
99        #endif
100
101        typedef std::pair<pos_type, size_type> PositionSizePair;
102        typedef std::map<std::string, PositionSizePair> FileNamePositionMap;
103
104    protected:
105
106        mutable OpenThreads::ReentrantMutex _serializerMutex;
107
108        class IndexBlock;
109        friend class IndexBlock;
110
111        class IndexBlock : public osg::Referenced
112        {
113        public:
114            IndexBlock(unsigned int blockSize=0);
115
116            inline pos_type getPosition() const { return _filePosition; }
117
118            inline unsigned int getBlockSize() const { return _blockSize; }
119
120
121            void setPositionNextIndexBlock(pos_type position);
122
123            inline pos_type getPositionNextIndexBlock() const { return _filePositionNextIndexBlock; }
124
125
126            static IndexBlock* read(std::istream& in, bool doEndianSwap);
127
128            std::string getFirstFileName() const;
129
130            bool getFileReferences(FileNamePositionMap& indexMap) const;
131
132
133            inline bool requiresWrite() const { return _requiresWrite; }
134
135            void write(std::ostream& out);
136
137            inline bool spaceAvailable(pos_type, size_type, const std::string& filename) const
138            {
139                unsigned requiredSize = sizeof(pos_type)+sizeof(size_type)+sizeof(unsigned int)+filename.size();
140                return (_offsetOfNextAvailableSpace + requiredSize)<_blockSize;
141            }
142
143            bool addFileReference(pos_type position, size_type size, const std::string& filename);
144
145
146
147        protected:
148
149            void allocateData(unsigned int blockSize);
150
151            virtual ~IndexBlock();
152            bool            _requiresWrite;
153            pos_type        _filePosition;
154
155            unsigned int    _blockSize;
156            pos_type        _filePositionNextIndexBlock;
157            unsigned int    _offsetOfNextAvailableSpace;
158            char*           _data;
159        };
160
161    public:
162        /** Functor used in internal implementations.*/
163        struct ReadFunctor
164        {
165            ReadFunctor(const std::string& filename, const osgDB::ReaderWriter::Options* options):
166                _filename(filename),
167                _options(options) {}
168
169            virtual ~ReadFunctor() {}
170            virtual osgDB::ReaderWriter::ReadResult doRead(osgDB::ReaderWriter& rw, std::istream& input) const = 0;
171
172            std::string _filename;
173            const osgDB::ReaderWriter::Options* _options;
174        };
175
176        /** Functor used in internal implementations.*/
177        struct WriteFunctor
178        {
179            WriteFunctor(const std::string& filename, const osgDB::ReaderWriter::Options* options):
180                _filename(filename),
181                _options(options) {}
182
183            virtual ~WriteFunctor() {}
184            virtual osgDB::ReaderWriter::WriteResult doWrite(osgDB::ReaderWriter& rw, std::ostream& output) const = 0;
185
186            std::string _filename;
187            const osgDB::ReaderWriter::Options* _options;
188        };
189
190    protected:
191        struct ReadObjectFunctor;
192        struct ReadImageFunctor;
193        struct ReadHeightFieldFunctor;
194        struct ReadNodeFunctor;
195        struct ReadShaderFunctor;
196
197
198        struct WriteObjectFunctor;
199        struct WriteImageFunctor;
200        struct WriteHeightFieldFunctor;
201        struct WriteNodeFunctor;
202        struct WriteShaderFunctor;
203
204
205        osgDB::ReaderWriter::ReadResult read(const ReadFunctor& readFunctor);
206        osgDB::ReaderWriter::WriteResult write(const WriteFunctor& writeFunctor);
207
208        typedef std::list< osg::ref_ptr<IndexBlock> >   IndexBlockList;
209
210        bool _open(std::istream& fin);
211
212        void writeIndexBlocks();
213
214        bool addFileReference(pos_type position, size_type size, const std::string& fileName);
215
216        static float        s_currentSupportedVersion;
217        float               _version;
218        ArchiveStatus       _status;
219        osgDB::ifstream     _input;
220        osgDB::fstream      _output;
221
222        std::string         _archiveFileName;
223        std::string         _masterFileName;
224        IndexBlockList      _indexBlockList;
225        FileNamePositionMap _indexMap;
226
227
228        template <typename T>
229        static inline void _write(char* ptr, const T& value)
230        {
231            std::copy(reinterpret_cast<const char*>(&value),reinterpret_cast<const char*>(&value)+sizeof(value),ptr);
232        }
233
234        template <typename T>
235        static inline void _read(char* ptr, T& value)
236        {
237            std::copy(ptr,ptr+sizeof(value),reinterpret_cast<char*>(&value));
238        }
239
240};
Note: See TracBrowser for help on using the browser.