diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-10-09 02:22:28 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-10-09 02:22:28 +0100 |
commit | eae58c6f56a90ef5c464a22e77eff71863716a75 (patch) | |
tree | 71e9841f3e2c081cbc581c1bd880e3f1d4efbb78 | |
parent | Remove unnecessary wrappers, adhocutil provides suitable ones (diff) | |
download | project2-1.2.1.tar.bz2 project2-1.2.1.tar.xz project2-1.2.1.zip |
Get rid of class Curl for being a) used wrong and b) just adding complicationproject2-1.2.1
-rw-r--r-- | project2/url/curlHelper.cpp | 49 | ||||
-rw-r--r-- | project2/url/curlHelper.h | 21 | ||||
-rw-r--r-- | project2/url/downloadToFile.cpp | 6 | ||||
-rw-r--r-- | project2/url/urlStream.cpp | 13 |
4 files changed, 18 insertions, 71 deletions
diff --git a/project2/url/curlHelper.cpp b/project2/url/curlHelper.cpp index d6ca64b..bd331e8 100644 --- a/project2/url/curlHelper.cpp +++ b/project2/url/curlHelper.cpp @@ -22,31 +22,14 @@ CurlHelper::CurlHelper() { } -Curl::Curl(const std::string & url) : - CurlHandle(url) -{ -} - CurlHelper::~CurlHelper() { } -size_t -Curl::curlReadHelperHandle(const char * ptr, size_t size, size_t nmemb, void *stream) -{ - return (*static_cast<const Curl::ReadHandler *>(stream))(ptr, size * nmemb); -} - -size_t -Curl::curlSendHelperHandle(char * ptr, size_t size, size_t nmemb, void *stream) -{ - return (*static_cast<const Curl::SendHandler *>(stream))(ptr, size * nmemb); -} - -CurlPtr +AdHoc::Net::CurlHandlePtr CurlHelper::newCurl(ExecContext * ec) const { - CurlPtr c = new Curl(getUrl(ec)); + AdHoc::Net::CurlHandlePtr c = new AdHoc::Net::CurlHandle(getUrl(ec)); setCurlOpts(c.get(), ec); return c; } @@ -71,34 +54,6 @@ VariableCurlHelper::setCurlOpts(AdHoc::Net::CurlHandle * c, ExecContext * ec) co c->setopt<long>(CURLOPT_TIMEOUT_MS, timeout(ec)); } -void -Curl::performRead(const ReadHandler & h) -{ - setReadHandler(h); - CurlHandle::perform(); -} - -void -Curl::setReadHandler(const ReadHandler & h) -{ - setopt(CURLOPT_WRITEDATA, &h); - setopt(CURLOPT_WRITEFUNCTION, &curlReadHelperHandle); -} - -void -Curl::performSend(const SendHandler & h) -{ - setSendHandler(h); - CurlHandle::perform(); -} - -void -Curl::setSendHandler(const SendHandler & h) -{ - setopt(CURLOPT_READDATA, &h); - setopt(CURLOPT_READFUNCTION, &curlSendHelperHandle); -} - std::string VariableCurlHelper::getUrl(ExecContext * ec) const { diff --git a/project2/url/curlHelper.h b/project2/url/curlHelper.h index ad73a31..d56173c 100644 --- a/project2/url/curlHelper.h +++ b/project2/url/curlHelper.h @@ -4,32 +4,13 @@ #include "variables.h" #include <curlHandle.h> -class Curl : public AdHoc::Net::CurlHandle { - public: - Curl(const std::string & url); - virtual ~Curl() { } - - typedef boost::function2<size_t, const char *, size_t> ReadHandler; - typedef boost::function2<size_t, char *, size_t> SendHandler; - - void performRead(const ReadHandler &); - void setReadHandler(const ReadHandler &); - void performSend(const SendHandler &); - void setSendHandler(const SendHandler &); - - private: - static size_t curlReadHelperHandle(const char * ptr, size_t size, size_t nmemb, void *stream); - static size_t curlSendHelperHandle(char * ptr, size_t size, size_t nmemb, void *stream); -}; -typedef boost::intrusive_ptr<Curl> CurlPtr; - /// Project2 helper component to provide common access to remote resources via libcurl class CurlHelper { public: CurlHelper(); virtual ~CurlHelper(); - CurlPtr newCurl(ExecContext *) const; + AdHoc::Net::CurlHandlePtr newCurl(ExecContext *) const; virtual void setCurlOpts(AdHoc::Net::CurlHandle *, ExecContext *) const; protected: diff --git a/project2/url/downloadToFile.cpp b/project2/url/downloadToFile.cpp index 3b90ffc..1d73cce 100644 --- a/project2/url/downloadToFile.cpp +++ b/project2/url/downloadToFile.cpp @@ -17,13 +17,15 @@ class Download : public Task, VariableCurlHelper { void execute(ExecContext * ec) const { - CurlPtr c = newCurl(ec); FILE * file = fopen(destination(ec), "w"); if (!file) { throw syscall_error(errno); } AdHoc::ScopeExit tidy([=]{ fclose(file); }); - c->performRead([=](const char * data, size_t len) -> size_t { return fwrite(data, 1, len, file); }); + auto c = newCurl(ec); + c->setopt(CURLOPT_READDATA, file); + c->setopt(CURLOPT_READFUNCTION, &fwrite); + c->perform(); } private: diff --git a/project2/url/urlStream.cpp b/project2/url/urlStream.cpp index 21bf5c2..1c681c7 100644 --- a/project2/url/urlStream.cpp +++ b/project2/url/urlStream.cpp @@ -14,8 +14,17 @@ class UrlStream : public Stream, VariableCurlHelper { void runStream(const Sink & sink, ExecContext * ec) const { - CurlPtr c = newCurl(ec); - c->performRead(sink); + auto c = newCurl(ec); + c->setopt(CURLOPT_READDATA, &sink); + c->setopt(CURLOPT_READFUNCTION, &readToSink); + c->perform(); + } + + static size_t readToSink(const void *ptr, size_t size, size_t nmemb, void * sink) + { + auto s = static_cast<const Sink *>(sink); + auto d = static_cast<const char *>(ptr); + return (*s)(d, size * nmemb); } }; |