Added memory_stream.h, rewrote tests
parent
69e8f9c9db
commit
0c18c4db03
@ -0,0 +1,60 @@
|
||||
#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
|
@ -1,48 +0,0 @@
|
||||
// This file is shared between several test programs
|
||||
#include "../stream.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace Mangle::Stream;
|
||||
|
||||
// A simple dummy stream
|
||||
const char _data[12] = "hello world";
|
||||
|
||||
class DummyInput : public Stream
|
||||
{
|
||||
private:
|
||||
int pos;
|
||||
|
||||
public:
|
||||
DummyInput() : pos(0)
|
||||
{
|
||||
isSeekable = true;
|
||||
hasPosition = true;
|
||||
hasSize = true;
|
||||
}
|
||||
|
||||
size_t read(void *buf, size_t count)
|
||||
{
|
||||
assert(pos >= 0 && pos <= 11);
|
||||
if(count+pos > 11)
|
||||
count = 11-pos;
|
||||
assert(count <= 11);
|
||||
|
||||
memcpy(buf, _data+pos, count);
|
||||
pos += count;
|
||||
|
||||
assert(pos >= 0 && pos <= 11);
|
||||
return count;
|
||||
}
|
||||
|
||||
void seek(size_t npos)
|
||||
{
|
||||
if(npos > 11) npos = 11;
|
||||
pos = npos;
|
||||
}
|
||||
|
||||
size_t tell() const { return pos; }
|
||||
size_t size() const { return 11; }
|
||||
|
||||
bool eof() const { return pos == 11; }
|
||||
};
|
@ -1,13 +1,14 @@
|
||||
#include "dummy_input.cpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
#include "../servers/memory_stream.h"
|
||||
|
||||
using namespace Mangle::Stream;
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
Stream *inp = new DummyInput();
|
||||
Stream *inp = new MemoryStream("hello world", 11);
|
||||
|
||||
cout << "Size: " << inp->size() << endl;
|
||||
cout << "Pos: " << inp->tell() << "\nSeeking...\n";
|
Loading…
Reference in New Issue