osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/URI.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 #include <osgEarth/URI>
00020 #include <osgEarth/HTTPClient>
00021 #include <osgEarth/Registry>
00022 #include <osgDB/FileNameUtils>
00023 #include <osgDB/ReadFile>
00024 #include <osgDB/ReaderWriter>
00025 #include <fstream>
00026 #include <sstream>
00027 
00028 #define LC "[URI] "
00029 
00030 using namespace osgEarth;
00031 
00032 //------------------------------------------------------------------------
00033 
00034 namespace
00035 {
00041     const osgDB::Options*
00042     fixOptions( const osgDB::Options* input )
00043     {
00044         if ( input && input->getObjectCacheHint() == osgDB::Options::CACHE_NONE )
00045         {
00046             return input;
00047         }
00048         else
00049         {
00050             return Registry::instance()->cloneOrCreateOptions( input );
00051         }
00052     }
00053 }
00054 
00055 //------------------------------------------------------------------------
00056 
00057 URIStream::URIStream( const URI& uri ) :
00058 _fileStream( 0L )
00059 {
00060     if ( osgDB::containsServerAddress(uri.full()) )
00061     {
00062         HTTPResponse res = HTTPClient::get( uri.full() );
00063         if ( res.isOK() )
00064         {
00065             std::string buf = res.getPartAsString(0);
00066             _bufStream.str(buf);
00067         }
00068     }
00069     else
00070     {
00071         _fileStream = new std::ifstream( uri.full().c_str() );
00072     }
00073 }
00074 
00075 URIStream::~URIStream()
00076 {
00077     if ( _fileStream )
00078         delete _fileStream;
00079 }
00080 
00081 URIStream::operator std::istream& ()
00082 {
00083     static std::istringstream s_nullStream;
00084 
00085     if ( _fileStream )
00086         return *_fileStream;
00087     else
00088         return _bufStream;
00089 }
00090 
00091 //------------------------------------------------------------------------
00092 
00093 std::string
00094 URIContext::getOSGPath( const std::string& target ) const
00095 {
00096     return osgEarth::getFullPath( _referrer, target );
00097 }
00098 
00099 URIContext
00100 URIContext::add( const URIContext& sub ) const
00101 {
00102     return URIContext( osgEarth::getFullPath(_referrer, sub._referrer) );
00103 }
00104 
00105 URIContext
00106 URIContext::add( const std::string& sub ) const
00107 {
00108     return URIContext(osgDB::concatPaths( _referrer, sub ));
00109 }
00110 
00111 void
00112 URIContext::store( osgDB::Options* options )
00113 {
00114     if ( options )
00115     {
00116         options->setDatabasePath( _referrer );
00117         options->setPluginStringData( "osgEarth::URIContext::referrer", _referrer );
00118     }
00119 }
00120 
00121 URIContext::URIContext( const osgDB::Options* options )
00122 {
00123     if ( options )
00124     {
00125         _referrer = options->getPluginStringData( "osgEarth::URIContext::referrer" );
00126     }
00127 }
00128 
00129 //------------------------------------------------------------------------
00130 
00131 URI::URI()
00132 {
00133     //nop
00134 }
00135 
00136 URI::URI( const std::string& location )
00137 {
00138     _baseURI = location;
00139     _fullURI = location;
00140 }
00141 
00142 URI::URI( const std::string& location, const URIContext& context )
00143 {
00144     _context = context;
00145     _baseURI = location;
00146     _fullURI = context.getOSGPath( _baseURI );
00147 }
00148 
00149 URI::URI( const char* location )
00150 {
00151     _baseURI = std::string(location);
00152     _fullURI = _baseURI;
00153 }
00154 
00155 URI
00156 URI::append( const std::string& suffix ) const
00157 {
00158     URI result;
00159     result._baseURI = _baseURI + suffix;
00160     result._fullURI = _fullURI + suffix;
00161     result._context = _context;
00162     return result;
00163 }
00164 
00165 osg::Object*
00166 URI::readObject( const osgDB::Options* options, ResultCode* out_code ) const
00167 {
00168     if ( empty() ) {
00169         if ( out_code ) *out_code = RESULT_NOT_FOUND;
00170         return 0L;
00171     }
00172 
00173     osg::ref_ptr<const osgDB::Options> myOptions = fixOptions(options);
00174 
00175     osg::ref_ptr<osg::Object> object;
00176     ResultCode result = (ResultCode)HTTPClient::readObjectFile( _fullURI, object, myOptions.get() );
00177     if ( out_code ) *out_code = result;
00178 
00179     return object.release();
00180 }
00181 
00182 osg::Image*
00183 URI::readImage( const osgDB::Options* options, ResultCode* out_code ) const
00184 {
00185     if ( empty() ) {
00186         if ( out_code ) *out_code = RESULT_NOT_FOUND;
00187         return 0L;
00188     }
00189 
00190     osg::ref_ptr<const osgDB::Options> myOptions = fixOptions(options);
00191 
00192     //OE_INFO << LC << "readImage: " << _fullURI << std::endl;
00193 
00194     osg::ref_ptr<osg::Image> image;
00195     ResultCode result = (ResultCode)HTTPClient::readImageFile( _fullURI, image, myOptions.get() );
00196     if ( out_code ) *out_code = result;
00197 
00198     return image.release();
00199 }
00200 
00201 osg::Node*
00202 URI::readNode( const osgDB::Options* options, ResultCode* out_code ) const
00203 {
00204     if ( empty() ) {
00205         if ( out_code ) *out_code = RESULT_NOT_FOUND;
00206         return 0L;
00207     }
00208 
00209     osg::ref_ptr<const osgDB::Options> myOptions = fixOptions(options);
00210 
00211     osg::ref_ptr<osg::Node> node;
00212     ResultCode result = (ResultCode)HTTPClient::readNodeFile( _fullURI, node, myOptions.get() );
00213     if ( out_code ) *out_code = result;
00214     return node.release();
00215 }
00216 
00217 std::string
00218 URI::readString( ResultCode* out_code ) const
00219 {
00220     if ( empty() ) {
00221         if ( out_code ) *out_code = RESULT_NOT_FOUND;
00222         return 0L;
00223     }
00224 
00225     std::string str;
00226     ResultCode result = (ResultCode)HTTPClient::readString( _fullURI, str );
00227     if ( out_code ) *out_code = result;
00228     return str;
00229 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines