More refactoring.

Change-Id: Iaf937feb0486d2f008a47584c0a75edccbde2b34
This commit is contained in:
Peter Johnson
2015-06-21 22:03:51 -07:00
parent c4d4679ed9
commit b88a9295bf
6 changed files with 54 additions and 25 deletions

View File

@@ -29,21 +29,6 @@ NtImpl::WriteDouble(char* &buf, double val)
*buf++ = (char)(v & 0xff);
}
double
NtImpl::ReadDouble(char* &buf)
{
std::uint64_t val = (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf;
return *reinterpret_cast<double*>(&val);
}
size_t
NT_WriteType(char *buf, NT_Type type, unsigned int proto_rev)
{

View File

@@ -62,8 +62,6 @@ Read32(char* &buf)
return val;
}
double ReadDouble(char* &buf);
} // namespace NtImpl
#endif /* NT_UTIL_H */

View File

@@ -7,9 +7,22 @@
#include "nt_raw_istream.h"
#include <cstring>
using namespace NtImpl;
void
raw_istream::anchor()
{
}
bool
raw_mem_istream::read(void *data, std::size_t len)
{
if (len > m_left)
return false;
std::memcpy(data, m_cur, len);
m_cur += len;
m_left -= len;
return true;
}

View File

@@ -16,6 +16,7 @@ class raw_istream
{
void anchor();
public:
raw_istream() {}
virtual ~raw_istream() {}
virtual bool read(void *data, std::size_t len) = 0;
virtual void close() = 0;
@@ -24,6 +25,20 @@ private:
raw_istream& operator= (const raw_istream&);
};
class raw_mem_istream : public raw_istream
{
public:
raw_mem_istream(const char *mem, std::size_t len)
: m_cur(mem), m_left(len)
{}
virtual ~raw_mem_istream() {}
virtual bool read(void *data, std::size_t len);
virtual void close() {}
private:
const char *m_cur;
std::size_t m_left;
};
} // namespace NtImpl
#endif /* NT_RAW_ISTREAM_H_ */

View File

@@ -17,6 +17,21 @@
using namespace NtImpl;
static double
read_double(char* &buf)
{
std::uint64_t val = (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf; val <<= 8; val |= (*((unsigned char *)buf)) & 0xff;
++buf;
return *reinterpret_cast<double*>(&val);
}
WireDecoder::WireDecoder(raw_istream& is, unsigned int proto_rev)
: m_is(is)
{
@@ -31,6 +46,15 @@ WireDecoder::~WireDecoder()
std::free(m_buf);
}
bool
WireDecoder::ReadDouble(double *val)
{
char *buf;
if (!Read(&buf, 8)) return false;
*val = read_double(buf);
return true;
}
void
WireDecoder::Realloc(std::size_t len)
{
@@ -124,7 +148,7 @@ WireDecoder::ReadValue(NT_Type type, NT_Value *value)
value->data.arr_double.arr =
(double *)std::malloc(size * sizeof(double));
for (unsigned int i=0; i<size; ++i)
value->data.arr_double.arr[i] = NtImpl::ReadDouble(buf);
value->data.arr_double.arr[i] = read_double(buf);
break;
}
case NT_STRING_ARRAY:

View File

@@ -70,13 +70,7 @@ public:
return true;
}
bool ReadDouble(double *val)
{
char *buf;
if (!Read(&buf, 8)) return false;
*val = NtImpl::ReadDouble(buf);
return true;
}
bool ReadDouble(double *val);
bool ReadULEB128(unsigned long *val)
{