-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandmanager.cpp
More file actions
96 lines (83 loc) · 2.98 KB
/
Copy pathcommandmanager.cpp
File metadata and controls
96 lines (83 loc) · 2.98 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
// CommandManager handles the operations of looping through the command
// data file. Interface between library object, command object, and data
// file.
//
// Assumptions:
// -- data file will be correctly formatted
// -- data file will contain no empty lines
//
// Implementation:
// -- reaching eof character breaks the for loop
// -- prints error messages on invalid input from file
//---------------------------------------------------------------------------
#include "commandmanager.h"
#include "commandfactory.h"
#include "Library.h"
#include "patron.h"
#include <sstream>
//---------------------------------------------------------------------------
// Constructor
CommandManager::CommandManager() {
unstoredCommands[3] = true;
unstoredCommands[7] = true;
}
//---------------------------------------------------------------------------
// runCommands
bool CommandManager::performCommands(istream &inFile, Library *library) {
// instantiate bool tracker
bool success = false;
// create storage for command char
for (;;) {
// read the line of data and store it
string dataLine;
getline(inFile, dataLine);
// check for eof
if (inFile.eof()) {
// data file read succesfully
success = true;
break;
}
stringstream commandLine(dataLine);
// read the first char of ss and create command obj
char commandType = 0;
commandLine >> commandType;
CommandFactory commFactory;
// create pointer to empty command
Command *newCommand = nullptr;
// createCommand must check for type errors
newCommand = commFactory.createCommand(commandType);
// if the type was invalid, delete the command object and continue loop
if (newCommand == nullptr) {
// output error message
cout << endl
<< "ERROR: Command: \"" << commandType
<< "\" is an invalid command" << endl;
continue;
}
// send the rest of the data line to the empty command object
// buildCommand will handle format for each sub command
if (newCommand->buildCommand(commandLine, library)) {
// if all formatting was correct, execute command.
// execute will add to patron history if applicable
if (newCommand->execute()) {
// delete executed commands that are not stored in patron
// history lists
if (unstoredCommands[commandType - 'A']) {
delete newCommand;
newCommand = nullptr;
}
}
// delete commands that did not execute
else {
delete newCommand;
newCommand = nullptr;
}
}
// if command could not succesfully be built, deallocate memory
else {
delete newCommand;
newCommand = nullptr;
}
}
return success;
}