LibPkg
download.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 // Copyright � 2003-2005 Graham Shaw.
3 // Distribution and use are subject to the GNU Lesser General Public License,
4 // a copy of which may be found in the file !LibPkg.Copyright.
5 
6 #ifndef LIBPKG_DOWNLOAD
7 #define LIBPKG_DOWNLOAD
8 
9 #include <string>
10 #include <fstream>
11 
12 #include "curl/curl.h"
13 
14 // Define the following to add extra logging of the download stage
15 // #define LOG_DOWNLOAD
16 
17 #ifdef LOG_DOWNLOAD
18 #include "libpkg/log.h"
19 #endif
20 
21 namespace pkg {
22 
23 using std::string;
24 
26 class download
27 {
28 public:
30  typedef unsigned long long size_type;
31 
33  static const size_type npos=static_cast<size_type>(-1);
34 
35  // An enumeration for describing the state of the download. */
37  {
45  };
46 
47 private:
49  state_type _state;
50 
52  CURL* _ceasy;
53 
55  CURLcode _result;
56 
57 
59  char* _error_buffer;
60 
62  string _url;
63 
65  std::ofstream _out;
66 
68  size_type _bytes_done;
69 
71  size_type _bytes_total;
72 
73  #ifdef LOG_DOWNLOAD
74 
75  log *_log;
76  #endif
77 
78 public:
83 download(const string& url,const string& pathname);
84 
86  ~download();
87 
91  state_type state() const
92  { return _state; }
93 
97  CURLcode result() const
98  { return _result; }
99 
103  string message() const
104  { return _error_buffer; }
105 
109  size_type bytes_done()
110  { return _bytes_done; }
111 
115  size_type bytes_total()
116  { return _bytes_total; }
117 
124  size_t write_callback(char* buffer,size_t size,size_t nitems);
125 
131  int progress_callback(double dltotal,double dlnow);
132 
136  void message_callback(CURLMsg* msg);
137 
138 #ifdef LOG_DOWNLOAD
139 
141  int debug_callback(curl_infotype type,char *data, size_t size);
145  void log_to(log *use_log);
146 #endif
147 
148 private:
155  static CURLM* _cmulti;
156 
159  static unsigned int _cmulti_refcount;
160 public:
162  static void poll_all();
163 };
164 
165 }; /* namespace pkg */
166 
167 #endif
string message() const
Get libcurl error message.
Definition: download.h:103
The state in which the download has been successfully completed.
Definition: download.h:42
The state in which the download is in progress.
Definition: download.h:39
The namespace used to hold the package management library.
Definition: auto_dir.cc:12
static const size_type npos
A null value for use in place of a byte count.
Definition: download.h:33
size_type bytes_done()
Get number of bytes downloaded.
Definition: download.h:109
~download()
Destroy download action.
Definition: download.cc:109
static void poll_all()
Poll all download operations.
Definition: download.cc:193
int progress_callback(double dltotal, double dlnow)
Handler for CURLOPT_PROGRESSFUNCTION callbacks.
Definition: download.cc:132
download(const string &url, const string &pathname)
Construct download action.
Definition: download.cc:53
unsigned long long size_type
A type for representing byte counts.
Definition: download.h:30
Class to log actions that occur in LibPkg.
Definition: log.h:215
void message_callback(CURLMsg *msg)
Handler for Curl messages.
Definition: download.cc:140
size_t write_callback(char *buffer, size_t size, size_t nitems)
Handler for CURLOPT_WRITEFUNCTION callbacks.
Definition: download.cc:126
A class for downloading a file from a URL.
Definition: download.h:26
state_type state() const
Get current state of the download.
Definition: download.h:91
state_type
Definition: download.h:36
The state in which the download has failed.
Definition: download.h:44
size_type bytes_total()
Get total number of bytes to download.
Definition: download.h:115
CURLcode result() const
Get libcurl result code.
Definition: download.h:97

Reference Manual LibPkg Version 0.6.1 (28 Jan 2015)