~drizzle-trunk/drizzle/development

1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
2
 *
3
 * PrimeBase Media Stream for MySQL
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
18
 *
19
 * Original Author: Paul McCullagh
20
 * Continued development: Barry Leslie
21
 *
22
 * 2007-06-07
23
 *
24
 * CORE SYSTEM:
25
 * Basic input and output streams.
26
 *
27
 * These objects wrap the system streams, and simplify things.
28
 * I also want to standardize exceptions and implement
29
 * socket based streams.
30
 *
31
 */
32
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
33
#pragma once
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
34
#ifndef __CSSTREAM_H__
35
#define __CSSTREAM_H__
36
37
#define DEFAULT_BUFFER_SIZE		64000
38
39
#include "CSDefs.h"
40
#include "CSFile.h"
41
#include "CSSocket.h"
42
43
class CSInputStream : public CSRefObject {
44
public:
45
	CSInputStream() { }
46
	virtual ~CSInputStream() { }
47
48
	/*
49
	 * Closes this input stream and releases any system
50
	 * resources associated  with the stream.
51
	 */
52
	virtual void close() = 0;
53
54
	/*
55
	 * Reads up to len bytes of data from the input stream into an
56
	 * array of bytes. Return the number of bytes read.
57
	 *
58
	 * Zero will be returned in the case of EOF.
59
	 *
60
	 * This call blocks until at least one byte is
61
	 * returned.
62
	 */
63
	virtual size_t read(char *b, size_t len) = 0;
64
65
	/* Reads the next byte of data from the input stream.
66
	 * Returns -1 if EOF.
67
	 */
68
	virtual int read() = 0;
69
70
	/* Read one character ahead. */
71
	virtual int peek() = 0;
72
73
	/*
74
	 *  Reset this output stream to the start inorder to restart the write.
75
	 */
76
	virtual void reset() = 0; 
77
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
78
	/* Return the name of the file, or whatever: */
79
	virtual const char *identify() = 0;
80
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
81
	/*
82
	 * Read a line from the input stream. This function
83
	 * handles all types of line endings. The function
84
	 * return NULL on EOF.
85
	 */
86
	CSStringBuffer *readLine();
87
};
88
89
class CSOutputStream : public CSRefObject {
90
public:
91
 	/*
92
	 * Closes this input stream and releases any system
93
	 * resources associated  with the stream.
94
	 */
95
	virtual void close() = 0;
96
97
	/*
98
	 * Writes len bytes from the specified byte array starting at
99
	 * offset off to this output stream.
100
	 */
101
	virtual void write(const char *b, size_t len) = 0;
102
103
	/*
104
	 * Returns the default EOL indicator.
105
	 * Will be \n, \r or \r\n.
106
	 */
107
	virtual const char *getEOL() = 0;
108
109
	/*
110
	 *  Flushes this output stream and forces any buffered output
111
	 * bytes to be written out.
112
	 */
113
	virtual void flush() = 0;
114
115
	/*
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
116
	 * Writes the specified byte to this output stream.
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
117
	 */
118
	virtual void write(char b) = 0; 
119
120
	/*
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
121
	 * Reset this output stream to the start inorder to restart the write.
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
122
	 */
123
	virtual void reset() = 0; 
124
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
125
	virtual const char *identify() = 0;
126
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
127
	/*
128
	 * Write a line. Terminator is specific to the
129
	 * type of output stream and may depend on the
130
	 * current platform.
131
	 */
132
	void printLine(const char *cstr);
133
134
	/*
135
	 * Various write types:
136
	 */
137
	virtual void print(const char *cstr);
138
	virtual void print(CSString *s);
139
	virtual void print(int value);
140
	virtual void print(uint64_t value);
141
};
142
143
class CSStream : public CSObject {
144
public:
145
	static void pipe(CSOutputStream *out, CSInputStream *in);
146
};
147
148
/* File Stream: */
149
150
class CSFileInputStream : public CSInputStream {
151
public:
152
	CSFileInputStream(): iFile(NULL), iReadOffset(0) { }
153
	virtual ~CSFileInputStream();
154
155
	virtual void close();
156
157
	virtual size_t read(char *b, size_t len);
158
159
	virtual int read();
160
161
	virtual int peek();
162
163
	/*
164
	 *  Reset this output stream to the start inorder to restart the read.
165
	 */
166
	virtual void reset(); 
167
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
168
	virtual const char *identify();
169
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
170
	static CSFileInputStream *newStream(CSFile* f);
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
171
	static CSFileInputStream *newStream(CSFile* f, off64_t offset);
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
172
173
private:
174
	CSFile	*iFile;
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
175
	off64_t	iReadOffset;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
176
};
177
178
class CSFileOutputStream : public CSOutputStream {
179
public:
180
	CSFileOutputStream(): iFile(NULL), iWriteOffset(0) { }
181
	virtual ~CSFileOutputStream();
182
183
	virtual void close();
184
185
	virtual void write(const char *b, size_t len);
186
187
	virtual const char *getEOL();
188
189
	virtual void flush();
190
191
	virtual void write(char b);
192
193
	virtual void reset(); 
194
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
195
	virtual const char *identify();
196
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
197
	static CSFileOutputStream *newStream(CSFile* f);
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
198
	static CSFileOutputStream *newStream(CSFile* f, off64_t offset);
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
199
200
private:
201
	CSFile	*iFile;
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
202
	off64_t	iWriteOffset;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
203
};
204
205
/* Socket Stream */
206
207
class CSSocketInputStream : public CSInputStream {
208
public:
209
	CSSocketInputStream(): iSocket(NULL) { }
210
	virtual ~CSSocketInputStream();
211
212
	virtual void close();
213
214
	virtual size_t read(char *b, size_t len);
215
216
	virtual int read();
217
218
	virtual int peek();
219
220
	virtual void reset(); 
221
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
222
	virtual const char *identify();
223
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
224
	static CSSocketInputStream *newStream(CSSocket *s);
225
226
private:
227
	CSSocket* iSocket;
228
};
229
230
class CSSocketOutputStream : public CSOutputStream {
231
public:
232
	CSSocketOutputStream(): iSocket(NULL) { }
233
	virtual ~CSSocketOutputStream();
234
235
	virtual void close();
236
237
	virtual void write(const char *b, size_t len);
238
239
	virtual const char *getEOL() { return "\n"; };
240
241
	virtual void flush();
242
243
	virtual void write(char b);
244
245
	virtual void reset(); 
246
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
247
	virtual const char *identify();
248
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
249
	static CSSocketOutputStream *newStream(CSSocket *s);
250
251
private:
252
	CSSocket* iSocket;
253
};
254
255
/* Buffered Stream: */
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
256
#ifdef DEBUG_disabled
257
#define CS_STREAM_BUFFER_SIZE			80
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
258
#else
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
259
#define CS_STREAM_BUFFER_SIZE			(32 * 1024)
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
260
#endif
261
262
class CSBufferedInputStream : public CSInputStream {
263
public:
264
	CSBufferedInputStream(): iStream(NULL), iBuffTotal(0), iBuffPos(0) { }
265
	virtual ~CSBufferedInputStream();
266
267
	virtual void close();
268
269
	virtual size_t read(char *b, size_t len);
270
271
	virtual int read();
272
273
	virtual int peek();
274
275
	virtual void reset(); 
276
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
277
	virtual const char *identify();
278
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
279
	static CSBufferedInputStream *newStream(CSInputStream* i);
280
281
private:
282
	CSInputStream* iStream;
283
	u_char iBuffer[CS_STREAM_BUFFER_SIZE];
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
284
	uint32_t iBuffTotal;
285
	uint32_t iBuffPos;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
286
};
287
288
class CSBufferedOutputStream : public CSOutputStream {
289
public:
290
	CSBufferedOutputStream(): iStream(NULL), iBuffTotal(0) { }
291
	virtual ~CSBufferedOutputStream();
292
293
	virtual void close();
294
295
	virtual void write(const char *b, size_t len);
296
297
	virtual const char *getEOL() { return "\n"; };
298
299
	virtual void flush();
300
301
	virtual void write(char b);
302
303
	virtual void reset(); 
304
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
305
	virtual const char *identify();
306
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
307
	static CSBufferedOutputStream *newStream(CSOutputStream* i);
308
309
private:
310
	CSOutputStream* iStream;
311
	u_char iBuffer[CS_STREAM_BUFFER_SIZE];
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
312
	uint32_t iBuffTotal;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
313
};
314
315
/* memory Stream */
316
class CSMemoryInputStream : public CSInputStream {
317
public:
318
	CSMemoryInputStream(): iMemory(NULL), iMemTotal(0), iMemPos(0) { }
319
	~CSMemoryInputStream(){}
320
321
	virtual void close() {}
322
323
	virtual size_t read(char *b, size_t len)
324
	{
325
		if (len > (iMemTotal - iMemPos))
326
			len = iMemTotal - iMemPos;
327
		
328
		memcpy(b, iMemory + iMemPos, len);
329
		iMemPos += len;	
330
		return len;
331
	}
332
333
	virtual int read()
334
	{
335
		int b = -1;
336
		if (iMemPos < iMemTotal) 
337
			b = iMemory[iMemPos++];
338
		return b;
339
	}
340
341
	virtual int peek()
342
	{
343
		int b = -1;
344
		if (iMemPos < iMemTotal) 
345
			b = iMemory[iMemPos];
346
		return b;
347
	}
348
349
	virtual void reset() {iMemPos = 0;}
350
	
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
351
	virtual const char *identify() 
352
	{
353
		return "memory stream";
354
	}
355
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
356
	static CSMemoryInputStream *newStream(const u_char* buffer, uint32_t length);
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
357
358
private:
359
	const u_char *iMemory;
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
360
	uint32_t iMemTotal;
361
	uint32_t iMemPos;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
362
};
363
364
365
class CSMemoryOutputStream : public CSOutputStream {
366
public:
367
	CSMemoryOutputStream(): iMemory(NULL), iMemTotal(0), iMemSpace(0), iMemMin(0), iMemPos(NULL){ }
368
	virtual ~CSMemoryOutputStream();
369
370
	virtual void close() {}
371
372
	virtual void write(const char *b, size_t len);
373
	virtual const char *getEOL() { return "\n"; };
374
375
	virtual void flush() {}
376
377
	virtual void write(char b);
378
379
	const u_char *getMemory(size_t *len)
380
	{
381
		*len = iMemPos - iMemory;
382
		return iMemory;
383
	}
384
	
385
	virtual void reset();
386
	
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
387
	virtual const char *identify();
388
	
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
389
	static CSMemoryOutputStream *newStream(size_t init_length, size_t min_alloc);
390
391
private:
392
	u_char *iMemory;
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
393
	uint32_t iMemTotal;
394
	uint32_t iMemSpace;
395
	uint32_t iMemMin;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
396
	u_char *iMemPos;
397
};
398
399
class CSStaticMemoryOutputStream : public CSOutputStream {
400
public:
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
401
	CSStaticMemoryOutputStream(u_char *mem, off64_t size): iMemory(mem), iMemSpace(size), iMemSize(size), iMemPos(mem){ }
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
402
	virtual ~CSStaticMemoryOutputStream() {}
403
404
	virtual void close() {}
405
406
	virtual void write(const char *b, size_t len);
407
	virtual const char *getEOL() { return "\n"; };
408
409
	virtual void flush() {}
410
411
	virtual void write(char b);
412
	
413
	virtual void reset() 
414
	{
415
		iMemPos = iMemory;
416
		iMemSpace = iMemSize;
417
	}
418
	
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
419
	virtual const char *identify() 
420
	{
421
		return "memory stream";
422
	}
423
	
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
424
	off64_t getSize() { return iMemPos - iMemory; }
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
425
426
private:
427
	u_char *iMemory;
1548.2.2 by Barry.Leslie at PrimeBase
A lot of minor changes to clean up the code and to get it to build with Drizzle.
428
	off64_t iMemSpace;
429
	off64_t iMemSize;
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
430
	u_char *iMemPos;
431
};
432
433
typedef size_t (* CSStreamReadCallbackFunc) (void *caller_data, char *buffer, size_t buffer_size, u_char reset);
434
435
class CSCallbackInputStream : public CSInputStream {
436
public:
437
	CSCallbackInputStream(): callback(NULL), cb_data(NULL), havePeek(false), doReset(false) { }
438
	~CSCallbackInputStream(){}
439
440
	virtual void close() {}
441
442
	virtual size_t read(char *b, size_t len)
443
	{
444
		size_t size = 0;
445
		
446
		if (havePeek) {
447
			havePeek = false;
448
			*b =  peek_char;
449
			b++; len--;
450
			if (len) {
451
				size = callback(cb_data, b, len, doReset);
452
			}
453
				
454
			size++;			
455
		} else
456
			size = callback(cb_data, b, len, doReset);
457
			
458
		if (doReset)
459
			doReset = false;
460
			
461
		return size;
462
	}
463
464
	virtual int read()
465
	{
466
		char c;
467
		
468
		if (havePeek) {
469
			havePeek = false;
470
			return peek_char;
471
		}
472
		if (!callback(cb_data, &c, 1, doReset))
473
			c = -1;
474
			
475
		if (doReset)
476
			doReset = false;
477
		
478
		return c;
479
	}
480
481
	virtual int peek()
482
	{
483
		if (!havePeek) {
484
			if (callback(cb_data, &peek_char, 1, doReset))
485
				havePeek = true;
486
			else
487
				return -1;
488
		}
489
		return peek_char;
490
	}
491
492
	virtual void reset() 
493
	{
494
		havePeek = false;
495
		doReset = false;
496
	}
497
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
498
	virtual const char *identify() 
499
	{
500
		return "callback stream";
501
	}
502
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
503
	static CSCallbackInputStream *newStream(CSStreamReadCallbackFunc callback, void *user_data);
504
505
private:
506
	CSStreamReadCallbackFunc callback;
507
	void *cb_data;
508
	char peek_char;
509
	bool havePeek;
510
	bool doReset;
511
};
1841.1.3 by Barry.Leslie at PrimeBase
Merged changes from lp:pbms. These changes should remove any danger
512
1548.2.1 by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin.
513
#endif
514
515