mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-15 14:18:37 +00:00
AGS: Added MemoryStream
Found upstream e3208df5afeed6cef118dc71a0af45cfce72f4d7
This commit is contained in:
parent
46ddd20145
commit
f982d82cc7
@ -81,6 +81,7 @@ MODULE_OBJS = \
|
||||
shared/util/ini_util.o \
|
||||
shared/util/lzw.o \
|
||||
shared/util/misc.o \
|
||||
shared/util/memory_stream.o \
|
||||
shared/util/multi_file_lib.o \
|
||||
shared/util/path.o \
|
||||
shared/util/proxy_stream.o \
|
||||
|
155
engines/ags/shared/util/memory_stream.cpp
Normal file
155
engines/ags/shared/util/memory_stream.cpp
Normal file
@ -0,0 +1,155 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ags/lib/std/algorithm.h"
|
||||
#include "ags/shared/util/memory_stream.h"
|
||||
|
||||
namespace AGS3 {
|
||||
namespace AGS {
|
||||
namespace Shared {
|
||||
|
||||
MemoryStream::MemoryStream(const std::vector<char> &cbuf, DataEndianess stream_endianess)
|
||||
: DataStream(stream_endianess)
|
||||
, _cbuf(&cbuf.front())
|
||||
, _len(cbuf.size())
|
||||
, _buf(nullptr)
|
||||
, _mode(kStream_Read)
|
||||
, _pos(0) {
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(const String &cbuf, DataEndianess stream_endianess)
|
||||
: DataStream(stream_endianess)
|
||||
, _cbuf(cbuf.GetCStr())
|
||||
, _len(cbuf.GetLength())
|
||||
, _buf(nullptr)
|
||||
, _mode(kStream_Read)
|
||||
, _pos(0) {
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(std::vector<char> &buf, StreamWorkMode mode, DataEndianess stream_endianess)
|
||||
: DataStream(stream_endianess)
|
||||
, _len(buf.size())
|
||||
, _buf(&buf)
|
||||
, _mode(mode)
|
||||
, _pos(buf.size()) {
|
||||
_cbuf = (mode == kStream_Read) ? &buf.front() : nullptr;
|
||||
}
|
||||
|
||||
MemoryStream::~MemoryStream() {
|
||||
}
|
||||
|
||||
void MemoryStream::Close() {
|
||||
_cbuf = nullptr;
|
||||
_buf = nullptr;
|
||||
_pos = -1;
|
||||
}
|
||||
|
||||
bool MemoryStream::Flush() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MemoryStream::IsValid() const {
|
||||
return _cbuf != nullptr || _buf != nullptr;
|
||||
}
|
||||
|
||||
bool MemoryStream::EOS() const {
|
||||
return _pos >= _len;
|
||||
}
|
||||
|
||||
soff_t MemoryStream::GetLength() const {
|
||||
return _len;
|
||||
}
|
||||
|
||||
soff_t MemoryStream::GetPosition() const {
|
||||
return _pos;
|
||||
}
|
||||
|
||||
bool MemoryStream::CanRead() const {
|
||||
return (_cbuf != nullptr) && (_mode == kStream_Read);
|
||||
}
|
||||
|
||||
bool MemoryStream::CanWrite() const {
|
||||
return (_buf != nullptr) && (_mode == kStream_Write);
|
||||
}
|
||||
|
||||
bool MemoryStream::CanSeek() const {
|
||||
return CanRead(); // TODO: support seeking in writable stream?
|
||||
}
|
||||
|
||||
size_t MemoryStream::Read(void *buffer, size_t size) {
|
||||
if (EOS()) {
|
||||
return 0;
|
||||
}
|
||||
soff_t remain = _len - _pos;
|
||||
assert(remain > 0);
|
||||
size_t read_sz = std::min((size_t)remain, size);
|
||||
memcpy(buffer, _cbuf + _pos, read_sz);
|
||||
_pos += read_sz;
|
||||
return read_sz;
|
||||
}
|
||||
|
||||
int32_t MemoryStream::ReadByte() {
|
||||
if (EOS()) {
|
||||
return -1;
|
||||
}
|
||||
return _cbuf[(size_t)(_pos++)];
|
||||
}
|
||||
|
||||
size_t MemoryStream::Write(const void *buffer, size_t size) {
|
||||
if (!_buf) {
|
||||
return 0;
|
||||
}
|
||||
_buf->resize(_buf->size() + size);
|
||||
memcpy(_buf->data() + _pos, buffer, size);
|
||||
_pos += size;
|
||||
_len += size;
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t MemoryStream::WriteByte(uint8_t val) {
|
||||
if (!_buf) {
|
||||
return -1;
|
||||
}
|
||||
_buf->push_back(val);
|
||||
_pos++; _len++;
|
||||
return val;
|
||||
}
|
||||
|
||||
bool MemoryStream::Seek(soff_t offset, StreamSeek origin) {
|
||||
if (!CanSeek()) {
|
||||
return false;
|
||||
}
|
||||
switch (origin) {
|
||||
case kSeekBegin: _pos = 0 + offset; break;
|
||||
case kSeekCurrent: _pos = _pos + offset; break;
|
||||
case kSeekEnd: _pos = _len + offset; break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
_pos = std::max<soff_t>(0, _pos);
|
||||
_pos = std::min<soff_t>(_len, _pos); // clamp to EOS
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Shared
|
||||
} // namespace AGS
|
||||
} // namespace AGS3
|
93
engines/ags/shared/util/memory_stream.h
Normal file
93
engines/ags/shared/util/memory_stream.h
Normal file
@ -0,0 +1,93 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
//=============================================================================
|
||||
//
|
||||
// MemoryStream does reading and writing over the buffer of chars stored in
|
||||
// memory. Currently has rather trivial implementation. Does not own a buffer
|
||||
// itself, but works with the provided std::vector reference, which means that
|
||||
// the buffer *must* persist until stream is closed.
|
||||
// TODO: perhaps accept const char* for reading mode, for compatibility with
|
||||
// the older code, and maybe to let also read String objects?
|
||||
// TODO: separate StringStream for reading & writing String object?
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
#ifndef AGS_SHARED_UTIL_MEMORY_STREAM_H
|
||||
#define AGS_SHARED_UTIL_MEMORY_STREAM_H
|
||||
|
||||
#include "ags/lib/std/vector.h"
|
||||
#include "ags/shared/util/data_stream.h"
|
||||
#include "ags/shared/util/string.h"
|
||||
|
||||
namespace AGS3 {
|
||||
namespace AGS {
|
||||
namespace Shared {
|
||||
|
||||
class MemoryStream : public DataStream {
|
||||
public:
|
||||
// Construct memory stream in the read-only mode over a const std::vector;
|
||||
// vector must persist in memory until the stream is closed.
|
||||
MemoryStream(const std::vector<char> &cbuf, DataEndianess stream_endianess = kLittleEndian);
|
||||
// Construct memory stream in the read-only mode over a const String;
|
||||
// String object must persist in memory until the stream is closed.
|
||||
MemoryStream(const String &cbuf, DataEndianess stream_endianess = kLittleEndian);
|
||||
// Construct memory stream in the chosen mode over a given std::vector;
|
||||
// vector must persist in memory until the stream is closed.
|
||||
MemoryStream(std::vector<char> &buf, StreamWorkMode mode, DataEndianess stream_endianess = kLittleEndian);
|
||||
~MemoryStream() override;
|
||||
|
||||
void Close() override;
|
||||
bool Flush() override;
|
||||
|
||||
// Is stream valid (underlying data initialized properly)
|
||||
bool IsValid() const override;
|
||||
// Is end of stream
|
||||
bool EOS() const override;
|
||||
// Total length of stream (if known)
|
||||
soff_t GetLength() const override;
|
||||
// Current position (if known)
|
||||
soff_t GetPosition() const override;
|
||||
bool CanRead() const override;
|
||||
bool CanWrite() const override;
|
||||
bool CanSeek() const override;
|
||||
|
||||
size_t Read(void *buffer, size_t size) override;
|
||||
int32_t ReadByte() override;
|
||||
size_t Write(const void *buffer, size_t size) override;
|
||||
int32_t WriteByte(uint8_t b) override;
|
||||
|
||||
bool Seek(soff_t offset, StreamSeek origin) override;
|
||||
|
||||
private:
|
||||
const char *_cbuf;
|
||||
size_t _len;
|
||||
std::vector<char> *_buf;
|
||||
const StreamWorkMode _mode;
|
||||
soff_t _pos;
|
||||
};
|
||||
|
||||
} // namespace Shared
|
||||
} // namespace AGS
|
||||
} // namespace AGS3
|
||||
|
||||
#endif
|
@ -43,6 +43,12 @@ namespace AGS3 {
|
||||
namespace AGS {
|
||||
namespace Shared {
|
||||
|
||||
// TODO: merge with FileWorkMode (historical mistake)
|
||||
enum StreamWorkMode {
|
||||
kStream_Read,
|
||||
kStream_Write
|
||||
};
|
||||
|
||||
class Stream : public IAGSStream {
|
||||
public:
|
||||
virtual ~Stream() {}
|
||||
|
Loading…
Reference in New Issue
Block a user