verifierUtils::Node Class Reference

Inherits from the ByteTree and implements the case where the ByteTree has children. More...

#include <node.hpp>

+ Inheritance diagram for verifierUtils::Node:
+ Collaboration diagram for verifierUtils::Node:

Public Member Functions

 Node ()
 Constructs an empty Node instance.
 Node (std::string &s)
 Parses a string to generate the corresponding Node and removes the data read from it.
 Node (std::vector< uint8_t > &v)
 Creates a Node from the corresponding vector of bytes and removes the data read from it.
 ~Node ()
 Destroys each element in children.
std::string toString ()
 Turns this object into a string.
std::vector< uint8_t > toVector ()
 Turns this object into a vector of bytes.
bool isNode ()
 To know if the instance used is a Node.
bool isLeaf ()
 To know if the instance used is a Leaf.
unsigned int size ()
 The number of bytes in a Leaf and the number of ByteTrees in a Node.
void prettyPrint (std::string indent)
 Recursively prints an indented version of the ByteTree (mainly for debugging)
void addChild (ByteTree *bt)
 Adds the ByteTree given in the argument to the children attribute in a Node; exits with exit code 1 if the ByteTree is a Leaf.
ByteTreegetChild (unsigned int i)
 Returns the child of index i if we have a Node; exits with exit code 1 if the ByteTree is a Leaf.
- Public Member Functions inherited from verifierUtils::ByteTree
 ByteTree ()
 Empty constructor to please the compiler.
 ~ByteTree ()
 Empty destructor to please the compiler.
virtual mpz_class toInteger ()
 Turns this object into an arbitrarily large integer.
virtual std::vector< bool > toBoolArray ()
 Turns this object into a vector of boolean.
virtual bool compare (std::string s)
 Compare the bytes contained in this leaf with those in the input.

Private Attributes

std::vector< ByteTree * > children
 The ByteTree contained in this node. They can be both Node's or Leaf's.

Additional Inherited Members

- Static Public Member Functions inherited from verifierUtils::ByteTree
static ByteTreeparseString (std::string &s)
 Parses a string by constructing a ByteTree of the correct kind.
static ByteTreeparseVector (std::vector< uint8_t > &v)
 Parses a vector of bytes by constructing a ByteTree of the correct kind; returns Leaf(0) if the vector is empty.
static ByteTreeparseFile (std::string path)
 Parses a file containing the string representation of a bytetree and returns the said bytetree. If the file does not exist, returns NULL.
static ByteTreestring2ByteTree (std::string s)
 Turns a string into its ByteTree representation. Not to be mistaken with parseString!

Detailed Description

Inherits from the ByteTree and implements the case where the ByteTree has children.

See Also
btrtest.cpp

Definition at line 25 of file node.hpp.

Constructor & Destructor Documentation

Node::Node ( )

Constructs an empty Node instance.

Definition at line 22 of file node.cpp.

{
}
Node::Node ( std::string &  s)

Parses a string to generate the corresponding Node and removes the data read from it.

Parameters
[in,out]sThe string to parse. It must start with "00".

Definition at line 34 of file node.cpp.

{
if ((s.compare(0,2,"00") != 0) || (s.size()%2 !=0))
{
std::cout<<"ERROR: in Node(string), input does not "
"start with '00' or has an odd length"
<<std::endl<<"input: '"<<s<<"'"<<std::endl;
exit(1);
}
else
{
// reading number of children
unsigned int l = octuple2num(s.substr(2,8));
children.resize(l);
s = s.substr(10,std::string::npos);
for (unsigned int i=0; i<l; i++)
}
}
Node::Node ( std::vector< uint8_t > &  v)

Creates a Node from the corresponding vector of bytes and removes the data read from it.

Parameters
[in,out]vThe byte representation of the node we want to create. Must start with 0x0.

Definition at line 55 of file node.cpp.

