Boost logo

Boost :

From: Hoeffner, Detlef (Detlef.Hoeffner_at_[hidden])
Date: 2000-05-31 05:36:45


Dear boosters,

I would like to contribute some classes that form a binary iostream. The
design of the classes corresponds to that of the standard iostream.

Each stream can be configured to use network or hostbyteorder. wstring's can
optionally be streamed direct or in UTF format for JAVA compatability.

I would like to get feedback form you all.

Thanks

Detlef

---------------------------- biostream.hpp
---------------------------------------

// Boost general library biostream.hpp header file
---------------------------//

// (C) Copyright Detlef Hoeffner 2000.
// Permission to copy, use, modify, sell and distribute this software is
granted
// provided this copyright notice appears in all copies. This software is
// provided "as is" without express or implied warranty, and with no claim
as to
// its suitability for any purpose.
// Author: Detlef Hoeffner detlef.hoeffner_at_[hidden]

// $Id$ or other version information

#ifndef BOOST_BIOSTREAM_H
#define BOOST_BIOSTREAM_H

#include <boost/stdint.h>
#include <string>
#include <streambuf>

#ifdef WIN32
#include <winsock.h>
#else
#include <netinet/in.h>
#endif

namespace boost
{
   
class BOOST_DECL bios_base
{
protected:
   typedef char charT;
   typedef std::streambuf bufT;
   typedef bufT::pos_type pos_type;
   typedef bufT::off_type off_type;
   
   bufT* _buf;
   
   bool _useUTF;
   bool _useNetworkByteOrder;
   
   bios_base( bufT* buf, bool useNetworkByteOrder = false, bool useUTF =
false ) :
      _buf( buf ), _useNetworkByteOrder( useNetworkByteOrder ), _useUTF(
useUTF ) {}
public:
   int sync();
   off_type tellp() const { return _buf->in_avail(); }
   pos_type seekpos( pos_type sp, std::ios_base::openmode which =
std::ios_base::in | std::ios_base::out );
};

class BOOST_DECL bistream : public bios_base
{
   uint16_t getUInt16() { uint16_t n; get( &n, 2 ); return
_useNetworkByteOrder ? ntohs( n ) : n; }
   uint32_t getUInt32() { uint32_t n; get( &n, 4 ); return
_useNetworkByteOrder ? ntohl( n ) : n; }
   void getUInt64( uint64_t& n ) {
      get( &n, 8 );
      if( _useNetworkByteOrder )
      {
         (reinterpret_cast<uint32_t*>(&n))[0] =
ntohl((reinterpret_cast<uint32_t*>(&n))[0]);
         (reinterpret_cast<uint32_t*>(&n))[1] =
ntohl((reinterpret_cast<uint32_t*>(&n))[1]);
      }
   }
public:
   explicit bistream( bufT* buf, bool useNetworkByteOrder = false, bool
useUTF = false );
   
   charT get() { return _buf->sgetc(); }
   
   void get( void* buf, unsigned n ) { _buf->sgetn(
static_cast<charT*>(buf), n ); }
   
   std::streampos tellg();
   
   bistream& operator>>( bool& rVal ) { rVal = get()?true:false; return
*this; }
   
   bistream& operator>>( int8_t& rVal ) { rVal = get(); return *this; }
   
   bistream& operator>>( uint8_t& rVal ) { rVal = get(); return *this; }
   
   bistream& operator>>( int16_t& rVal ) { rVal = getUInt16( ); return
*this; }
   
   bistream& operator>>( uint16_t& rVal ) { rVal = getUInt16( );
return *this; }
   
   bistream& operator>>( int32_t& rVal ) { rVal = getUInt32(); return
*this; }
   
   bistream& operator>>( uint32_t& rVal ) { rVal = getUInt32(); return
*this; }
   
   bistream& operator>>( int64_t& rVal ) { getUInt64(
*reinterpret_cast<uint64_t*>(&rVal) ); return *this; }
   
   bistream& operator>>( uint64_t& rVal ) { getUInt64( rVal ); return *this;
}
   
   bistream& operator>>( float& rVal ) { return *this >>
*reinterpret_cast<uint32_t*>(&rVal); }
   
   bistream& operator>>( double& rVal ) { getUInt64(
*reinterpret_cast<uint64_t*>(&rVal) ); return *this; }
   
   bistream& operator>>( std::string& rVal );
   
