OSDN Git Service

COUT切断機能を追加
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast / core / common / stream.cpp
1 // ------------------------------------------------
2 // File : stream.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc:
6 //              Basic stream handling functions. 
7 //
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
20
21
22 #include "stream.h"
23 #include "common.h"
24 #include "sys.h"
25 #ifdef _DEBUG
26 #include "chkMemoryLeak.h"
27 #define DEBUG_NEW new(__FILE__, __LINE__)
28 #define new DEBUG_NEW
29 #endif
30
31
32 // --------------------------------------------------
33 void MemoryStream::convertFromBase64()
34 {       
35         char *out = buf;
36         char *in = buf;
37         
38         int rl=len;
39     while(rl >= 4) 
40         {
41                 out += String::base64WordToChars(out,in);
42                 in += 4;
43         rl -= 4;
44     }
45     *out = 0;
46         len = out-buf;
47 }
48 // -------------------------------------
49 void FileStream::openReadOnly(const char *fn)
50 {
51         file = fopen(fn,"rb");
52
53     if (!file)
54         throw StreamException("Unable to open file");
55 }
56 // -------------------------------------
57 void FileStream::openWriteReplace(const char *fn)
58 {
59         file = fopen(fn,"wb");
60
61         if (!file)
62         throw StreamException("Unable to open file");
63 }
64 // -------------------------------------
65 void FileStream::openWriteAppend(const char *fn)
66 {
67         file = fopen(fn,"ab");
68
69         if (!file)
70                 throw StreamException("Unable to open file");
71 }
72
73 // -------------------------------------
74 void FileStream::close()
75 {
76         if (file)
77         {
78                 fclose(file);
79                 file = NULL;
80         }
81 }
82 // -------------------------------------
83 void FileStream::rewind()
84 {
85         if (file)
86                 fseek(file,0,SEEK_SET);
87 }
88 // -------------------------------------
89 int FileStream::length()
90 {
91         int len = 0;
92         if (file)
93         {
94                 int old = ftell(file);
95                 fseek(file,0,SEEK_END);
96                 len = ftell(file);
97                 fseek(file,old,SEEK_SET);
98
99         }
100         return len;
101 }
102
103 // -------------------------------------
104 bool FileStream::eof()
105 {
106         if (file)
107                 return (feof(file)!=0);
108         else
109                 return true;
110 }
111 // -------------------------------------
112 int FileStream::read(void *ptr, int len)
113 {
114         if (!file)
115                 return 0;
116         if (feof(file))
117         throw StreamException("End of file");
118
119         int r = (int)fread(ptr,1,len,file);
120
121         updateTotals(r, 0);
122     return r;
123 }
124
125 // -------------------------------------
126 void FileStream::write(const void *ptr, int len)
127 {
128         if (!file)
129                 return;
130     fwrite(ptr,1,len,file);
131         updateTotals(0, len);
132
133 }
134 // -------------------------------------
135 void FileStream::flush()
136 {
137         if (!file)
138                 return;
139     fflush(file);
140 }
141 // -------------------------------------
142 int FileStream::pos()
143 {
144         if (!file)
145                 return 0;
146     return ftell(file);
147 }
148
149 // -------------------------------------
150 void FileStream::seekTo(int pos)
151 {
152         if (!file)
153                 return;
154         fseek(file,pos,SEEK_SET);
155 }
156
157 // -------------------------------------
158 void Stream::writeTo(Stream &out, int len)
159 {
160         char tmp[4096];
161         while (len)
162         {
163                 int rlen = sizeof(tmp);
164                 if (rlen > len)
165                         rlen = len;
166
167                 read(tmp,rlen);
168                 out.write(tmp,rlen);
169
170                 len-=rlen;
171         }
172 }
173 // -------------------------------------
174 int     Stream::writeUTF8(unsigned int code)
175 {
176         if (code < 0x80)
177         {
178                 writeChar(code);
179                 return 1;
180         }else 
181         if (code < 0x0800)
182         {
183                 writeChar(code>>6 | 0xC0);
184                 writeChar(code & 0x3F | 0x80);
185                 return 2;
186         }else if (code < 0x10000)
187         {
188                 writeChar(code>>12 | 0xE0);
189                 writeChar(code>>6 & 0x3F | 0x80);
190                 writeChar(code & 0x3F | 0x80);
191                 return 3;
192         }else 
193         {
194                 writeChar(code>>18 | 0xF0);
195                 writeChar(code>>12 & 0x3F | 0x80);
196                 writeChar(code>>6 & 0x3F | 0x80);
197                 writeChar(code & 0x3F | 0x80);
198                 return 4;
199         }
200                 
201 }
202
203 // -------------------------------------
204 void Stream::skip(int len)
205 {
206         char tmp[4096];
207         while (len)
208         {
209                 int rlen = sizeof(tmp);
210                 if (rlen > len)
211                         rlen = len;
212                 read(tmp,rlen);
213                 len-=rlen;
214         }
215
216 }
217
218
219 // -------------------------------------
220 void Stream::updateTotals(unsigned int in, unsigned int out)
221 {
222         totalBytesIn += in;
223         totalBytesOut += out;
224
225         unsigned int tdiff = sys->getTime()-lastUpdate;
226         if (tdiff >= 5)
227         {
228                 bytesInPerSec = (totalBytesIn-lastBytesIn)/tdiff;
229                 bytesOutPerSec = (totalBytesOut-lastBytesOut)/tdiff;
230                 lastBytesIn = totalBytesIn;
231                 lastBytesOut = totalBytesOut;
232                 lastUpdate = sys->getTime();
233         }
234 }
235 // -------------------------------------
236 int     Stream::readLine(char *in, int max)
237 {
238     int i=0;
239         max -= 2;
240
241         while(max--)
242     {                
243         char c;         
244         read(&c,1);
245                 if (c == '\n')
246                         break;
247                 if (c == '\r')
248                         continue;
249         in[i++] = c;
250     }
251     in[i] = 0;
252         return i;
253 }
254 // -------------------------------------
255 void Stream::write(const char *fmt,va_list ap)
256 {
257         char tmp[4096];
258         vsprintf(tmp,fmt,ap);
259     write(tmp,strlen(tmp));
260 }
261 // -------------------------------------
262 void Stream::writeStringF(const char *fmt,...)
263 {
264         va_list ap;
265         va_start(ap, fmt);
266         write(fmt,ap);
267         va_end(ap);     
268 }
269 // -------------------------------------
270 void Stream::writeString(const char *str)
271 {
272         write(str,strlen(str));
273 }
274 // -------------------------------------
275 void Stream::writeLineF(const char *fmt,...)
276 {
277         va_list ap;
278         va_start(ap, fmt);
279
280         write(fmt,ap);
281
282         // special thanks: \96¼\96³\82µ\82Ì\92N\82©
283         va_end(ap);
284
285         if (writeCRLF)
286             write("\r\n",2);
287         else
288                 write("\n",1);
289 }
290
291 // -------------------------------------
292 void Stream::writeLine(const char *str)
293 {
294         writeString(str);
295
296         if (writeCRLF)
297             write("\r\n",2);
298         else
299                 write("\n",1);
300 }
301
302 // -------------------------------------
303 int     Stream::readWord(char *in, int max)
304 {
305         int i=0;
306     while (!eof())
307     {
308         char c = readChar();
309
310         if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'))
311         {
312                 if (i)
313                 break;          // stop reading
314             else
315                         continue;       // skip whitespace
316         }
317
318         if (i >= (max-1))
319                 break;
320
321         in[i++] = c;
322     }
323
324         in[i]=0;
325     return i;
326 }
327
328
329 // --------------------------------------------------
330 int Stream::readBase64(char *p, int max)
331 {       
332         char vals[4];
333
334         int cnt=0;
335         while (cnt < (max-4))
336         {
337                 read(vals,4);
338                 int rl = String::base64WordToChars(p,vals);
339                 if (!rl)
340                         break;
341
342                 p+=rl;
343                 cnt+=rl;
344         }
345         *p = 0;
346         return cnt;
347 }
348
349
350 // -------------------------------------
351 int Stream::readBits(int cnt)
352 {
353         int v = 0;
354
355         while (cnt)
356         {
357                 if (!bitsPos)
358                         bitsBuffer = readChar();
359
360                 cnt--;
361
362                 v |= (bitsBuffer&(1<<(7-bitsPos)))?(1<<cnt):0;
363
364                 bitsPos = (bitsPos+1)&7;
365         }
366
367         return v;
368 }