iostream hierarchy to support endianess

M

mathieu

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;
};
 
M

mathieu

mathieu said:
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.lang.c++.moderated/msg/958518518f57dda1

and his binio is still available at:

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

Mathieu
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top