forked from mandovinnie/Lute-Tab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_buf.cc
292 lines (242 loc) · 6.3 KB
/
i_buf.cc
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "win.h"
#include <stdlib.h>
#include "tab.h"
#include "i_buf.h"
int my_min(int a, int b);
short my_create(const char *name, int m_mode);
i_buf::i_buf()
{
dbg0(Proceedure, "starting i_buf");
start = new link;
// fprintf(stderr, "create %X\n", start);
if ( start == NULL)
dbg0(Error, "i_buf, new failed\n");
start->prev = NULL;
start->next = NULL;
start->bytes = bytes = (char *)malloc(BUFSIZ);
buf_l = start;
read_ptr = 0;
size = BUFSIZ;
num_bytes = 0;
dbg2(Proceedure, "ptr %d size %d\n",
(void *)read_ptr, (void *)size);
// dbg3(Warning, "starting i_buf ptr %d size %d bufsiz %d\n",
// (void *)read_ptr, (void *)size, (void *)BUFSIZ);
}
void i_buf::more_buffer()
{
dbg0(Flow, "more_buffer: \n");
// dbg1(Warning, "more_buffer: size %d\n", (void *) size);
buf_l->next = new link;
if (buf_l->next == NULL)
dbg0(Error, "more, new failed\n");
// dbg2(Warning, "more_buffer: size %d, addr %X",
// (void *) size, (void *)buf_l->next);
buf_l->next->prev = buf_l;
buf_l->next->next = NULL;
buf_l->next->bytes = (char *) malloc(BUFSIZ);
if (buf_l->next->bytes == NULL)
dbg0(Error, "more, new failed\n");
buf_l = buf_l->next;
bytes = buf_l->bytes;
size +=BUFSIZ;
// dbg1(Warning, "new size %d\n", (void *) size);
}
i_buf::~i_buf()
{
link *ttt;
dbg2(Proceedure, "ending i_buf ptr %d size %d\n",
(void *)read_ptr, (void *)size);
// fprintf(stderr, "delete %X\n", start);
ttt = start;
while ( ttt->next )
ttt = ttt->next;
while (ttt->prev) {
free (ttt->bytes);
ttt = ttt->prev;
free (ttt->next);
}
/* the following 3 lines were commented out - on feb 8 08 I uncommented it */
if (start->bytes)
free (start->bytes);
delete start;
}
void i_buf::PutByte(const char c)
{
if (read_ptr >= size) {
dbg1(Flow, "PutByte: size %d\n", (void *)read_ptr);
more_buffer();
}
bytes[read_ptr%BUFSIZ] = c;
// dbg2(Warning, "put_byte: size %d %c\n",
// (void *)read_ptr, (void *)c);
read_ptr++;
num_bytes++;
return;
}
unsigned char i_buf::GetByte()
{
unsigned char c;
// dbg2(Warning, "i_buf: GetByte: read_ptr %d size %d",
// (void *)read_ptr,
// (void *)size);
if (read_ptr >= num_bytes) {
/* eof jump here */
return((unsigned char )EOF);
}
if (read_ptr >= size /* wbc july 96 - 1 */) {
dbg1(Error, "i_buf: GetByte: overflow %d\n", (void *)read_ptr);
return (0);
}
buf_l = start;
if (read_ptr >= BUFSIZ) {
int j = read_ptr/BUFSIZ;
while (j-- && buf_l->next) {
buf_l = buf_l->next;
}
}
c = buf_l->bytes[read_ptr%BUFSIZ];
// dbg2(Warning, " c %c %d\n", (void *)c, (void *)c);
read_ptr++;
return (c);
}
unsigned char i_buf::unGet(const char c)
{
read_ptr--;
buf_l = start;
// fprintf(stderr, "unGet %d\n", read_ptr);
if (read_ptr >= BUFSIZ) {
int j = read_ptr/BUFSIZ;
while (j-- && buf_l->next) {
buf_l = buf_l->next;
}
}
buf_l->bytes[read_ptr%BUFSIZ] = c;
return (c);
}
char * i_buf::GetLine(char *buf, int buflen)
{
return (buffer::GetLine(buf, buflen));
}
//void i_buf::PutLine(char * l)
//{
// buffer::PutLine(l);
//}
void i_buf::SignedQuad(int c)
{
PutByte((unsigned)((c & 0xff000000) >> 24 & 0xff));
PutByte((unsigned)((c & 0xff0000) >> 16 & 0xff));
PutByte((unsigned)((c & 0xff00) >> 8 & 0xff));
PutByte((unsigned)((c & 0xff) & 0xff));
}
void i_buf::SignedTrio(int c)
{
PutByte((unsigned)(c & 0xff0000) >> 16 & 0xff);
PutByte((unsigned)(c & 0xff00) >> 8 & 0xff);
PutByte((unsigned)(c & 0xff) & 0xff);
}
void i_buf::SignedPair(unsigned short c)
{
PutByte((unsigned)(c & 0xff00) >> 8 & 0xff);
PutByte((unsigned)(c & 0xff) & 0xff);
}
int i_buf::Seek(const long offset, const int how)
{
switch (how) {
case rew:
if (offset > BUFSIZ)
dbg2(Error, "Seek cant do rew offset %u how %d\n",
(void *)offset, (void *)how);
buf_l = start;
bytes = buf_l->bytes;
read_ptr = offset;
break;
case cur:
dbg2(Error, "Seek cant do cur offset %u how %d\n",
(void *)offset, (void *)how);
read_ptr += offset;
break;
case end:
dbg2(Error, "Seek cant do endoffset %u how %d\n",
(void *)offset, (void *)how);
read_ptr = size - offset;
break;
}
if (read_ptr > size) return (0);
if (read_ptr < 0) return (0);
return (1);
}
int my_min(int a, int b)
{
return( a < b ? a:b);
}
void i_buf::dump(const char *fname, const mode mode) // dump to a file
{
dbg2(Flow, "Dump; writing %s size %d\n",
(void *)fname, (void *)num_bytes);
#ifdef MAC
short refNum;
OSErr error;
int n_written, file_bytes;
long int wsize;
int m_mode;
Seek(rew, 0);
if (mode == Creat) m_mode = 1;
else if (mode == Append) m_mode = 2;
file_bytes = num_bytes;
refNum = my_create(fname, m_mode);
dbg1(Flow, "i_buf: dump: %s", (void *)fname);
while (file_bytes > 0) {
wsize = my_min(file_bytes, BUFSIZ);
error = FSWrite(refNum, &wsize, buf_l->bytes);
if (wsize == BUFSIZ && buf_l->next) buf_l = buf_l->next;
file_bytes -= wsize;
}
error = FSClose(refNum);
dbg1(Warning, "i_buff::dump: done%c", (void *)NEWLINE);
#else /* not MAC */
static FILE *fp;
int n_written, file_bytes;
// printf("i_buf: dump: %s\n", fname);
Seek(rew, 0);
if (!strcmp(fname, "stdout")) { // write to stdout
if (mode == Creat)
#ifdef BUILD_FOR_WINDOWS
fp = _fdopen(1, "wb");
#else
fp = fdopen(1, "wb");
#endif
}
/*
else if (!strncmp(fname, "stdpipe", 7)) { // write to a pipe
printf("i_buf: dump: writing to a pipe \n");
if (mode == Creat)
#ifdef BUILD_FOR_WINDOWS
fp = _fdopen(1, "wb");
#else
fp = fdopen(1, "wb");
#endif
}
*/
else {
if (mode == Creat)
fp = fopen(fname, "wb");
else if (mode == Append)
fp = fopen(fname, "ab");
}
if (fp == NULL) {
dbg1(Error, "tab: i_buf: dump: can't open %s for output\n", (void *)fname);
}
file_bytes = num_bytes;
while (file_bytes > 0) {
n_written = fwrite(buf_l->bytes, 1, my_min(file_bytes, BUFSIZ), fp);
if (buf_l->next) buf_l = buf_l->next;
file_bytes -= n_written;
dbg1(Flow, "%u bytes left\n", (void *)file_bytes);
}
if (strcmp(fname, "stdout"))
fclose(fp);
else if (mode == Append)
fclose(fp);
#endif
}