Skip to content

Commit

Permalink
Renamed ReadStream -> SeekableReadStream and added a few compilation …
Browse files Browse the repository at this point in the history
…fixes
  • Loading branch information
sev- committed May 14, 2024
1 parent 3b312d1 commit 44bc1e0
Show file tree
Hide file tree
Showing 26 changed files with 154 additions and 144 deletions.
54 changes: 27 additions & 27 deletions src/common/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ bool Stream::pastEOF() const {
return _pos > _size;
}

/* ReadStream */
/* SeekableReadStream */

BufferView ReadStream::readByteView(size_t len) {
BufferView SeekableReadStream::readByteView(size_t len) {
BufferView res(_data + _pos, len);
_pos += len;
return res;
}

ssize_t ReadStream::read(void *dest, size_t len) {
ssize_t SeekableReadStream::read(void *dest, size_t len) {
if (eof())
return 0;

Expand All @@ -83,11 +83,11 @@ ssize_t ReadStream::read(void *dest, size_t len) {
return len;
}

ssize_t ReadStream::readZlibBytes(size_t len, uint8_t *dest, size_t destLen) {
ssize_t SeekableReadStream::readZlibBytes(size_t len, uint8_t *dest, size_t destLen) {
size_t p = _pos;
_pos += len;
if (pastEOF()) {
throw std::runtime_error("ReadStream::readZlibBytes: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readZlibBytes: Read past end of stream!");
}

unsigned long outLen = destLen;
Expand All @@ -100,89 +100,89 @@ ssize_t ReadStream::readZlibBytes(size_t len, uint8_t *dest, size_t destLen) {
return outLen;
}

uint8_t ReadStream::readByte() {
uint8_t SeekableReadStream::readByte() {
size_t p = _pos;
_pos += 1;
if (pastEOF()) {
throw std::runtime_error("ReadStream::readByte: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readByte: Read past end of stream!");
}

return _data[p];
}

int8_t ReadStream::readSByte() {
int8_t SeekableReadStream::readSByte() {
return (int8_t)readByte();
}

uint16_t ReadStream::readUint16() {
uint16_t SeekableReadStream::readUint16() {
size_t p = _pos;
_pos += 2;
if (pastEOF()) {
throw std::runtime_error("ReadStream::readUint16: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readUint16: Read past end of stream!");
}

return endianness
? boost::endian::load_little_u16(&_data[p])
: boost::endian::load_big_u16(&_data[p]);
}

int16_t ReadStream::readInt16() {
int16_t SeekableReadStream::readInt16() {
return (int16_t)readUint16();
}

uint16_t ReadStream::readUint16BE() {
uint16_t SeekableReadStream::readUint16BE() {
size_t p = _pos;

_pos += 2;

if (pastEOF()) {
throw std::runtime_error("ReadStream::readUint16BE: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readUint16BE: Read past end of stream!");
}

return (_data[p] << 8 | _data[p+1]);
}

int16_t ReadStream::readSint16BE() {
int16_t SeekableReadStream::readSint16BE() {
return (int16_t)readUint16BE();
}

uint32_t ReadStream::readUint32() {
uint32_t SeekableReadStream::readUint32() {
size_t p = _pos;
_pos += 4;
if (pastEOF()) {
throw std::runtime_error("ReadStream::readUint32: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readUint32: Read past end of stream!");
}

return endianness
? boost::endian::load_little_u32(&_data[p])
: boost::endian::load_big_u32(&_data[p]);
}

int32_t ReadStream::readInt32() {
int32_t SeekableReadStream::readInt32() {
return (int32_t)readUint32();
}

uint32_t ReadStream::readUint32BE() {
uint32_t SeekableReadStream::readUint32BE() {
size_t p = _pos;

_pos += 4;

if (pastEOF()) {
throw std::runtime_error("ReadStream::readUint32BE: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readUint32BE: Read past end of stream!");
}

return (_data[p] << 24 | _data[p+1] << 16 | _data[p+2] << 8 | _data[p+3]);
}

int32_t ReadStream::readSint32BE() {
int32_t SeekableReadStream::readSint32BE() {
return (int32_t)readUint32BE();
}

double ReadStream::readDouble() {
double SeekableReadStream::readDoubleBE() {
size_t p = _pos;
_pos += 4;
if (pastEOF()) {
throw std::runtime_error("ReadStream::readDouble: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readDouble: Read past end of stream!");
}

uint64_t f64bin = endianness
Expand All @@ -192,7 +192,7 @@ double ReadStream::readDouble() {
return *(double *)(&f64bin);
}

uint32_t ReadStream::readVarInt() {
uint32_t SeekableReadStream::readVarInt() {
uint32_t val = 0;
uint8_t b;
do {
Expand All @@ -202,11 +202,11 @@ uint32_t ReadStream::readVarInt() {
return val;
}

std::string ReadStream::readString(size_t len) {
std::string SeekableReadStream::readString(size_t len) {
size_t p = _pos;
_pos += len;
if (pastEOF()) {
throw std::runtime_error("ReadStream::readString: Read past end of stream!");
throw std::runtime_error("SeekableReadStream::readString: Read past end of stream!");
}

char *str = new char[len + 1];
Expand All @@ -217,7 +217,7 @@ std::string ReadStream::readString(size_t len) {
return res;
}

std::string ReadStream::readCString() {
std::string SeekableReadStream::readCString() {
std::string res;
char ch = readSByte();
while (ch) {
Expand All @@ -227,7 +227,7 @@ std::string ReadStream::readCString() {
return res;
}

std::string ReadStream::readPascalString() {
std::string SeekableReadStream::readPascalString() {
uint8_t len = readByte();
return readString(len);
}
Expand Down
10 changes: 5 additions & 5 deletions src/common/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ class Stream : public BufferView {
bool pastEOF() const;
};

/* ReadStream */
/* SeekableReadStream */

class ReadStream : public Stream {
class SeekableReadStream : public Stream {
public:
ReadStream(uint8_t *d, size_t s, Endianness e = kBigEndian, size_t p = 0)
SeekableReadStream(uint8_t *d, size_t s, Endianness e = kBigEndian, size_t p = 0)
: Stream(d, s, e, p) {}

ReadStream(const BufferView &view, Endianness e = kBigEndian, size_t p = 0)
SeekableReadStream(const BufferView &view, Endianness e = kBigEndian, size_t p = 0)
: Stream(view, e, p) {}

BufferView readByteView(size_t len);
Expand All @@ -88,7 +88,7 @@ class ReadStream : public Stream {
int16_t readInt16();
uint32_t readUint32();
int32_t readInt32();
double readDouble();
double readDoubleBE();
uint32_t readVarInt();
std::string readString(size_t len);
std::string readCString();
Expand Down
7 changes: 6 additions & 1 deletion src/common/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <sstream>

#include "common/util.h"
#include "common/log.h"

namespace Common {

Expand Down Expand Up @@ -80,7 +81,7 @@ std::string escapeString(const char *str, size_t size) {
return res;
}

std::string escapeString(std::string str) {
std::string escapeString(Common::String str) {
return escapeString(str.c_str(), str.size());
}

Expand All @@ -98,3 +99,7 @@ int compareIgnoreCase(const std::string &a, const std::string &b) {
}

} // namespace Common

void warning(const Common::String &msg) {
Common::warning(msg);
}
9 changes: 8 additions & 1 deletion src/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@

#include <cstdint>
#include <string>
#include "common/str.h"
#include "common/log.h"

#define FOURCC(a0, a1, a2, a3) ((uint32_t)((a3) | ((a2) << 8) | ((a1) << 16) | ((a0) << 24)))

#define STR_INNER(x) #x
#define STR(x) STR_INNER(x)

#define toPrintable(s) escapeString(s)

namespace Common {

std::string fourCCToString(uint32_t fourcc);
std::string floatToString(double f);
std::string byteToString(uint8_t byte);
std::string escapeString(const char *str, size_t size);
std::string escapeString(std::string str);
std::string escapeString(Common::String str);
int stricmp(const char *a, const char *b);
int compareIgnoreCase(const std::string &a, const std::string &b);

Expand All @@ -42,4 +46,7 @@ struct Pair {

} // namespace Common

void warning(const Common::String &msg);


#endif // COMMON_UTIL_H
4 changes: 2 additions & 2 deletions src/director/castmember.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Director {

/* CastMember */

void CastMember::read(Common::ReadStream&) {}
void CastMember::read(Common::SeekableReadStream&) {}

void CastMember::writeJSON(Common::JSONWriter &json) const {
json.startObject();
Expand All @@ -21,7 +21,7 @@ void CastMember::writeJSON(Common::JSONWriter &json) const {

/* ScriptMember */

void ScriptMember::read(Common::ReadStream &stream) {
void ScriptMember::read(Common::SeekableReadStream &stream) {
scriptType = static_cast<ScriptType>(stream.readUint16());
}

Expand Down
6 changes: 3 additions & 3 deletions src/director/castmember.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Common {
class JSONWriter;
class ReadStream;
class SeekableReadStream;
}

namespace Director {
Expand Down Expand Up @@ -38,7 +38,7 @@ struct CastMember {

CastMember(DirectorFile *d, MemberType t) : dir(d), type(t) {}
virtual ~CastMember() = default;
virtual void read(Common::ReadStream &stream);
virtual void read(Common::SeekableReadStream &stream);
virtual void writeJSON(Common::JSONWriter &json) const;
};

Expand All @@ -53,7 +53,7 @@ struct ScriptMember : CastMember {

ScriptMember(DirectorFile *m) : CastMember(m, kScriptMember) {}
virtual ~ScriptMember() = default;
virtual void read(Common::ReadStream &stream);
virtual void read(Common::SeekableReadStream &stream);
virtual void writeJSON(Common::JSONWriter &json) const;
};

Expand Down
Loading

0 comments on commit 44bc1e0

Please sign in to comment.