streams implementation - byte buffer

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::eek:stream
{
public:
ostringstream(char* buffer, int buflen)
: m_buf(buffer, buflen), std::eek: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::eek: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?
 
B

block111

seems like what you posted has some BIG mistakes...
for simple structs as yours I think it's enough to copy memory byte by
byte:

test the_struct;
unsigned char buf[128]; //assume it
//has enough space to hold the_struct

//to buffer:
memcpy(buf,&the_struct,sizeof(the_struct));

//from buffer:
memcpy(&the_struct,buf,sizeof(the_struct));


this should answer part1, part2... but the need for this itself is very
questionable...
 
M

ma740988

Unless I'm missing something your solution assumes that padding etc
with regard to compiler a and b on two different platforms are the
same. Wont work well for tranmitting data between different platfoms.
The struct shown was for 'demonstration' purposes. The 'real' struct
is quite large.
 
B

block111

dump (used sparingly) it into a byte buffer
means to copy to a byte buffer and nothing else.

I didn't look inside that jngcomp::utils::eek:stringstream and probably
all it does is the same as printing to a stream, e.g. it converts 15
into "15" and nothing else, that's why you get 1 and 5. From the first
glance I thought there was something wrong with the code because you
put in integers into buffer and access then as bytes.

If you are interested how to make it work yourself, you may provide
friend operators for >> and << and print/read into your strutcs to/from
a stream. Example:
struct test {
unsigned int idx : 16;
unsigned int jdx : 32;
bool kdx : 1;
friend ostream& operator<<(ostream& s, const test& t){
return s<<t.idx<<'\n'<<t.jdx<<'\n'<<t.kde<<'\n';
}
... similar for operator>>
};

but I'd better use something that's already developed and tested by
others, for example boost.serialization
 
M

ma740988

serialization

Somehow I overlooked the boost library (once again). Adimittidely,
this is pretty swanky and quite sophisticated for my level but I'm
almost convinced the solution lies here in serialization.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top