-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLed.cpp
More file actions
419 lines (372 loc) · 8.43 KB
/
Led.cpp
File metadata and controls
419 lines (372 loc) · 8.43 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
#include "Led.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
Led::Led(string file) :
filename_{ file },
currentline_{ 0 },
lastline_{ 0 }
{}
Led::~Led() {}
void Led::run() {
bool file_existed = false;
if (filename_ == "")
file_existed = false;
else {
//reads in file if it's existed
file_existed = ReadFile(filename_);
}
//display header info
if (file_existed) {
cout << "\"" << filename_ << "\"" << " " << lastline_ << " lines" << endl;
}
else {
if (filename_ == "") {
cout << "\"?\"";
}
else {
cout << "\"" + filename_ + "\"";
}
cout << " " << "[New File]" << endl;
}
cout << "Entering command mode.\n";
//exit flag
bool exit_flag = false;
while (!exit_flag) {
cout << "? ";
string command_line;
getline(cin, command_line);
Command cmd(currentline_, lastline_);
if (!cmd.parse(command_line)) {
cout << "Invalid command\n";
continue;
}
int n1 = cmd.getFirstNumber();
int n2 = cmd.getSecondNumber();
char command = cmd.getCommand();
//check range
if (!(n1 >= 0 && n1 <= n2 && n2 <= lastline_)) {
cout << "Invalid range\n";
continue;
}
switch (command)
{
case'a':
Append(n1);
break;
case 'i':
Insert(n1);
break;
case 'v':
PasteBelow(n1);
break;
case 'u':
PasteAbove(n1);
break;
case 'd':
Delete(n1, n2);
break;
case 'x':
Cut(n1, n2);
break;
case 'r':
Replace(n1, n2);
break;
case 'j':
Join(n1, n2);
break;
case 'p':
Print(n1, n2);
break;
case 'c':
Change(n1, n2);
break;
case '-':
Up(n1);
break;
case '+':
Down(n1);
break;
case 'g':
Goto(n1);
break;
case 'w':
WriteToFile(file_existed);
break;
case 'q':
Exit(file_existed);
exit_flag = true;
break;
}
cin.clear();
clearerr(stdin);
}
}
bool Led::ReadFile(const std::string filename) {
ifstream fin;
fin.open(filename);
if (fin) {
string line;
int n = 0;
while (getline(fin, line)) {
buffer_.push_back(line);
++n;
}
currentline_ = n;
lastline_ = n;
return true;
}
else {
return false;
}
}
void Led::Insert(const int n1) {
if (lastline_ == 0) {
Append(0);
}
else {
Append(n1 - 1);
}
}
void Led::Append(const int n1) {
std::stringstream user_input;
user_input << cin.rdbuf();
string line;
list<string>::iterator it;
if (!buffer_.empty()) {
it = buffer_.begin();
advance(it, n1);
}
currentline_ = n1;
while (getline(user_input, line)) {
if (lastline_ != 0) {
buffer_.insert(it, line);
++currentline_;
}
else {
buffer_.push_back(line);
it = buffer_.begin();
++it;
currentline_ = 1;
}
++lastline_;
}
}
void Led::Print(const int n1, const int n2) const {
if (isEmpty()) return;
list<string>::const_iterator it;
list<string>::const_iterator it1;
list<string>::const_iterator it2;
it1 = buffer_.begin();
it2 = buffer_.begin();
advance(it1, n1 - 1);
advance(it2, n2);
int n = n1;
for (it = it1; it != it2; ++it) {
if (n != currentline_) {
cout << n << ": ";
}
else {
cout << n << "> ";
}
cout << *it << endl;
++n;
}
}
void Led::Goto(const int n1) {
if (isEmpty()) return;
currentline_ = n1;
}
bool Led::isEmpty() const {
if (buffer_.empty()) {
cout << "Empty buffer" << endl;
return true;
}
else {
return false;
}
}
void Led::Delete(const int n1, const int n2) {
list<string>::iterator range_begin;
list<string>::iterator range_end;
range_begin = buffer_.begin();
range_end = buffer_.begin();
advance(range_begin, n1 - 1);
advance(range_end, n2);
//update last line
lastline_ -= n2 - n1 + 1;
//set current address
if (range_end == buffer_.end())
currentline_ = n1 - 1;
else
currentline_ = n1;
buffer_.erase(range_begin, range_end);
}
void Led::Cut(const int n1, const int n2) {
list<string>::iterator range_begin;
list<string>::iterator range_end;
range_begin = buffer_.begin();
range_end = buffer_.begin();
advance(range_begin, n1 - 1);
advance(range_end, n2);
//update last line
lastline_ -= n2 - n1 + 1;
//set current address
if (range_end == buffer_.end())
currentline_ = n1 - 1;
else
currentline_ = n1;
clipboard_.clear();
clipboard_.insert(clipboard_.begin(), range_begin, range_end);
buffer_.erase(range_begin, range_end);
//if (buffer_.empty()) {
// currentline_ = 0;
// lastline_ = 0;
//}
//cout << "CL:" << currentline_ << endl;
//cout << "LL:" << lastline_ << endl;
}
void Led::PasteBelow(const int n1) {
list<string>::iterator range_begin;
range_begin = buffer_.begin();
advance(range_begin, n1);
buffer_.insert(range_begin, clipboard_.begin(), clipboard_.end());
//update address info
currentline_ = n1 + clipboard_.size();
lastline_ += clipboard_.size();
}
void Led::PasteAbove(const int n1) {
PasteBelow(n1 - 1);
}
void Led::Replace(const int n1, const int n2) {
Delete(n1, n2);
Insert(n1);
}
void Led::Join(const int n1, const int n2) {
list<string>::iterator first = buffer_.begin();
list<string>::iterator range_begin = buffer_.begin();
list<string>::iterator range_end = buffer_.begin();
advance(first, n1 - 1);
advance(range_begin, n1);
advance(range_end, n2);
for (list<string>::iterator it = range_begin; it != range_end; ++it)
(*first) += ' ' + (*it);
Delete(n1 + 1, n2);
currentline_ = n1;
}
void Led::Change(const int n1, const int n2) {
string find_str;
string to_str;
cout << "Change what? ";
getline(cin, find_str);
cout << setw(13) << "To what? ";
getline(cin, to_str);
string::size_type len_to_str = to_str.size();
string::size_type len_find_str = find_str.size();
//counter for occurrence
int cnt = 0;
list<string>::iterator range_begin;
list<string>::iterator range_end;
range_begin = buffer_.begin();
range_end = buffer_.begin();
advance(range_begin, n1 - 1);
advance(range_end, n2);
int line_last_occurence = currentline_;
int tmp_line = n1;
for (list<string>::iterator it = range_begin; it != range_end; ++it) {
string::size_type pos = (*it).find(find_str);
while (pos != string::npos) {
(*it).replace(pos, len_find_str, to_str);
++cnt;
pos = (*it).find(find_str, pos + len_to_str);
line_last_occurence = tmp_line;
}
++tmp_line;
}
//update address info: last occurence been replaced
currentline_ = line_last_occurence;
cout << "Changed " << cnt << " occurrence(s)" << endl;
}
void Led::Up(const int n1) {
list<string>::iterator current = buffer_.begin();
list<string>::iterator dest = buffer_.begin();
//set to current line
advance(current, currentline_ - 1);
//set destination line
//if doesn't exceed the top of file
if (currentline_ - n1 > 0) {
advance(dest, currentline_ - n1 - 1);
currentline_ = currentline_ - n1;
buffer_.insert(dest, (*current));
buffer_.erase(current);
}
else {
currentline_ = 1;
cout << "Top of file reached\n";
}
}
void Led::Down(const int n1) {
list<string>::iterator current = buffer_.begin();
list<string>::iterator dest = buffer_.begin();
//set to current line
advance(current, currentline_ - 1);
//set destination line
//if doesn't exceed the end of file
if (currentline_ + n1 <= lastline_) {
advance(dest, currentline_ + n1);
currentline_ = currentline_ + n1;
buffer_.insert(dest, (*current));
buffer_.erase(current);
}
else {
currentline_ = lastline_;
cout << "End of file reached\n";
}
}
void Led::WriteToFile(bool& file_existed) {
//file does not exist
if (!file_existed && filename_ == "") {
cout << "Enter a file name: ";
getline(cin, filename_);
}
ofstream fout(filename_);
//output error
while (!fout) {
cout << "Write to file error" << endl;
//an incorrect non-existing file name, gives user opportunity to correct
if (!file_existed) {
cout << "Please try to enter a valid file name: ";
getline(cin, filename_);
fout.open(filename_);
}
else {
return;
}
}
for (list<string>::iterator it = buffer_.begin(); it != buffer_.end(); ++it) {
fout << (*it) << endl;
}
fout.close();
//writes to file successfully, update file status by reference
file_existed = true;
cout << buffer_.size() << " lines written to file: \"" << filename_ << "\"" << endl;
}
void Led::Exit(bool& file_existed) {
bool valid_input = false;
string user_input = "";
while (!valid_input) {
cout << "Save changes to a file (y/n)? ";
getline(cin, user_input);
if (user_input == "y" || user_input == "n") {
valid_input = true;
break;
}
cout << "bad answer: " << user_input << endl;
cout << "Enter y for yes and n for no." << endl;
}
if (user_input == "y")
WriteToFile(file_existed);
cout << "Bye" << endl;
}