-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cc
119 lines (108 loc) · 2.56 KB
/
Buffer.cc
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
#include <fstream>
#include <iostream>
#include "Buffer.h"
//TODO: your code here
//implement the functions in ListBuffer
Buffer::Buffer() {
/* std::cout << 1234; */
head = new List();
tail = new List();
head->back = tail;
tail->front = head;
}
Buffer::~Buffer() {
delete head;
delete tail;
}
void Buffer::writeToFile(const string &filename) const {
std::ofstream File;
File.open(filename);
List *tmp;
tmp = new List;
tmp = head->back;
/* std::cout << tmp->line; */
for (int i = 0; i < LineNum; i++) {
File << tmp->line <<'\n';
tmp = tmp->back;
}
File.close();
std::cout << ":" << Bytes + LineNum << " byte(s) written\n";
}
void Buffer::showLines(int from, int to) {
for (int i = from; i <= to; i++) {
std::cout << i << '\t';
moveToLine(i);
}
}
void Buffer::deleteLines(int from, int to){
int i = 1;
List *tmp;
tmp = new List;
tmp = head->back;
while (i != from) {
tmp = tmp->back;
i++;
}
for (i; i <= to; i++) {
List *del = tmp;
tmp = del->back;
del->front->back = tmp ;
tmp->front = del->front;
Bytes -= del->line.size();
delete del;
}
LineNum -= (to - from + 1);
currentLineNum = from;
if( LineNum == 0)
currentLineNum = 0;
}
void Buffer::insertLine(const string &text){
int i = 1;
List *tmp;
tmp = new List;
tmp = head->back;//?????????????
while ( i != currentLineNum) {
if (currentLineNum == 0) {
currentLineNum = 1;
break;
}
tmp = tmp->back;
i++;
}
List *New;
New = new List;
New->line = text;
New->front = tmp->front;
New->front->back = New;
New->back = tmp;
tmp->front = New;
LineNum++;
Bytes += text.size();
}
void Buffer::appendLine(const string &text){
int i = 0;
List *tmp;
tmp = new List;
tmp = head;//?????????????
/* std::cout << currentLineNum << " "; */
while ( i != currentLineNum ) {
tmp = tmp->back;
i++;
}
List *New2;
New2 = new List;
New2->line = text;
/* std::cout << 1234; */
New2->front = tmp;/* std::cout << 1234; */
New2->back = tmp->back;/* std::cout << 1234; */
New2->back->front = New2;/* std::cout << 1234; */
tmp->back = New2;/* std::cout << 1234; */
currentLineNum++;
LineNum++;
/* std::cout << sizeof(text); */
Bytes += text.size();
}
void Buffer::moveToLine(int idx) {
std::cout << getLine(idx) << '\n';
currentLineNum = idx;
}