diff --git a/examples/osgarchive/osgarchive.cpp b/examples/osgarchive/osgarchive.cpp index ad9fe579d..e1b47dd7f 100644 --- a/examples/osgarchive/osgarchive.cpp +++ b/examples/osgarchive/osgarchive.cpp @@ -146,7 +146,7 @@ int main( int argc, char **argv ) if (insert) { osgDB::Archive archive; - archive.create(archiveFilename); + archive.open(archiveFilename, osgDB::Archive::WRITE); for (FileNameList::iterator itr=files.begin(); itr!=files.end(); diff --git a/include/osgDB/Archive b/include/osgDB/Archive index 1fd1ac9f4..8e734864a 100644 --- a/include/osgDB/Archive +++ b/include/osgDB/Archive @@ -40,14 +40,12 @@ class OSGDB_EXPORT Archive : public ReaderWriter enum Status { READ, - WRITE + WRITE, + CREATE }; /** open the archive.*/ - virtual bool create(const std::string& filename, unsigned int indexBlockSize=4096); - - /** open the archive.*/ - virtual bool open(const std::string& filename, Status status); + virtual bool open(const std::string& filename, Status status, unsigned int indexBlockSizeHint=4096); /** close the archive.*/ virtual void close(); diff --git a/src/osgDB/Archive.cpp b/src/osgDB/Archive.cpp index bdaa5433d..2295133a9 100644 --- a/src/osgDB/Archive.cpp +++ b/src/osgDB/Archive.cpp @@ -115,12 +115,14 @@ void Archive::IndexBlock::write(std::ostream& out) { if (_filePosition==pos_type(0)) { + osg::notify(osg::NOTICE)<<"Archive::IndexBlock::write() setting _filePosition"<(&_blockSize), sizeof(_blockSize)); out.write(reinterpret_cast(&_filePositionNextIndexBlock), sizeof(_filePositionNextIndexBlock)); @@ -181,29 +183,14 @@ Archive::~Archive() { close(); } +#include -bool Archive::create(const std::string& filename, unsigned int indexBlockSize) -{ - _status = WRITE; - _output.open(filename.c_str()); - _output<<"osga"; - _output.write(reinterpret_cast(&s_currentSupportedVersion),sizeof(float)); - - IndexBlock *indexBlock = new IndexBlock(indexBlockSize); - if (indexBlock) - { - indexBlock->write(_output); - _indexBlockList.push_back(indexBlock); - } - return true; -} - -bool Archive::open(const std::string& filename, Status status) +bool Archive::open(const std::string& filename, Status status, unsigned int indexBlockSize) { if (status==READ) { _status = status; - _input.open(filename.c_str()); + _input.open(filename.c_str(), std::ios_base::binary | std::ios_base::in); if (_input) { @@ -255,23 +242,71 @@ bool Archive::open(const std::string& filename, Status status) _input.close(); return false; } - else // status==WRITE + else if (status==WRITE) { - if (open(filename,READ)) + if (status==WRITE && open(filename,READ)) { _input.close(); + struct stat results; + pos_type end_of_file = 0; + if (stat(filename.c_str(), &results) == 0) + { + // The size of the file in bytes is in + // results.st_size + end_of_file = results.st_size; + } + _status = WRITE; - _output.open(filename.c_str()); + + _output.open(filename.c_str(), std::ios_base::binary | std::ios_base::out); + + + osg::notify(osg::NOTICE)<<"File position after open = "<<(int)_output.tellp()<<" is_open "<<_output.is_open()<(&s_currentSupportedVersion),sizeof(float)); + + IndexBlock *indexBlock = new IndexBlock(indexBlockSize); + if (indexBlock) + { + indexBlock->write(_output); + _indexBlockList.push_back(indexBlock); + } + + osg::notify(osg::NOTICE)<<"File position after write = "<<(int)_output.tellp()<writeObject(obj, _output, options);