// $Id: IOBuffer.h,v 1.4 2000/07/17 06:39:41 greear Exp $ // $Revision: 1.4 $ $Author: greear $ $Date: 2000/07/17 06:39:41 $ // //ScryMUD Server Code //Copyright (C) 1998 Ben Greear // //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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // To contact the Author, Ben Greear: greear@cyberhighway.net, (preferred) // greearb@agcs.com // #ifndef __IOBUFFER_INCLUDE__BEN_ #define __IOBUFFER_INCLUDE__BEN_ #define TRUE 1 #define FALSE 0 #include #include #include #include #include #include #include #include #include #include "string2.h" #define IO_LOGFILE (*(IOBuffer::getLogFile())) #define IOBUF_RD_LEN 8192 class IOBuffer { protected: /** NOTE: when head == tail, the buffer is empty. * * head points to index of next insertion * tail points to lasy byte in the queue */ char *string; int max_len; //length of string in bytes int head; /* index of next insertion */ int tail; /* index of last byte in queue */ // Read buffer char read_buf[IOBUF_RD_LEN]; public: static int string_cnt; static int total_bytes; static LogStream* logfile; IOBuffer (); //default constructor IOBuffer (const IOBuffer& S); IOBuffer (const int m_len); ~IOBuffer (); static void setLogFile(LogStream* dafile);// { logfile = dafile; } static LogStream* getLogFile();// { return logfile; } int getCurLen() const ; int getMaxLen() const { return max_len - 1; } /** This gets a copy of the bytes out of the buffer. Repeated calls * to this method will return the same thing. Use dropFromTail() to * remove the bytes from the buffer, ie to iterate through. */ int peekBytes(char* buf, int len_to_get) const; /** Actually removes oldest 'len' bytes from the buffer. */ int dropFromTail(int len); //returns -1 if it failed ///********************* OPERATORS ******************************/// operator const char* () const; /** Doesn't release memory, but virtually clears out the data. */ void clear(); /** Releases all the memory it can. */ void purge(); int append(const char* bytes, int len); void ensureCapacity(int max_length); int write(const int desc); int read(const int desc, const int max_to_read); String toString(); }; #endif