|
|
#ifndef __GP_CURL_PROTOTYPE____
#define __GP_CURL_PROTOTYPE____
#include "core.h"
class _GP_CURL{ public:
char * memory; CURL * curl; size_t size;
private:
static size_t WriteMemoryCallback(void * ptr, size_t nsize, size_t nmemb, void * data) { size_t realsize = nsize * nmemb; _GP_CURL * me = (_GP_CURL *) data; me->memory = (char *) realloc(me->memory, me->size + realsize + 1); if (me->memory == NULL) { /* out of memory! */ return -1; }; memcpy(&(me->memory[me->size]), ptr, realsize); me->size += realsize; me->memory[me->size] = 0; return realsize; }
public:
_GP_CURL() { memory = NULL; size = 0; curl = curl_easy_init(); }
~_GP_CURL() { if (memory) { free(memory); } }
_GP_CURL(CSTR url) { CURLcode res; curl = curl_easy_init(); memory = NULL; size = 0; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) this); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); res = curl_easy_perform(curl); /* always cleanup */ curl_easy_cleanup(curl); }; }
void Query(CSTR url) { CURLcode res; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) this); // curl_easy_setopt( curl, CURLOPT_USERAGENT, "libcurl-agent/1.0" );
res = curl_easy_perform(curl); /* always cleanup */ curl_easy_cleanup(curl); }; }
size_t getSize(void) { return size; }
CSTR getResult(void) { return (CSTR) memory; }};
#endif
|