mirror of
https://github.com/ksherlock/profuse.git
synced 2024-12-23 11:31:43 +00:00
git-svn-id: https://profuse.googlecode.com/svn/branches/v2@126 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
parent
dcd31997ed
commit
5c48dcb813
98
DataWriter.cpp
Normal file
98
DataWriter.cpp
Normal 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
72
DataWriter.h
Normal 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
|
Loading…
Reference in New Issue
Block a user