size_t handle_data void ptr size_t size size_t nmemb void stream int n

 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
size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream) {
int numbytes = size*nmemb;
// The data is not null-terminated, so get the last character, and replace
// it with '\0'.
char lastchar = *((char *) ptr + numbytes - 1);
*((char *) ptr + numbytes - 1) = '\0';
contents.append((char *)ptr);
contents.append(1,lastchar);
*((char *) ptr + numbytes - 1) = lastchar; // Might not be necessary.
return size*nmemb;
}
string f$curl_test (const string &s) {
CURL* curl = curl_easy_init();
// CURLcode res;
//int s_len = (int) s.size();
if(curl) {
curl_easy_setopt(curl,CURLOPT_URL, &s);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, handle_data);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
int c_len = (int) contents.size();
return c_len;
}