bytetree.cpp
Go to the documentation of this file.
1 
11 #include <cstdlib>
12 #include <fstream>
13 
14 #include "bytetree.hpp"
15 #include "leaf.hpp"
16 #include "node.hpp"
17 
18 using namespace verifierUtils;
19 
20 
22 {
23 }
24 
25 
27 {
28 }
29 
30 
32 {
33  return false;
34 }
35 
36 
38 {
39  return false;
40 }
41 
42 
43 std::string ByteTree::toString()
44 {
45  std::string emptyString;
46  return emptyString;
47 }
48 
49 
51 {
52  return 0;
53 }
54 
55 
56 std::vector<uint8_t> ByteTree::toVector()
57 {
58  std::vector<uint8_t> emptyVector;
59  return emptyVector;
60 }
61 
62 
63 std::vector<bool> ByteTree::toBoolArray()
64 {
65  std::cout<<"ERROR: in ByteTree::toBoolArray(): trying to turn a"
66  <<" non-leaf into an array of booleans.\nAborting."
67  <<std::endl;
68  prettyPrint("");
69  std::cout<<std::endl;
70  exit(1);
71 }
72 
73 
75 {
76  ByteTree * res;
77  if (s.compare(0,2,"01") == 0)
78  res = new Leaf(s);
79  else if (s.compare(0,2,"00") == 0)
80  res = new Node(s);
81  else
82  {
83  std::cout<<"ERROR in ByteTree::parseString(): string "
84  "does not correspond to a bytetree! Here is the"
85  " problematic string: "<<s<<std::endl;
86  exit(1);
87  }
88  return res;
89 }
90 
91 
92 ByteTree * ByteTree::parseVector(std::vector<uint8_t> &v)
93 {
94  ByteTree * res;
95  if (v[0] == 1)
96  {
97  unsigned int l = v[1] * 0x1000000
98  + v[2] * 0x10000
99  + v[3] * 0x100
100  + v[4];
101  std::vector<uint8_t> content(v.begin()+5,v.begin()+l+5);
102  res = new Leaf(content);
103  v.assign(v.begin()+l+5,v.end());
104  }
105  else if (v[0] == 0)
106  res = new Node(v);
107  else
108  {
109  std::cout<<"ERROR in ByteTree::parseVector(): vector "
110  "does not correspond to a bytetree! Here is the"
111  " problematic vector:\nv= ";
112  for (unsigned int i=0; i<v.size(); i++)
113  std::cout<<v[i]<<", ";
114  std::cout<<std::endl;
115  exit(1);
116  }
117  return res;
118 }
119 
120 
121 ByteTree * ByteTree::parseFile(std::string path)
122 {
123  std::vector<uint8_t> content;
124  std::ifstream file(path);
125  uint8_t c;
126  while (file.good())
127  {
128  c = file.get();
129  if (file.good())
130  content.push_back(c);
131  }
132  if (content.size() == 0)
133  return NULL;
134  else
135  return parseVector(content);
136 }
137 
138 
140 {
141  std::vector<uint8_t> content(s.size());
142  for (unsigned int i=0; i<s.size(); i++)
143  content[i] = s[i];
144  return new Leaf(content);
145 }
146 
147 
148 unsigned int ByteTree::size()
149 {
150  return 0;
151 }
152 
153 
154 void ByteTree::prettyPrint(std::string indent)
155 {
156 }
157 
158 
159 bool ByteTree::compare(std::string s)
160 {
161  return true;
162 }
163 
164 
166 {
167  std::cout<<"ERROR: in ByteTree::addChild(): trying to access "
168  "the children of a non-Node ByteTree. Aborting."
169  <<std::endl;
170  prettyPrint("");
171  std::cout<<std::endl;
172  exit(1);
173 }
174 
175 
176 ByteTree * ByteTree::getChild(unsigned int i)
177 {
178  std::cout<<"ERROR: in ByteTree::getChild(): trying to access "
179  "the children of a non-Node ByteTree. Aborting."
180  <<std::endl;
181  prettyPrint("");
182  std::cout<<std::endl;
183  exit(1);
184 }