curl-users
Re: Question of using Libcurl
Date: Fri, 12 Jan 2001 16:54:59 +0100 (MET)
On Fri, 12 Jan 2001, Gandt, Eric wrote:
> I have been usable to get libcurl to output the data received dumped into
> a local character buffer which I can then parse I have gotten a small
> sample program to work by creating and reading external files, but before
> I can go any further I need a way to save the data received into a local
> buffer, since writing external files, is not allowable in the environment
> it wish to use the product.
You're not the first one to ask for this.
> I would also suggest adding an example that shows how to do this.
... and that is the reason why I added a section to the FAQ that shows you
exactly how it can be done:
5.2 How can I receive all data into a large memory chunk?
You are in full control of the callback function that gets called every
time there is data received from the remote server. You can make that
callback do whatever you want. You do not have to write the receivied data
to a file.
One solution to this problem could be to have a pointer to a struct that
you pass to the callback function. You set the pointer using the
curl_easy_setopt(CURLOPT_FILE) function. Then that pointer will be passed
to the callback instead of a FILE * to a file:
/* imaginary struct */
struct MemoryStruct {
char *memory;
size_t size;
};
/* imaginary callback function */
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;
}
... I've personally written code that use this approach and it worked like a
charm! :-)
BTW, this piece of code was originally brought to me by Bjorn Reese!
-- Daniel Stenberg -- curl project maintainer -- http://curl.haxx.se/Received on 2001-01-12