{
if (v[0] != 0)
{
std::cout<<"ERROR: in Node(vector), input does not "
"start with 0x0.\nv= " <<std::endl;
for (unsigned int i=0; i<v.size(); i++)
std::cout<<std::hex<<v[i]<<",";
std::cout<<std::endl;
exit(1);
}
else
{
// reading number of children
unsigned int l = v[1]*0x1000000
+ v[2]*0x10000
+ v[3]*0x100
+ v[4];
children.resize(l);
v.assign(v.begin()+5, v.end());
for (unsigned int i=0; i<l; i++)
}
}
Node::~Node ( )

Destroys each element in children.

Definition at line 27 of file node.cpp.

{
for (unsigned int i=0; i<children.size(); i++)
free(children[i]);
}

Member Function Documentation

void Node::addChild ( ByteTree bt)
virtual

Adds the ByteTree given in the argument to the children attribute in a Node; exits with exit code 1 if the ByteTree is a Leaf.

Parameters
btThe ByteTree to add.

Reimplemented from verifierUtils::ByteTree.

Definition at line 138 of file node.cpp.

{
children.push_back(bt);
}
ByteTree * Node::getChild ( unsigned int  i)
virtual

Returns the child of index i if we have a Node; exits with exit code 1 if the ByteTree is a Leaf.

Returns
The ith element of the children attribute.

Reimplemented from verifierUtils::ByteTree.

Definition at line 144 of file node.cpp.

{
return children[i];
}
bool Node::isLeaf ( )
virtual

To know if the instance used is a Leaf.

Returns
true if the type of this object is an instance of the class Leaf, false otherwise.

Reimplemented from verifierUtils::ByteTree.

Definition at line 117 of file node.cpp.

{
return false;
}
bool Node::isNode ( )
virtual

To know if the instance used is a Node.

Returns
true if the type of this object is an instance of the class Node, false otherwise.

Reimplemented from verifierUtils::ByteTree.

Definition at line 111 of file node.cpp.

{
return true;
}
void Node::prettyPrint ( std::string  indent)
virtual

Recursively prints an indented version of the ByteTree (mainly for debugging)

Parameters
indentThe characters to be printed in the indentation.

Reimplemented from verifierUtils::ByteTree.

Definition at line 129 of file node.cpp.

{
std::cout<<indent<<num2str(children.size())<<std::endl;
for (unsigned int i=0; i<children.size(); i++)
children[i]->prettyPrint(indent+" ");
std::cout<<std::endl;
}
unsigned int Node::size ( )
virtual

The number of bytes in a Leaf and the number of ByteTrees in a Node.

Returns
The size of this instance.

Reimplemented from verifierUtils::ByteTree.

Definition at line 123 of file node.cpp.

{
return children.size();
}
std::string Node::toString ( )
virtual

Turns this object into a string.

Returns
The string representation of this instance.

Reimplemented from verifierUtils::ByteTree.

Definition at line 81 of file node.cpp.

{
std::string result;
std::vector<uint8_t> bytes = toVector();
for (unsigned int i=0; i<bytes.size(); i++)
result += byte2str(bytes[i]);
return result;
}
std::vector< uint8_t > Node::toVector ( )
virtual

Turns this object into a vector of bytes.

This aim is to allow an easy hashing of the ByteTree's.

Returns
The byte representation of the ByteTree.

Reimplemented from verifierUtils::ByteTree.

Definition at line 91 of file node.cpp.

{
std::vector<uint8_t> result;
result.push_back(0); // Node starts with a 0x00 byte
// Adding the number of children
uint32_t byteNumber = children.size();
result.push_back((byteNumber>>24) % 0x100);
result.push_back((byteNumber>>16) % 0x100);
result.push_back((byteNumber>> 8) % 0x100);
result.push_back( byteNumber % 0x100);
// Adding the byte representation of the children
for (unsigned int i=0; i<children.size(); i++)
{
std::vector<uint8_t> bytesChild = children[i]->toVector();
result.insert(result.end(),bytesChild.begin(), bytesChild.end());
}
return result;
}

Member Data Documentation

std::vector<ByteTree *> verifierUtils::Node::children
private

The ByteTree contained in this node. They can be both Node's or Leaf's.

Definition at line 33 of file node.hpp.


The documentation for this class was generated from the following files: