473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 6652
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.