-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileEdit.cpp
311 lines (264 loc) · 6.44 KB
/
FileEdit.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
//
// Copyright(c) 2018 Daniel Arad.
//
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
// ---------------------------------------------------------------------
// File: FileEdit.cpp
// ---------------------------------------------------------------------
#include "FileEdit.h"
FileEdit::FileEdit(const std::string &file) : _file(file), _isReplaced(true)
{
initFileType(); //Unnecessary yet.
initLines();
}
FileEdit::FileEdit(const std::string &src, const std::string &dst) : _file(src), _newFile(dst), _isReplaced(false)
{
initFileType(); //Unnecessary yet.
initLines();
}
FileEdit::~FileEdit()
{
saveChanges();
}
void FileEdit::replaceAll(const std::string &toRemove, const std::string &newStr)
{
for(Line& line : _lines)
{
line.replace(toRemove, newStr);
}
}
void FileEdit::replaceAll(const std::string &toRemove, const std::string &newStr, const std::string &var)
{
unsigned long beforeSize; //toRemove length of part before var.
unsigned long afterSize;
unsigned long varSize; //length of var
unsigned int beforeStart; //start of before part in toRemove (0)
unsigned long afterStart; //start of after part in toRemove
long beforePos; //position of part before var in Line.
long afterPos; //position of part after var in Line.
unsigned long toRemoveSize;
unsigned int newBeforeStart;
unsigned long newBeforeSize;
unsigned long newAfterStart;
unsigned long newAfterSize;
unsigned long newStrSize;
varSize = var.length();
newStrSize = newStr.length();
newBeforeStart = 0;
newBeforeSize = newStr.find(var);
newAfterSize = newStrSize - newBeforeSize - varSize;
newAfterStart = newStrSize - newAfterSize;
toRemoveSize = toRemove.length();
beforeSize = toRemove.find(var);
afterSize = toRemoveSize - beforeSize - varSize;
beforeStart = 0;
afterStart = toRemoveSize - afterSize;
for(Line& line : _lines)
{
beforePos = line.find(toRemove.substr(0, beforeSize));
if(beforePos != std::string::npos)
{
afterPos = line.find(toRemove.substr(afterStart, toRemoveSize), beforePos + beforeSize);
if(afterPos != std::string::npos)
{
line.replace(toRemove.substr(afterStart, afterSize), newStr.substr(newAfterStart, newAfterSize), beforePos + beforeSize);
line.replace(toRemove.substr(beforeStart, beforeSize), newStr.substr(newBeforeStart, newBeforeSize));
}
}
}
}
void FileEdit::initFileType()
{
unsigned int dotPos;
unsigned int fileNameLength;
dotPos = _file.find_last_of('.');
if(dotPos != std::string::npos)
{
fileNameLength = _file.length();
_fileType = _file.substr(dotPos, fileNameLength);
}
else
{
_fileType = "";
}
}
void FileEdit::initLines()
{
std::ifstream file(_file);
std::string line;
while(std::getline(file, line))
{
_lines.emplace_back(Line(line));
}
file.close();
}
void FileEdit::saveChanges() const
{
std::ofstream file;
if(_isReplaced)
{
remove(_file.c_str());
file.open(_file);
}
else
{
file.open(_newFile);
}
if (file.is_open())
{
for(const Line& line : _lines)
{
file << line.getLine() << '\n';
}
file.close();
}
}
std::vector<Line> &FileEdit::getLines()
{
return _lines;
}
void FileEdit::operator<<(const std::string &str)
{
this->_lines.emplace_back(Line(str));
}
Line::Line(const std::string &str) : _line(str){}
Line::Line(const Line &line) : _line(line.getLine()) {}
const std::string &Line::getLine() const
{
return _line;
}
void Line::setLine(const std::string &str)
{
_line = str;
}
void Line::setLine(const Line line)
{
_line = line.getLine();
}
unsigned long Line::length() const
{
return _line.length();
}
unsigned long Line::find(const std::string &str) const
{
return _line.find(str);
}
unsigned long Line::find(const std::string &str, const unsigned long &pos) const
{
return _line.find(str, pos);
}
void Line::append(const std::string &str)
{
_line += str;
}
void Line::append(const Line &line)
{
_line += line.getLine();
}
void Line::remove(const std::string &str)
{
unsigned long size; //str length
long pos = -1; //position in _line
size = str.length();
pos = _line.find(str);
if(pos != -1) //if str was found in _line.
{
//the string in middle of _line gets cut of.
_line = _line.substr(0, pos) + _line.substr(pos + size, _line.length());
}
}
void Line::remove(const Line &line)
{
std::string str = line.getLine();
unsigned long size; //str length
unsigned long pos; //position in _line
size = str.length();
pos = _line.find(str);
if(pos != std::string::npos) //if str was found in _line.
{
//the string in middle of _line gets cut of.
_line = _line.substr(0, pos) + _line.substr(pos + size, _line.length());
}
}
void Line::replace(const std::string &src, const std::string &dst)
{
unsigned long size; //str length
unsigned long pos; //position in _line
size = src.length();
pos = _line.find(src);
if(pos != std::string::npos) //if str was found in _line.
{
//the string in middle of _line gets replaced.
_line = _line.substr(0, pos) + dst + _line.substr(pos + size, _line.length());
}
}
void Line::replace(const std::string &src, const std::string &dst, const unsigned long& pos)
{
unsigned long size; //str length
unsigned long posInLine; //position in _line
size = src.length();
posInLine = _line.find(src, pos);
if(posInLine != std::string::npos) //if str was found in _line.
{
//the string in middle of _line gets replaced.
_line = _line.substr(0, posInLine) + dst + _line.substr(posInLine + size, _line.length());
}
}
std::string Line::operator+(const std::string &str) const
{
return _line + str;
}
std::string Line::operator-(const std::string &str) const
{
unsigned long size; //str length
long pos = -1; //position in _line
size = str.length();
pos = _line.find(str);
if(pos != -1) //if str was found in _line.
{
//the string in middle of _line gets cut of.
return _line.substr(0, pos) + _line.substr(pos + size, _line.length());
}
return _line; //if string was not found.
}
Line &Line::operator+=(const std::string &str)
{
append(str);
return *this;
}
Line &Line::operator+=(const Line &line)
{
append(line);
return *this;
}
Line &Line::operator-=(const std::string &str)
{
remove(str);
return *this;
}
Line &Line::operator-=(const Line &line)
{
remove(line);
return *this;
}
Line &Line::operator=(const std::string &str)
{
setLine(str);
return *this;
}
Line &Line::operator=(const Line &line)
{
setLine(line);
return *this;
}
Line &Line::operator<<(const std::string &str)
{
append(str);
return *this;
}
Line &Line::operator<<(const Line &line)
{
append(line);
return *this;
}