summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/oblique/kbuffer.cpp
blob: b7e5ae01dabf4dc921f518a65f4b19f85e31e5da (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
83
84
85
86
87
// Author: Eray Ozkural (exa) <[email protected]>, (c) 2002
//
// Copyright: GNU LGPL: http://www.gnu.org/licenses/lgpl.html

#include "kbuffer.h"
#include <algorithm>
#include <iostream>
#include <iomanip>

TDEBuffer::TDEBuffer()
{
  bufPos = buf.end(); // will become 0 in the beginning
}

TDEBuffer::~TDEBuffer(){
}

/** open a memory buffer */
bool TDEBuffer::open(int ) {
   // ignore mode
   buf.erase(buf.begin(), buf.end()); // erase buffer
   buf.reserve(8); // prevent iterators from ever being 0 and start with a reasonable mem
   bufPos = buf.end();              // reset position
   return true;
}

/** Close buffer */
void TDEBuffer::close(){
}

/** No descriptions */
void TDEBuffer::flush(){
}

/** query buffer size */
TQ_ULONG TDEBuffer::size() const {
  return buf.size();
}

/** read a block of memory from buffer, advances read/write position */
TQ_LONG TDEBuffer::readBlock(char* data, long unsigned int maxLen) {
  int len;
  if ((long unsigned)(buf.end()-bufPos) > maxLen)
    len = maxLen;
  else
    len = buf.end()-bufPos;
  std::vector<char>::iterator bufPosNew = bufPos + len;
  for (std::vector<char>::iterator it=bufPos; it < bufPosNew; it++, data++)
    *data = *it;
  bufPos = bufPosNew;
  return len;
}

/** write a block of memory into buffer */
TQ_LONG TDEBuffer::writeBlock(const char *data, long unsigned int len){
  int pos = bufPos-buf.begin();
  copy(data, data+len, inserter(buf,bufPos));
  bufPos = buf.begin() + pos + len;
  return len;
}

/** read a byte */
int TDEBuffer::getch() {
  if (bufPos!=buf.end())
    return *(bufPos++);
  else
    return -1;
}

/** write a byte */
int TDEBuffer::putch(int c) {
  int pos = bufPos-buf.begin();
  buf.insert(bufPos, c);
  bufPos = buf.begin() + pos + 1;
  return c;
}

/** undo last getch()
 */
int TDEBuffer::ungetch(int c) {
  if (bufPos!=buf.begin()) {
    bufPos--;
    return c;
  }
  else
    return -1;
}