git-svn-id: https://profuse.googlecode.com/svn/branches/v2@126 aa027e90-d47c-11dd-86d7-074df07e0730

This commit is contained in:
ksherlock 2009-12-07 22:19:24 +00:00
parent dcd31997ed
commit 5c48dcb813
2 changed files with 170 additions and 0 deletions

98
DataWriter.cpp Normal file
View File

@ -0,0 +1,98 @@
#include "DataWriter.h"
#include "Endian.h"
#include <cstring>
using namespace ProFUSE;
DataWriter::DataWriter(unsigned size)
{
_size = size;
_release = true;
_offset = 0;
_buffer = new uint8_t[size];
}
DataWriter::DataWriter(unsigned size, void *buffer)
{
_size = size;
_buffer = (uint8_t *)buffer;
_release = false;
_offset = 0;
}
DataWriter::~DataWriter()
{
if (_release && _buffer) delete[] _buffer;
}
void DataWriter::write8(uint8_t data)
{
_buffer[_offset] = data;
_offset += 1;
}
void DataWriter::write(const void *data, unsigned size)
{
std::memcpy(pointer(), data, size);
_offset += size;
}
DataWriterLE::DataWriterLE(unsigned size) :
DataWriter(size)
{}
DataWriterLE::DataWriterLE(unsigned size, void *buffer) :
DataWriter(size, buffer)
{}
void DataWriterLE::write16(uint16_t data)
{
LittleEndian::Write16(pointer(), data);
_offset += 2;
}
void DataWriterLE::write24(uint32_t data)
{
LittleEndian::Write24(pointer(), data);
_offset += 3;
}
void DataWriterLE::write32(uint32_t data)
{
LittleEndian::Write32(pointer(), data);
_offset += 4;
}
DataWriterBE::DataWriterBE(unsigned size) :
DataWriter(size)
{}
DataWriterBE::DataWriterBE(unsigned size, void *buffer) :
DataWriter(size, buffer)
{}
void DataWriterBE::write16(uint16_t data)
{
BigEndian::Write16(pointer(), data);
_offset += 2;
}
void DataWriterBE::write24(uint32_t data)
{
BigEndian::Write24(pointer(), data);
_offset += 3;
}
void DataWriterBE::write32(uint32_t data)
{
BigEndian::Write32(pointer(), data);
_offset += 4;
}

72
DataWriter.h Normal file
View File

@ -0,0 +1,72 @@
#ifndef __DATAWRITER_H__
#define __DATAWRITER_H__
#include <stdint.h>
namespace ProFUSE {
class DataWriter {
public:
DataWriter(unsigned size);
DataWriter(unsigned size, void *data);
virtual ~DataWriter();
void write8(uint8_t);
virtual void write16(uint16_t) = 0;
virtual void write24(uint32_t) = 0;
virtual void write32(uint32_t) = 0;
void write(const void *data, unsigned size);
void setOffset(unsigned o) { _offset = o; }
unsigned offset() const { return _offset; }
void forward(unsigned count) { _offset += count; }
void rewind(unsigned count) { _offset -= count; }
void *data() const { return _buffer; }
unsigned size() const { return _size; }
protected:
uint8_t *pointer() const { return _offset + _buffer; }
bool _release;
unsigned _size;
unsigned _offset;
uint8_t *_buffer;
};
class DataWriterLE : public DataWriter {
public:
DataWriterLE(unsigned);
DataWriterLE(unsigned, void *);
virtual void write8(uint8_t);
virtual void write16(uint16_t);
virtual void write24(uint32_t);
virtual void write32(uint32_t);
};
class DataWriterBE : public DataWriter {
public:
DataWriterBE(unsigned);
DataWriterBE(unsigned, void *);
virtual void write8(uint8_t);
virtual void write16(uint16_t);
virtual void write24(uint32_t);
virtual void write32(uint32_t);
};
}
#endif