Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERF: Initial erf packer tool #19

Closed
wants to merge 11 commits into from

COMMON: Set and implement a seekable base for memory writer streams

  • Loading branch information...
Nostritius committed Jun 25, 2018
commit 6a49c6ce1c6274a8849e82a0202bcecbe63b69e4
Copy path View file
@@ -78,9 +78,23 @@ size_t MemoryWriteStream::size() const {
return _bufSize;
}

size_t MemoryWriteStream::seek(ptrdiff_t offset, SeekableWriteStream::Origin whence) {
assert((size_t)_pos <= _bufSize);

const size_t oldPos = _pos;
const size_t newPos = evalSeek(offset, whence, _pos, 0, size());
if (newPos > _bufSize)
throw Exception(kSeekError);

_pos = newPos;
_ptr = _ptr - oldPos + newPos;

return oldPos;
}


MemoryWriteStreamDynamic::MemoryWriteStreamDynamic(bool disposeMemory, size_t capacity) :
_data(0, disposeMemory), _ptr(0), _capacity(0), _size(0) {
_data(0, disposeMemory), _ptr(0), _capacity(0), _size(0) {

reserve(capacity);
}
@@ -99,9 +113,10 @@ void MemoryWriteStreamDynamic::reserve(size_t s) {
if (_data)
memcpy(newData, _data.get(), _size);

const size_t oldPos = pos();
_data.dispose();
_data.reset(newData);
_ptr = _data.get() + _size;
_ptr = _data.get() + oldPos;
}

void MemoryWriteStreamDynamic::ensureCapacity(size_t newLen) {
@@ -114,12 +129,14 @@ void MemoryWriteStreamDynamic::ensureCapacity(size_t newLen) {
size_t MemoryWriteStreamDynamic::write(const void *dataPtr, size_t dataSize) {
assert(dataPtr);

ensureCapacity(_size + dataSize);
ensureCapacity(pos() + dataSize);

std::memcpy(_ptr, dataPtr, dataSize);

if (pos() + dataSize > _size)
_size = MAX(pos() + dataSize, _size);

_ptr += dataSize;
_size += dataSize;

return dataSize;
}
@@ -136,10 +153,27 @@ void MemoryWriteStreamDynamic::dispose() {
_capacity = 0;
}

size_t MemoryWriteStreamDynamic::pos() const {
return _ptr - _data.get();
}

size_t MemoryWriteStreamDynamic::size() const {
return _size;
}

size_t MemoryWriteStreamDynamic::seek(ptrdiff_t offset, SeekableWriteStream::Origin whence) {
assert((size_t)pos() <= _size);

const size_t oldPos = pos();
const size_t newPos = evalSeek(offset, whence, pos(), 0, size());
if (newPos > _size)
throw Exception(kSeekError);

_ptr = _data.get() + newPos;

return oldPos;
}

byte *MemoryWriteStreamDynamic::getData() {
return _data.get();
}
Copy path View file
@@ -58,12 +58,12 @@

namespace Common {

/** Simple memory based 'stream', which implements the WriteStream interface for
/** Simple memory based 'stream', which implements the SeekableWriteStream interface for
* a plain memory block.
*
* Writing past the size of the memory block will fail with an exception.
*/
class MemoryWriteStream : boost::noncopyable, public WriteStream {
class MemoryWriteStream : boost::noncopyable, public SeekableWriteStream {
public:
MemoryWriteStream(byte *buf, size_t len) : _ptr(buf), _bufSize(len), _pos(0) { }
~MemoryWriteStream() { }
@@ -79,6 +79,9 @@ class MemoryWriteStream : boost::noncopyable, public WriteStream {
/** Return the total size of the memory block. */
size_t size() const;

/** Seek offset bytes from the origin whence. */
size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin);

private:
byte *_ptr;

@@ -90,7 +93,7 @@ class MemoryWriteStream : boost::noncopyable, public WriteStream {
*
* As long as more memory can be allocated, writing into the stream won't fail.
*/
class MemoryWriteStreamDynamic : boost::noncopyable, public WriteStream {
class MemoryWriteStreamDynamic : boost::noncopyable, public SeekableWriteStream {
public:
MemoryWriteStreamDynamic(bool disposeMemory = false, size_t capacity = 0);
~MemoryWriteStreamDynamic();
@@ -102,9 +105,14 @@ class MemoryWriteStreamDynamic : boost::noncopyable, public WriteStream {
void setDisposable(bool disposeMemory);
void dispose();

/** Return the current writing position within the stream. */
size_t pos() const;
/** Return the number of bytes written to this stream in total. */
size_t size() const;

/** Seek offset bytes from the origin whence. */
size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin);

byte *getData();

private:
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.