LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2003, 03:41 AM   #1
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Rep: Reputation: 15
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!

Last edited by powerplane; 05-13-2003 at 06:51 AM.
 
Old 05-13-2003, 06:51 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 05-13-2003, 07:10 AM   #3
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Original Poster
Rep: Reputation: 15
@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.
 
Old 05-13-2003, 07:54 AM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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...
 
Old 05-14-2003, 10:19 PM   #5
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Original Poster
Rep: Reputation: 15
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sockets and Packet Stream Question Chryzmo Programming 2 11-12-2005 08:54 PM
switch lang rohr Debian 7 07-04-2005 08:51 AM
What a good lang. Dark_Sniper* Programming 19 05-21-2005 05:38 AM
LANG settings r_213 Linux - Software 4 01-13-2005 03:26 AM
What lang for What titanium_geek Programming 6 03-28-2003 04:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration