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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
*
* PrimeBase Media Stream for MySQL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Original Author: Paul McCullagh
* Continued development: Barry Leslie
*
* 2007-06-07
*
* CORE SYSTEM:
* Basic input and output streams.
*
* These objects wrap the system streams, and simplify things.
* I also want to standardize exceptions and implement
* socket based streams.
*
*/
#ifndef __CSSTREAM_H__
#define __CSSTREAM_H__
#define DEFAULT_BUFFER_SIZE 64000
#include "CSDefs.h"
#include "CSFile.h"
#include "CSSocket.h"
class CSInputStream : public CSRefObject {
public:
CSInputStream() { }
virtual ~CSInputStream() { }
/*
* Closes this input stream and releases any system
* resources associated with the stream.
*/
virtual void close() = 0;
/*
* Reads up to len bytes of data from the input stream into an
* array of bytes. Return the number of bytes read.
*
* Zero will be returned in the case of EOF.
*
* This call blocks until at least one byte is
* returned.
*/
virtual size_t read(char *b, size_t len) = 0;
/* Reads the next byte of data from the input stream.
* Returns -1 if EOF.
*/
virtual int read() = 0;
/* Read one character ahead. */
virtual int peek() = 0;
/*
* Reset this output stream to the start inorder to restart the write.
*/
virtual void reset() = 0;
/* Return the name of the file, or whatever: */
virtual const char *identify() = 0;
/*
* Read a line from the input stream. This function
* handles all types of line endings. The function
* return NULL on EOF.
*/
CSStringBuffer *readLine();
};
class CSOutputStream : public CSRefObject {
public:
/*
* Closes this input stream and releases any system
* resources associated with the stream.
*/
virtual void close() = 0;
/*
* Writes len bytes from the specified byte array starting at
* offset off to this output stream.
*/
virtual void write(const char *b, size_t len) = 0;
/*
* Returns the default EOL indicator.
* Will be \n, \r or \r\n.
*/
virtual const char *getEOL() = 0;
/*
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
virtual void flush() = 0;
/*
* Writes the specified byte to this output stream.
*/
virtual void write(char b) = 0;
/*
* Reset this output stream to the start inorder to restart the write.
*/
virtual void reset() = 0;
virtual const char *identify() = 0;
/*
* Write a line. Terminator is specific to the
* type of output stream and may depend on the
* current platform.
*/
void printLine(const char *cstr);
/*
* Various write types:
*/
virtual void print(const char *cstr);
virtual void print(CSString *s);
virtual void print(int value);
virtual void print(uint64_t value);
};
class CSStream : public CSObject {
public:
static void pipe(CSOutputStream *out, CSInputStream *in);
};
/* File Stream: */
class CSFileInputStream : public CSInputStream {
public:
CSFileInputStream(): iFile(NULL), iReadOffset(0) { }
virtual ~CSFileInputStream();
virtual void close();
virtual size_t read(char *b, size_t len);
virtual int read();
virtual int peek();
/*
* Reset this output stream to the start inorder to restart the read.
*/
virtual void reset();
virtual const char *identify();
static CSFileInputStream *newStream(CSFile* f);
static CSFileInputStream *newStream(CSFile* f, off64_t offset);
private:
CSFile *iFile;
off64_t iReadOffset;
};
class CSFileOutputStream : public CSOutputStream {
public:
CSFileOutputStream(): iFile(NULL), iWriteOffset(0) { }
virtual ~CSFileOutputStream();
virtual void close();
virtual void write(const char *b, size_t len);
virtual const char *getEOL();
virtual void flush();
virtual void write(char b);
virtual void reset();
virtual const char *identify();
static CSFileOutputStream *newStream(CSFile* f);
static CSFileOutputStream *newStream(CSFile* f, off64_t offset);
private:
CSFile *iFile;
off64_t iWriteOffset;
};
/* Socket Stream */
class CSSocketInputStream : public CSInputStream {
public:
CSSocketInputStream(): iSocket(NULL) { }
virtual ~CSSocketInputStream();
virtual void close();
virtual size_t read(char *b, size_t len);
virtual int read();
virtual int peek();
virtual void reset();
virtual const char *identify();
static CSSocketInputStream *newStream(CSSocket *s);
private:
CSSocket* iSocket;
};
class CSSocketOutputStream : public CSOutputStream {
public:
CSSocketOutputStream(): iSocket(NULL) { }
virtual ~CSSocketOutputStream();
virtual void close();
virtual void write(const char *b, size_t len);
virtual const char *getEOL() { return "\n"; };
virtual void flush();
virtual void write(char b);
virtual void reset();
virtual const char *identify();
static CSSocketOutputStream *newStream(CSSocket *s);
private:
CSSocket* iSocket;
};
/* Buffered Stream: */
#ifdef DEBUG_disabled
#define CS_STREAM_BUFFER_SIZE 80
#else
#define CS_STREAM_BUFFER_SIZE (32 * 1024)
#endif
class CSBufferedInputStream : public CSInputStream {
public:
CSBufferedInputStream(): iStream(NULL), iBuffTotal(0), iBuffPos(0) { }
virtual ~CSBufferedInputStream();
virtual void close();
virtual size_t read(char *b, size_t len);
virtual int read();
virtual int peek();
virtual void reset();
virtual const char *identify();
static CSBufferedInputStream *newStream(CSInputStream* i);
private:
CSInputStream* iStream;
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
uint32_t iBuffTotal;
uint32_t iBuffPos;
};
class CSBufferedOutputStream : public CSOutputStream {
public:
CSBufferedOutputStream(): iStream(NULL), iBuffTotal(0) { }
virtual ~CSBufferedOutputStream();
virtual void close();
virtual void write(const char *b, size_t len);
virtual const char *getEOL() { return "\n"; };
virtual void flush();
virtual void write(char b);
virtual void reset();
virtual const char *identify();
static CSBufferedOutputStream *newStream(CSOutputStream* i);
private:
CSOutputStream* iStream;
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
uint32_t iBuffTotal;
};
/* memory Stream */
class CSMemoryInputStream : public CSInputStream {
public:
CSMemoryInputStream(): iMemory(NULL), iMemTotal(0), iMemPos(0) { }
~CSMemoryInputStream(){}
virtual void close() {}
virtual size_t read(char *b, size_t len)
{
if (len > (iMemTotal - iMemPos))
len = iMemTotal - iMemPos;
memcpy(b, iMemory + iMemPos, len);
iMemPos += len;
return len;
}
virtual int read()
{
int b = -1;
if (iMemPos < iMemTotal)
b = iMemory[iMemPos++];
return b;
}
virtual int peek()
{
int b = -1;
if (iMemPos < iMemTotal)
b = iMemory[iMemPos];
return b;
}
virtual void reset() {iMemPos = 0;}
virtual const char *identify()
{
return "memory stream";
}
static CSMemoryInputStream *newStream(const u_char* buffer, uint32_t length);
private:
const u_char *iMemory;
uint32_t iMemTotal;
uint32_t iMemPos;
};
class CSMemoryOutputStream : public CSOutputStream {
public:
CSMemoryOutputStream(): iMemory(NULL), iMemTotal(0), iMemSpace(0), iMemMin(0), iMemPos(NULL){ }
virtual ~CSMemoryOutputStream();
virtual void close() {}
virtual void write(const char *b, size_t len);
virtual const char *getEOL() { return "\n"; };
virtual void flush() {}
virtual void write(char b);
const u_char *getMemory(size_t *len)
{
*len = iMemPos - iMemory;
return iMemory;
}
virtual void reset();
virtual const char *identify();
static CSMemoryOutputStream *newStream(size_t init_length, size_t min_alloc);
private:
u_char *iMemory;
uint32_t iMemTotal;
uint32_t iMemSpace;
uint32_t iMemMin;
u_char *iMemPos;
};
class CSStaticMemoryOutputStream : public CSOutputStream {
public:
CSStaticMemoryOutputStream(u_char *mem, off64_t size): iMemory(mem), iMemSpace(size), iMemSize(size), iMemPos(mem){ }
virtual ~CSStaticMemoryOutputStream() {}
virtual void close() {}
virtual void write(const char *b, size_t len);
virtual const char *getEOL() { return "\n"; };
virtual void flush() {}
virtual void write(char b);
virtual void reset()
{
iMemPos = iMemory;
iMemSpace = iMemSize;
}
virtual const char *identify()
{
return "memory stream";
}
off64_t getSize() { return iMemPos - iMemory; }
private:
u_char *iMemory;
off64_t iMemSpace;
off64_t iMemSize;
u_char *iMemPos;
};
typedef size_t (* CSStreamReadCallbackFunc) (void *caller_data, char *buffer, size_t buffer_size, u_char reset);
class CSCallbackInputStream : public CSInputStream {
public:
CSCallbackInputStream(): callback(NULL), cb_data(NULL), havePeek(false), doReset(false) { }
~CSCallbackInputStream(){}
virtual void close() {}
virtual size_t read(char *b, size_t len)
{
size_t size = 0;
if (havePeek) {
havePeek = false;
*b = peek_char;
b++; len--;
if (len) {
size = callback(cb_data, b, len, doReset);
}
size++;
} else
size = callback(cb_data, b, len, doReset);
if (doReset)
doReset = false;
return size;
}
virtual int read()
{
char c;
if (havePeek) {
havePeek = false;
return peek_char;
}
if (!callback(cb_data, &c, 1, doReset))
c = -1;
if (doReset)
doReset = false;
return c;
}
virtual int peek()
{
if (!havePeek) {
if (callback(cb_data, &peek_char, 1, doReset))
havePeek = true;
else
return -1;
}
return peek_char;
}
virtual void reset()
{
havePeek = false;
doReset = false;
}
virtual const char *identify()
{
return "callback stream";
}
static CSCallbackInputStream *newStream(CSStreamReadCallbackFunc callback, void *user_data);
private:
CSStreamReadCallbackFunc callback;
void *cb_data;
char peek_char;
bool havePeek;
bool doReset;
};
#endif
|