summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2012-03-19 19:36:50 +0000
committerrandomdan <randomdan@localhost>2012-03-19 19:36:50 +0000
commit5b79df018a82fb6bb86b52f36dff812c7c8b05d8 (patch)
tree86e4dde85ea30f2a0b69dac4b6cfb4e888401ec3
parentAdd init function to presenter because none default ones get cached between r... (diff)
downloadproject2-5b79df018a82fb6bb86b52f36dff812c7c8b05d8.tar.bz2
project2-5b79df018a82fb6bb86b52f36dff812c7c8b05d8.tar.xz
project2-5b79df018a82fb6bb86b52f36dff812c7c8b05d8.zip
Add a task to download a URL to a file
-rw-r--r--project2/url/Jamfile.jam3
-rw-r--r--project2/url/downloadToFile.cpp36
2 files changed, 37 insertions, 2 deletions
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 : : <name>curl ;
lib p2url :
- urlRows.cpp
- curlHelper.cpp
+ [ glob *.cpp ]
../../libmisc/curlsup.cpp
:
<library>../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);
+