OSDN Git Service

cmd=viewxml時のHTTPレスポンスヘッダを修正
[peercast-im/PeerCastIM.git] / c: / Git / PeerCast.root / PeerCast / core / common / stream.h
1 // ------------------------------------------------
2 // File : stream.h
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //
7 // (c) 2002 peercast.org
8 // ------------------------------------------------
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 // ------------------------------------------------
19
20 #ifndef _STREAM_H
21 #define _STREAM_H
22
23 // -------------------------------------
24
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include "common.h"
29 #include "sys.h"
30 #include "id.h"
31
32 // -------------------------------------
33 class Stream
34 {
35 public:
36         Stream()
37         :writeCRLF(true)
38         ,totalBytesIn(0)
39         ,totalBytesOut(0)
40         ,lastBytesIn(0)
41         ,lastBytesOut(0)
42         ,bytesInPerSec(0)
43         ,bytesOutPerSec(0)
44         ,lastUpdate(0)
45         ,bitsBuffer(0)
46         ,bitsPos(0)
47         {
48         }
49         virtual ~Stream() {}
50
51         virtual int readUpto(void *,int) {return 0;}
52         virtual int read(void *,int)=0;
53         virtual void write(const void *,int) = 0;
54     virtual bool eof()
55     {
56         throw StreamException("Stream can`t eof");
57                 return false;
58     }
59
60         virtual void rewind()
61         {
62         throw StreamException("Stream can`t rewind");
63         }
64
65         virtual void seekTo(int)
66         {
67         throw StreamException("Stream can`t seek");
68         }
69
70         void writeTo(Stream &out, int len);
71         virtual void skip(int i);
72
73         virtual void close()
74         {
75         }
76
77         virtual void    setReadTimeout(unsigned int ) 
78         {
79         }
80         virtual void    setWriteTimeout(unsigned int )
81         {
82         }
83         virtual void    setPollRead(bool)
84         {
85         }
86
87         virtual int             getPosition() {return 0;}
88
89
90         // binary
91     char        readChar()
92     {
93         char v;
94         read(&v,1);
95         return v;
96     }
97     short       readShort()
98     {
99         short v;
100         read(&v,2);
101                 CHECK_ENDIAN2(v);
102         return v;
103     }
104     long        readLong()
105     {
106         long v;
107         read(&v,4);
108                 CHECK_ENDIAN4(v);
109         return v;
110     }
111         int readInt()
112         {
113                 return readLong();
114         }
115         ID4 readID4()
116         {
117                 ID4 id;
118                 read(id.getData(),4);
119                 return id;
120         }
121         int     readInt24()
122         {
123                 int v=0;
124         read(&v,3);
125                 CHECK_ENDIAN3(v);
126         }
127
128
129
130         long readTag()
131         {
132                 long v = readLong();
133                 return SWAP4(v);
134         }
135
136         int     readString(char *s, int max)
137         {
138                 int cnt=0;
139                 while (max)
140                 {
141                         char c = readChar();
142                         *s++ = c;
143                         cnt++;
144                         max--;
145                         if (!c)
146                                 break;
147                 }
148                 return cnt;
149         }
150
151         virtual bool    readReady() {return true;}
152         virtual int numPending() {return 0;}
153
154
155         void writeID4(ID4 id)
156         {
157                 write(id.getData(),4);
158         }
159
160         void    writeChar(char v)
161         {
162                 write(&v,1);
163         }
164         void    writeShort(short v)
165         {
166                 CHECK_ENDIAN2(v);
167                 write(&v,2);
168         }
169         void    writeLong(long v)
170         {
171                 CHECK_ENDIAN4(v);
172                 write(&v,4);
173         }
174         void writeInt(int v) {writeLong(v);}
175
176         void    writeTag(long v)
177         {
178                 //CHECK_ENDIAN4(v);
179                 writeLong(SWAP4(v));
180         }
181
182         void    writeTag(char id[4])
183         {
184                 write(id,4);
185         }
186
187         int     writeUTF8(unsigned int);
188
189         // text
190     int readLine(char *in, int max);
191
192     int         readWord(char *, int);
193         int             readBase64(char *, int);
194
195         void    write(const char *,va_list);
196         void    writeLine(const char *);
197         void    writeLineF(const char *,...);
198         void    writeString(const char *);
199         void    writeStringF(const char *,...);
200
201         bool    writeCRLF;
202
203         int             readBits(int);
204
205         void    updateTotals(unsigned int,unsigned int);
206
207
208         unsigned char bitsBuffer;
209         unsigned int bitsPos;
210
211         unsigned int totalBytesIn,totalBytesOut;
212         unsigned int lastBytesIn,lastBytesOut;
213         unsigned int bytesInPerSec,bytesOutPerSec;
214         unsigned int lastUpdate;
215
216 };
217
218
219 // -------------------------------------
220 class FileStream : public Stream
221 {
222 public:
223         FileStream() {file=NULL;}
224
225     void        openReadOnly(const char *);
226     void        openWriteReplace(const char *);
227     void        openWriteAppend(const char *);
228         bool    isOpen(){return file!=NULL;}
229         int             length();
230         int             pos();
231
232         virtual void    seekTo(int);
233         virtual int             getPosition() {return pos();}
234         virtual void    flush();
235     virtual int         read(void *,int);
236     virtual void        write(const void *,int);
237     virtual bool        eof();
238     virtual void        rewind();
239     virtual void        close();
240
241         FILE *file;
242 };
243 // -------------------------------------
244 class MemoryStream : public Stream
245 {
246 public:
247         MemoryStream()
248         :buf(NULL)
249         ,len(0)
250         ,pos(0)
251         ,own(false)
252         {
253         }
254
255         MemoryStream(void *p, int l)
256         :buf((char *)p)
257         ,len(l)
258         ,pos(0)
259         ,own(false)
260         {
261         }
262
263         MemoryStream(int l)
264         :buf(new char[l])
265         ,len(l)
266         ,pos(0)
267         ,own(true)
268         {
269         }
270
271         ~MemoryStream() {free2();}
272
273         void readFromFile(FileStream &file)
274         {
275                 len = file.length();
276                 buf = new char[len];
277                 own = true;
278                 pos = 0;
279                 file.read(buf,len);
280         }
281
282         void free2()
283         {
284                 if (own && buf)
285                 {
286                         delete buf;
287                         buf = NULL;
288                         own = false;
289                 }
290
291         }
292
293         virtual int read(void *p,int l)
294     {
295                 if (pos+l <= len)
296                 {
297                         memcpy(p,&buf[pos],l);
298                         pos += l;
299                         return l;
300                 }else
301                 {
302                         memset(p,0,l);
303                         return 0;
304                 }
305     }
306
307         virtual void write(const void *p,int l)
308     {
309                 if ((pos+l) > len)
310                         throw StreamException("Stream - premature end of write()");
311                 memcpy(&buf[pos],p,l);
312                 pos += l;
313     }
314
315     virtual bool eof()
316     {
317         return pos >= len;
318     }
319
320         virtual void rewind()
321         {
322                 pos = 0;
323         }
324
325         virtual void seekTo(int p)
326         {
327                 pos = p;
328         }
329
330         virtual int getPosition()
331         {
332                 return pos;
333         }
334
335         void    convertFromBase64();
336
337
338         char *buf;
339         bool own;
340         int len,pos;
341 };
342 // --------------------------------------------------
343 class IndirectStream : public Stream
344 {
345 public:
346
347         void init(Stream *s)
348         {
349                 stream = s;
350         }
351
352         virtual int read(void *p,int l)
353     {
354                 return stream->read(p,l);
355     }
356
357         virtual void write(const void *p,int l)
358     {
359                 stream->write(p,l);
360     }
361
362     virtual bool eof()
363     {
364         return stream->eof();
365     }
366
367     virtual void close()
368     {
369         stream->close();
370     }
371
372         Stream *stream;
373 };
374
375 // -------------------------------------
376
377 class SockBufStream : public Stream
378 {
379 public:
380         SockBufStream(Stream &sockStream, int bufsize=128*1024)
381                 : sock(sockStream), mem(bufsize)
382                 {
383                 }
384
385         ~SockBufStream()
386                 {
387                         flush();
388                         mem.free2();
389                 }
390
391         virtual int read(void *p,int l)
392                 {
393                         return sock.read(p, l);
394                 }
395
396         virtual void write(const void *p, int len)
397                 {
398                         if ( mem.pos+len > mem.len )
399                                 flush();
400
401                         mem.write(p, len);
402                 }
403
404         void flush()
405                 {
406                         if ( mem.pos > 0 ) {
407                                 sock.write(mem.buf, mem.pos);
408                                 clearWriteBuffer();
409                         }
410                 }
411
412         void clearWriteBuffer()
413                 {
414                         mem.rewind();
415                 }
416
417 private:
418         Stream &sock;
419         MemoryStream mem;
420 };
421
422 // -------------------------------------
423 class WriteBufferStream : public Stream
424 {
425 public:
426         WriteBufferStream(Stream *out_)
427         :buf(NULL)
428         ,own(false)
429         ,len(0)
430         ,pos(0)
431         ,out(out_)
432         {
433         }
434
435         WriteBufferStream(void *p, int l, Stream *out_)
436         :buf((char *)p)
437         ,own(false)
438         ,len(l)
439         ,pos(0)
440         ,out(out_)
441         {
442         }
443
444         WriteBufferStream(int l, Stream *out_)
445         :buf(NULL)
446         ,own(true)
447         ,len(l)
448         ,pos(0)
449         ,out(out_)
450         {
451                 buf = (char*)malloc(l);
452                 if (!buf)
453                         throw GeneralException("cannot allocate memory", 0x1234);
454         }
455
456         virtual ~WriteBufferStream()
457         {
458                 try {
459                         flush();
460                 } catch (StreamException &) {}
461                 free();
462         }
463
464         void readFromFile(FileStream &file)
465         {
466                 len = file.length();
467                 buf = new char[len];
468                 own = true;
469                 pos = 0;
470                 file.read(buf,len);
471         }
472
473         void flush()
474         {
475                 if (!out || !buf) return;
476                 out->write(buf, pos);
477                 pos = 0;
478         }
479
480         void free()
481         {
482                 if (own && buf)
483                 {
484                         ::free(buf);
485                         buf = NULL;
486                         own = false;
487                 }
488
489         }
490
491         virtual int read(void *p,int l)
492     {
493                 return 0;
494     }
495
496         virtual void write(const void *p,int l)
497     {
498                 char *cp = (char *) p;
499                 while ((pos + l) >= len) {
500                         int n = len - pos;
501                         memcpy(&buf[pos], cp, n);
502                         l -= n;
503                         cp += n;
504                         pos = len;
505                         flush();
506                         if (pos != 0) return;
507                 }
508                 if (l > 0) {
509                         memcpy(&buf[pos], cp, l);
510                         pos += l;
511                 }
512     }
513
514     virtual bool eof()
515     {
516         return true;
517     }
518
519         virtual void rewind()
520         {
521                 pos = 0;
522         }
523
524         virtual void seekTo(int p)
525         {
526                 pos = p;
527         }
528
529         virtual int getPosition()
530         {
531                 return pos;
532         }
533
534         void    convertFromBase64();
535
536
537         char *buf;
538         bool own;
539         int len,pos;
540         Stream *out;
541 };
542
543 // write\82³\82ê\82½\82à\82Ì\82ð\8eÌ\82Ä\82Ä\83o\83C\83g\90\94\82¾\82¯\83J\83E\83\93\83g\82·\82é\83X\83g\83\8a\81[\83\80
544 class DummyStream : public Stream
545 {
546 private:
547         unsigned long length;
548
549 public:
550         DummyStream() : length(0) {};
551
552         void write(const void *p, int l)
553         {
554                 length += l;
555         }
556
557         unsigned long getLength()
558         {
559                 return length;
560         }
561
562         // dummy functions
563         int read(void *, int) { return 0; }
564 };
565
566 #endif
567