nlib
CurlInputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_CURLINPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_CURLINPUTSTREAM_H_
5 
6 #include "nn/nlib/Platform.h"
7 #ifdef NLIB_HAS_LIBCURL
8 
9 #ifndef CAFE
10 #include <curl/curl.h> // NOLINT
11 #else
12 #include <cafe/curl/curl.h> // NOLINT
13 #endif
14 #include "nn/nlib/InputStream.h"
15 
16 NLIB_NAMESPACE_BEGIN
17 
19  public:
20  enum BufferSize {
21  BUFFER_SIZE_1X = 0,
22  BUFFER_SIZE_2X,
23  BUFFER_SIZE_3X,
24  BUFFER_SIZE_4X
25  };
26 
27  public:
28  CurlInputStream() NLIB_NOEXCEPT : prv_(NULL) {}
30  CURL* GetEasyHandle() const NLIB_NOEXCEPT;
31  CURLcode GetCurlError() const NLIB_NOEXCEPT;
32  CURLMcode GetCurlMultiError() const NLIB_NOEXCEPT;
33  NLIB_CHECK_RESULT errno_t Init(BufferSize buffer_size) NLIB_NOEXCEPT;
34  NLIB_CHECK_RESULT errno_t Init() NLIB_NOEXCEPT { return Init(BUFFER_SIZE_1X); }
35  CURLcode SetUrl(const char* url) NLIB_NOEXCEPT;
36  bool StartDownload() NLIB_NOEXCEPT;
37 
38  private:
39  NLIB_VIS_HIDDEN static size_t ParseStreamCallback(char* contents, size_t length,
40  size_t nmemb, void *userp) NLIB_NOEXCEPT;
41  virtual size_t FillBuffer_(void* p, size_t nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
42  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
43  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
44  struct CallbackInfo {
45  unsigned char* buf;
46  size_t pos;
47  size_t bufsize;
48  };
49 
50  private:
51  struct CurlInputStreamPrivate;
52  CurlInputStreamPrivate* prv_;
53 };
54 
55 NLIB_NAMESPACE_END
56 
57 #endif // NLIB_HAS_LIBCURL
58 #endif // INCLUDE_NN_NLIB_CURLINPUTSTREAM_H_
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:210
Defines the base class for input streams.
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
Definition: Platform_unix.h:74
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
C-based declaration of the basic API.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:16
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
NLIB_CHECK_RESULT errno_t Init() noexcept
Executes Init(BUFFER_SIZE_1X).
The stream class for downloading using libcurl.
CurlInputStream() noexcept
Instantiates the object with default parameters (default constructor). The stream must be initialized...
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24