   bistream& operator>>( std::wstring& rVal );
   
   bistream& readUTF( std::wstring& rVal );
};

class BOOST_DECL bostream : public bios_base
{
   void put( unsigned u ) { _buf->sputc( u ); }
   
   void putUInt16( uint16_t val ) {
      uint16_t n = _useNetworkByteOrder ? htons( val ) : val;
      put( &n, 2 );
   }
   void putUInt32( uint32_t val ) {
      uint32_t n = _useNetworkByteOrder ? htonl( val ) : val;
      put( &n, 4 );
   }
   void putUInt64( uint64_t val ) {
      if( _useNetworkByteOrder )
      {
         (reinterpret_cast<uint32_t*>(&val))[0] =
htonl((reinterpret_cast<uint32_t*>(&val))[0]);
         (reinterpret_cast<uint32_t*>(&val))[1] =
htonl((reinterpret_cast<uint32_t*>(&val))[1]);
      }
      put( &val, 8 );
   }
public:
   explicit bostream( bufT* buf, bool useNetworkByteOrder = false, bool
useUTF = false );
   
   void put( const void* buf, unsigned n ) { _buf->sputn( static_cast<const
charT*>(buf), n ); }
   
   std::streampos tellp();
   
   bostream& operator<<( bool val ) { put( val?1:0 ); return *this; }
   
   bostream& operator<<( int8_t val ) { put( val ); return *this; }
   
   bostream& operator<<( uint8_t val ) { put( val ); return *this; }
   
   bostream& operator<<( int16_t val ) { putUInt16( val ); return *this; }
   
   bostream& operator<<( uint16_t val ) { putUInt16( val ); return *this; }
   
   bostream& operator<<( int32_t val ) { putUInt32( val ); return *this; }
   
   bostream& operator<<( uint32_t val ) { putUInt32( val ); return *this; }
   
   bostream& operator<<( int64_t val ) { putUInt64( val ); return *this; }
   
   bostream& operator<<( uint64_t val ) { putUInt64( val ); return *this; }
   
   bostream& operator<<( float val ) { return *this <<
*reinterpret_cast<uint32_t*>(&val); }
   
   bostream& operator<<( double val ) { putUInt64(
*reinterpret_cast<uint64_t*>(&val) ); return *this; }
   
   bostream& operator<<( const std::string& val );
   
   bostream& operator<<( const std::wstring& val );
   
   void writeUTF( const std::wstring& str );
};

}

#endif

____________________________________________________________________________
_________

// Boost general library biostream.hpp header file
---------------------------//

// (C) Copyright Detlef Hoeffner 2000.
// Permission to copy, use, modify, sell and distribute this software is
granted
// provided this copyright notice appears in all copies. This software is
// provided "as is" without express or implied warranty, and with no claim
as to
// its suitability for any purpose.
// Author: Detlef Hoeffner detlef.hoeffner_at_[hidden]

// $Id$ or other version information

#ifndef BOOST_BIOSTREAMUTIL_H
#define BOOST_BIOSTREAMUTIL_H

#include <boost/biostream.hpp>
#include <vector>

namespace boost {

        template<typename T> class shared_ptr;

template < class S, class T >
inline bistream& operator>>( bistream& is, std::pair<S,T>& rVal )
{
   return is >> rVal.first >> rVal.second;
}

template < class S >
inline bistream& operator>>( bistream& is, std::vector<S>& v )
{
        uint32_t size;
        is >> size;
        v.reserve( size );
        while( size-- > 0 )
        {
                S val;
                is >> val;
                v.push_back( val );
        }
        return is;
}

template <class T>
bistream& operator>>( bistream& in, boost::shared_ptr<T>& sptr )
{
   T* v = new T;
   in >> *v;
   sptr = shared_ptr<T>( v );
   return in;
}

template < class S, class T >
inline bostream& operator<<( bostream& os, const std::pair<S,T>& rVal )
{
   return os << rVal.first << rVal.second;
}

template < class S >
inline bostream& operator<<( bostream& os, const std::vector<S>& v )
{
        os << uint32_t( v.size() );
   for( unsigned i = 0; i < v.size(); i++ )
      os << v[i];
        return os;
}

template <class T>
bostream& operator<<( bostream& out, const boost::shared_ptr<T>& sptr )
{
   T& v = *sptr;
   return out << v;
}

}
#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk