streaming to a raw buffer

D

David Rubin

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::eek:stream 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
 
S

Siemel Naran

David Rubin said:
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::eek:stream 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;
}
 
T

tom_usenet

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
 
T

tom_usenet

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
 
D

David Harmon

On 18 Jun 2004 09:03:13 -0700 in comp.lang.c++, (e-mail address removed)
(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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top