00001 /* 00002 Copyright (c) 2010, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 #pragma once 00024 00025 #include "cinder/Cinder.h" 00026 #include "cinder/Stream.h" 00027 #include <iostream> 00028 00029 namespace cinder { 00030 00031 class Url { 00032 public: 00033 Url() {} 00035 explicit Url( const std::string &urlString, bool isEscaped = false ); 00036 00038 std::string str() const { return mStr; } 00040 const char* c_str() const { return mStr.c_str(); } 00041 00043 static std::string encode( const std::string &unescaped ); 00044 00045 private: 00046 std::string mStr; 00047 }; 00048 00049 inline std::ostream& operator<<( std::ostream &out, const Url &url ) 00050 { 00051 out << url.str(); 00052 return out; 00053 } 00054 00056 // This is an abstract base class for implementing IStreamUrl 00057 class IStreamUrlImpl { 00058 protected: 00059 IStreamUrlImpl( const std::string &user, const std::string &password ) 00060 : mUser( user ), mPassword( password ) {} 00061 00062 public: 00063 virtual ~IStreamUrlImpl() {} 00064 00065 std::string getUser() const { return mUser; } 00066 std::string getPassword() const { return mPassword; } 00067 00068 virtual size_t readDataAvailable( void *dest, size_t maxSize ) = 0; 00069 virtual void seekAbsolute( off_t absoluteOffset ) = 0; 00070 virtual void seekRelative( off_t relativeOffset ) = 0; 00071 virtual off_t tell() const = 0; 00072 virtual off_t size() const = 0; 00073 00074 virtual bool isEof() const = 0; 00075 virtual void IORead( void *t, size_t size ) = 0; 00076 00077 protected: 00078 const std::string mUser, mPassword; 00079 }; 00081 00083 typedef shared_ptr<class IStreamUrl> IStreamUrlRef; 00084 00086 class IStreamUrl : public IStream { 00087 public: 00089 static IStreamUrlRef createRef( const std::string &url, const std::string &user = "", const std::string &password = "" ); 00090 00091 virtual size_t readDataAvailable( void *dest, size_t maxSize ) { return mImpl->readDataAvailable( dest, maxSize ); } 00092 virtual void seekAbsolute( off_t absoluteOffset ) { return mImpl->seekAbsolute( absoluteOffset ); } 00093 virtual void seekRelative( off_t relativeOffset ) { return mImpl->seekRelative( relativeOffset ); } 00094 virtual off_t tell() const { return mImpl->tell(); } 00095 virtual off_t size() const { return mImpl->size(); } 00096 00097 virtual bool isEof() const { return mImpl->isEof(); } 00098 00099 std::string getUser() const { return mImpl->getUser(); } 00100 std::string getPassword() const { return mImpl->getPassword(); } 00101 00102 protected: 00103 IStreamUrl( const std::string &url, const std::string &user, const std::string &password ); 00104 00105 virtual void IORead( void *t, size_t size ) { mImpl->IORead( t, size ); } 00107 virtual void IOWrite( const void *t, size_t size ) { throw std::exception(); } 00108 00109 shared_ptr<IStreamUrlImpl> mImpl; 00110 }; 00111 00112 IStreamUrlRef loadUrlStream( const Url &url ); 00113 IStreamUrlRef loadUrlStream( const std::string &url, const std::string &user = "", const std::string &password = "" ); 00114 00115 } // namespace cinder