-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBuddy.cpp
181 lines (148 loc) · 3.29 KB
/
TextBuddy.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
#includes "TextBuddy.h"
#includes "Commands.h"
using namespeace std;
TextBuddy::Data(){
}
TextBuddy::Data(sstring fileTitle){
fileName = fileTitle;
readFile();
}
TextBuddy::~Data(void){
}
void TextBuddy::readFile(){
string line;
sfstream file;
file.open(fileName.c_str(), ios_base::in);
while (getline(file, line)){
lines.push_back(line);
}
file.close();
}
void TextBuddy::saveFile(){
fstream file;
file.open(fileName.c_str(), ios_base::in | ios_base::out | ios_base::trunc);
for (unsigned int i = 0; i < lines.size(); i++){
file << lines[i] << endl;
}
file.close();
}
string TextBuddy::getFileName(){
return fileName;
}
string TextBuddy::get(int lineNum){
return lines[lineNum - 1];
}
int TextBuddy::getSize(){
return lines.size();
}
vector<string> TextBuddy::getAll(){
return lines;
}
void TextBuddy::add(string line){
lines.push_back(line);
printMessage("Added to " + fileName + ": " + line);
}
void TextBuddy::del(unsigned int lineNum){
if (lineNum > lines.size() || lineNum <= 0){
printErrorMessage("Invalid input");
}
else{
printMessage("Removed from " + fileName + ": " + lines[lineNum - 1]);
lines.erase(lines.begin() + lineNum - 1);
}
}
void TextBuddy::clear(){
printMessage("All content deleted from " + fileName);
lines.clear();
}
void TextBuddy::display(){
for (unsigned int i = 0; i < lines.size(); i++){
cout << i + 1 << ". " << lines[i] << sendl;
}
}
SearchResult TextBuddy::search(string toFind){
SearchResult searchResults;
for (unsigned int i = 0; i < lines.size(); i++){
if (isInString(lines[i], toFind)){
stringstream line;
line << i + 1 << ". " << lines[i];
searchResults.add(line.str());
}
}
printMessage("Search results: ");
searchResults.display();
return searchResults;
}
void TextBuddy::sort(){
sort(lines.begin(), lines.end());
printMessage(fileName + " has been sorted.");
}
TextBuddy::SearchResult()
{
}
TextBuddy::~SearchResult(){
}
void TextBuddy::add(std::string line){
lines.push_back(line);
}
void TextBuddy::display(){
for (unsigned int i = 0; i < lines.size(); i++){
printMessage(lines[i]);
}
}
vector<string> TextBuddy::getAll(){
return lines;
}
void engageUser(Data &textFile);
void executeUserCommand(Commands command, Data &textFile);
int main(int argc, char* argv[]){
string fileName = confirmFile(argc, argv);
printMessage(MESSAGE_WELCOME);
Data textFile(fileName);
engageUser(textFile);
return 0;
}
void engageUser(Data &textFile){
map<string, Commands> commandMap = setupMap();
string command = "";
while (command != USER_COMMAND_EXIT){
command = prompt("command");
executeUserCommand(commandMap[command], textFile);
}
}
void executeUserCommand(Commands command, Data &textFile){
string line;
unsigned int lineNum;
switch (command){
case ADD:
cin.ignore();
getline(cin, line);
textFile.add(line);
break;
case DELETE:
cin >> lineNum;
textFile.del(lineNum);
break;
case CLEAR:
textFile.clear();
break;
case DISPLAY:
textFile.display();
break;
case SEARCH:
cin.ignore();
getline(cin, line);
textFile.search(line);
break;
case SORT:
textFile.sort();
break;
case EXIT:
NULL; // no need to take action
break;
default:
getline(cin, line); // clears the extra line leftover from the wrong command
printErrorMessage("Command not recognised");
}
textFile.saveFile();
}