From d7c4e6f26e599a84fcd5f29e067ecc887c960bba Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 14 May 2008 14:59:50 +0000 Subject: [PATCH] Moved the cache file writing into StreamObject so that the cache file is only created once data is being read. --- src/osgPlugins/curl/ReaderWriterCURL.cpp | 90 ++++++++++++++---------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/src/osgPlugins/curl/ReaderWriterCURL.cpp b/src/osgPlugins/curl/ReaderWriterCURL.cpp index afcf65817..804f33779 100644 --- a/src/osgPlugins/curl/ReaderWriterCURL.cpp +++ b/src/osgPlugins/curl/ReaderWriterCURL.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -26,23 +27,48 @@ class EasyCurl : public osg::Referenced { public: - struct StreamPair + struct StreamObject { - StreamPair(std::ostream* stream1, std::ostream* stream2=0): + StreamObject(std::ostream* stream1, const std::string& cacheFileName): _stream1(stream1), - _stream2(stream2) {} + _cacheFileName(cacheFileName) + { + _foutOpened = false; + } + + void write(const char* ptr, size_t realsize) + { + if (_stream1) _stream1->write(ptr, realsize); + + if (!_cacheFileName.empty()) + { + if (!_foutOpened) + { + osg::notify(osg::INFO)<<"Writing to cache: "<<_cacheFileName<_stream1) sp->_stream1->write((const char*)ptr, realsize); - if (sp->_stream2) sp->_stream2->write((const char*)ptr, realsize); + sp->write((const char*)ptr, realsize); return realsize; } @@ -68,7 +94,7 @@ class EasyCurl : public osg::Referenced } - osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamPair& sp) + osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp) { if(!proxyAddress.empty()) { @@ -254,8 +280,15 @@ class ReaderWriterCURL : public osgDB::ReaderWriter cacheFileName = cacheFilePath + "/" + osgDB::getServerAddress(fileName) + "/" + osgDB::getServerFileName(fileName); + + std::string path = osgDB::getFilePath(cacheFileName); + + if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) + { + cacheFileName.clear(); + } } - + if (!cacheFilePath.empty() && osgDB::fileExists(cacheFileName)) { osg::notify(osg::INFO) << "Reading cache file " << cacheFileName << std::endl; @@ -285,14 +318,16 @@ class ReaderWriterCURL : public osgDB::ReaderWriter std::stringstream buffer; - - EasyCurl::StreamPair sp(&buffer); + ReadResult curlResult; - //ReadResult result = _easyCurl.read(proxyAddress, fileName, sp); - ReadResult result = getEasyCurl().read(proxyAddress, fileName, sp); + EasyCurl::StreamObject sp(&buffer, cacheFileName); - if (result.status()==ReadResult::FILE_LOADED) + //ReadResult result = _easyCurl.read(proxyAddress, fileName, sp); + curlResult = getEasyCurl().read(proxyAddress, fileName, sp); + + if (curlResult.status()==ReadResult::FILE_LOADED) { + osg::ref_ptr local_opt = const_cast(options); if (!local_opt) local_opt = new Options; @@ -302,34 +337,11 @@ class ReaderWriterCURL : public osgDB::ReaderWriter local_opt->getDatabasePathList().pop_front(); - if (!cacheFilePath.empty() && readResult.validObject()) - { - osg::notify(osg::INFO)<<"Writing cache file "<