-
Notifications
You must be signed in to change notification settings - Fork 5
/
TrieReader.h
237 lines (208 loc) · 5.85 KB
/
TrieReader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Parse input from socket.
*
* Currently supported input formats:
* Trie
*/
#ifndef _TrieReader_H_
#define _TrieReader_H_
#include "Tools.h"
#include "ServerSocket.h"
#include <sstream>
#include <string>
#include <iostream>
#include <cstdlib> // exit()
#include <cassert>
/**
* Base class to read input
*/
class TrieReader
{
public:
TrieReader(int i, std::string name, ServerSocket *ssocket, bool vrbs, bool dbg, bool pos = 0)
: id(i), positive(pos), filename(name), ifs(ssocket), verbose(vrbs), debug(dbg),
rate(time(NULL)), n(0), occs(0)
{ }
bool hasChild()
{
if (!ifs->good())
{
if (debug) std::cerr << "ifstream exausted normally at " << filename << std::endl;
return false;
}
char c = ifs->peek();
if (!ifs->good())
{
if (debug) std::cerr << "ifstream exausted normally at " << filename << std::endl;
return false;
}
if (c == '(')
return true;
return false;
}
char readChild()
{
if (n % 1000000 == 0)
rate = std::time(NULL); // Reset timer
char c = ifs->getc();
if (c != '(')
perror(std::string("expecting ( byte but got ") + c);
c = ifs->getc();
if (!isDna(c))
{
ifs->debug();
perror(std::string("expecting dna byte but got ") + c);
}
++n;
return c;
}
ulong readOccs()
{
occs = ifs->getulong();
return occs;
}
char readClose() // Returns the left-char symbol
{
char leftChar = ifs->getc();
char c = ifs->getc();
if (c != ')')
perror(std::string("expecting ) byte but got ") + c);
return leftChar;
}
void checkR()
{
char c = ifs->getc();
if (c != 'R')
{
std::cerr << "expecting R byte but got " << c;
for (int i = 0; i < 10 && ifs->good(); ++i)
std::cerr << (char)ifs->getc();
std::cerr << std::endl << " at file " << filename << std::endl;
if (!ifs->good())
std::cerr << "error: ifs was not good" << std::endl;
std::exit(1);
}
ulong checksum = ifs->getulong();
//if (debug) std::cerr << " checksum = " << checksum << ", total occs = "
// << n << "in reader " << filename << std::endl;
if (checksum != n)
{
std::cerr << "error: total number traversed = " << n << " but checksum was "
<< checksum << " (" << filename << ")" << std::endl;
std::exit(1);
}
}
bool good()
{
return ifs->good();
}
ulong getOccs()
{ return occs; }
int getId()
{ return id; }
int isPositive()
{ return positive; }
std::string getName()
{ return filename; }
virtual ~TrieReader()
{
delete ifs;
}
// Check whether or not we have successfully exausted all of input
void checkEof()
{
if (!ifs->good())
{
if (debug) std::cerr << "ifs " << id << " was exausted normally." << std::endl;
return;
}
std::cerr << std::endl << "WARNING: Something is wrong... more input pending at " << filename << std::endl
<< "Next ten bytes are (quoted): \"";
for (int i = 0; i < 10 && ifs->good(); ++i)
std::cerr << (char) ifs->getc();
std::cerr << "\"" << std::endl;
if (ifs->good())
std::cerr << "ifs is still good" << std::endl;
else
std::cerr << "ifs is not good anymore!" << std::endl;
}
double getRate()
{
return std::difftime(time(NULL), rate);
}
/**
* Sends message to client and requests to stop the current branch
* FIXME requires checksum
*/
void sendHalt(ulong depth)
{
ifs->writeHalt(n, depth);
}
protected:
/**
* Extracts an ulong from ifs
*
* If there is no number to extract, return 1.
*/
/* ulong readUlong()
{
std::string str;
char c = ifs->get();
while (ifs->good() && isNumber(c))
{
str += c;
c = ifs->get();
}
if (ifs->good())
ifs->putback(c); // Restore the next non-number
if (str.empty())
{
std::cerr << "TrieReader::readUlong(): unable to read ulong from " << filename << std::endl;
if (!ifs->good())
std::cerr << "ifs was not good" << std::endl;
std::cerr << "debug: ";
for (int i = 0; i < 10 && ifs->good(); ++i)
std::cerr << (char) ifs->get();
std::cerr << std::endl;
if (!ifs->good())
std::cerr << "ifs was not good" << std::endl;
std::cerr << "TrieReader::readUlong(): unable to read ulong from " << filename << " after n = " << n << std::endl;
std::exit(1);
}
ulong value;
std::stringstream(str) >> value;
return value;
}*/
bool isNumber(char c)
{
if (c >= '0' && c <= '9')
return true;
return false;
}
bool isDna(char c)
{
if (c == 'A' || c == 'C' || c == 'G' || c == 'T' || c == 'N')
return true;
return false;
}
void perror(std::string str)
{
std::cerr << std::endl << "error: " << str << " at reader " << filename << std::endl;
std::exit(1);
}
int id;
bool positive;
std::string filename;
ServerSocket *ifs;
bool verbose;
bool debug;
time_t rate;
ulong n;
ulong occs;
private:
TrieReader();
// No copy constructor and assignment
TrieReader(TrieReader const&);
TrieReader& operator = (TrieReader const&);
};
#endif // _TrieReader_H_