Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
URLSessionDownloadTask.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <curl/curl.h>
26
28
29#define _Class _URLSessionDownloadTask
30
31#pragma mark - URLSessionTask
32
33#define CURL_WRITEFUNC_ABORT 0
34
38static size_t writeFunction(char *data, size_t size, size_t count, ident self) {
39
41
42 const size_t bytesWritten = fwrite(data, size, count, this->file);
43 this->urlSessionTask.bytesReceived += bytesWritten;
44
45 return bytesWritten;
46}
47
51static void setup(URLSessionTask *self) {
52
54
56
57 assert(this->file);
58
59 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEFUNCTION, writeFunction);
60 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEDATA, self);
61}
62
63#pragma mark - Class lifecycle
64
68static void initialize(Class *clazz) {
69
70 ((URLSessionTaskInterface *) clazz->interface)->setup = setup;
71}
72
78 static Class *clazz;
79 static Once once;
80
81 do_once(&once, {
82 clazz = _initialize(&(const ClassDef) {
83 .name = "URLSessionDownloadTask",
84 .superclass = _URLSessionTask(),
85 .instanceSize = sizeof(URLSessionDownloadTask),
86 .interfaceOffset = offsetof(URLSessionDownloadTask, interface),
87 .interfaceSize = sizeof(URLSessionDownloadTaskInterface),
89 });
90 });
91
92 return clazz;
93}
94
95#undef _Class
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define super(type, obj, method,...)
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
static size_t writeFunction(char *data, size_t size, size_t count, ident self)
The CURLOPT_WRITEFUNCTION callback.
static void initialize(Class *clazz)
Use download tasks to save remote resources to file.
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
ident interface
The interface of the Class.
Definition: Class.h:105
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
Use download tasks to save remote resources to file.
Class * _URLSessionDownloadTask(void)
The URLSessionDownloadTask archetype.
URL session tasks are handles to pending URL operations.
ident handle
The backing libcurl handle.
void setup(URLSessionTask *)
Sets up this task.
Class * _URLSessionTask(void)
The URLSessionTask archetype.