From a061e4b3a3d045c2bcccc0a5e07e9762eaddb0b0 Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 19 Mar 2012 19:36:50 +0000 Subject: Add a task to download a URL to a file --- project2/url/Jamfile.jam | 3 +-- project2/url/downloadToFile.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 project2/url/downloadToFile.cpp diff --git a/project2/url/Jamfile.jam b/project2/url/Jamfile.jam index 1710e17..1f48e2a 100644 --- a/project2/url/Jamfile.jam +++ b/project2/url/Jamfile.jam @@ -5,8 +5,7 @@ alias glibmm : : : : lib curl : : curl ; lib p2url : - urlRows.cpp - curlHelper.cpp + [ glob *.cpp ] ../../libmisc/curlsup.cpp : ../common//p2common diff --git a/project2/url/downloadToFile.cpp b/project2/url/downloadToFile.cpp new file mode 100644 index 0000000..dcd9710 --- /dev/null +++ b/project2/url/downloadToFile.cpp @@ -0,0 +1,36 @@ +#include "curlHelper.h" +#include "task.h" +#include "scopeObject.h" +#include "scriptLoader.h" +#include "exceptions.h" +#include "../libmisc/curlsup.h" + +/// Project2 component to create a row set from the contents of a file accessible via libcurl +class Download : public Task, VariableCurlHelper { + public: + Download(ScriptNodePtr p) : + SourceObject(p), + Task(p), + VariableCurlHelper(p), + destination(p, "destination") + { + } + + void execute() const + { + CurlPtr c = newCurl(); + FILE * file = fopen(destination(), "w"); + if (!file) { + throw syscall_error(errno); + } + ScopeObject tidy([=]{ fclose(file); }); + c->performRead([=](const char * data, size_t len) -> size_t { return fwrite(data, 1, len, file); }); + } + + private: + + const Variable destination; +}; + +DECLARE_LOADER("download", Download); + -- cgit v1.2.3