I’m developing a linux app programmed in C which processes gdk pixbuf images and then should send them to a remote server via ftp (libcurl). The images are saved into a gchar buffer with gdk_pixbuf_save_to_buffer.
The problem is I don’t know how to use the data in this buffer in conjunction with libcurl read callback function to send the image properly to the remote server. All my attempts so far have produced random bytes into the resulting file.
There is an example of the libcurl read callback function which looks like this:
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t retcode = fread(ptr, size, nmemb, stream);
return retcode;
}
I would need to do my own callback function that gets the gchar buffer as input instead of file stream and reads size*nmemb bytes from it and stores it to *ptr.
This is related to my previous question.
The
read_callback()is a function which CURL calls when it need to obtain data that will be uploaded to the server. Imagine thatread_callback()is something similar tofread(). When called it performs any voodoo-mumbo-jumbo that is needed, but in the end uploadable data must be stored in*ptrbuffer, which is curl’s internal buffer. For your in-memory buffermemcpy()will do just fine as body ofread_callback(), so you don’t need realfread()at all.size * nmembtells you how big buffer curl has reserved for a single chunk of data. The lastvoid*is a pointer which was set by CURLOPT_READDATA option – it’s a do-what-ever-you-need-with-it kind of pointer, so it can point to a structure containing data which you’re uploading and a some additional info e.g. current progress.You may use this as a sample: