Changeset 8321

Show
Ignore:
Timestamp:
05/14/08 16:59:50
Author:
robert
Message:

Moved the cache file writing into StreamObject? so that the cache file is only
created once data is being read.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenSceneGraph/trunk/src/osgPlugins/curl/ReaderWriterCURL.cpp

    r8320 r8321  
    1919#include <iostream> 
    2020#include <sstream> 
     21#include <fstream> 
    2122 
    2223#include <curl/curl.h> 
     
    2728    public: 
    2829     
    29         struct StreamPair 
    30         { 
    31             StreamPair(std::ostream* stream1, std::ostream* stream2=0): 
     30        struct StreamObject 
     31        { 
     32            StreamObject(std::ostream* stream1, const std::string& cacheFileName): 
    3233                _stream1(stream1), 
    33                 _stream2(stream2) {} 
    34          
    35             std::ostream* _stream1; 
    36             std::ostream* _stream2; 
     34                _cacheFileName(cacheFileName) 
     35            { 
     36                _foutOpened = false; 
     37            } 
     38             
     39            void write(const char* ptr, size_t realsize) 
     40            { 
     41                if (_stream1) _stream1->write(ptr, realsize); 
     42                 
     43                if (!_cacheFileName.empty()) 
     44                { 
     45                    if (!_foutOpened) 
     46                    { 
     47                        osg::notify(osg::INFO)<<"Writing to cache: "<<_cacheFileName<<std::endl; 
     48                        _fout.open(_cacheFileName.c_str(), std::ios::out | std::ios::binary); 
     49                        _foutOpened = true; 
     50                    } 
     51                     
     52                    if (_fout) 
     53                    { 
     54                        _fout.write(ptr, realsize); 
     55                    } 
     56                } 
     57            } 
     58         
     59            std::ostream*   _stream1; 
     60             
     61            bool            _foutOpened; 
     62            std::string     _cacheFileName; 
     63            std::ofstream   _fout; 
    3764        }; 
    3865     
     
    4067        { 
    4168            size_t realsize = size * nmemb; 
    42             StreamPair* sp = (StreamPair*)data; 
    43  
    44             if (sp->_stream1) sp->_stream1->write((const char*)ptr, realsize); 
    45             if (sp->_stream2) sp->_stream2->write((const char*)ptr, realsize); 
     69            StreamObject* sp = (StreamObject*)data; 
     70 
     71            sp->write((const char*)ptr, realsize); 
    4672 
    4773            return realsize; 
     
    6995 
    7096 
    71         osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamPair& sp) 
     97        osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp) 
    7298        { 
    7399            if(!proxyAddress.empty()) 
     
    255281                                osgDB::getServerAddress(fileName) + "/" +  
    256282                                osgDB::getServerFileName(fileName); 
    257             } 
    258                                                          
     283 
     284                std::string path = osgDB::getFilePath(cacheFileName); 
     285                 
     286                if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) 
     287                { 
     288                    cacheFileName.clear(); 
     289                } 
     290            } 
     291 
    259292            if (!cacheFilePath.empty() && osgDB::fileExists(cacheFileName)) 
    260293            { 
     
    286319         
    287320            std::stringstream buffer; 
    288  
    289             EasyCurl::StreamPair sp(&buffer); 
    290              
     321            ReadResult curlResult; 
     322             
     323            EasyCurl::StreamObject sp(&buffer, cacheFileName); 
     324 
    291325            //ReadResult result = _easyCurl.read(proxyAddress, fileName, sp); 
    292             ReadResult result = getEasyCurl().read(proxyAddress, fileName, sp); 
    293  
    294             if (result.status()==ReadResult::FILE_LOADED) 
    295             { 
     326            curlResult = getEasyCurl().read(proxyAddress, fileName, sp); 
     327 
     328            if (curlResult.status()==ReadResult::FILE_LOADED) 
     329            { 
     330 
    296331                osg::ref_ptr<Options> local_opt = const_cast<Options*>(options); 
    297332                if (!local_opt) local_opt = new Options; 
     
    303338                local_opt->getDatabasePathList().pop_front(); 
    304339 
    305                 if (!cacheFilePath.empty() && readResult.validObject()) 
    306                 { 
    307                     osg::notify(osg::INFO)<<"Writing cache file "<<cacheFileName<<std::endl; 
    308                      
    309                     std::string filePath = osgDB::getFilePath(cacheFileName); 
    310                     if (osgDB::fileExists(filePath) || osgDB::makeDirectory(filePath)) 
    311                     { 
    312                         switch(objectType) 
    313                         { 
    314                         case(OBJECT): osgDB::writeObjectFile( *(readResult.getObject()), cacheFileName ); break; 
    315                         case(IMAGE): osgDB::writeImageFile( *(readResult.getImage()), cacheFileName ); break; 
    316                         case(HEIGHTFIELD): osgDB::writeHeightFieldFile( *(readResult.getHeightField()), cacheFileName ); break; 
    317                         case(NODE): osgDB::writeNodeFile( *(readResult.getNode()), cacheFileName ); break; 
    318                         default: break; 
    319                         } 
    320                     } 
    321                     else 
    322                     { 
    323                         osg::notify(osg::NOTICE)<<"Error: Failed to created directory "<<filePath<<std::endl; 
    324                     } 
    325  
    326                 } 
    327  
    328340                return readResult; 
    329341            } 
    330342            else 
    331343            { 
    332                 return result; 
     344                return curlResult; 
    333345            } 
    334346        }