LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C lang question: about stream (https://www.linuxquestions.org/questions/programming-9/c-lang-question-about-stream-59525/)

powerplane 05-13-2003 03:41 AM

C question: about stream
 
I am coding a download program with libcurl. libcurl provide a hook API, which can let you register your own callback function process the download data. Interface of that callback function is fwrite-like,for example:
Code:

size_t
    my_write(const void *ptr, size_t size, size_t nmemb, FILE *stream);

You can just simplied register fwrite as the call back function, and it will call fwrite to write down the download data to disk.

My problem is I do not want to store the data, I just want to buffer them to a block of mem, so I can do sth with them.
In fact, I can store the data to a temp file, and read them into a mem block and then remove the temp file, but don't you think it rediculous and resource-watste?
I think anything like one of belows may make my work easier:
1) any thing like a virtual file stream, not a real stream, and I can just threat it as a mem block buffer?
2) or a fwrite-like memory-copying function?
3)your better solution!

Mara 05-13-2003 06:51 AM

You've got 2 options, I guess: buffer in the memory or buffer on disk. I think a buffer on disk is a better solution, especially if you have much data. Just redirect it to file. That's my opinion.

powerplane 05-13-2003 07:10 AM

@Mara:
Thanks for reply.
I just want to buffer html headers to mem. A header is not a very big, generally. And I think it is not worth store sth like a html header to disk, plus I have to unlink the file when I don't need it any more.:D

Mara 05-13-2003 07:54 AM

I don't know libcurl, but I think it should offer also a non-stream possibility. If not, you can use plain sockets (but it requires more code that you have now, I guess) or just use the disk buffer. It won't slow everything much. I don't know which solution is better in your case. That's all I can say... :)

powerplane 05-14-2003 10:19 PM

Ok,thanks!!
This is what I am going to do. Infact, libcurl can let you send a ptr to stream, hence you can use it in your callback function.

so, I will code sth like this

code:
size_t
my_write(const void *ptr, size_t size, size_t nmemb, void *stream)
{
ALLOCA_OR_REALLOCA(stream);
MY_MEMCOPY(stream, ptr);
return nmemb * size;
}


All times are GMT -5. The time now is 10:33 PM.