cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: terminating null in buffers for Read/Write callbacks/calls etc

From: Frank Mcgeough <fmcgeough_at_mac.com>
Date: Fri, 05 Feb 2010 15:55:53 -0500

On Feb 5, 2010, at 12:57 PM, Michael Wood wrote:

> On 5 February 2010 15:25, vick <vijayx_at_gmail.com> wrote:
>> Can we assume that a terminating null will be added to data bytes
>> downloaded. i.e, libcurl will either add it or check if its there,
>> and if
>> not, add it?
>> Or is it entirely left to the user/website?
>
> No, I don't see how that would work. Otherwise how could you download
> binary files containing '\0' bytes?
>

Right. You definitely don't want that. If you know that you're getting
a string back then you just use the callbacks. If you are using C++
you could use a std::string.

size_t writeDataToString(void *buffer, size_t size, size_t nmemb, void
*userp)
{
     char *d = (char*)buffer;
     string *b = (std::string *)(userp);

     int result = size * nmemb;
     if (b != NULL && result > 0)
     {
         b->append(d, result);
     }
     return result;
}

and set that function up with CURLOPT_WRITEFUNCTION and set your
std::string using CURLOPT_WRITEDATA. But, in any case, this is left to
the user of libcurl to provide.

Good luck.

> --
> Michael Wood <esiotrot_at_gmail.com>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-02-05