Statistics
| Branch: | Tag: | Revision:

dvbd / src / circularbuffer.h @ 904d298f

History | View | Annotate | Download (1.78 KB)

1
// -*- c++ -*-
2
/*
3
  Copyright 2003 John Knottenbelt
4
  
5
  This program is free software; you can redistribute it and/or modify
6
  it under the terms of the GNU General Public License as published by
7
  the Free Software Foundation; either version 2 of the License, or
8
  (at your option) any later version.
9
 
10
  This program is distributed in the hope that it will be useful,
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
  GNU General Public License for more details.
14
 
15
  You should have received a copy of the GNU General Public License
16
  along with this program; if not, write to the Free Software
17
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
18
*/
19

    
20
#if !defined __CIRCULARBUFFER_H
21
#define __CIRCULARBUFFER_H
22

    
23
#include <string>
24

    
25
class CircularBuffer {
26
public:
27
  CircularBuffer(int initSize, int maxSize);
28
  virtual ~CircularBuffer();
29

    
30
  // returns number of bytes waiting to be sent
31
  int getUsed() const {
32
    return used;
33
  }
34

    
35
  // returns number of free bytes remaining in the buffer
36
  int getFree() const {
37
    return maxSize - used;
38
  }
39

    
40
  int getMaxSize() const {
41
    return maxSize;
42
  }
43

    
44
  bool hasOverflowed() const {
45
    return overflow;
46
  }
47

    
48
  void clearOverflowed() {
49
    overflow = false;
50
  }
51

    
52
  bool write(const char *data, int size);
53
  bool write(const std::string &data);
54

    
55
  bool read(char *data, int size);
56
  bool read(std::string &data, int count);
57
  bool readLine(std::string &data, char ch = '\n');
58

    
59
  void discard(int size);
60

    
61
protected:
62
  void getSections(char *&buffer1, int &count1, 
63
                   char *&buffer2, int &count2);
64

    
65
  void setOverflowed() {
66
    overflow = true;
67
  }
68

    
69
private:
70
  char *buffer;
71
  int start, size, maxSize, used;
72
  bool overflow;
73

    
74
  void grow();
75
};
76

    
77
#endif // __CIRCULARBUFFER_H