# Cinder

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- 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
    00028 typedef void CURL;
    00029 typedef void CURLM;
    00030
    00031 namespace cinder {
    00032
    00033 class Url {
    00034  public:
    00035     Url() {}
    00036     explicit Url( const std::string &urlString );
    00037
    00038     std::string str() const { return mStr; }
    00039     const char* c_str() const { return mStr.c_str(); }
    00040
    00041  private:
    00042     std::string     mStr;
    00043 };
    00044
    00046 typedef shared_ptr<class IStreamUrl>    IStreamUrlRef;
    00047
    00049 class IStreamUrl : public IStream {
    00050   public:
    00052     static IStreamUrlRef    createRef( const std::string &url, const std::string &user, const std::string &password );
    00053     ~IStreamUrl();
    00054
    00055     virtual size_t      readDataAvailable( void *dest, size_t maxSize );
    00056     virtual void        seekAbsolute( off_t absoluteOffset );
    00057     virtual void        seekRelative( off_t relativeOffset );
    00058     virtual off_t       tell() const { return mBufferFileOffset + mBufferOffset; }
    00059     virtual off_t       size() const;
    00060
    00061     virtual bool        isEof() const;
    00062
    00063     std::string         getUser() const { return mUser; }
    00064     std::string         getPassword() const { return mPassword; }
    00065     long                getResponseCode() const;
    00066     std::string         getEffectiveUrl() const;
    00067
    00068
    00069   protected:
    00070     IStreamUrl( const std::string &url, const std::string &user, const std::string &password );
    00071
    00072     virtual void        IORead( void *t, size_t size );
    00073     virtual void        IOWrite( const void *t, size_t size ) {}
    00074
    00075     int                 bufferRemaining() const { return mBufferedBytes - mBufferOffset; }
    00076     void                fillBuffer( int wantBytes ) const;
    00077
    00078     static size_t       writeCallback( char *buffer, size_t size, size_t nitems, void *userp );
    00079
    00080   private:
    00081     CURL                    *mCurl;
    00082     CURLM                   *mMulti;
    00083     const std::string       mUser, mPassword;
    00084     std::string             mUserColonPassword;
    00085
    00086     mutable int still_running;              // Is background url fetch still in progress
    00087     mutable bool            mStartedRead;
    00088
    00089     mutable off_t           mSize;
    00090     mutable bool            mSizeCached;
    00091     mutable long            mResponseCode;
    00092     mutable char            *mEffectiveUrl;
    00093
    00094     mutable uint8_t     *mBuffer;
    00095     mutable int         mBufferSize;
    00096     mutable int         mBufferOffset, mBufferedBytes;
    00097     mutable off_t       mBufferFileOffset;  // where in the file the buffer starts
    00098     static const int    DEFAULT_BUFFER_SIZE = 4096;
    00099 };
    00100
    00101 IStreamUrlRef       loadUrlStream( const Url &url );
    00102 IStreamUrlRef       loadUrlStream( const std::string &url, const std::string &user = "", const std::string &password = "" );
    00103
    00104 } // namespace cinder