lib/fdbuf/fdbuf.h
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #ifndef FDBUF__H__
       
    18 #define FDBUF__H__
       
    19 
       
    20 #include "config.h"
       
    21 #include <string.h>
       
    22 #include <fcntl.h>
       
    23 
       
    24 #ifdef _REENTRANT
       
    25 #include <pthread.h>
       
    26 #endif
       
    27 
       
    28 #ifndef FDBUF_SIZE
       
    29 #define FDBUF_SIZE 4096
       
    30 #endif
       
    31 
       
    32 class mystring;
       
    33 
       
    34 class fdbuf 
       
    35 {
       
    36 public:
       
    37   enum flagbits { flag_eof=1, flag_error=2, flag_closed=4 };
       
    38 
       
    39   fdbuf(int fdesc, bool dc, unsigned bufsz = FDBUF_SIZE);
       
    40   ~fdbuf();
       
    41   bool error() const;
       
    42   bool closed() const;
       
    43   bool close();
       
    44 #ifdef _REENTRANT
       
    45   void lock() { pthread_mutex_lock(&mutex); }
       
    46   void unlock() { pthread_mutex_unlock(&mutex); }
       
    47 #else
       
    48 #ifdef FDBUF_MUTEX_DEBUG
       
    49   void lock();
       
    50   void unlock();
       
    51 #else
       
    52   void lock() { }
       
    53   void unlock() { }
       
    54 #endif
       
    55 #endif
       
    56 protected:
       
    57   char* const buf;
       
    58   unsigned buflength;		// Length of the data in the buffer
       
    59   unsigned bufstart;		// Start of the data in the buffer
       
    60   unsigned offset;		// Current file read/write offset
       
    61   int errnum;			// Saved error flag
       
    62   unsigned flags;		// Status flags
       
    63 
       
    64   const unsigned bufsize;	// Total buffer size
       
    65   const int fd;
       
    66   const bool do_close;		// True to close on destructor
       
    67 
       
    68 #ifdef _REENTRANT
       
    69   pthread_mutex_t mutex;
       
    70 #else
       
    71 #ifdef FDBUF_MUTEX_DEBUG
       
    72   unsigned mutex;
       
    73 #endif
       
    74 #endif
       
    75 };
       
    76 
       
    77 #include "fdbuf/fdibuf.h"
       
    78 #include "fdbuf/fdobuf.h"
       
    79 
       
    80 bool fdbuf_copy(fdibuf&, fdobuf&, bool noflush = false);
       
    81 
       
    82 #endif // FDBUF__H__