-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_editor.cpp
368 lines (281 loc) · 10.3 KB
/
text_editor.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
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
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
class Node {
public:
string data;
Node* next;
Node(string data) { // constructor
this -> data = data;
this -> next = NULL;
}
};
void display(Node*);
class Editor {
public:
void add_line(Node* &root) { // add a user given line to the end of text
string line;
cout << "Enter the line to insert:" << endl;
getline(cin, line, '\n'); // input text until enter is pressed
if(root == NULL) { // insert line in an empty list
root = new Node(line);
return;
}
Node* root_ref = root;
while(root_ref -> next != NULL) { // traverse the whole list
root_ref = root_ref -> next;
}
Node* temp = new Node(line);
root_ref -> next = temp; // insert the line at end
}
void add_line_at(Node* &root) { // add a line at a particular index
string line;
cout << "Enter the line to insert:" << endl;
getline(cin, line, '\n');
int line_no;
cout << "Enter index to insert at: ";
cin >> line_no;
Node* temp = new Node(line);
if(line_no == 1) { // add node to the head of list
temp -> next = root;
root = temp;
return;
}
Node* root_ref = root;
for(int i=1; i<line_no-1; i++) { // traverse till the specified index is reached
root_ref = root_ref -> next;
}
temp -> next = root_ref -> next; // insert the node
root_ref -> next = temp;
}
void add_line_at(Node* &root, int line_no) { // add a line at given index
string line;
cout << "Enter the line to insert:" << endl;
getline(cin, line, '\n');
Node* temp = new Node(line);
if(line_no == 1) { // add node to the head of list
temp -> next = root;
root = temp;
return;
}
Node* root_ref = root;
for(int i=1; i<line_no-1; i++) { // traverse till the specified index is reached
root_ref = root_ref -> next;
}
temp -> next = root_ref -> next; // insert the node
root_ref -> next = temp;
}
void add_line_at(Node* &root, int line_no, string line) {
Node* temp = new Node(line);
if(line_no == 1) { // add node to the head of list
temp -> next = root;
root = temp;
return;
}
Node* root_ref = root;
for(int i=1; i<line_no-1; i++) { // traverse till the specified index is reached
root_ref = root_ref -> next;
}
temp -> next = root_ref -> next; // insert the node
root_ref -> next = temp;
}
void replace_line(Node* &root) { // replace a line
int line_no;
cout << "Enter the line number to replace: ";
cin >> line_no;
cin.ignore();
delete_line(root, line_no); // delete the line at index
add_line_at(root, line_no); // replace it with a new line
}
void interchange_lines(Node* &root) { // interchange two lines using their index
int index1, index2; // index of the lines to interchange
cout << "Enter the index for line 1: ";
cin >> index1;
cout << "Enter the index for line 2: ";
cin >> index2;
Node* root_ref = root;
for(int i=1; i<index1; i++) {
root_ref = root_ref -> next;
}
string line1 = root_ref -> data; // get text of line 1
root_ref = root;
for(int i=1; i<index2; i++) {
root_ref = root_ref -> next;
}
string line2 = root_ref -> data; // get text of line 2
// insert the line at the other index
add_line_at(root, index1, line2);
delete_line(root, index1+1);
add_line_at(root, index2, line1);
delete_line(root, index2+1);
}
void delete_line(Node* &root) { // delete a line whose index is given by the user
int line_no;
cout << "Enter line number to delete: ";
cin >> line_no;
if(line_no == 1) { // delete node at the head of list
Node* temp = root;
root = root -> next;
delete temp; // free up space
return;
}
Node* root_ref = root;
for(int i=1; i<line_no-1; i++) { // traverse till the specified index is reached
root_ref = root_ref -> next;
}
Node* temp = root_ref -> next;
root_ref -> next = temp -> next;
delete temp; // free up space
}
void delete_line(Node* &root, int line_no) { // delete the line at given index
if(line_no == 1) { // delete node at the head of list
Node* temp = root;
root = root -> next;
delete temp; // free up space
return;
}
Node* root_ref = root;
for(int i=1; i<line_no-1; i++) { // traverse till the specified index is reached
root_ref = root_ref -> next;
}
Node* temp = root_ref -> next;
root_ref -> next = temp -> next;
delete temp; // free up space
}
void open_file(Node* &root) { // read a file and return the head of linked list
ifstream file; // open file in read mode
string line;
file.open("sample.txt");
file.seekg(0, ios::beg); // set the pointer to the start of file
if(!file) {
cout << "File couldn't open successfully." << endl;
return;
}
else {
cout << "File opened successfully." << endl;
}
while(getline(file, line)) { // execute until file is read completely
//getline(file, line); // read a single line from file
if(root == NULL) {
root = new Node(line); // head node
continue;
}
Node* root_ref = root;
while(root_ref -> next != NULL) {
root_ref = root_ref -> next; // traverse till the end of linked list
}
Node* temp = new Node(line);
root_ref -> next = temp;
}
file.close();
}
void save_file(Node* &root) { // save the modified file
ofstream file;
file.open("sample.txt"); // open in output mode
Node* root_ref = root;
while(root_ref != NULL) {
string line = root_ref -> data;
file << line << endl; // write data in the file
root_ref = root_ref -> next;
}
cout << "File saved successfully." << endl;
file.close();
}
void push_root(Node* root, stack<Node*> &st) { // push the current root into stack
Node* head = NULL;
while(root != NULL) { // create a copy of linked list
if(head == NULL) {
head = new Node(root -> data);
root = root -> next;
continue;
}
Node* head_ref = head;
while(head_ref -> next != NULL) {
head_ref = head_ref -> next;
}
Node* temp = new Node(root -> data);
head_ref -> next = temp;
root = root -> next;
}
st.push(head); // push the head of the list in stack
}
void undo(Node* &root, stack<Node*> &st) { // restore the previous state of program
if(st.size() > 1) {
st.pop();
root = st.top(); // get the previous root
}
else {
cout << "\nUNDO unavailable..." << endl;
}
}
void display(Node* root) { // display the content of file
cout << "Current text:" << endl;
int i=0;
while(root != NULL) {
cout << ++i << "> "; // display line number
cout << root -> data << endl;
root = root -> next;
}
cout << endl;
}
void begin(Node* &root) { // contoller function
open_file(root);
stack<Node*> st;
push_root(root, st);
int select;
while(1) {
cout << "Enter your Selection:" << endl;
cout << "1. Insert line" << endl;
cout << "2. Insert line at index" << endl;
cout << "3. Replace a line" << endl;
cout << "4. Interchange two lines" << endl;
cout << "5. Delete line" << endl;
cout << "6. UNDO" << endl;
cout << "7. Display text file" << endl;
cout << "8. Save File" << endl;
cout << "-1. Exit" << endl;
cout << ">> ";
cin >> select;
cin.ignore();
switch(select)
{
case 1:
add_line(root);
push_root(root, st);
break;
case 2:
add_line_at(root);
push_root(root, st);
break;
case 3:
replace_line(root);
break;
case 4:
interchange_lines(root);
break;
case 5:
delete_line(root);
break;
case 6:
undo(root, st);
break;
case 7:
display(root);
break;
case 8:
save_file(root);
break;
case -1:
cout << "Editor exited...";
return;
}
}
}
};
int main() {
Node* root = NULL;
Editor a;
a.begin(root);
return 0;
}