Some helper functions to help adapt between vectors and strings.

This commit is contained in:
JoelKatz
2012-02-16 15:49:10 -08:00
parent 5d3fcdb8ac
commit b6946e40d6
3 changed files with 9 additions and 0 deletions

View File

@@ -70,6 +70,7 @@ public:
// Convert to/from wire format (256-bit nodeID, 1-byte depth)
void addIDRaw(Serializer &s) const;
std::string getRawString() const;
static int getRawIDLength(void) { return 33; }
SHAMapNode(const void *ptr, int len);
};

View File

@@ -111,6 +111,13 @@ void SHAMapNode::addIDRaw(Serializer &s) const
s.add8(mDepth);
}
std::string SHAMapNode::getRawString() const
{
Serializer s(33);
addIDRaw(s);
return s.getString();
}
SHAMapNode SHAMapNode::getChildNodeID(int m) const
{ // This can be optimized to avoid the << if needed
assert((m>=0) && (m<16));

View File

@@ -56,6 +56,7 @@ class Serializer
void* getDataPtr() { return &mData.front(); }
const std::vector<unsigned char>& peekData() const { return mData; }
std::vector<unsigned char> getData() const { return mData; }
std::string getString() const { return std::string(reinterpret_cast<const char *>(getDataPtr()), getLength()); }
void secureErase() { memset(&(mData.front()), 0, mData.size()); erase(); }
void erase() { mData.clear(); }
int removeLastByte();