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

This commit is contained in:
ksherlock 2009-12-15 20:16:28 +00:00
parent 23e6ad6592
commit 7d8a4e9907
4 changed files with 188 additions and 140 deletions

105
Endian/Endian.cpp Normal file
View File

@ -0,0 +1,105 @@
#include "Endian.h"
using namespace LittleEndian {
uint16_t Read16(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return p[0] | (p[1] << 8);
}
uint32_t Read24(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]) | (p[1] << 8) | (p[2] << 16);
}
uint32_t Read32(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
void Write16(void *vp, uint16_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
}
void Write24(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
}
void Write32(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
}
}
using namespace BigEndian {
inline uint16_t Read16(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 8) | (p[1]);
}
inline uint32_t Read24(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 16) | (p[1] << 8) | (p[2]);
}
inline uint32_t Read32(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]);
}
inline void Write16(void *vp, uint16_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 8) & 0xff;
p[1] = (x) & 0xff;
}
inline void Write24(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 16) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x) & 0xff;
}
inline void Write32(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 24) & 0xff;
p[1] = (x >> 16) & 0xff;
p[2] = (x >> 8) & 0xff;
p[3] = (x) & 0xff;
}
}

View File

@ -14,24 +14,11 @@ namespace LittleEndian {
return (p[0]);
}
inline uint16_t Read16(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return p[0] | (p[1] << 8);
}
uint16_t Read16(const void *vp);
inline uint32_t Read24(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]) | (p[1] << 8) | (p[2] << 16);
}
uint32_t Read24(const void *vp);
inline uint32_t Read32(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
uint32_t Read32(const void *vp);
inline uint8_t Read8(const void *vp, unsigned offset)
@ -62,29 +49,12 @@ namespace LittleEndian {
p[0] = x;
}
inline void Write16(void *vp, uint16_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
}
void Write16(void *vp, uint16_t x);
void Write24(void *vp, uint32_t x);
void Write32(void *vp, uint32_t x);
inline void Write24(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
}
inline void Write32(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
}
inline void Write8(void *vp, unsigned offset, uint8_t x)
{
@ -118,24 +88,11 @@ namespace BigEndian {
return p[0];
}
inline uint16_t Read16(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 8) | (p[1]);
}
uint16_t Read16(const void *vp);
inline uint32_t Read24(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 16) | (p[1] << 8) | (p[2]);
}
uint32_t Read24(const void *vp);
inline uint32_t Read32(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]);
}
uint32_t Read32(const void *vp);
inline uint8_t Read8(const void *vp, unsigned offset)
@ -167,30 +124,13 @@ namespace BigEndian {
p[0] = x;
}
inline void Write16(void *vp, uint16_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 8) & 0xff;
p[1] = (x) & 0xff;
}
void Write16(void *vp, uint16_t x);
void Write24(void *vp, uint32_t x);
void Write32(void *vp, uint32_t x);
inline void Write24(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 16) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x) & 0xff;
}
inline void Write32(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 24) & 0xff;
p[1] = (x >> 16) & 0xff;
p[2] = (x >> 8) & 0xff;
p[3] = (x) & 0xff;
}
inline void Write8(void *vp, unsigned offset, uint8_t x)
{
Write8(offset + (uint8_t *)vp, x);

View File

@ -1,71 +1,15 @@
#ifndef __IOBUFFER_H__
#define __IOBUFFER_H__
#include "../Endian.h"
#include "Endian.h"
#include <cstring>
namespace LittleEndian {
class IOBuffer {
public:
IOBuffer(void *vp, unsigned size)
{
_buffer = vp;
_size = size;
_offset = 0;
}
void write8(uint8_t value)
{
Write8(_buffer, _offset, value);
_offset += 1;
}
void write16(uint16_t value)
{
Write16(_buffer, _offset, value);
_offset += 2;
}
void write24(uint32_t value)
{
Write24(_buffer, _offset, value);
_offset += 3;
}
void write32(uint32_t value)
{
Write32(_buffer, _offset, value);
_offset += 4;
}
void writeBytes(const void *value, unsigned count)
{
std::memcpy(_offset + (uint8_t *)_buffer, value, count);
_offset += count;
}
void writeZero(unsigned count)
{
uint8_t *cp = _offset + (uint8_t *)_buffer;
for (unsigned i = 0; i < count; ++i)
{
cp[i] = 0;
}
_offset += count;
}
unsigned offset() const { return _offset; }
void setOffset(unsigned offset) { _offset = offset; }
unsigned size() const { return _size; }
private:
void *_buffer;
unsigned _size;
unsigned _offset;
};
namespace LittleEndian {
#include "IOBuffer.t.cpp"
}
namespace BigEndian {
#inclue "IOBuffer.t.cpp"
}
#endif

59
Endian/IOBuffer.t.cpp Normal file
View File

@ -0,0 +1,59 @@
class IOBuffer {
public:
IOBuffer(void *vp, unsigned size)
{
_buffer = vp;
_size = size;
_offset = 0;
}
void write8(uint8_t value)
{
Write8(_buffer, _offset, value);
_offset += 1;
}
void write16(uint16_t value)
{
Write16(_buffer, _offset, value);
_offset += 2;
}
void write24(uint32_t value)
{
Write24(_buffer, _offset, value);
_offset += 3;
}
void write32(uint32_t value)
{
Write32(_buffer, _offset, value);
_offset += 4;
}
void writeBytes(const void *value, unsigned count)
{
std::memcpy(_offset + (uint8_t *)_buffer, value, count);
_offset += count;
}
void writeZero(unsigned count)
{
uint8_t *cp = _offset + (uint8_t *)_buffer;
for (unsigned i = 0; i < count; ++i)
{
cp[i] = 0;
}
_offset += count;
}
unsigned offset() const { return _offset; }
void setOffset(unsigned offset) { _offset = offset; }
unsigned size() const { return _size; }
private:
void *_buffer;
unsigned _size;
unsigned _offset;
};