-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtextBuff.cpp
202 lines (152 loc) · 6.63 KB
/
textBuff.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <textBuff.h>
#include <resizeBuff.h>
#include <strTools.h>
textBuff::textBuff(int inNumBytes,bool inOverwrite) {
head = 0;
tail = 0;
isFull = false;
overwrite = inOverwrite;
buff = NULL;
returnStr = NULL;
while(inNumBytes && !resizeBuff(inNumBytes,&buff)) { // While inNumBytes>0 and resizing the buffer fails..
inNumBytes = inNumBytes/2; // Try for 1/2 the number of bytes?
}
numBytes = inNumBytes; // Note how many bytes we ended up with.
}
// Destructor, clean up what we allocated.
textBuff::~textBuff(void) {
resizeBuff(0,&buff);
resizeBuff(0,&returnStr);
}
// Add a charactor and update the state. If its full it can take two different actions.
// If overwrite is true the oldest char will be bumped off and the new char will be saved.
// If overwrite is false the no action is taken an false is returned. In all other cases
// true is returned.
bool textBuff::addChar(char inChar) {
if (overwrite && full()) { // If we're full and overwrite is true..
(void)readChar(); // Make room by dumping the oldest char.
}
if (!full()) { // If not full..
buff[tail] = inChar; // Save off the incoming char.
inc(&tail); // Increment the tail pointer.
isFull = tail==head; // Check and note if we are full.
return true; // Return a success.
}
return false; // Couldn't save the new char, return a failure.
}
// Add a c string till we got it all, or the poor thing is full. If its full and not
// overwriting, your going to loose your '\0'. Just sayin'.. Otherwise if its full and
// overwriting you will loose some of your oldest data.
bool textBuff::addStr(char* inCStr,bool andNULL) {
int
i;
bool success;
success = false; // Well, we ain't a success yet.
i = 0; // Start up our counter..
while(inCStr[i]!='\0') { // While we are not pointing at the NULL char..
success = addChar(inCStr[i]); // Blindly stuff the char into the buffer.
i++; // Increment counter.
}
if (andNULL) { // If they want the null char saved..
success = addChar('\0'); // We add one in. Its the little things we do for you.
}
return success; // Return if they all went in.
}
// Pass back the next char on the list, unless we got nothin' then pass back a /0.
// Don't change anything.
char textBuff::peekHead(void) {
if (empty()) return '\0'; // If empty, return a \0.
return buff[head]; // Otherwise, return the char that's next to read.
}
// Pass back the nTh char on the list, unless we got nothin' then pass back a /0.
// Don't change anything.
char textBuff::peekIndex(int index) {
int absIndex;
if (empty()) return '\0'; // If empty, return a '\0'.
if (index>numChars()) return '\0'; // If we don't have that one, return a '\0'.
absIndex = head + index; // Take a shot at the absolute index
if (absIndex>=numBytes) { // If we are past the buffer size..
absIndex = absIndex - numBytes; // Subtract the buffer size from the absolute.
} //
return buff[absIndex]; // Otherwise, return the char that's next to read.
}
// If not empty, read the next charactor, update the state
// and return the read charactor. Otherwize return a null
// charactor.
char textBuff::readChar(void) {
char theChar;
if (!empty()) { // If we have some chars..
theChar = buff[head]; // Save off the oldest char.
inc(&head); // Do the indexing update.
isFull = false; // In any case we are no longer full.
return theChar; // Return the oldest char.
} else { // Else, we were empty..
return '\0'; // Pass back a \0.
}
}
// Hand back a c string of.. Either the first full string found.
// Or.. All of the text with a '\0' appended to it. If empty, it
// returns Just a c string consisting of '\0'.
char* textBuff::readStr(void) {
int i;
resizeBuff(1,&returnStr); // Resize the buff for empty string.
returnStr[0] = '\0'; // Copy in the \0.
if (!empty() && resizeBuff(strlen()+1,&returnStr)) { // If there are bytes AND we can allocate enough..
i = -1; // Starting at -1.
do { // Repeat..
i++; // Bump up the index. (Now starting at zero)
returnStr[i] = readChar(); // Read a char into our returnStr.
} while(returnStr[i]!='\0'&&!empty()); // If we DIDN'T read a null and STILL have chars..
if (returnStr[i]!='\0') { // If we didn't get a NULL char.
i++; // Bump up the index.
returnStr[i] = '\0'; // Pop a '\0' after the last char.
} //
} //
return returnStr; // Send it on it's way.
}
// Return the numnber of charactors THAT CAN BE stored in this buffer.
int textBuff::buffSize(void) { return numBytes; }
// Return the numnber of charactors THAT ARE stored in the buffer.
int textBuff::numChars(void) {
if (empty()) { // If its empty..
return 0; // Well, that would be zero.
} else if (full()) { // If its full..
return numBytes; // Return the number of bytes we store.
} else if (head<tail) { // Head less that tail.
return tail - head; // Data is between them, return the difference.
} else { // And lastly.. Head is greater than tail..
return numBytes - (head - tail); // Kinda' opposite. Data is between them on the other side.
}
}
// How many chars would we read out if asked for a string? Not including the trailing NULL.
int textBuff::strlen(void) {
int count;
int max;
int strIdx;
if (empty()) return 0; // Well if we got none we give none.
strIdx = head; // Copied from readChar().
max = numChars(); // store the number of chars we have.
count = 0; // And starting at zero chars to read.
while(buff[strIdx]!='\0'&&count<max) { // If we're not pointing at null, and not at the limit of data..
count++; // Bump up the char count.
inc(&strIdx); // Increment the local index.
} //
return count; // And when complete, we return count.
}
// Return whethere the buffer is empty or not.
bool textBuff::empty(void) { return ((head==tail)&&!isFull); }
// Return if the buffer is full or not.
bool textBuff::full(void) { return isFull; }
// Just clear the data NOT release RAM.
void textBuff::clear(void) {
head = 0;
tail = 0;
isFull = false; // In any case we are no longer full. (Missed that in the original)
}
// Increment an index. Hop over to zero if we go past the end.
void textBuff::inc(int* index) {
*index = *index + 1; // Bump up this index.
if (*index>=numBytes) { // If this index went off the end..
*index = 0; // Set it to zero.
}
}