-
Notifications
You must be signed in to change notification settings - Fork 0
/
outbuf.h
94 lines (87 loc) · 2.53 KB
/
outbuf.h
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
92
93
94
// -*- C++ -*-
//
// OutputBuffer class interface.
//
// Copyright 1992-2021 Deven T. Corzine <[email protected]>
//
// SPDX-License-Identifier: MIT
//
// Check if previously included.
#ifndef _OUTBUF_H
#define _OUTBUF_H 1
// Output buffer consisting of linked list of output blocks.
class OutputBuffer {
public:
Block *head; // first data block
Block *tail; // last data block
OutputBuffer() { // constructor
head = tail = NULL;
}
~OutputBuffer() { // destructor
Block *block;
while (head) { // Free any remaining blocks in queue.
block = head;
head = block->next;
delete block;
}
tail = NULL;
}
char *GetData() { // Save buffer in string and erase.
Block *block;
char *p;
int len = 0;
for (block = head; block; block = block->next) {
len += block->free - block->data;
}
if (!len) return NULL;
char *buf = new char[++len];
for (p = buf; head; p += len) {
block = head;
head = block->next;
len = block->free - block->data;
strncpy(p, block->data, len);
delete block;
}
tail = NULL;
*p = 0;
return buf;
}
boolean out(int byte) { // Output one byte, return if new.
boolean select;
if ((select = boolean(!tail))) {
head = tail = new Block;
} else if (tail->free >= tail->block + Block::BlockSize) {
tail->next = new Block;
tail = tail->next;
}
*tail->free++ = byte;
return select;
}
boolean out(int byte1, int byte2) { // Output two bytes, return if new.
boolean select;
if ((select = boolean(!tail))) {
head = tail = new Block;
} else if (tail->free >= tail->block + Block::BlockSize - 1) {
tail->next = new Block;
tail = tail->next;
}
*tail->free++ = byte1;
*tail->free++ = byte2;
return select;
}
boolean out(int byte1, int byte2, // Output three bytes, return if new.
int byte3) {
boolean select;
if ((select = boolean(!tail))) {
head = tail = new Block;
} else if (tail->free >= tail->block + Block::BlockSize - 2) {
tail->next = new Block;
tail = tail->next;
}
*tail->free++ = byte1;
*tail->free++ = byte2;
*tail->free++ = byte3;
return select;
}
};
#endif // outbuf.h