-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceListText.cpp
More file actions
252 lines (212 loc) · 5.21 KB
/
PieceListText.cpp
File metadata and controls
252 lines (212 loc) · 5.21 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
#include "PieceListText.h"
#include "Helpers.h"
#include "Parser.h"
#include <cassert>
#include <sstream>
#include <ctype.h>
#include <cstdio>
std::string const cScratchFileName = "scratch.txt";
PieceListText::PieceListText()
{
mFirstPiece = nullptr;
mLength = 0;
mFileName = "";
}
PieceListText::~PieceListText()
{
Piece * piece = mFirstPiece;
Piece * tmp = piece;
while (piece != nullptr)
{
tmp = piece->getNext();
delete piece;
piece = tmp;
}
}
void PieceListText::insert(int pos, char c, QFont font)
{
Piece * p = split(pos);
int last_index = p->getFilePos() + p->getLength();
int scratchFileSize = Helpers::GetFileSize(mScratchFileName);
if (!(p->getFile() == cScratchFileName && last_index == scratchFileSize && p->getFont() == font))
{
Piece * q = new Piece(mScratchFileName, 0, scratchFileSize, p->getNext(), font);
p->setNext(q);
p = q;
}
mScratchFileStream << c;
mScratchFileStream.flush();
p->setLength(p->getLength() + 1);
mLength++;
std::string letter(1,c);
notify(UpdateEvent(pos, pos, letter));
}
void PieceListText::insert(int pos, std::string const& str, QFont font)
{
for (int i=0; i<str.size(); i++)
insert(pos++, str[i], font);
}
Piece * PieceListText::split(int pos)
{
if (pos == 0) return mFirstPiece;
//--- set p to piece containing pos
Piece * p = mFirstPiece;
int len = p->getLength();
while (pos > len) {
p = p->getNext();
len = len + p->getLength();
}
//--- split piece p
if (pos != len) {
int len2 = len - pos;
int len1 = p->getLength() - len2;
p->setLength(len1);
Piece * q = new Piece(p->getFile(), len2, p->getFilePos() + len1, p->getNext());
p->setNext(q);
}
return p;
}
void PieceListText::delete_(int from, int to)
{
Piece * a = split(from);
Piece * b = split(to);
a->setNext(b->getNext());
delete b; b = nullptr;
notify(UpdateEvent(from, to, ""));
}
char PieceListText::charAt(int pos)
{
if (pos >= mLength)
return '\0';
Piece * piece = mFirstPiece;
// skip dummy piece
if (piece->getNext() == nullptr)
return '\0';
piece = piece->getNext();
int count = 0;
while (piece != nullptr)
{
if (pos < count + piece->getLength())
{
// in this piece
std::string text = piece->getText();
return text[pos - count];
}
count += piece->getLength();
piece = piece->getNext();
}
return '\0';
}
QFontMetrics PieceListText::metricsAt(int pos)
{
if (pos >= mLength)
return QFontMetrics(QFont());
Piece * piece = mFirstPiece;
// skip dummy piece
if (piece->getNext() == nullptr)
return QFontMetrics(QFont());
piece = piece->getNext();
int count = 0;
while (piece != nullptr)
{
if (pos < count + piece->getLength())
{
// in this piece
return QFontMetrics(piece->getFont());
}
count += piece->getLength();
piece = piece->getNext();
}
return QFontMetrics(QFont());
}
Piece * PieceListText::getFirst() const
{
return mFirstPiece;
}
int PieceListText::getLength() const
{
return mLength;
}
bool PieceListText::load(std::string const& file)
{
mFileName = file;
int length = 0;
Piece * piece = Parser::parseFile(file, length);
if (piece != nullptr)
{
mFirstPiece = piece;
mLength = length;
mScratchFileName = cScratchFileName;
mScratchFileStream.open(mScratchFileName, std::ofstream::out | std::ofstream::trunc);
return true;
}
else
{
mFirstPiece = nullptr;
mLength = 0;
assert(false);
return false;
}
}
bool PieceListText::save(std::string const& file)
{
return Parser::writeFile(file, mFirstPiece);
}
void PieceListText::addListener(UpdateEventListener * listener)
{
mListeners.push_back(listener);
}
void PieceListText::removeListener(UpdateEventListener * listener)
{
TListenersIter iter = std::find(mListeners.begin(), mListeners.end(), listener);
if (iter != mListeners.end())
mListeners.erase(iter);
}
void PieceListText::notify(UpdateEvent e)
{
for (UpdateEventListener * listener : mListeners)
listener->update(e);
}
Word PieceListText::wordAt(int tpos)
{
Word word;
int len = 0;
int it = tpos;
// check to the left
char c = charAt(it);
while (!isspace(c) && it > 0)
{
it--;
len++;
c = charAt(it);
}
word.tpos = (it != 0) ? it+1 : 0;
// now to the right
it = tpos+1;
c = charAt(it);
while (!isspace(c) && it < mLength-1)
{
it++;
len++;
c = charAt(it);
}
word.len = len + 1;
return word;
}
std::string PieceListText::getFileContent()
{
Piece * piece = getFirst();
std::stringstream s;
while (piece != nullptr)
{
s << piece->getText();
piece = piece->getNext();
}
return s.str();
}
int PieceListText::find(std::string const& word)
{
std::string s = getFileContent();
size_t idx = s.find(word);
return (idx == std::string::npos) ? -1 : int(idx);
}