469,275 Members | 1,463 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

iostream hierarchy to support endianess

Hello,

I am working on a project where I need to serialize my objects to a
stream in a mixed ASCII/binary way. Using the c++ iostream was
extremely straighforward and I quickly made progress. Unfortunately I
need to handle endianess (reading big endian binary file on little
endian architecture and vice versa). In order to do that I copy paste a
hierachy-like structure for my own IOStream. Basically I have (*)
All I need to do then is to redefine the binary stream operation
(istream::read, ostream::write) to byteswap the bytes array depending
on the SwapCode value. The formatted operations remain exactly the
same. Not supporting endianness is not an option for me (this is in the
file format description).

Is there another (easier?) way to achieve this ?

Thanks for your comments,
Mathieu

(*)
class IOSBase {};
class IOS : public IOSBase
{
public:
IOS () {}
~IOS() {}
SwapCode const &GetSwapCode() const { return m_SwapCode; }
protected:
SwapCode m_SwapCode;
};
class IStream : virtual public IOS
{
....// skipped to avoid too much details
};
class OStream : virtual public IOS
{
....// skipped to avoid too much details
};
class IOStream : public IStream, public OStream{};

And then I can simply do:
class StringStream : public IOStream
{
std::stringstream Internal;
};
or
class IFStream : public IStream
{
std::ifstream Internal;
};

Jun 6 '06 #1
1 2929

mathieu wrote:
Hello,

I am working on a project where I need to serialize my objects to a
stream in a mixed ASCII/binary way. Using the c++ iostream was
extremely straighforward and I quickly made progress. Unfortunately I
need to handle endianess (reading big endian binary file on little
endian architecture and vice versa). In order to do that I copy paste a
hierachy-like structure for my own IOStream. Basically I have (*)
All I need to do then is to redefine the binary stream operation
(istream::read, ostream::write) to byteswap the bytes array depending
on the SwapCode value. The formatted operations remain exactly the
same. Not supporting endianness is not an option for me (this is in the
file format description).

Is there another (easier?) way to achieve this ?

Thanks for your comments,
Mathieu


I did not have the right keyword to search into this very same
newsgroup. Instead searching for "binary streams" lead to exactly what
I wanted. Most refs were sent by Dietmar Kuehl

http://groups.google.com/group/comp....8518518f57dda1

and his binio is still available at:

http://www.dietmar-kuehl.de/cxxrt/

Mathieu

Jun 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sigmund Skjelnes | last post: by
5 posts views Thread by SpOiLeR | last post: by
4 posts views Thread by Gernot Frisch | last post: by
8 posts views Thread by | last post: by
1 post views Thread by Gustaf Liljegren | last post: by
8 posts views Thread by mmu2643 | last post: by
2 posts views Thread by Lorenzo Castelli | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.