cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Writing data to a char buffer instead of a file

From: Tim Tassonis <timtas_at_cubic.ch>
Date: Wed, 14 May 2008 18:15:50 +0200

Hi Yuriy

Yuriy Sosov wrote:
> - After the Daniel's response, I gave it another thought and I now I
> think it is not worth adding it as a feature if this functionality was
> not intended to be there by design.

I still think it would be good, as the problem/solution is really a very
  generic one, the necessary code is small and it will save users of the
library some code, which might contain errors.

For example, the callback I've written before was buggy for a long time,
  since realloc might change the pointer address. It only mattered when
realloc did that, so I never noticed until I fetched a rather large page....

> - I think it is a good idea though to provide an example of an
> efficient call back function in the documentation. My idea is that an
> efficient one would allocate memory in chunks as needed to save the
> response pieces during curl_easy_perform. After curl_easy_perform is
> over the whole response can be copied from the small chunks in one
> step.

I'm not really sure that's worth the hassle, as a modern c library will
allocate the memory in chunks anyway for malloc() etc. It would just
complicate the code for probably very little gain on most systems. If
you want to optimize the allocation, you can always write a
sophisticated callback, but for general use, I'd stick with a
straightforward implementation.

Bye
Tim

>
>
> On Wed, May 14, 2008 at 10:34 AM, Tim Tassonis <timtas_at_cubic.ch> wrote:
>> Hi Daniel
>>
>> Daniel Stenberg wrote:
>>> On Tue, 13 May 2008, Yuriy Sosov wrote:
>>>
>>>> I need the the output in a char buffer and not a file. I also want it to
>>>> be fast. I can implement it in a callback function, but it would be better
>>>> though if it was a standard feature.
>>> Why so? The callback is the standard feature and there are probably
>>> hundreds of applications out there that use it to store the received data in
>>> memory instead of to a file.
>> Exactly, hundreds of application each implementing the more or less same
>> functionality over and over again....
>>
>> I've been doing this as well, and really think such a function would
>> actually be useful, as it is a standard requirement to store the result of
>> request in a buffer instead of a file.
Received on 2008-05-14