cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: getting results back into a dynamically allocated buffer...

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Wed, 22 Nov 2000 07:47:17 +0100 (MET)

On Tue, 21 Nov 2000, Michael Mealling wrote:

> This may be a FAQ but here goes:

I think it is about time I add this to the FAQ.

> I'm posting some XML to a server and expecting some XML back. Setting
> CURLOPT_WRITEFUNCTION to a function that fills in a buffer that I set by
> fudging CURLOPT_FILE by passing it a (char *) instead of a (FILE *) is
> kind of obvious. What I'm trying to do is figure out a way to keep track
> of how big the buffer is so I can realloc it to take care of large
> results from the server. I'm sure that, with all of the XML stuff going
> on these days, this would almost be a standard problem...

This is a suggested solution on how it could be done:

struct MemoryStruct {
  char *memory;
  size_t size;
};

size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
  register int realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)data;

  mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
  if (mem->memory) {
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
  }
  return realsize;
}

-- 
  Daniel Stenberg -- curl project maintainer -- http://curl.haxx.se/
_______________________________________________
Curl-library mailing list
Curl-library_at_lists.sourceforge.net
http://lists.sourceforge.net/mailman/listinfo/curl-library
Received on 2000-11-22