Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > streams implementation - byte buffer

Reply
Thread Tools

streams implementation - byte buffer

 
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      04-25-2005
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?

 
Reply With Quote
 
 
 
 
block111@mail.ru
Guest
Posts: n/a
 
      04-25-2005
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...

 
Reply With Quote
 
 
 
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      04-25-2005
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.

 
Reply With Quote
 
block111@mail.ru
Guest
Posts: n/a
 
      04-25-2005
> 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: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

 
Reply With Quote
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      04-25-2005
>>> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bytebuffer to byte array and Object streams Jordi Java 4 04-23-2007 09:57 PM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
MMS Ripper MMST Streams Buffer Overflow Vulnerability AeoN Computer Support 0 07-11-2005 05:43 PM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM
ObjectInput/Output streams and byte[] Ike Java 1 09-26-2003 04:07 PM



Advertisments