M
ma740988
A few days ago I recieved yet again advice on implementing a buffer of
bytes. At issue: (part 1) how do I take the contents of a struct,
then dump (used sparingly) it into a byte buffer. Similarily, take the
contents of a (part 2) buffer and dump (used sparingly) it into a
struct.
So I found an implementation that handles part 1. Well partially.
#include <ostream>
namespace jngcomp { namespace utils
{
// Output string buffer class
class stringbuf : public std::streambuf
{
public:
stringbuf(char* buffer, int buflen)
: m_buffer(buffer), m_buflen(buflen)
{
setp(m_buffer, m_buffer + m_buflen);
}
char* getpptr()
{
return pptr();
}
void setpptr(char* p)
{
setp(p, m_buffer + m_buflen);
}
protected:
virtual int_type overflow(int_type c)
{
return EOF;
}
char* m_buffer;
int m_buflen;
};
// Output string stream class which is suppled with a buffer to
write to.
// Ie, stream equivalent of sprintf.
class ostringstream : public std:stream
{
public:
ostringstream(char* buffer, int buflen)
: m_buf(buffer, buflen), std:stream(&m_buf)
{
}
char* getpptr()
{
return m_buf.getpptr();
}
void setpptr(char* p)
{
m_buf.setpptr(p);
}
protected:
stringbuf m_buf;
};
}}
struct test {
unsigned int idx : 16;
unsigned int jdx : 32;
bool kdx : 1;
};
int main()
{
test t_;
t_.idx = 15;
t_.jdx = 25;
char buffer[10];
jngcomp::utils:stringstream strm(buffer, sizeof(buffer));
strm << t_.idx ;
std::cout << buffer[0] << std::endl;
std::cout << buffer[1] << std::endl;
}
What's interesting to me is the output is
1
5
With this approach how does one get 15 to spread across 4 bytes
withouth having to do
strm << 0 << 0 << t_.idx; ?
Tips/hints on part 2?
bytes. At issue: (part 1) how do I take the contents of a struct,
then dump (used sparingly) it into a byte buffer. Similarily, take the
contents of a (part 2) buffer and dump (used sparingly) it into a
struct.
So I found an implementation that handles part 1. Well partially.
#include <ostream>
namespace jngcomp { namespace utils
{
// Output string buffer class
class stringbuf : public std::streambuf
{
public:
stringbuf(char* buffer, int buflen)
: m_buffer(buffer), m_buflen(buflen)
{
setp(m_buffer, m_buffer + m_buflen);
}
char* getpptr()
{
return pptr();
}
void setpptr(char* p)
{
setp(p, m_buffer + m_buflen);
}
protected:
virtual int_type overflow(int_type c)
{
return EOF;
}
char* m_buffer;
int m_buflen;
};
// Output string stream class which is suppled with a buffer to
write to.
// Ie, stream equivalent of sprintf.
class ostringstream : public std:stream
{
public:
ostringstream(char* buffer, int buflen)
: m_buf(buffer, buflen), std:stream(&m_buf)
{
}
char* getpptr()
{
return m_buf.getpptr();
}
void setpptr(char* p)
{
m_buf.setpptr(p);
}
protected:
stringbuf m_buf;
};
}}
struct test {
unsigned int idx : 16;
unsigned int jdx : 32;
bool kdx : 1;
};
int main()
{
test t_;
t_.idx = 15;
t_.jdx = 25;
char buffer[10];
jngcomp::utils:stringstream strm(buffer, sizeof(buffer));
strm << t_.idx ;
std::cout << buffer[0] << std::endl;
std::cout << buffer[1] << std::endl;
}
What's interesting to me is the output is
1
5
With this approach how does one get 15 to spread across 4 bytes
withouth having to do
strm << 0 << 0 << t_.idx; ?
Tips/hints on part 2?