-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.cpp
More file actions
315 lines (285 loc) · 6.26 KB
/
Editor.cpp
File metadata and controls
315 lines (285 loc) · 6.26 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
#include"Editor.h"
#include<fstream>
#include<iostream>
using namespace std;
void colorText(int value)
{
COORD coord;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
FlushConsoleInputBuffer(hConsole);
SetConsoleTextAttribute(hConsole, value + 240);
}
void placeCursorAt(Point coordinate)
{
COORD coord;
coord.X = coordinate.getX();
coord.Y = coordinate.getY();
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE),
coord);
}
template<class ItemType>
int findIndexofLargest(const ItemType theArray[], int size)
{
int indexSoFar = 0; // Index of largest entry found so far
for (int currentIndex = 1; currentIndex < size; currentIndex++)
{
// At this point, theArray[indexSoFar] >= all entries in
// theArray[0..currentIndex - 1]
if (theArray[currentIndex] > theArray[indexSoFar])
indexSoFar = currentIndex;
} // end for
return indexSoFar; // Index of largest entry
}
Editor::Editor() //Constructor
{
}
Editor::Editor(string fileName)
{
LinkedList<string> tempArray;
string keyword;
lastLinePosition = 0;
ifstream infile;
infile.open(fileName);
while (!infile.eof()) //Loops through file to grab each line in the file. Places the lines in aList.
{
lastLinePosition++;
getline(infile, line);
aList.insert(lastLinePosition, line);
}
infile.close();
infile.open("keywords.txt");
while (!infile.eof()) //Loops though keyword file to grab each keyword and place it in the Binary Search Tree
{
infile >> keyword;
BST.add(keyword);
}
}
void Editor::displayLines()
{
system("CLS");
// Displays each word and colors it blue if it is a keyword
int position;
string nextLine, nextWord, line;
for (position = 1; position <= aList.getLength(); position++)
{
nextLine = aList.getEntry(position);
int i = 0;
while (i < nextLine.length())
{
string word = "";
// isolate a word at a time (can contain underscores)
if (isalpha(nextLine[i]))
{
while (isalpha(nextLine[i]) || nextLine[i] == '_')
{
word += nextLine[i];
i++;
}
if (BST.contains(word))
{
colorText(1);
}
else
{
colorText(0);
}
cout << word;
}
else
{
colorText(0);
cout << nextLine[i];
i++;
}
}
cout << endl;
}
placeCursorAt(aPoint);
} // end displayLines
void Editor::Run(string fileName) //Gives the user the ability to manipulate the file.
{
displayLines();
string tempLine;
const char QUIT = 'q';
char command;
char character;
while ((command = _getwch()) != QUIT) //Allows user to input key functions until they quit (q).
{
switch (command)
{
case 'h': MoveLeft();
break;
case 'j': MoveDown();
break;
case 'k': MoveUp();
break;
case 'l': MoveRight();
break;
case 'x': Erase();
break;
case 'd': DeleteLine();
break;
case 'u': Undo();
break;
case ':':
{
//Set the cursor to the end of the editor
aPoint.setX(aList.getEntry(lastLinePosition).length());
aPoint.setY(aList.getLength() - 1);
displayLines();
char character = _getwch();
//Saves the file if user presses w and quits the program without saving the file if the user presses q
if (character == 'w')
{
saveFile(fileName);
}
else if (character == 'q')
{
return;
}
}
break;
default:
break;
}
displayLines();
}
if (lastLinePosition == 0)
{
return;
}
else
{
//Set the cursor to the end of the text
Point EditorEnd;
EditorEnd.setX(aList.getEntry(lastLinePosition).length());
EditorEnd.setY(aList.getLength() - 1);
placeCursorAt(EditorEnd);
return;
}
}
void Editor::MoveRight() //Moves the cursor to the right by one spot
{
if (aList.getLength() == 0)
{
return;
}
if ((aList.getEntry(aPoint.getY() + 1).length() == aPoint.getX() + 1))
{
return;
}
aPoint.setX(aPoint.getX() + 1);
}
void Editor::MoveUp() //Moves the cursor up one line
{
if (aPoint.getY() == 0)
{
return;
}
if (aPoint.getX() > aList.getEntry(aPoint.getY()).length() - 1)
{
aPoint.setX(aList.getEntry(aPoint.getY()).length() - 1);
}
aPoint.setY(aPoint.getY() - 1);
}
void Editor::MoveDown() //Moves the cursor down one line
{
if (aPoint.getY() + 1 == lastLinePosition)
{
return;
}
if (lastLinePosition == 0)
{
return;
}
if (aPoint.getX() > aList.getEntry(aPoint.getY() + 2).length() - 1)
{
aPoint.setX(aList.getEntry(aPoint.getY() + 2).length() - 1);
}
aPoint.setY(aPoint.getY() + 1);
}
void Editor::MoveLeft() //Moves the cursor to the left by one spot
{
if (aPoint.getX() == 0)
{
return;
}
aPoint.setX(aPoint.getX() - 1);
}
void Editor::Erase() //Deletes the character at the cursor
{
if (lastLinePosition == 0)
{
return;
}
deleteCount++;
string currentLine;
currentLine = aList.getEntry(aPoint.getY() + 1);
CommandPlus command;
command.setValue(currentLine);
command.setPosition(aPoint.getY() + 1);
command.setCommand('x');
aStack.push(command);
currentLine.erase(aPoint.getX(), 1);
aList.replace(aPoint.getY() + 1, currentLine);
}
void Editor::DeleteLine() //Deletes the line the cursor is on if the user presses d again
{
if (aList.isEmpty())
{
return;
}
char command = _getwch();
if (command == 'd')
{
deleteCount++;
string currentLine;
currentLine = aList.getEntry(aPoint.getY() + 1);
CommandPlus command;
command.setValue(currentLine);
command.setPosition(aPoint.getY() + 1);
command.setCommand('d');
aStack.push(command);
displayLines();
if (aPoint.getY() + 1 == aList.getLength())
{
aPoint.setY(aPoint.getY());
aList.remove(aPoint.getY() + 1);
}
else
{
aList.remove(aPoint.getY() + 1);
}
aPoint.setX(0);
lastLinePosition--;
}
}
void Editor::Undo() //Undo the previous change made
{
if (!aStack.isEmpty())
{
CommandPlus command = aStack.peek();
if (command.getCommand() == 'x')
{
aList.replace(command.getPosition(), command.getValue());
}
else if (command.getCommand() == 'd')
{
aList.insert(command.getPosition(), command.getValue());
lastLinePosition++;
}
aStack.pop();
}
}
void Editor::saveFile(string fileName) //Saves the content that was changed into the textfile
{
ofstream outFile;
// open the file the user is working on as an output file
outFile.open(fileName, ofstream::out);
// write out all the lines from the linked list
for (int i = 1; i <= aList.getLength(); i++) {
outFile << aList.getEntry(i) << endl;
}
outFile.close();
}