forked from teamnwah/openmw-tes3coop
Added memory_stream.h, rewrote tests
This commit is contained in:
parent
69e8f9c9db
commit
0c18c4db03
6 changed files with 73 additions and 58 deletions
60
stream/servers/memory_stream.h
Normal file
60
stream/servers/memory_stream.h
Normal file
|
@ -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
|
|
@ -6,13 +6,13 @@ I_OGRE=$(shell pkg-config --cflags OGRE)
|
|||
L_OGRE=$(shell pkg-config --libs OGRE)
|
||||
L_AUDIERE=-laudiere
|
||||
|
||||
ogre_client_test: ogre_client_test.cpp dummy_input.cpp ../stream.h ../clients/iwrapper.h ../clients/ogre_datastream.h
|
||||
ogre_client_test: ogre_client_test.cpp ../stream.h ../clients/iwrapper.h ../clients/ogre_datastream.h
|
||||
$(GCC) $< -o $@ $(I_OGRE) $(L_OGRE)
|
||||
|
||||
audiere_client_test: audiere_client_test.cpp dummy_input.cpp ../stream.h ../clients/iwrapper.h ../clients/audiere_file.h ../clients/audiere_file.cpp
|
||||
audiere_client_test: audiere_client_test.cpp ../stream.h ../clients/iwrapper.h ../clients/audiere_file.h ../clients/audiere_file.cpp
|
||||
$(GCC) $< -o $@ ../clients/audiere_file.cpp $(L_AUDIERE)
|
||||
|
||||
dummy_test: dummy_test.cpp dummy_input.cpp ../stream.h
|
||||
memory_test: memory_test.cpp ../stream.h ../servers/memory_stream.h
|
||||
$(GCC) $< -o $@
|
||||
|
||||
clean:
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
#include "dummy_input.cpp"
|
||||
#include "../servers/memory_stream.h"
|
||||
#include "../clients/audiere_file.h"
|
||||
#include <audiere.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Mangle::Stream;
|
||||
using namespace audiere;
|
||||
using namespace std;
|
||||
|
||||
|
@ -10,7 +11,7 @@ int main()
|
|||
{
|
||||
char str[12];
|
||||
memset(str, 0, 12);
|
||||
Stream *inp = new DummyInput();
|
||||
Stream *inp = new MemoryStream("hello world", 11);
|
||||
FilePtr p(new AudiereFile(inp, true));
|
||||
cout << "pos=" << p->tell() << endl;
|
||||
p->read(str, 2);
|
||||
|
|
|
@ -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";
|
|
@ -1,13 +1,14 @@
|
|||
#include "dummy_input.cpp"
|
||||
#include "../servers/memory_stream.h"
|
||||
#include "ogre_datastream.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace Mangle::Stream;
|
||||
using namespace Ogre;
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
Stream *inp = new DummyInput();
|
||||
Stream *inp = new MemoryStream("hello world", 11);
|
||||
DataStreamPtr p(new MangleDataStream("hello", inp, true));
|
||||
cout << "Name: " << p->getName() << endl;
|
||||
cout << "As string: " << p->getAsString() << endl;
|
||||
|
|
Loading…
Reference in a new issue