-
Notifications
You must be signed in to change notification settings - Fork 2
/
testparse.cpp
205 lines (165 loc) · 4.07 KB
/
testparse.cpp
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
#include <dirent.h>
#include <err.h>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include "parser.h"
using namespace std;
#ifdef DEBUG
bool debug=true;
#else
bool debug=false;
#endif
int main(int argc, char **argv)
try
{
fldformat frm;
field message(&frm);
char format_dir[]="./formats";
char filename[sizeof(format_dir)+NAME_MAX+1];
DIR *frmdir;
struct dirent *de;
int frmcounter=0;
string msgbuf;
string msgbuf2;
size_t msglen=0;
size_t msglen1=0;
size_t msglen2=0;
FILE *outfile;
if(argc>2)
{
printf("Usage %s [filename]\nfilename is the file to read the message from. If omitted, read from standard input\n", argv[0]);
return 1;
}
frmdir=opendir(format_dir);
if(!frmdir)
err(2, "Can't open %s", format_dir);
while((de=readdir(frmdir)))
{
#ifdef _DIRENT_HAVE_D_TYPE
if(de->d_type!=DT_LNK && de->d_type!=DT_REG && de->d_type!=DT_UNKNOWN)
continue;
#endif
if(strcmp(de->d_name+strlen(de->d_name)-4, ".frm"))
continue;
sprintf(filename, "%s/%s", format_dir, de->d_name);
if(debug)
printf("Loading %s\n", filename);
frm.load_format(filename);
frmcounter++;
}
closedir(frmdir);
if(frmcounter==0)
{
printf("Error: No formats loaded\n");
return 3;
}
if(debug)
printf("Info: Loaded %d formats\n", frmcounter);
if(debug)
frm.print_format();
if(argc>1)
{
std::ifstream infile(argv[1]);
if(!infile)
err(4, "Cannot open file %s", argv[1]);
if(debug)
printf("Reading from %s\n", argv[1]);
msgbuf.assign(std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>());
infile.close();
}
else
{
if(debug)
printf("Reading from stdin\n");
msgbuf.assign(std::istreambuf_iterator<char>(cin), std::istreambuf_iterator<char>());
}
msglen=msgbuf.length();
msglen1=msglen;
try
{
message.parse(msgbuf);
}
catch(const exception& e)
{
printf("Error: Unable to parse message: %s\n", e.what());
sprintf(filename, "imessage%ld", time(NULL));
outfile=fopen(filename, "w");
for(msglen=0; msglen<msglen1; fputc(msgbuf[msglen++], outfile));
fclose(outfile);
if(debug)
printf("%s file is written\n", filename);
return 6;
}
if(debug)
printf("%s parsed, length: %lu\n", message.get_description().c_str(), (unsigned long)message.get_cached_blength());
message.print_message();
message.reset_altformat();
if(debug)
printf("Building %s, estimated length: %lu\n", message.get_description().c_str(), (unsigned long)message.get_blength());
try
{
msglen2=message.serialize(msgbuf2);
}
catch(const exception& e)
{
if(debug)
printf("Error: Unable to build %s: %s\n", message.get_description().c_str(), e.what());
sprintf(filename, "imessage%ld", time(NULL));
outfile=fopen(filename, "w");
for(msglen=0; msglen<msglen1; fputc(msgbuf[msglen++], outfile));
fclose(outfile);
if(debug)
printf("%s file is written\n", filename);
return 7;
}
if(debug)
printf("%s built. Length: %lu/%lu\n", message.get_description().c_str(), (unsigned long)msglen2, (unsigned long)msgbuf2.length());
if(msglen2!=msglen)
{
if(debug)
printf("Warning: Total length mismatch (%lu != %lu)\n", (unsigned long)msglen, (unsigned long)msglen2);
}
else
for(msglen=0; msglen<msglen2; msglen++)
if(msgbuf[msglen]!=msgbuf2[msglen])
{
if(debug)
printf("Warning: Messages don't match (starting from byte %lu)\n", (unsigned long)msglen);
break;
}
if(msglen2==msglen)
{
if(debug)
printf("Rebuilt message matches original\n");
}
else
{
sprintf(filename, "imessage%ld", time(NULL));
outfile=fopen(filename, "w");
for(msglen=0; msglen<msglen1; fputc(msgbuf[msglen++], outfile));
fclose(outfile);
filename[0]='o';
outfile=fopen(filename, "w");
for(msglen=0; msglen<msglen2; fputc(msgbuf2[msglen++], outfile));
fclose(outfile);
if(debug)
printf("i%s and o%s files are written\n", filename+1, filename+1);
return 8;
}
return 0;
}
catch(const exception& e)
{
printf("Error: Unhandled exception: %s\n", e.what());
return 9;
}
catch(...)
{
printf("Error: Unhandled exception\n");
return 9;
}