struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
//increase the size of "memory" by the size of the bytes that we have read
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
//copy to the end of the memory chunk, the piece of data that we read from curl (pointed to by ptr, size of realsize):
memcpy(&(mem->memory[mem->size]), ptr, realsize);
//update the size of the memory chunk:
mem->size += realsize;
//put a 0 at the end of the memory chunk
mem->memory[mem->size] = 0;
}
return realsize;
}
void FabwareMain::curl_init() {
//init the memory to hold what we gather with CURL
struct MemoryStruct chunk;
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
//init curl:
curl_global_init( CURL_GLOBAL_ALL ) ;
//create a curl handle:
CURL *curl_handle = curl_easy_init() ;
//the login page:
//set curl options:
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); //when redirected, follow the redirections
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20060601 Firefox/2.0.0.3 (Ubuntu-edgy)");
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.somewhere.com");
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, "");
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
//exec curl
curl_easy_perform(curl_handle);
//convert the memory chunk to a qstring
resData
= QString::fromUtf8 (chunk.
memory);
ui.textEdit->append (resData);
//parse the qstring
//clean up after ourselves:
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
//clean up our memory chunk
if(chunk.memory) {
free(chunk.memory);
}
}
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
//increase the size of "memory" by the size of the bytes that we have read
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
//copy to the end of the memory chunk, the piece of data that we read from curl (pointed to by ptr, size of realsize):
memcpy(&(mem->memory[mem->size]), ptr, realsize);
//update the size of the memory chunk:
mem->size += realsize;
//put a 0 at the end of the memory chunk
mem->memory[mem->size] = 0;
}
return realsize;
}
void FabwareMain::curl_init() {
//init the memory to hold what we gather with CURL
struct MemoryStruct chunk;
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
//init curl:
curl_global_init( CURL_GLOBAL_ALL ) ;
//create a curl handle:
CURL *curl_handle = curl_easy_init() ;
//the login page:
//set curl options:
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); //when redirected, follow the redirections
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20060601 Firefox/2.0.0.3 (Ubuntu-edgy)");
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.somewhere.com");
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, "");
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
//exec curl
curl_easy_perform(curl_handle);
//convert the memory chunk to a qstring
resData = QString::fromUtf8 (chunk.memory);
ui.textEdit->append (resData);
//parse the qstring
//clean up after ourselves:
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
//clean up our memory chunk
if(chunk.memory) {
free(chunk.memory);
}
}
To copy to clipboard, switch view to plain text mode
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
//increase the size of "memory" by the size of the bytes that we have read
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
//parse out the stuff that I want
//How do I get this QString (qmemData) to be accessable outside of this function
}
return realsize;
}
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
//increase the size of "memory" by the size of the bytes that we have read
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
QString qmemData = QString::fromUtf8 ((char *)ptr);
//parse out the stuff that I want
//How do I get this QString (qmemData) to be accessable outside of this function
}
return realsize;
}
To copy to clipboard, switch view to plain text mode
Bookmarks