Add methods for getting array and string length of Metal Claw fields

This commit is contained in:
Gary Talent 2017-10-15 09:04:00 -05:00
parent d096d00bea
commit 2554f8b3e1
2 changed files with 31 additions and 3 deletions

View File

@ -46,6 +46,28 @@ int MetalClawReader::op(const char*, bool *val) {
return 0;
}
size_t MetalClawReader::arrayLength(const char*) {
size_t len = 0;
if (m_fieldPresence.get(m_field)) {
// read the length
if (m_buffIt + sizeof(ArrayLength) < m_buffLen) {
len = ox::bigEndianAdapt(*((ArrayLength*) &m_buff[m_buffIt]));
}
}
return len;
}
size_t MetalClawReader::stringLength(const char*) {
size_t len = 0;
if (m_fieldPresence.get(m_field)) {
// read the length
if (m_buffIt + sizeof(StringLength) < m_buffLen) {
len = ox::bigEndianAdapt(*((StringLength*) &m_buff[m_buffIt]));
}
}
return len;
}
void MetalClawReader::setFields(int fields) {
m_fields = fields;
m_buffIt = (fields / 8 + 1) - (fields % 8 == 0);

View File

@ -19,6 +19,9 @@ namespace ox {
class MetalClawReader {
private:
typedef uint32_t ArrayLength;
typedef uint32_t StringLength;
FieldPresenseMask m_fieldPresence;
int m_fields = 0;
int m_field = 0;
@ -48,6 +51,11 @@ class MetalClawReader {
template<size_t L>
int op(const char*, ox::bstring<L> *val);
size_t arrayLength(const char*);
// stringLength returns the length of the string, including the null terminator.
size_t stringLength(const char*);
void setFields(int fields);
OpType opType() {
@ -76,7 +84,6 @@ int MetalClawReader::op(const char*, ox::bstring<L> *val) {
int err = 0;
if (m_fieldPresence.get(m_field)) {
// read the length
typedef uint32_t StringLength;
size_t size = 0;
if (m_buffIt + sizeof(StringLength) < m_buffLen) {
size = ox::bigEndianAdapt(*((StringLength*) &m_buff[m_buffIt]));
@ -125,10 +132,9 @@ int MetalClawReader::op(const char*, T *val, size_t valLen) {
int err = 0;
if (m_fieldPresence.get(m_field)) {
// read the length
typedef uint32_t ArrayLength;
size_t len = 0;
if (m_buffIt + sizeof(ArrayLength) < m_buffLen) {
len = ox::bigEndianAdapt(*((T*) &m_buff[m_buffIt]));
len = ox::bigEndianAdapt(*((ArrayLength*) &m_buff[m_buffIt]));
m_buffIt += sizeof(ArrayLength);
} else {
err = MC_BUFFENDED;