osgEarth 2.1.1
|
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 #ifndef OSGEARTH_URI 00020 #define OSGEARTH_URI 00021 00022 #include <osgEarth/Common> 00023 #include <osgEarth/FileUtils> 00024 #include <osg/Image> 00025 #include <osg/Node> 00026 #include <osgDB/ReaderWriter> 00027 #include <iostream> 00028 #include <sstream> 00029 00030 namespace osgEarth 00031 { 00032 class Config; 00033 class URI; 00034 00049 class OSGEARTH_EXPORT URIContext 00050 { 00051 public: 00053 URIContext() { } 00054 00056 URIContext( const std::string& referrer ) : _referrer(referrer) { } 00057 00059 URIContext( const URIContext& rhs ) : _referrer(rhs._referrer) { } 00060 00064 void store( osgDB::Options* options ); 00065 00067 URIContext( const osgDB::Options* options ); 00068 00070 const std::string& referrer() const { return _referrer; } 00071 00073 bool empty() const { return _referrer.empty(); } 00074 00077 URIContext add( const URIContext& subContext ) const; 00078 00080 URIContext add( const std::string& subPath ) const; 00081 00083 std::string getOSGPath( const std::string& target ) const; 00084 00085 private: 00086 friend class URI; 00087 std::string _referrer; 00088 }; 00089 00093 class OSGEARTH_EXPORT URIStream 00094 { 00095 public: 00096 URIStream( const URI& uri ); 00097 00098 virtual ~URIStream(); 00099 00100 public: 00101 // auto-cast to istream 00102 operator std::istream& (); 00103 00104 protected: 00105 friend class URI; 00106 std::istream* _fileStream; 00107 std::stringstream _bufStream; 00108 }; 00109 00114 class OSGEARTH_EXPORT URI 00115 { 00116 public: 00120 URI(); 00121 00125 URI( const std::string& location ); 00126 00130 URI( const std::string& location, const URIContext& context ); 00131 00135 URI( const char* location ); 00136 00137 public: 00138 00140 const std::string& base() const { return _baseURI; } 00141 00143 const std::string& full() const { return _fullURI; } 00144 00146 const std::string& operator * () const { return _fullURI; } 00147 00149 const URIContext& context() const { return _context; } 00150 00152 bool empty() const { return _baseURI.empty(); } 00153 00155 URI append( const std::string& suffix ) const; 00156 00157 public: 00158 00159 bool operator < ( const URI& rhs ) const { return _fullURI < rhs._fullURI; } 00160 00161 public: // convenience reader methods 00162 00164 enum ResultCode { 00165 RESULT_OK, 00166 RESULT_CANCELED, 00167 RESULT_NOT_FOUND, 00168 RESULT_SERVER_ERROR, 00169 RESULT_TIMEOUT, 00170 RESULT_NO_READER, 00171 RESULT_READER_ERROR, 00172 RESULT_UNKNOWN_ERROR 00173 }; 00174 00176 osg::Object* readObject( 00177 const osgDB::Options* options =0L, 00178 ResultCode* out_code =0L ) const; 00179 00181 osg::Image* readImage( 00182 const osgDB::Options* options =0L, 00183 ResultCode* out_code =0L ) const; 00184 00186 osg::Node* readNode( 00187 const osgDB::Options* options =0L, 00188 ResultCode* out_code =0L ) const; 00189 00191 std::string readString( 00192 ResultCode* out_code =0L ) const; 00193 00194 public: 00196 URI( const URI& rhs ) : _baseURI(rhs._baseURI), _fullURI(rhs._fullURI), _context(rhs._context) { } 00197 00198 public: 00199 //TODO: methods to load data from the URI. 00200 00201 protected: 00202 std::string _baseURI; 00203 std::string _fullURI; 00204 URIContext _context; 00205 }; 00206 } 00207 00208 #endif // OSGEARTH_URI