cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: CURLOPT_INFILESIZE and padded files

From: Peter Seebach <seebs_at_plethora.net>
Date: Wed, 17 Jan 2007 06:23:55 -0600

In message <Pine.LNX.4.64.0701170908280.10229_at_yvahk3.pbagnpgbe.fr>, Daniel Stenberg writes:
>I don't see how adding your own little read callback makes your code less
>lightweight.

Well, it's more code.

The read callback has to be able to, from the "stream" pointer, figure
out where it is in the buffer, and it needs to somehow find out from the
rest of my code what the size limit is. So, either I make the stream
pointer into some kind of structure, or I use a couple of globals. I
think the globals are less code, even if they're less reliable. Let's
see:

        off_t max_file_size, position;

        size_t curlreadfn(void *ptr, size_t size, size_t nmemb, void *stream)
        {
                size_t total = size * nmemb;
                if (position + total > max_file_size) {
                        total = max_file_size - position;
                }
                memcpy(ptr, stream + position, total);
                position += total;
                return total;
        }

I'm pretty sure that's close to minimal. It may or may not have any
bugs...

But it's certainly much less lightweight, and much less readable,
than the hypothetical:

        curl_easy_setopt(curl, CURLOPT_SENDLIMIT, size);

-s
Received on 2007-01-17