-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
446 lines (367 loc) · 12.4 KB
/
Parser.cpp
File metadata and controls
446 lines (367 loc) · 12.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
#include <string>
#include <fstream>
using std::ifstream;
using std::ofstream;
///// Definitions of Text File Format /////
#define ID_COL 1
#define EVENT_COL 0
#define TYPE_COL 2
#define DISP_COL 5
///////////////////////////////////////////
const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* const DELIMITER = "|";
//////////////////////////////////////////////////////////////////////////////////
enum STATES{
STATE_DEL_NO_DISP,
STATE_DEL_DISP,
STATE_NEW_REC_NO_DISP,
STATE_NEW_REC_DISP,
STATE_UP_NO_DISP,
STATE_UP_DISP_GUY,
STATE_DISP_ALREADY,
STATE_DISP_STOP_STILL_UP,
STATE_NONE //Fail has occurred
};
enum ACTIONS{
ACTION_NEW_NO_DISP,
ACTION_NEW_DISP,
ACTION_UP_NO_DISP,
ACTION_UP_DISP,
ACTION_DELETE_NO_DISP,
ACTION_DELETE_DISP,
ACTIONS_FINISH,
ACTIONS_NONE //Fail has occurred
};
struct DATA{
};
// Abstract calss only for inheritance
class FSMstate {
protected:
public:
FSMstate(){}
virtual ~FSMstate(){}
virtual void performState(int& newRecordsCounter, ofstream& fout) = 0;
};
// State 0
class stateDelNoDisp: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state Del No Disp was Performed - state 0"<<endl;
}
};
// State 1
class stateDelDisp: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state Del Disp was Performed - state 1: Relevant For Recording"<<endl<<endl;
fout<<"newRecordsCounter: "<<newRecordsCounter<<". state Del Disp was Performed - state 1: Relevant For Recording"<<endl<<endl;
}
};
// State 2
class stateNewRecNoDisp: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
newRecordsCounter++;
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state New Rec No Disp was Performed - state 2"<<endl;
}
};
// State 3
class stateNewRecDisp: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
newRecordsCounter++;
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state New Rec Disp was Performed - state 3: Relevant For Recording"<<endl;
fout<<"newRecordsCounter: "<<newRecordsCounter<<". state New Rec Disp was Performed - state 3: Relevant For Recording"<<endl;
}
};
// State 4
class stateUpNoDisp: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state Up No Disp was Performed - state 4"<<endl;
}
};
// State 5
class stateUpDispGuy: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state Up Disp Guy was Performed - state 5: Relevant For Recording"<<endl;
fout<<"newRecordsCounter: "<<newRecordsCounter<<". state Up Disp Guy was Performed - state 5: Relevant For Recording"<<endl;
}
};
// State 6
class stateDispAlready: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state Disp Already was Performed - state 6"<<endl;
}
};
// State 7
class stateDispStopStillUp: public FSMstate
{
public:
void performState(int& newRecordsCounter, ofstream& fout)
{
std::cout<<"newRecordsCounter: "<<newRecordsCounter<<". state Disp Stop Still Up Performed - state 7: Relevant For Recording"<<endl<<endl;
fout<<"newRecordsCounter: "<<newRecordsCounter<<". state Disp Stop Still Up Performed - state 7: Relevant For Recording"<<endl<<endl;
}
};
class FSM {
private:
ifstream _fin;
ofstream _fout;
char _filePathIn[120];
char _filePathOut[120];
int _numLinesToSkeep;
int _id;
int _newRecordsCounter;
DATA _newRecord;
DATA _prevRecord;
DATA _curRecord;
int _current; // Current State in FSM
int _next[8][6];
FSMstate* _states[8];
public:
FSM(char* file_path, char* file_path_out, int id, int numLinesToSkeep);
~FSM();
ACTIONS GetNextAction();
void PerformOneStep();
void CreateLogFile();
};
FSM::FSM(char* file_path_in, char* file_path_out, int id, int numLinesToSkeep): _id(id), _numLinesToSkeep(numLinesToSkeep), _newRecordsCounter(0)
{
strcpy(_filePathIn, file_path_in);
strcpy(_filePathOut, file_path_out);
_fin.open("C:\\Users\\user\\Desktop\\data_input.txt"); // open a file
if (!_fin.good())
{
exit(1); // exit if file not found
}
_fout.open("C:\\Users\\user\\Desktop\\data_output.txt",std::ofstream::out); // open a file
// Skeep numLinesToSkeep from start
for(int i=0; i<numLinesToSkeep; i++)
{
char buf[MAX_CHARS_PER_LINE];
_fin.getline(buf, MAX_CHARS_PER_LINE);
}
_states[0] = new stateDelNoDisp();
_states[1] = new stateDelDisp();
_states[2] = new stateNewRecNoDisp();
_states[3] = new stateNewRecDisp();
_states[4] = new stateUpNoDisp();
_states[5] = new stateUpDispGuy();
_states[6] = new stateDispAlready();
_states[7] = new stateDispStopStillUp();
_current = STATE_DEL_NO_DISP;
_next[STATE_DEL_NO_DISP][ACTION_NEW_NO_DISP] = STATE_NEW_REC_NO_DISP;
_next[STATE_DEL_NO_DISP][ACTION_NEW_DISP] = STATE_NEW_REC_DISP;
_next[STATE_DEL_NO_DISP][ACTION_UP_NO_DISP] = STATE_DEL_NO_DISP; // Not Relevant.
_next[STATE_DEL_NO_DISP][ACTION_UP_DISP] = STATE_DEL_NO_DISP; // Not Relevant.
_next[STATE_DEL_NO_DISP][ACTION_DELETE_NO_DISP] = STATE_DEL_NO_DISP; // Not Relevant.
_next[STATE_DEL_NO_DISP][ACTION_DELETE_DISP] = STATE_DEL_NO_DISP; // Not Relevant.
_next[STATE_DEL_DISP][ACTION_NEW_NO_DISP] = STATE_NEW_REC_NO_DISP;
_next[STATE_DEL_DISP][ACTION_NEW_DISP] = STATE_NEW_REC_DISP;
_next[STATE_DEL_DISP][ACTION_UP_NO_DISP] = STATE_DEL_DISP; // Not Relevant.
_next[STATE_DEL_DISP][ACTION_UP_DISP] = STATE_DEL_DISP; // Not Relevant.
_next[STATE_DEL_DISP][ACTION_DELETE_NO_DISP] = STATE_DEL_DISP; // Not Relevant.
_next[STATE_DEL_DISP][ACTION_DELETE_DISP] = STATE_DEL_DISP; // Not Relevant.
_next[STATE_NEW_REC_NO_DISP][ACTION_NEW_NO_DISP] = STATE_NEW_REC_NO_DISP; // Not Relevant.
_next[STATE_NEW_REC_NO_DISP][ACTION_NEW_DISP] = STATE_NEW_REC_NO_DISP;
_next[STATE_NEW_REC_NO_DISP][ACTION_UP_NO_DISP] = STATE_UP_NO_DISP;
_next[STATE_NEW_REC_NO_DISP][ACTION_UP_DISP] = STATE_UP_DISP_GUY;
_next[STATE_NEW_REC_NO_DISP][ACTION_DELETE_NO_DISP] = STATE_DEL_NO_DISP;
_next[STATE_NEW_REC_NO_DISP][ACTION_DELETE_DISP] = STATE_NEW_REC_NO_DISP; // Not Relevant. maybe an alternative of 1.
_next[STATE_NEW_REC_DISP][ACTION_NEW_NO_DISP] = STATE_NEW_REC_DISP; // Not Relevant.
_next[STATE_NEW_REC_DISP][ACTION_NEW_DISP] = STATE_NEW_REC_DISP; // Not Relevant.
_next[STATE_NEW_REC_DISP][ACTION_UP_NO_DISP] = STATE_DISP_STOP_STILL_UP;
_next[STATE_NEW_REC_DISP][ACTION_UP_DISP] = STATE_DISP_ALREADY;
_next[STATE_NEW_REC_DISP][ACTION_DELETE_NO_DISP] = STATE_DEL_DISP;
_next[STATE_NEW_REC_DISP][ACTION_DELETE_DISP] = STATE_DEL_DISP;
_next[STATE_UP_NO_DISP][ACTION_NEW_NO_DISP] = STATE_UP_NO_DISP; // Not Relevant.
_next[STATE_UP_NO_DISP][ACTION_NEW_DISP] = STATE_UP_NO_DISP; // Not Relevant.
_next[STATE_UP_NO_DISP][ACTION_UP_NO_DISP] = STATE_UP_NO_DISP;
_next[STATE_UP_NO_DISP][ACTION_UP_DISP] = STATE_UP_DISP_GUY;
_next[STATE_UP_NO_DISP][ACTION_DELETE_NO_DISP] = STATE_DEL_NO_DISP;
_next[STATE_UP_NO_DISP][ACTION_DELETE_DISP] = STATE_UP_NO_DISP; // Not Relevant. maybe an alternative of 1.
_next[STATE_UP_DISP_GUY][ACTION_NEW_NO_DISP] = STATE_UP_DISP_GUY; // Not Relevant.
_next[STATE_UP_DISP_GUY][ACTION_NEW_DISP] = STATE_UP_DISP_GUY; // Not Relevant.
_next[STATE_UP_DISP_GUY][ACTION_UP_NO_DISP] = STATE_DISP_STOP_STILL_UP;
_next[STATE_UP_DISP_GUY][ACTION_UP_DISP] = STATE_DISP_ALREADY;
_next[STATE_UP_DISP_GUY][ACTION_DELETE_NO_DISP] = STATE_DEL_DISP;
_next[STATE_UP_DISP_GUY][ACTION_DELETE_DISP] = STATE_DEL_DISP;
_next[STATE_DISP_ALREADY][ACTION_NEW_NO_DISP] = STATE_DISP_ALREADY; // Not Relevant.
_next[STATE_DISP_ALREADY][ACTION_NEW_DISP] = STATE_DISP_ALREADY; // Not Relevant.
_next[STATE_DISP_ALREADY][ACTION_UP_NO_DISP] = STATE_DISP_STOP_STILL_UP;
_next[STATE_DISP_ALREADY][ACTION_UP_DISP] = STATE_DISP_ALREADY;
_next[STATE_DISP_ALREADY][ACTION_DELETE_NO_DISP] = STATE_DEL_DISP;
_next[STATE_DISP_ALREADY][ACTION_DELETE_DISP] = STATE_DEL_DISP;
_next[STATE_DISP_STOP_STILL_UP][ACTION_NEW_NO_DISP] = STATE_DISP_STOP_STILL_UP; // Not Relevant.
_next[STATE_DISP_STOP_STILL_UP][ACTION_NEW_DISP] = STATE_DISP_STOP_STILL_UP; // Not Relevant.
_next[STATE_DISP_STOP_STILL_UP][ACTION_UP_NO_DISP] = STATE_UP_NO_DISP;
_next[STATE_DISP_STOP_STILL_UP][ACTION_UP_DISP] = STATE_UP_DISP_GUY;
_next[STATE_DISP_STOP_STILL_UP][ACTION_DELETE_NO_DISP] = STATE_DEL_NO_DISP;
_next[STATE_DISP_STOP_STILL_UP][ACTION_DELETE_DISP] = STATE_DISP_STOP_STILL_UP; // Not Relevant. maybe an alternative of 1.
}
FSM::~FSM()
{
_fin.close();
_fout.close();
}
ACTIONS FSM::GetNextAction()
{
int curLineId=-1;
char buf[MAX_CHARS_PER_LINE];
_fin.getline(buf, MAX_CHARS_PER_LINE);
if (_fin.eof())
{
return ACTIONS_FINISH;
}
// parse the line into blank-delimited tokens
int n = 0; // a for-loop index
// array to store memory addresses of the tokens in buf
const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
// parse the line
token[0] = strtok(buf, DELIMITER); // first token
if (token[0]) // zero if line is blank
{
for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER); // subsequent tokens
if (!token[n]) break; // no more tokens
}
}
if(token[ID_COL])
{
std::string str_dec2(token[ID_COL]);
curLineId=std::stoi(str_dec2,nullptr,0);
}
while (curLineId!=_id)
{
char buf[MAX_CHARS_PER_LINE];
_fin.getline(buf, MAX_CHARS_PER_LINE);
if (_fin.eof())
{
return ACTIONS_FINISH;
}
// parse the line into blank-delimited tokens
n = 0; // a for-loop index
// array to store memory addresses of the tokens in buf
//const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
for(int temp=0; temp<MAX_TOKENS_PER_LINE ; temp++)
{
token[temp]=NULL;
}
// parse the line
token[0] = strtok(buf, DELIMITER); // first token
if (token[0]) // zero if line is blank
{
for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER); // subsequent tokens
if (!token[n]) break; // no more tokens
}
}
// Check Validity of line
if(token[ID_COL])
{
std::string str_dec2(token[ID_COL]);
curLineId=std::stoi(str_dec2,nullptr,0);
}
}
// process (print) the tokens
//for (int i = 0; i < n; i++) // n = #of tokens
//{
// cout << "Token[" << i << "] = " << token[i]<<endl;
//}
std::string str_dec3(token[DISP_COL]);
int disp_f = std::stoi(str_dec3,nullptr,0);
char str[15];
int ret=0;
strcpy(str, token[EVENT_COL]);
// Check if str first three letters eqeuals "New"
if(str[0]=='N' && str[1]== 'e' && str[2]=='w')
{
ret = 1;
}
if(ret==1 && disp_f==0)
{
return ACTION_NEW_NO_DISP;
}
if(ret==1 && disp_f==1)
{
return ACTION_NEW_DISP;
}
// Check if str first two letters eqeuals "Up"
ret = 0;
if(str[0]=='U' && str[1]== 'p')
{
ret = 1;
}
if(ret==1 && disp_f==0)
{
return ACTION_UP_NO_DISP;
}
if(ret==1 && disp_f==1)
{
return ACTION_UP_DISP;
}
// Check if str first three letters eqeuals "Del"
ret = 0;
if(str[0]=='D' && str[1]== 'e' && str[2]=='l')
{
ret = 1;
}
if(ret==1 && disp_f==0)
{
return ACTION_DELETE_NO_DISP;
}
if(ret==1 && disp_f==1)
{
return ACTION_DELETE_DISP;
}
return ACTIONS_NONE; //Failure has occurred
}
void FSM::PerformOneStep()
{
ACTIONS action = GetNextAction();
if (action == ACTIONS_FINISH)
{
return;
}
// We get here only if action is OK and we are not finished yet
_current = _next[_current][action];
_states[_current]->performState(_newRecordsCounter, _fout);
}
void FSM::CreateLogFile()
{
int numSteps = 1000;
for(int i=0; i < numSteps; i++)
{
PerformOneStep();
}
}
int main()
{
FSM MyFsm("C:\\Users\\user\\Desktop\\data_input.txt","C:\\Users\\user\\Desktop\\data_output.txt",1,1);
MyFsm.CreateLogFile();
cout<<endl;
}