-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatastruct.h
248 lines (208 loc) · 6.08 KB
/
datastruct.h
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
#include <iostream>
#include<fstream>
#include<string>
#include<exception>
#define SIZE 100
#define state_initial 0
#define state_editing 1
#define state_saved 2
class TextBlock {//一行的最小单位,长度为100的块
private:
char* text;
TextBlock *next_block;
public:
friend class TextNode;
friend class MainWindow;
TextBlock() {
text = new char[SIZE];
next_block = NULL;
}
~TextBlock() {
delete[] text;
next_block = NULL;
}
};
class TextNode{//指示一行的结点
private:
TextNode * nextnode;//下一行对应结点
TextBlock * first;
TextBlock * end;
int size;//当前容量
int length;//当前长度
public:
friend class Text;
friend class MainWindow;
TextNode():size(0),length(0){
nextnode = NULL;
first = NULL;
end = NULL;
}
~TextNode(){
TextBlock *cur, *next;
cur = first;
while (cur) {
next = cur->next_block;
delete cur;
cur = next;
}
nextnode = NULL;
}
int Index(std::string string_aim, int position);
void Input(std::string s);//用字符串s作为此行内容
void Extend();//扩充容量
void Renew();//重分配容量
//void Insert(std::string::const_iterator begin, std::string::const_iterator end, int position);
char& operator [](int i) {
if (i < size) {
int j = 0;
TextBlock *cur = first;
while (j < i / SIZE) {
cur = cur->next_block;
++j;
}
return cur->text[i - j *SIZE];
}
}
};
class Block{//指示文本中一块内容的类
public:
friend class Text;
friend class MainWindow;
Block(){
line_begin = line_end = position_begin = position_end = 0;
}
bool Block_Set(int y1, int y2, int x1 , int x2, std::string &s){
line_begin = y1;
line_end = y2;
position_begin = x1;
position_end = x2;
block_string = s;
return well_set();
}
void Reset(){
line_begin = line_end = position_begin = position_end = 0;
block_string = "";
}
bool well_set(){
if(line_begin == line_end && position_begin == position_end)
return 0;
return 1;
}
private:
int line_begin;//起始行
int line_end;//结束行
int position_begin;//起始行位置
int position_end;//结束行位置
std::string block_string;//此块所对应的字符串
};
class Cursor{
private:
int line;
int position;
int English;
int Chinese;
public:
friend class Text;
friend class MainWindow;
Cursor():line(1), position(1), English(0), Chinese(0){}
Cursor(int l, int p): line(l), position(p), English(0), Chinese(0){}
};
class Text{//代表文本的主数据结构
public:
enum direction {left,right,up,down};
friend class MainWindow;
Text():block(), cursor(1,1){
headnode = NULL;
tailnode = NULL;
lines = 0;
}
Text(std::string Filename) {
this->Text_Set(Filename);
}
~Text(){//析构函数
TextNode *curnode = headnode;
while(curnode){
TextNode *next = curnode->nextnode;
delete curnode;
curnode = next;
}
file.close();
}
void Remove_BOM(char *buffer);
void Add_BOM(char *buffer);
void Cursor_Set(int l,int p){cursor=Cursor(l,p);};//测试用,删
void InputFilename();
void New_File(std::string File_Name);
void Save_File(std::string New_Name);
void Quit_File();
bool Text_Set(std::string Filename);//根据提供文件名创建此类
bool Replace(std::string string_aim, std::string string_replace, int position, int line);//从某行某位置后面开始匹配目标字符串并把其用另一个串代替
bool Index(std::string string_aim, int line, int position, int &aimline, int &aimposition);//从某行某位置后面开始匹配目标字符串,若成功则返回字串的头位置。
bool Index_second_edition(std::string string_aim, int line, int position, int &aimline, int &aimposition);
void MoveCursor(direction dir);
void Insert_at_Cursor(std::string s);
void Delete_at_Cursor(direction dir);
void MoveCursor_to_start();
void MoveCursor_to_end();
void Count_CE();
void Output();//输出文本全部内容
void Clear(){//清除数据
TextNode *curnode = headnode;
while(curnode){
TextNode *next = curnode->nextnode;
delete curnode;
curnode = next;
}
file.close();
filename ="";
lines = 0;
headnode = NULL;
tailnode = headnode;
}
int Get_Max_Length(){
int max_length = 0;
TextNode *cur = headnode;
while(cur){
max_length = max_length > cur->length ? max_length : cur->length;
cur = cur->nextnode;
}
return max_length;
}
bool Block_Set(int line_begin, int position_begin, int line_end, int positon_end);//设置块
void Block_Delete(int line_begin, int position_begin, int line_end, int positon_end);//删除块
int isCursorBehind();//真实光标在后返回1 真实光标在前返回-1 两者重叠返回0
void cursor_reset(){
chunk=false;
cursor.line=1;
cursor.position=1;
cursor.Chinese=0;
cursor.English=0;
cursor_virtual=cursor;
}
void Index_at_Cursor(std::string string_aim);
void Index_at_Cursor_2(std::string string_aim);
void BlokCopy_at_Cursor();
void BlockDelete_at_Cursor();
void Cut_at_Cursor();
TextNode& operator[](int row){
int i = 1;
TextNode* cur = headnode;
while(i < row){
cur = cur->nextnode;
++i;
}
return *cur;
}
private:
std::fstream file;//文件
std::string filename;//当前文件名
TextNode* headnode;//头结点
TextNode* tailnode;//尾结点
int lines;//行数
Block block;//块
Cursor cursor;
Cursor cursor_virtual;
bool chunk;
bool Insert(int line, int position, std::string s);//向文本中某行某位置插入字符串
bool Delete(int line, int position);//删除某行某位置的一个字符
};