Cinder

  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

include/cinder/Url.h

Go to the documentation of this file.
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 
00055 inline std::istream& operator>>( std::istream &is, Url &url )
00056 {
00057     std::string temp;
00058     is >> temp;
00059     url = Url( temp );
00060     return is;
00061 }
00062 
00064 // This is an abstract base class for implementing IStreamUrl
00065 class IStreamUrlImpl {
00066   protected:
00067     IStreamUrlImpl( const std::string &user, const std::string &password )
00068         : mUser( user ), mPassword( password ) {}
00069 
00070   public:
00071     virtual ~IStreamUrlImpl() {}
00072   
00073     std::string         getUser() const { return mUser; }
00074     std::string         getPassword() const { return mPassword; }
00075 
00076     virtual size_t      readDataAvailable( void *dest, size_t maxSize ) = 0;
00077     virtual void        seekAbsolute( off_t absoluteOffset ) = 0;
00078     virtual void        seekRelative( off_t relativeOffset ) = 0;
00079     virtual off_t       tell() const = 0;
00080     virtual off_t       size() const = 0;
00081     
00082     virtual bool        isEof() const = 0;
00083     virtual void        IORead( void *t, size_t size ) = 0;
00084     
00085   protected:
00086     const std::string       mUser, mPassword;
00087 };
00089 
00091 typedef std::shared_ptr<class IStreamUrl>   IStreamUrlRef;
00092 
00094 class IStreamUrl : public IStream {
00095   public:
00097     static IStreamUrlRef        create( const Url &url, const std::string &user = "", const std::string &password = "" );
00098     
00099     virtual size_t      readDataAvailable( void *dest, size_t maxSize ) { return mImpl->readDataAvailable( dest, maxSize ); }
00100     virtual void        seekAbsolute( off_t absoluteOffset ) { return mImpl->seekAbsolute( absoluteOffset ); }
00101     virtual void        seekRelative( off_t relativeOffset ) { return mImpl->seekRelative( relativeOffset ); }
00102     virtual off_t       tell() const { return mImpl->tell(); }
00103     virtual off_t       size() const { return mImpl->size(); }
00104     
00105     virtual bool        isEof() const { return mImpl->isEof(); }
00106 
00107     std::string         getUser() const { return mImpl->getUser(); }
00108     std::string         getPassword() const { return mImpl->getPassword(); }
00109 
00110   protected:
00111     IStreamUrl( const std::string &url, const std::string &user, const std::string &password );
00112 
00113     virtual void        IORead( void *t, size_t size ) { mImpl->IORead( t, size ); }
00115     virtual void        IOWrite( const void *t, size_t size ) { throw std::exception(); }
00116     
00117     std::shared_ptr<IStreamUrlImpl> mImpl;
00118 };
00119 
00120 IStreamUrlRef       loadUrlStream( const Url &url );
00121 IStreamUrlRef       loadUrlStream( const std::string &url, const std::string &user = "", const std::string &password = "" );
00122 
00123 } // namespace cinder