2019-01-18 17:04:17 +01:00
|
|
|
#include "bbstream.hpp"
|
2019-01-18 21:26:57 +01:00
|
|
|
#include <set>
|
2014-01-31 08:23:00 +13:00
|
|
|
|
2019-01-18 21:26:57 +01:00
|
|
|
static std::set<bbStream*> stream_set;
|
2014-01-31 08:23:00 +13:00
|
|
|
|
2019-01-18 21:26:57 +01:00
|
|
|
#ifdef _DEBUG
|
2019-01-18 17:04:17 +01:00
|
|
|
void debugStream(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (stream_set.count(s))
|
|
|
|
|
return;
|
|
|
|
|
ThrowRuntimeException("Stream does not exist");
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
2019-01-18 21:26:57 +01:00
|
|
|
#else
|
|
|
|
|
#define debugStream
|
|
|
|
|
#endif
|
2014-01-31 08:23:00 +13:00
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
bbStream::bbStream()
|
|
|
|
|
{
|
|
|
|
|
stream_set.insert(this);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
bbStream::~bbStream()
|
|
|
|
|
{
|
|
|
|
|
stream_set.erase(this);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
int bbEof(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
2014-01-31 08:23:00 +13:00
|
|
|
return s->eof();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
int bbReadAvail(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
2014-01-31 08:23:00 +13:00
|
|
|
return s->avail();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
int bbReadByte(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
int n = 0;
|
|
|
|
|
s->read((char*)&n, 1);
|
2014-01-31 08:23:00 +13:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
int bbReadShort(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
int n = 0;
|
|
|
|
|
s->read((char*)&n, 2);
|
2014-01-31 08:23:00 +13:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
int bbReadInt(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
int n = 0;
|
|
|
|
|
s->read((char*)&n, 4);
|
2014-01-31 08:23:00 +13:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
float bbReadFloat(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
float n = 0;
|
|
|
|
|
s->read((char*)&n, 4);
|
2014-01-31 08:23:00 +13:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
BBStr* bbReadString(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
int len;
|
|
|
|
|
BBStr* str = new BBStr();
|
|
|
|
|
if (s->read((char*)&len, 4)) {
|
|
|
|
|
char* buff = new char[len];
|
|
|
|
|
if (s->read(buff, len)) {
|
2019-01-18 21:26:57 +01:00
|
|
|
*str = std::string(buff, len);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
delete[] buff;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
BBStr* bbReadLine(bbStream* s)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
2014-01-31 08:23:00 +13:00
|
|
|
unsigned char c;
|
2019-01-18 17:04:17 +01:00
|
|
|
BBStr* str = new BBStr();
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (s->read((char*)&c, 1) != 1)
|
|
|
|
|
break;
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
break;
|
|
|
|
|
if (c != '\r')
|
|
|
|
|
*str += c;
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbWriteByte(bbStream* s, int n)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
s->write((char*)&n, 1);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbWriteShort(bbStream* s, int n)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
s->write((char*)&n, 2);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbWriteInt(bbStream* s, int n)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
s->write((char*)&n, 4);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbWriteFloat(bbStream* s, float n)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
s->write((char*)&n, 4);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbWriteString(bbStream* s, BBStr* t)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
int n = t->size();
|
|
|
|
|
s->write((char*)&n, 4);
|
|
|
|
|
s->write(t->data(), t->size());
|
2014-01-31 08:23:00 +13:00
|
|
|
delete t;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbWriteLine(bbStream* s, BBStr* t)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
debugStream(s);
|
|
|
|
|
s->write(t->data(), t->size());
|
|
|
|
|
s->write("\r\n", 2);
|
2014-01-31 08:23:00 +13:00
|
|
|
delete t;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void bbCopyStream(bbStream* s, bbStream* d, int buff_size)
|
|
|
|
|
{
|
|
|
|
|
if (debug) {
|
|
|
|
|
debugStream(s);
|
|
|
|
|
debugStream(d);
|
|
|
|
|
if (buff_size < 1 || buff_size > 1024 * 1024)
|
|
|
|
|
ThrowRuntimeException("Illegal buffer size");
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
2019-01-18 17:04:17 +01:00
|
|
|
char* buff = new char[buff_size];
|
|
|
|
|
while (s->eof() == 0 && d->eof() == 0) {
|
|
|
|
|
int n = s->read(buff, buff_size);
|
|
|
|
|
d->write(buff, n);
|
|
|
|
|
if (n < buff_size)
|
|
|
|
|
break;
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|
|
|
|
|
delete buff;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
bool stream_create()
|
|
|
|
|
{
|
2014-01-31 08:23:00 +13:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
bool stream_destroy()
|
|
|
|
|
{
|
2014-01-31 08:23:00 +13:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 17:04:17 +01:00
|
|
|
void stream_link(void (*rtSym)(const char*, void*))
|
|
|
|
|
{
|
|
|
|
|
rtSym("%Eof%stream", bbEof);
|
|
|
|
|
rtSym("%ReadAvail%stream", bbReadAvail);
|
|
|
|
|
rtSym("%ReadByte%stream", bbReadByte);
|
|
|
|
|
rtSym("%ReadShort%stream", bbReadShort);
|
|
|
|
|
rtSym("%ReadInt%stream", bbReadInt);
|
|
|
|
|
rtSym("#ReadFloat%stream", bbReadFloat);
|
|
|
|
|
rtSym("$ReadString%stream", bbReadString);
|
|
|
|
|
rtSym("$ReadLine%stream", bbReadLine);
|
|
|
|
|
rtSym("WriteByte%stream%byte", bbWriteByte);
|
|
|
|
|
rtSym("WriteShort%stream%short", bbWriteShort);
|
|
|
|
|
rtSym("WriteInt%stream%int", bbWriteInt);
|
|
|
|
|
rtSym("WriteFloat%stream#float", bbWriteFloat);
|
|
|
|
|
rtSym("WriteString%stream$string", bbWriteString);
|
|
|
|
|
rtSym("WriteLine%stream$string", bbWriteLine);
|
|
|
|
|
rtSym("CopyStream%src_stream%dest_stream%buffer_size=16384", bbCopyStream);
|
2014-01-31 08:23:00 +13:00
|
|
|
}
|