1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 20:23:53 +00:00
openmw/stream/servers/memory_stream.h

61 lines
1.1 KiB
C
Raw Normal View History

2009-12-29 15:16:33 +00:00
#ifndef MANGLE_STREAM_MEMSERVER_H
#define MANGLE_STREAM_MEMSERVER_H
#include <assert.h>
#include "../stream.h"
namespace Mangle {
namespace Stream {
/** A Stream wrapping a memory buffer
This will create a fully seekable stream out any pointer/length
pair you give it.
*/
class MemoryStream : public Stream
{
const void *data;
size_t length, pos;
public:
MemoryStream(const void *ptr, size_t len)
: data(ptr), length(len), pos(0)
{
isSeekable = true;
hasPosition = true;
hasSize = true;
}
size_t read(void *buf, size_t count)
{
assert(pos <= length);
// Don't read more than we have
if(count > (length - pos))
count = length - pos;
// Copy data
if(count)
memcpy(buf, ((char*)data)+pos, count);
// aaand remember to increase the count
pos += count;
return count;
}
void seek(size_t _pos)
{
pos = _pos;
if(pos > length)
pos = length;
}
size_t tell() const { return pos; }
size_t size() const { return length; }
bool eof() const { return pos == length; }
};
}} // namespaces
#endif