473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

streaming to a raw buffer

How can I stream data to a raw char buffer without using strstream
(which is deprecated), sprintf(), or any copy operation? Here is my
function:

const char *i2bs(unsigned int i, char *buf, int length)
{
// Generate a bit string representation of 'i'. Store the result
in
// 'buf' and return 'buf'. The behavior is undefined if 'buf' is
// null, or 'length' is not large enough to store the result.

static const char *tab[] = // byte to bit conversion table
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};

/*
std::streambuf sbuf; // cannot create basic_streambuf!
sbuf.pubsetbuf(buf, length);

std::ostream os(sbuf);
os << tab[(i >>28) & 0xf] << tab[(i >>24) & 0xf]
<< tab[(i >>20) & 0xf] << tab[(i >>16) & 0xf]
<< tab[(i >>12) & 0xf] << tab[(i >> 8) & 0xf]
<< tab[(i >> 4) & 0xf] << tab[(i >> 0) & 0xf];
*/
return buf;
}

/david
Jul 22 '05 #1
4 2141
"David Rubin" <da********@warpmail.net> wrote in message
How can I stream data to a raw char buffer without using strstream
(which is deprecated), sprintf(), or any copy operation? Here is my
function:
The constructor of streambuf is protected, so you can't create one directly.
Conceptually the class is abstract, and that's why you can't create it.
Create a filebuf or stringbuf.

const char *i2bs(unsigned int i, char *buf, int length)
{
// Generate a bit string representation of 'i'. Store the result
in
// 'buf' and return 'buf'. The behavior is undefined if 'buf' is
// null, or 'length' is not large enough to store the result.

static const char *tab[] = // byte to bit conversion table
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};

/*
std::streambuf sbuf; // cannot create basic_streambuf!
sbuf.pubsetbuf(buf, length);

std::ostream os(sbuf);
os << tab[(i >>28) & 0xf] << tab[(i >>24) & 0xf]
<< tab[(i >>20) & 0xf] << tab[(i >>16) & 0xf]
<< tab[(i >>12) & 0xf] << tab[(i >> 8) & 0xf]
<< tab[(i >> 4) & 0xf] << tab[(i >> 0) & 0xf];
*/
return buf;
}

Jul 22 '05 #2
On 18 Jun 2004 09:03:13 -0700, da********@warpmail.net (David Rubin)
wrote:
How can I stream data to a raw char buffer without using strstream
(which is deprecated), sprintf(), or any copy operation?


Write a simpler version of strstreambuf:

class membuf: public std::streambuf
{
public:
membuf(char* mem, std::size_t length)
{
setg(mem, mem, mem + length);
setp(mem, mem + length);
}
};

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
On Fri, 18 Jun 2004 17:36:04 +0100, tom_usenet
<to********@hotmail.com> wrote:
On 18 Jun 2004 09:03:13 -0700, da********@warpmail.net (David Rubin)
wrote:
How can I stream data to a raw char buffer without using strstream
(which is deprecated), sprintf(), or any copy operation?


Write a simpler version of strstreambuf:

class membuf: public std::streambuf
{
public:
membuf(char* mem, std::size_t length)
{
setg(mem, mem, mem + length);
setp(mem, mem + length);
}


It's probably worth overloading the constructor too:

membuf(char const* mem, std::size_t length)
{
setg(mem, mem, mem + length);
}

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
On 18 Jun 2004 09:03:13 -0700 in comp.lang.c++, da********@warpmail.net
(David Rubin) wrote,
How can I stream data to a raw char buffer without using strstream
(which is deprecated), sprintf(), or any copy operation?
Feel free to use strstream if it suits your requirements. It's not
going to disappear today.
os << tab[(i >>28) & 0xf] << tab[(i >>24) & 0xf]
<< tab[(i >>20) & 0xf] << tab[(i >>16) & 0xf]
<< tab[(i >>12) & 0xf] << tab[(i >> 8) & 0xf]
<< tab[(i >> 4) & 0xf] << tab[(i >> 0) & 0xf];


os << std::bitset<32>(i);

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Larry Woods | last post by:
Could you give me an example of streaming a page to the client? Thanks. Larry Woods
3
by: Andreas Müller | last post by:
hi, I like to implement a streaming client but I have no idea how to draw the images. I have a buffer that holds a whole jpeg-image (i stream motion-jpeg), how can i draw this buffer directly...
0
by: Kai Bohli | last post by:
Hi ! I'm working with a lot of different labelprinters. Some of them are quite picky about how they receive data. Especially one of them needs to receive one graphic file at a time. In this...
1
by: JC | last post by:
Hi, I have to play a streaming audio. So in a thread I play audio as follow thread{ //some code for(;;){ //some code waveOutPrepareHeader(hwo, &(whdr), sizeof(WAVEHDR)); waveOutWrite(hwo,...
3
by: Ipsita | last post by:
Hi! I am trying SOAP with DIME attachments in web services. For example say, I have a file resume.pdf stored somewhere on my server. How does the web service send the file to the client, so that...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
0
by: the friendly display name | last post by:
Following problem: I want to stream a file to the client, but the logic takes too much CPU time (100% in fact..) this is the code: public class Download : System.Web.UI.Page {
3
by: Brad | last post by:
I have an aspx page that is sending pdf files to client browsers: it uses a filestream to read the pdf file and response.binarywrite to send content to the browser. This has worked great for years...
1
by: Almund | last post by:
Hi, I'm trying to implement streaming over http and got stuck with a problem trying to send chunks of data using the .Net NetworkStream object. All works fine as long as I send the entire data in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.