/* * Copyright (c) 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #pragma implementation #include "SockStream.h" #ifdef PROTOGEN # include "exception.h" #else # include # define ERROR(fmt, args...) { fprintf(stderr, fmt , ## args); exit(1); } # define FATAL(fmt, args...) { fprintf(stderr, fmt , ## args); abort(); } #endif /************************************************************************* * SockStreamBase constructors (work around GCC-2.7.1 BUG?) *************************************************************************/ SockStreamBase::SockStreamBase(const char *address) { _sb = NULL; _sb = new SockBuf(address); ios::init(_sb); } SockStreamBase::SockStreamBase(SockBuf *s) { _sb = s; ios::init(_sb); } SockStreamBase::~SockStreamBase() { if(_sb != NULL) delete _sb; } /************************************************************************* * The `endm' manipulator (FIXME: need rtti dynamic casts) *************************************************************************/ istream& endm(istream& is) // if EOM was not yet reached, discard all data until EOM // anyway clear the EOM flag. { #define MANY 0x7fffffff while(is && ! is.eof()) { is.ignore(MANY); is.get(); } // FIXME! rtti dynamic casting required SockBuf *sock = ((SockIStream*)&is)->rdbuf(); sock->setstate(0, Socket::eom_seen); if(! ( sock->state() & Socket::eot_seen )) is.clear(is.rdstate() & ~ios::eofbit); if(sock->state() & Socket::error) is.setstate(ios::failbit); return is; } istream& endt(istream& is) // if EOM was not yet reached, discard all data until EOM and consume // the EOM token. If EOT is found, consume it, otherwise discard all // data until EOT is found. { while(is && ! is.eof()) { endm(is); } // FIXME! rtti dynamic casting required SockBuf *sock = ((SockIStream*)&is)->rdbuf(); sock->setstate(0, Socket::eot_seen ); is.clear(is.rdstate() & ~ios::eofbit); if( sock->state() & Socket::error ) is.setstate(ios::failbit); return is; } ostream& endm(ostream& os) { os.flush(); ((SockIStream*)&os)->rdbuf()->send(0,0,Socket::msg_eom); return os; } ostream& endt(ostream& os) { os.flush(); ((SockIStream*)&os)->rdbuf()->send(0,0,Socket::msg_eot); return os; }