📄 streams.cpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include <cstring>#include <more/io/streams.hpp>using namespace more;using namespace more::io;////////////////////////////////////////////////////////////////////////////////template<typename Type>inline InputStream& readOctets( InputStream& rIn, Type& rType) throw( IOException ){ size_t nNoOfReadOctets; if( rIn.hasBeenClosed( ) ) { throw IOException( -1, "Input stream has been closed" ); } nNoOfReadOctets = rIn.read( &rType, sizeof( Type ) ); if( nNoOfReadOctets < sizeof( Type ) ) { throw IOException( -1, "Could not read enough octets from input stream" ); } return rIn;}////////////////////////////////////////////////////////////////////////////////template<typename T>inline InputStream& readArray( InputStream& rIn, Array<T>& rArray) throw( IOException ){ size_t nNoOfOctets; size_t nNoOfReadOctets; if( rIn.hasBeenClosed( ) ) { throw IOException( -1, "Input stream has been closed" ); } nNoOfOctets = rArray.getLength( ) * sizeof( T ); if( nNoOfOctets > 0 ) { nNoOfReadOctets = rIn.read( ( void* ) &rArray[0], nNoOfOctets ); if( nNoOfReadOctets < nNoOfOctets ) { throw IOException( -1, "Could not read enough octets from input stream" ); } } return rIn;}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( bool& rbBoolean) throw( IOException ){ unsigned char nBoolean; *this >> nBoolean; rbBoolean = ( nBoolean == 1 ); return *this;}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( char& rChar) throw( IOException ){ return readOctets( *this, rChar );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( unsigned char& rnUnsignedChar) throw( IOException ){ return readOctets( *this, rnUnsignedChar );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( short& rnShort) throw( IOException ){ return readOctets( *this, rnShort );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( unsigned short& rnUnsignedShort) throw( IOException ){ return readOctets( *this, rnUnsignedShort );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( int& rnInt) throw( IOException ){ return readOctets( *this, rnInt );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( unsigned int& rnUnsignedInt) throw( IOException ){ return readOctets( *this, rnUnsignedInt );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( long& rnLong) throw( IOException ){ return readOctets( *this, rnLong );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( unsigned long& rnUnsignedLong) throw( IOException ){ return readOctets( *this, rnUnsignedLong );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( float& rnFloat) throw( IOException ){ return readOctets( *this, rnFloat );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( double& rnDouble) throw( IOException ){ return readOctets( *this, rnDouble );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( String& rsString) throw( IOException ){ Array<char> chars; if( this -> hasBeenClosed( ) ) { throw IOException( -1, "Input stream has been closed" ); } chars.setLength( this -> available( ) ); *this >> chars; rsString = chars; return *this;}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<bool>& rArray) throw( IOException ){ for( size_t i = 0; i < rArray.getLength( ); i++ ) { *this >> rArray[i]; } return *this;}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<char>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<unsigned char>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<short>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<unsigned short>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<int>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<unsigned int>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<long>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<unsigned long>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<float>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream& InputStream::operator >>( Array<double>& rArray) throw( IOException ){ return readArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////InputStream::~InputStream( ){}////////////////////////////////////////////////////////////////////////////////template<typename Type>inline OutputStream& writeOctets( OutputStream& rOut, Type& rType) throw( IOException ){ if( rOut.hasBeenClosed( ) ) { throw IOException( -1, "Output stream has been closed" ); } rOut.write( &rType, sizeof( Type ) ); return rOut;}////////////////////////////////////////////////////////////////////////////////template<typename T>inline OutputStream& writeArray( OutputStream& rOut, const Array<T>& rArray) throw( IOException ){ if( rOut.hasBeenClosed( ) ) { throw IOException( -1, "Output stream has been closed" ); } if( rArray.getLength( ) > 0 ) { rOut.write( &rArray[0], rArray.getLength( ) * sizeof( T ) ); } return rOut;}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const bool& rbBoolean) throw( IOException ){ unsigned char nBoolean = rbBoolean; return *this << nBoolean;}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const char& rChar) throw( IOException ){ return writeOctets( *this, rChar );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const unsigned char& nUnsignedChar) throw( IOException ){ return writeOctets( *this, nUnsignedChar );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const short& nShort) throw( IOException ){ return writeOctets( *this, nShort );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const unsigned short& nUnsignedShort) throw( IOException ){ return writeOctets( *this, nUnsignedShort );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const int& nInt) throw( IOException ){ return writeOctets( *this, nInt );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const unsigned int& nUnsignedInt) throw( IOException ){ return writeOctets( *this, nUnsignedInt );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const long& nLong) throw( IOException ){ return writeOctets( *this, nLong );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const unsigned long& nUnsignedLong) throw( IOException ){ return writeOctets( *this, nUnsignedLong );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const float& nFloat) throw( IOException ){ return writeOctets( *this, nFloat );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const double& nDouble) throw( IOException ){ return writeOctets( *this, nDouble );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const char* pcString) throw( IOException ){ size_t nStrLen; if( hasBeenClosed( ) ) { throw IOException( -1, "Output stream has been closed" ); } nStrLen = strlen( pcString ); if( nStrLen > 0 ) { this -> write( pcString, nStrLen ); } return *this;}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const String& rsString) throw( IOException ){ if( hasBeenClosed( ) ) { throw IOException( -1, "Output stream has been closed" ); } if( rsString.getLength( ) > 0 ) { this -> write( rsString, rsString.getLength( ) ); } return *this;}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<bool>& rArray) throw( IOException ){ for( size_t i = 0; i < rArray.getLength( ); i++ ) { *this << rArray[i]; } return *this;}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<char>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<unsigned char>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<short>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<unsigned short>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<int>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<unsigned int>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<long>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<unsigned long>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<float>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<double>& rArray) throw( IOException ){ return writeArray( *this, rArray );}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<String>& rArray) throw( IOException ){ for( size_t i = 0; i < rArray.getLength( ); i++ ) { *this << rArray[i]; } return *this;}////////////////////////////////////////////////////////////////////////////////OutputStream& OutputStream::operator <<( const Array<const char*>& rArray) throw( IOException ){ for( size_t i = 0; i < rArray.getLength( ); i++ ) { *this << rArray[i]; } return *this;}////////////////////////////////////////////////////////////////////////////////OutputStream::~OutputStream( ){}////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -