I am using LibCURL to pull in data from various web sites. I have made the callback function work in the following way (which works properly but has horrible performance):

Qt Code:
  1. struct MemoryStruct {
  2. char *memory;
  3. size_t size;
  4. };
  5.  
  6. static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
  7.  
  8. size_t realsize = size * nmemb;
  9. struct MemoryStruct *mem = (struct MemoryStruct *)data;
  10.  
  11. //increase the size of "memory" by the size of the bytes that we have read
  12. mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
  13.  
  14. if (mem->memory) {
  15.  
  16.  
  17. //copy to the end of the memory chunk, the piece of data that we read from curl (pointed to by ptr, size of realsize):
  18. memcpy(&(mem->memory[mem->size]), ptr, realsize);
  19.  
  20. //update the size of the memory chunk:
  21. mem->size += realsize;
  22.  
  23. //put a 0 at the end of the memory chunk
  24. mem->memory[mem->size] = 0;
  25. }
  26. return realsize;
  27. }
  28.  
  29.  
  30. void FabwareMain::curl_init() {
  31.  
  32. //init the memory to hold what we gather with CURL
  33. struct MemoryStruct chunk;
  34. chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
  35. chunk.size = 0; /* no data at this point */
  36.  
  37. //init curl:
  38. curl_global_init( CURL_GLOBAL_ALL ) ;
  39.  
  40. //create a curl handle:
  41. CURL *curl_handle = curl_easy_init() ;
  42. //the login page:
  43.  
  44.  
  45. //set curl options:
  46.  
  47. curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "");
  48.  
  49. curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); //when redirected, follow the redirections
  50. 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)");
  51. curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
  52. curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.somewhere.com");
  53. curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, "");
  54. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  55. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  56. //exec curl
  57. curl_easy_perform(curl_handle);
  58.  
  59. //convert the memory chunk to a qstring
  60.  
  61. resData = QString::fromUtf8 (chunk.memory);
  62. ui.textEdit->append (resData);
  63.  
  64. //parse the qstring
  65.  
  66.  
  67.  
  68. //clean up after ourselves:
  69. curl_easy_cleanup(curl_handle);
  70. curl_global_cleanup();
  71.  
  72. //clean up our memory chunk
  73. if(chunk.memory) {
  74. free(chunk.memory);
  75. }
  76.  
  77. }
To copy to clipboard, switch view to plain text mode 

The problem with this method, is that although it works, it consumes a ton of memory and time due to copying the data many times. First CURL copies the data into a buffer, then the callback function copies it into the data chunk, then we convert it into a Qstring, then we copy that into the text edit box.

I have found that I can modify the callback function to put the data from CURL into a QString as it comes in, and parse out what I need on the fly. I can not however figure out how to make the data that is in the callback function QString be available outside of the function. I can't return the data as CURL calls the function, so I will need to store it in a way that can be accessed from the callback function and the rest of the Qt program.

Heres the current callback function that I am modifying:

Qt Code:
  1. struct MemoryStruct {
  2. char *memory;
  3. size_t size;
  4. };
  5.  
  6. static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
  7.  
  8. size_t realsize = size * nmemb;
  9. struct MemoryStruct *mem = (struct MemoryStruct *)data;
  10.  
  11. //increase the size of "memory" by the size of the bytes that we have read
  12. mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
  13.  
  14. if (mem->memory) {
  15.  
  16. QString qmemData = QString::fromUtf8 ((char *)ptr);
  17. //parse out the stuff that I want
  18. //How do I get this QString (qmemData) to be accessable outside of this function
  19.  
  20. }
  21. return realsize;
  22. }
To copy to clipboard, switch view to plain text mode 

How do I get the QString qmemData to be accessable outside of this function? Im sure its something simple I am missing somewhere.