1 // ------------------------------------------------
6 // Basic stream handling functions.
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.
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 // ------------------------------------------------
26 #include "chkMemoryLeak.h"
27 #define DEBUG_NEW new(__FILE__, __LINE__)
32 // --------------------------------------------------
33 void MemoryStream::convertFromBase64()
41 out += String::base64WordToChars(out,in);
48 // -------------------------------------
49 void FileStream::openReadOnly(const char *fn)
51 file = fopen(fn,"rb");
54 throw StreamException("Unable to open file");
56 // -------------------------------------
57 void FileStream::openWriteReplace(const char *fn)
59 file = fopen(fn,"wb");
62 throw StreamException("Unable to open file");
64 // -------------------------------------
65 void FileStream::openWriteAppend(const char *fn)
67 file = fopen(fn,"ab");
70 throw StreamException("Unable to open file");
73 // -------------------------------------
74 void FileStream::close()
82 // -------------------------------------
83 void FileStream::rewind()
86 fseek(file,0,SEEK_SET);
88 // -------------------------------------
89 int FileStream::length()
94 int old = ftell(file);
95 fseek(file,0,SEEK_END);
97 fseek(file,old,SEEK_SET);
103 // -------------------------------------
104 bool FileStream::eof()
107 return (feof(file)!=0);
111 // -------------------------------------
112 int FileStream::read(void *ptr, int len)
117 throw StreamException("End of file");
119 int r = (int)fread(ptr,1,len,file);
125 // -------------------------------------
126 void FileStream::write(const void *ptr, int len)
130 fwrite(ptr,1,len,file);
131 updateTotals(0, len);
134 // -------------------------------------
135 void FileStream::flush()
141 // -------------------------------------
142 int FileStream::pos()
149 // -------------------------------------
150 void FileStream::seekTo(int pos)
154 fseek(file,pos,SEEK_SET);
157 // -------------------------------------
158 void Stream::writeTo(Stream &out, int len)
163 int rlen = sizeof(tmp);
173 // -------------------------------------
174 int Stream::writeUTF8(unsigned int code)
183 writeChar(code>>6 | 0xC0);
184 writeChar(code & 0x3F | 0x80);
186 }else if (code < 0x10000)
188 writeChar(code>>12 | 0xE0);
189 writeChar(code>>6 & 0x3F | 0x80);
190 writeChar(code & 0x3F | 0x80);
194 writeChar(code>>18 | 0xF0);
195 writeChar(code>>12 & 0x3F | 0x80);
196 writeChar(code>>6 & 0x3F | 0x80);
197 writeChar(code & 0x3F | 0x80);
203 // -------------------------------------
204 void Stream::skip(int len)
209 int rlen = sizeof(tmp);
219 // -------------------------------------
220 void Stream::updateTotals(unsigned int in, unsigned int out)
223 totalBytesOut += out;
225 unsigned int tdiff = sys->getTime()-lastUpdate;
228 bytesInPerSec = (totalBytesIn-lastBytesIn)/tdiff;
229 bytesOutPerSec = (totalBytesOut-lastBytesOut)/tdiff;
230 lastBytesIn = totalBytesIn;
231 lastBytesOut = totalBytesOut;
232 lastUpdate = sys->getTime();
235 // -------------------------------------
236 int Stream::readLine(char *in, int max)
254 // -------------------------------------
255 void Stream::write(const char *fmt,va_list ap)
258 vsprintf(tmp,fmt,ap);
259 write(tmp,strlen(tmp));
261 // -------------------------------------
262 void Stream::writeStringF(const char *fmt,...)
269 // -------------------------------------
270 void Stream::writeString(const char *str)
272 write(str,strlen(str));
274 // -------------------------------------
275 void Stream::writeLineF(const char *fmt,...)
282 // special thanks:
\96¼
\96³
\82µ
\82Ì
\92N
\82©
291 // -------------------------------------
292 void Stream::writeLine(const char *str)
302 // -------------------------------------
303 int Stream::readWord(char *in, int max)
310 if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'))
313 break; // stop reading
315 continue; // skip whitespace
329 // --------------------------------------------------
330 int Stream::readBase64(char *p, int max)
335 while (cnt < (max-4))
338 int rl = String::base64WordToChars(p,vals);
350 // -------------------------------------
351 int Stream::readBits(int cnt)
358 bitsBuffer = readChar();
362 v |= (bitsBuffer&(1<<(7-bitsPos)))?(1<<cnt):0;
364 bitsPos = (bitsPos+1)&7;