working on RAB
This commit is contained in:
parent
af87acc86a
commit
07be9940a8
|
@ -1,8 +1,16 @@
|
|||
#include "RandomAccessBinary.h"
|
||||
#include <memory>
|
||||
|
||||
RandomAccessBinary::RandomAccessBinary()
|
||||
{
|
||||
this->ptr_data = std::make_unique<vector<uint8_t>>();
|
||||
this->ptr_data->reserve(1548);
|
||||
}
|
||||
|
||||
RandomAccessBinary::RandomAccessBinary(uint possible_packetsize)
|
||||
{
|
||||
this->ptr_data = std::make_unique<vector<uint8_t>>();
|
||||
this->ptr_data->reserve(possible_packetsize);
|
||||
}
|
||||
|
||||
RandomAccessBinary::~RandomAccessBinary()
|
||||
|
@ -10,84 +18,154 @@ RandomAccessBinary::~RandomAccessBinary()
|
|||
|
||||
}
|
||||
|
||||
const RandomAccessBinary::Endianness RandomAccessBinary::getEndianness()
|
||||
{
|
||||
return this->endianness;
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setEndianess(RandomAccessBinary::Endianness endianess)
|
||||
{
|
||||
|
||||
this->endianness = endianess;
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setBytes(uint position, const std::vector<uint8_t> &data)
|
||||
{
|
||||
|
||||
for(uint i = 0; i < data.size(); ++i)
|
||||
(*this->ptr_data)[position+i] = data[i];
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setByte(uint position, const uint8_t &data)
|
||||
{
|
||||
|
||||
(*this->ptr_data)[position] = data;
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setBit(uint64_t position, const bool &value)
|
||||
{
|
||||
|
||||
uint64_t bytePosition = position>>3;
|
||||
uint bitPosition = position%8;
|
||||
this->setBit(bytePosition, bitPosition, value);
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setBit(uint bytePosition, uint bitPosition, const bool &value)
|
||||
{
|
||||
|
||||
uint8_t byte = (*this->ptr_data)[bytePosition];
|
||||
uint8_t newbyte = 1;
|
||||
if (value)
|
||||
newbyte = byte | (newbyte << (7-bitPosition));
|
||||
else {
|
||||
newbyte = byte & ~(newbyte << (7-bitPosition));
|
||||
}
|
||||
(*this->ptr_data)[bytePosition] = newbyte;
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setBits(uint64_t position, uint bits, const uint &value)
|
||||
void RandomAccessBinary::setBits(uint64_t position, uint bits, const uint8_t &value)
|
||||
{
|
||||
|
||||
uint64_t bytePosition = position>>3;
|
||||
uint bitPosition = position%8;
|
||||
this->setBits(bytePosition, bitPosition, bits, value);
|
||||
}
|
||||
|
||||
void RandomAccessBinary::setBits(uint bytePosition, uint bitPosition, const uint &value)
|
||||
void RandomAccessBinary::setBits(uint bytePosition, uint bitPosition, uint bits, const uint8_t &value)
|
||||
{
|
||||
|
||||
uint8_t byte = (*this->ptr_data)[bytePosition];
|
||||
uint8_t newbyte = value;
|
||||
uint8_t mask = 0;
|
||||
for(uint i = bits-1; i >= 0; --i)
|
||||
mask |= 1 << i;
|
||||
mask <<= (7-bits-bitPosition);
|
||||
byte &= ~mask;
|
||||
newbyte <<= (7-bits-bitPosition);
|
||||
byte |= newbyte;
|
||||
(*this->ptr_data)[bytePosition] = byte;
|
||||
}
|
||||
|
||||
void RandomAccessBinary::set_uint16(uint position, uint16_t data)
|
||||
void RandomAccessBinary::set_uint16(uint position, const uint16_t &data)
|
||||
{
|
||||
|
||||
if (this->endianness == Endianness::BIG) {
|
||||
this->setByte(position, (uint8_t)(data >> 8));
|
||||
this->setbyte(position+1, (uint8_t)(data & 0x0FF));
|
||||
} else if (this->endianness == Endianness::LITTLE) {
|
||||
this->setbyte(position, (uint8_t)(data & 0x0FF));
|
||||
this->setByte(position+1, (uint8_t)(data >> 8));
|
||||
} else {
|
||||
//TODO
|
||||
}
|
||||
}
|
||||
|
||||
void RandomAccessBinary::set_uint32(uint position, uint32_t data)
|
||||
void RandomAccessBinary::set_uint32(uint position, const uint32_t &data)
|
||||
{
|
||||
|
||||
if (this->endianness == Endianness::BIG) {
|
||||
this->setByte(position+0, (uint8_t)((data >> 24) & 0x0FF));
|
||||
this->setByte(position+1, (uint8_t)((data >> 16) & 0x0FF));
|
||||
this->setByte(position+2, (uint8_t)((data >> 8) & 0x0FF));
|
||||
this->setbyte(position+3, (uint8_t)(data & 0x0FF));
|
||||
} else if (this->endianness == Endianness::LITTLE) {
|
||||
this->setByte(position+3, (uint8_t)((data >> 24) & 0x0FF));
|
||||
this->setByte(position+2, (uint8_t)((data >> 16) & 0x0FF));
|
||||
this->setByte(position+1, (uint8_t)((data >> 8) & 0x0FF));
|
||||
this->setbyte(position+0, (uint8_t)(data & 0x0FF));
|
||||
}
|
||||
}
|
||||
|
||||
void RandomAccessBinary::set_uint64(uint position, uint64_t data)
|
||||
void RandomAccessBinary::set_uint64(uint position, const uint64_t &data)
|
||||
{
|
||||
|
||||
if (this->endianness == Endianness::BIG) {
|
||||
this->setByte(position+0, (uint8_t)((data >> 56) & 0x0FF));
|
||||
this->setByte(position+1, (uint8_t)((data >> 48) & 0x0FF));
|
||||
this->setByte(position+2, (uint8_t)((data >> 40) & 0x0FF));
|
||||
this->setByte(position+3, (uint8_t)((data >> 32) & 0x0FF));
|
||||
this->setByte(position+4, (uint8_t)((data >> 24) & 0x0FF));
|
||||
this->setByte(position+5, (uint8_t)((data >> 16) & 0x0FF));
|
||||
this->setByte(position+6, (uint8_t)((data >> 8) & 0x0FF));
|
||||
this->setbyte(position+7, (uint8_t)(data & 0x0FF));
|
||||
} else if (this->endianness == Endianness::LITTLE) {
|
||||
this->setByte(position+7, (uint8_t)((data >> 56) & 0x0FF));
|
||||
this->setByte(position+6, (uint8_t)((data >> 48) & 0x0FF));
|
||||
this->setByte(position+5, (uint8_t)((data >> 40) & 0x0FF));
|
||||
this->setByte(position+4, (uint8_t)((data >> 32) & 0x0FF));
|
||||
this->setByte(position+3, (uint8_t)((data >> 24) & 0x0FF));
|
||||
this->setByte(position+2, (uint8_t)((data >> 16) & 0x0FF));
|
||||
this->setByte(position+1, (uint8_t)((data >> 8) & 0x0FF));
|
||||
this->setbyte(position+0, (uint8_t)(data & 0x0FF));
|
||||
}
|
||||
}
|
||||
|
||||
void RandomAccessBinary::set_float(uint position, float data)
|
||||
void RandomAccessBinary::set_float(uint position, const float &data)
|
||||
{
|
||||
|
||||
const uint32_t * conv = &data;
|
||||
this->set_uint32(position, *conv);
|
||||
}
|
||||
|
||||
void RandomAccessBinary::set_double(uint position, double data)
|
||||
void RandomAccessBinary::set_double(uint position, const double &data)
|
||||
{
|
||||
|
||||
const uint64_t * conv = &data;
|
||||
this->set_uint64(position, *conv);
|
||||
}
|
||||
|
||||
const uint8_t RandomAccessBinary::getByte(uint position)
|
||||
{
|
||||
|
||||
return (*this->ptr_data)[position];
|
||||
}
|
||||
|
||||
const std::vector<const uint8_t> RandomAccessBinary::getBytes(uint position, uint length)
|
||||
{
|
||||
|
||||
std::vector<const uint8_t> vec;
|
||||
for(uint i = position; i< position+length; ++i)
|
||||
vec.push_back((*this->ptr_data)[i]);
|
||||
return vec;
|
||||
}
|
||||
|
||||
const bool RandomAccessBinary::getBit(uint byteOffset, uint bitOffset)
|
||||
{
|
||||
|
||||
uint8_t byte = (*this->ptr_data)[byteOffset];
|
||||
return (byte & (1 << (7-bitOffset)) > 0;
|
||||
}
|
||||
|
||||
const bool RandomAccessBinary::getBit(uint64_t position)
|
||||
{
|
||||
|
||||
uint64_t byteOffset = position>>3;
|
||||
uint bitOffset = position%8;
|
||||
return this->getbit(byteOffset, bitOffset);
|
||||
}
|
||||
|
||||
const std::vector<const uint8_t> RandomAccessBinary::getBits(uint byteOffset, uint8_t bitOffset, uint64_t length)
|
||||
|
|
|
@ -7,14 +7,16 @@
|
|||
|
||||
class RandomAccessBinary
|
||||
{
|
||||
public:
|
||||
enum class Endianness{
|
||||
BIG,
|
||||
LITTLE,
|
||||
MIDDLE
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
RandomAccessBinary();
|
||||
RandomAccessBinary(uint possible_packetsize);
|
||||
explicit ~RandomAccessBinary();
|
||||
|
||||
void setEndianness(Endianness endianess);
|
||||
|
@ -25,8 +27,8 @@ public:
|
|||
|
||||
void setBit(uint64_t position, const bool& value);
|
||||
void setBit(uint bytePosition, uint bitPosition, const bool& value);
|
||||
void setBits(uint64_t position, uint bits, const uint& value);
|
||||
void setBits(uint bytePosition, uint bitPosition, const uint& value);
|
||||
void setBits(uint64_t position, uint bits, const uint8_t& value);
|
||||
void setBits(uint bytePosition, uint bitPosition, uint bits, const uint8_t& value);
|
||||
|
||||
void set_uint16(uint position, const uint16_t& data);
|
||||
void set_uint32(uint position, const uint32_t& data);
|
||||
|
@ -62,6 +64,7 @@ public:
|
|||
std::string get_hex_string();
|
||||
private:
|
||||
std::unique_ptr<std::vector<uint8_t>> ptr_data;
|
||||
Endianness endianness;
|
||||
};
|
||||
|
||||
#endif // RANDOMACCESSBINARY_H
|
||||
|
|
Reference in New Issue