summaryrefslogtreecommitdiff
path: root/lib/fdbuf/fdbuf.h
blob: 34b8de9c90547d2780e8b63585333f0fc62e09e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
//
// 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

#ifndef FDBUF__H__
#define FDBUF__H__

#include "config.h"
#include <string.h>
#include <fcntl.h>

#ifdef _REENTRANT
#include <pthread.h>
#endif

#ifndef FDBUF_SIZE
#define FDBUF_SIZE 4096
#endif

class mystring;

class fdbuf 
{
public:
  enum flagbits { flag_eof=1, flag_error=2, flag_closed=4 };

  fdbuf(int fdesc, bool dc, unsigned bufsz = FDBUF_SIZE);
  ~fdbuf();
  bool error() const;
  bool closed() const;
  bool close();
#ifdef _REENTRANT
  void lock() { pthread_mutex_lock(&mutex); }
  void unlock() { pthread_mutex_unlock(&mutex); }
#else
#ifdef FDBUF_MUTEX_DEBUG
  void lock();
  void unlock();
#else
  void lock() { }
  void unlock() { }
#endif
#endif
protected:
  char* const buf;
  unsigned buflength;		// Length of the data in the buffer
  unsigned bufstart;		// Start of the data in the buffer
  unsigned offset;		// Current file read/write offset
  int errnum;			// Saved error flag
  unsigned flags;		// Status flags

  const unsigned bufsize;	// Total buffer size
  const int fd;
  const bool do_close;		// True to close on destructor

#ifdef _REENTRANT
  pthread_mutex_t mutex;
#else
#ifdef FDBUF_MUTEX_DEBUG
  unsigned mutex;
#endif
#endif
};

#include "fdbuf/fdibuf.h"
#include "fdbuf/fdobuf.h"

bool fdbuf_copy(fdibuf&, fdobuf&, bool noflush = false);

#endif // FDBUF__H__