-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNP_CURL_Upload.h
More file actions
108 lines (66 loc) · 2.16 KB
/
NP_CURL_Upload.h
File metadata and controls
108 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
/**
NP_CURL_Upload.h
==================
NP_CURL_Upload_0
NP_CURL_Upload_1
https://curl.se/libcurl/c/curl_mime_data_cb.html
**/
struct NP_CURL_Upload_0
{
char* buffer;
size_t size;
size_t position ; // transitional to match _1
static size_t read_callback(void *buffer, size_t size, size_t nitems, void *arg) ;
};
inline size_t NP_CURL_Upload_0::read_callback(void *buffer, size_t size, size_t nitems, void *arg)
{
struct NP_CURL_Upload_0* upload = (struct NP_CURL_Upload_0 *)arg;
size_t copy_size = size * nitems;
if (copy_size > upload->size) copy_size = upload->size; // for buffered read make sure to stay in range
memcpy(buffer, upload->buffer, copy_size);
upload->buffer += copy_size; // move buffer pointer
upload->size -= copy_size; // decrease remaining size
return copy_size;
}
struct NP_CURL_Upload_1
{
char* buffer ;
curl_off_t size;
curl_off_t position ;
std::string desc() const ;
static size_t read_callback(char *buffer, size_t size, size_t nitems, void* arg) ;
};
inline std::string NP_CURL_Upload_1::desc() const
{
std::stringstream ss ;
ss << "-NP_CURL_Upload_1 size " << size << " position " << position << "\n" ;
std::string str = ss.str() ;
return str ;
}
inline size_t NP_CURL_Upload_1::read_callback(char* buffer, size_t size, size_t nitems, void* arg)
{
curl_off_t copy_sz = size*nitems ;
struct NP_CURL_Upload_1* p = (struct NP_CURL_Upload_1 *)arg;
curl_off_t sz = p->size - p->position; // remaining
if (sz > copy_sz) sz = copy_sz ; // for buffered read make sure to stay in range
if(false) std::cout
<< "[NP_CURL_Upload_1::read_callback"
<< " size " << size
<< " nitems " << nitems
<< " copy_sz " << copy_sz
<< " p.desc " << p->desc()
<< " sz " << sz
<< "\n"
;
if(sz) memcpy(buffer, p->buffer + p->position, sz);
p->position += sz ;
// not moving the buffer pointer unlike above
if(false) std::cout
<< "]NP_CURL_Upload_1::read_callback"
<< " size " << size
<< " nitems " << nitems
<< "\n"
;
return sz ;
}