472,984 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

streams implementation - byte buffer

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

Jul 23 '05 #1
4 6632
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...

Jul 23 '05 #2
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.

Jul 23 '05 #3
> 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::ostringstream 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

Jul 23 '05 #4
>>> 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.

Jul 23 '05 #5

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

Similar topics

1
by: bartek | last post by:
Hi, Consider the following implementation of swap() for some class... It looks a little bit like a hells-spawn. Though it should work for every object with continuous memory layout, wouldn't...
8
by: kelvSYC | last post by:
This seems to be easy, and perhaps I'm overthinking it a bit. Anyways... I want to use streams to write an int in binary form as a little-endian byte sequence. Firstly, is the easiest way...
30
by: Chris McDonald | last post by:
Under Linux, I'm using select() to detect when any of a collection of socket descriptors is ready for reading. Associated with each socket descriptor is a FILE pointer, obtained via fdopen(sd,...
4
by: Dave Bailey | last post by:
I have an application installed on a web server. When forst intalled it worked fine. The administrator rebooted the server and then when accessing the app the folowing error appears: CryptoAPI...
2
by: Peter Rilling | last post by:
One nice thing about collections and arrays is that they implement the IEnumerator and IEnumerable interfaces which allow for more then one iterator to walk the list of items without affecting the...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
3
by: Kirit Sælensminde | last post by:
>From thread http://groups.google.com/group/comp.lang.c++/browse_thread/thread/79d767efa42df516 "P.J. Plauger" <p...@dinkumware.comwrites: I'll take this at face value and I'll have to suppose...
0
by: chikas | last post by:
hallo all,I need help and any tips shall be appreciated! i want to implement a I2C API(from Beck SC12 microcontroller) in a 1 wire search C-code(use to search devices on 1 wire bus) sothat that i...
6
by: Ioannis Papadopoulos | last post by:
I would like to extend the functionality of all streams in C++ so I can do some fancy stuff like redirecting the streams on the fly. I don't want to reimplement the whole stream support in C++...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.