summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/oblique/kbuffer.cpp
blob: 04a28fb1fd0dcc53d92eb1ea25e115f25dded0a0 (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
88
89
90
91
// Author: Eray Ozkural (exa) <erayo@cs.bilkent.edu.tr>, (c) 2002
//
// Copyright: GNU LGPL: http://www.gnu.org/licenses/lgpl.html

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

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

KBuffer::~KBuffer(){
}

/** open a memory buffer */
bool KBuffer::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 KBuffer::close(){
}

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

/** query buffer size */
#ifdef USE_QT4
qint64 KBuffer::size() const {
#else // USE_QT4
TQ_ULONG KBuffer::size() const {
#endif // USE_QT4
  return buf.size();
}

/** read a block of memory from buffer, advances read/write position */
TQ_LONG KBuffer::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 KBuffer::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 KBuffer::getch() {
  if (bufPos!=buf.end())
    return *(bufPos++);
  else
    return -1;
}

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

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