CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
filebuff.hpp
Go to the documentation of this file.
1
21#ifndef __FILEBUFF_HPP__
22#define __FILEBUFF_HPP__
23// C++ libraries
24#include "fstream"
25#include "iostream"
26#include "list"
27#include "sstream"
28#include "string"
29#include "vector"
30// Code
31namespace CBuild {
38inline std::vector<std::string> parse_lines(std::string in) {
39 std::vector<std::string> ret;
40 std::string buff;
41 for (char c : in) {
42 if (c == '\n') {
43 ret.push_back(buff);
44 buff.clear();
45 } else {
46 buff.push_back(c);
47 }
48 }
49 if (buff.length() > 0) {
50 ret.push_back(buff);
51 }
52 return ret;
53}
75class filebuff {
76 protected:
80 std::string path;
95 virtual void load_buffer() = 0;
99 virtual void save_buffer() = 0;
100
101 public:
108 filebuff(std::string file, bool autorefresh = true) {
109 this->path = file;
110 this->autorefresh = autorefresh;
112 }
119 virtual char get_char(unsigned int pos) = 0;
127 virtual std::string get_str(unsigned int pos, unsigned int size) = 0;
135 virtual void set_char(char ch, unsigned int pos) = 0;
143 virtual void set_str(std::string str, unsigned int pos) = 0;
150 virtual void del_char(unsigned int pos) = 0;
157 virtual void del_str(unsigned int pos, unsigned int size) = 0;
161 virtual void update() = 0;
168 return this->state;
169 }
170};
172 protected:
173 std::list<std::string> buff;
174 void load_buffer() {
175 std::ifstream file(this->path);
176 std::string line;
177 while (std::getline(file, line)) {
178 this->buff.push_back(line);
179 }
180 file.close();
181 this->state = CBuild::MATCH;
182 }
183 void save_buffer() {
184 std::ofstream file(this->path);
185 for (auto line : this->buff) {
186 file << line << "\n";
187 }
188 file.close();
189 }
190
191 public:
192 line_filebuff(std::string file, bool refresh = false, bool new_file = false)
193 : filebuff(file, refresh) {
194 if (!new_file) {
195 this->load_buffer();
196 }
197 }
198 char get_char(unsigned int pos) {
199 signed long tmp_pos = pos;
200 for (auto line : this->buff) {
201 if ((tmp_pos - (signed long)line.length()) < 0) {
202 return line.at(tmp_pos);
203 } else {
204 tmp_pos = tmp_pos - (signed long)line.length();
205 }
206 }
207 return -1;
208 }
209 std::string get_str(unsigned int pos, unsigned int size) {
210 signed long tmp_pos = pos;
211 std::string ret = "";
212 for (unsigned int i = 0; i < this->buff.size(); i++) {
213 auto it = this->buff.begin();
214 std::advance(it, i);
215 std::string line = *it;
216 // std::string line = this->buff.at(i);
217 if ((tmp_pos - (signed long)line.length()) < 0) {
218 signed long size_tmp = size;
219 bool first = true;
220 while (true) {
221 if ((size_tmp - (signed long)line.length()) < 0) {
222 if (first) {
223 ret += line.substr(tmp_pos, size_tmp);
224 return ret;
225 } else {
226 ret += line.substr(0, size_tmp);
227 return ret;
228 }
229 } else {
230 if (first) {
231 ret += line.substr(tmp_pos);
232 } else {
233 ret += line;
234 }
235 i++;
236 auto it = this->buff.begin();
237 std::advance(it, i);
238 line = *it;
239 }
240 first = false;
241 size_tmp = size_tmp - line.length();
242 }
243 } else {
244 tmp_pos = tmp_pos - (signed long)line.length();
245 }
246 }
247 return std::string("");
248 }
249 void set_char(char ch, unsigned int pos) {
250 signed long tmp_pos = pos;
251 for (unsigned int i = 0; i < this->buff.size(); i++) {
252 auto it = this->buff.begin();
253 std::advance(it, i);
254 std::string line = *it;
255 if ((tmp_pos - (signed long)line.length()) < 0) {
256 std::string start, end;
257 start = line.substr(0, tmp_pos);
258 end = line.substr(pos);
259 it->clear();
260 it->append(start);
261 it->push_back(ch);
262 it->append(end);
263 goto end;
264 } else {
265 tmp_pos = tmp_pos - (signed long)line.length();
266 }
267 }
268 end:
269 if (this->autorefresh) {
270 this->save_buffer();
271 } else {
273 }
274 }
275 void set_str(std::string str, unsigned int pos) {
276 signed long tmp_pos = pos;
277 for (unsigned int i = 0; i < this->buff.size(); i++) {
278 auto it = this->buff.begin();
279 std::advance(it, i);
280 std::string line = *it;
281 if ((tmp_pos - (signed long)line.length()) < 0) {
282 std::string start, end;
283 start = line.substr(0, tmp_pos);
284 end = line.substr(pos);
285 it->clear();
286 it->append(start);
287 std::vector<std::string> processed_str = parse_lines(str);
288 it->append(processed_str.at(0));
289 for (unsigned int j = 1; j < processed_str.size(); j++) {
290 std::advance(it, 1);
291 this->buff.insert(it, processed_str.at(j));
292 }
293 it->append(end);
294 goto end;
295 } else {
296 tmp_pos = tmp_pos - (signed long)line.length();
297 }
298 }
299 end:
300 if (this->autorefresh) {
301 this->save_buffer();
302 } else {
304 }
305 }
306 void del_char(unsigned int pos) {
307 signed long tmp_pos = pos;
308 for (unsigned int i = 0; i < this->buff.size(); i++) {
309 auto it = this->buff.begin();
310 std::advance(it, i);
311 std::string line = *it;
312 if ((tmp_pos - (signed long)line.length()) < 0) {
313 std::string start, end;
314 start = line.substr(0, tmp_pos);
315 end = line.substr(tmp_pos + 1);
316 it->clear();
317 it->append(start);
318 it->append(end);
319 goto end;
320 } else {
321 tmp_pos = tmp_pos - (signed long)line.length();
322 }
323 }
324 end:
325 if (this->autorefresh) {
326 this->save_buffer();
327 } else {
329 }
330 }
331 void del_str(unsigned int pos, unsigned int size) {
332 signed long tmp_pos = pos;
333 for (unsigned int i = 0; i < this->buff.size(); i++) {
334 auto it = this->buff.begin();
335 std::advance(it, i);
336 std::string line = *it;
337 if ((tmp_pos - (signed long)line.length()) < 0) {
338 bool multiline = false;
339 std::string start, end;
340 start = line.substr(0, tmp_pos);
341 unsigned int del_size = 0;
342 if (line.length() - pos > size) {
343 end = line.substr(tmp_pos + size + 1);
344 } else {
345 end = "";
346 multiline = true;
347 del_size = line.substr(tmp_pos).length();
348 }
349 it->clear();
350 it->append(start);
351 it->append(end);
352 if (multiline) {
353 signed long tmp_size = size - del_size;
354 while (true) {
355 i++;
356 auto it = this->buff.begin();
357 std::advance(it, i);
358 std::string line = *it;
359 if ((tmp_size - (signed long)line.length()) < 0) {
360 std::string end = line.substr(tmp_size);
361 it->clear();
362 it->append(end);
363 goto end;
364 } else {
365 this->buff.erase(it);
366 }
367 tmp_size = tmp_size - line.length();
368 }
369 }
370 goto end;
371 } else {
372 tmp_pos = tmp_pos - (signed long)line.length();
373 }
374 }
375 end:
376 if (this->autorefresh) {
377 this->save_buffer();
378 } else {
380 }
381 }
390 std::string get_line(unsigned int pos) {
391 auto it = this->buff.begin();
392 std::advance(it, pos);
393 if ((long unsigned int)std::distance(buff.begin(), it) >= buff.size()) {
394 throw std::runtime_error("Error in CBuild::line_filebuff - line out of range, " +
395 std::to_string(pos) + " >= " + std::to_string(buff.size()));
396 }
397 return *it;
398 }
407 void set_line(std::string str, unsigned int pos) {
408 auto it = this->buff.begin();
409 std::advance(it, pos);
410 if ((long unsigned int)std::distance(buff.begin(), it) >= buff.size()) {
411 throw std::runtime_error("Error in CBuild::line_filebuff - line out of range, " +
412 std::to_string(pos) + " >= " + std::to_string(buff.size()));
413 }
414 this->buff.insert(it, str);
415 if (this->autorefresh) {
416 this->save_buffer();
417 } else {
419 }
420 }
428 void del_line(unsigned int pos) {
429 auto it = this->buff.begin();
430 std::advance(it, pos);
431 if ((long unsigned int)std::distance(buff.begin(), it) >= buff.size()) {
432 throw std::runtime_error("Error in CBuild::line_filebuff - line out of range, " +
433 std::to_string(pos) + " >= " + std::to_string(buff.size()));
434 }
435 this->buff.erase(it);
436 if (this->autorefresh) {
437 this->save_buffer();
438 } else {
440 }
441 }
442 void update() {
443 if (this->state == CBuild::MATCH) {
444 return;
445 } else if (this->state == CBuild::BUFFER_NEWER) {
446 this->save_buffer();
447 return;
448 } else if (this->state == CBuild::FILE_NEWER) {
449 this->load_buffer();
450 return;
451 } else {
452 return;
453 }
454 }
455};
457 protected:
458 std::string buff;
459 void load_buffer() {
460 std::ifstream file(this->path);
461 std::stringstream str;
462 str << file.rdbuf();
463 this->buff = str.str();
465 }
466 void save_buffer() {
467 std::ofstream file(this->path);
468 file.write(this->buff.c_str(), this->buff.length());
470 file.close();
471 }
472
473 public:
474 str_filebuff(std::string file, bool refresh = false, bool new_file = false)
475 : filebuff(file, refresh) {
476 if (!new_file) {
477 this->load_buffer();
478 }
479 }
480 char get_char(unsigned int pos) {
481 if (pos < this->buff.length()) {
482 return this->buff.at(pos);
483 } else {
484 return -1;
485 }
486 }
487 std::string get_str(unsigned int pos, unsigned int size) {
488 if ((pos + size) < this->buff.length()) {
489 return this->buff.substr(pos, pos + size);
490 } else if (pos < this->buff.length()) {
491 return this->buff.substr(pos);
492 } else {
493 return std::string("");
494 }
495 }
496 void set_char(char ch, unsigned int pos) {
497 if (pos < this->buff.length()) {
498 std::string start, end;
499 start = this->buff.substr(0, pos);
500 end = this->buff.substr(pos);
501 this->buff = start + ch + end;
502 }
503 if (this->autorefresh) {
504 this->save_buffer();
505 } else {
507 }
508 }
509 void set_str(std::string str, unsigned int pos) {
510 std::string start, end;
511 start = this->buff.substr(0, pos);
512 end = this->buff.substr(pos);
513 this->buff = start + str + end;
514 if (this->autorefresh) {
515 this->save_buffer();
516 } else {
518 }
519 }
520 void del_char(unsigned int pos) {
521 std::string start, end;
522 start = this->buff.substr(0, pos);
523 end = this->buff.substr(pos + 1);
524 this->buff = start + end;
525 if (this->autorefresh) {
526 this->save_buffer();
527 } else {
529 }
530 }
531 void del_str(unsigned int pos, unsigned int size) {
532 std::string start, end;
533 start = this->buff.substr(0, pos);
534 end = this->buff.substr(pos + size + 1);
535 this->buff = start + end;
536 if (this->autorefresh) {
537 this->save_buffer();
538 } else {
540 }
541 }
542 void update() {
543 if (this->state == CBuild::MATCH) {
544 return;
545 } else if (this->state == CBuild::BUFFER_NEWER) {
546 this->save_buffer();
547 return;
548 } else if (this->state == CBuild::FILE_NEWER) {
549 this->load_buffer();
550 return;
551 } else {
552 return;
553 }
554 }
555};
556} // namespace CBuild
557#endif // __FILEBUFF_HPP__
Base class of filebuffer.
Definition filebuff.hpp:75
virtual void load_buffer()=0
Load buffer from file.
filebuff(std::string file, bool autorefresh=true)
Create filebuffer, simply store some vars.
Definition filebuff.hpp:108
virtual void del_str(unsigned int pos, unsigned int size)=0
Remove sequence of characters from file.
CBuild::buffer_state get_state()
Get state of file.
Definition filebuff.hpp:167
std::string path
Path to file.
Definition filebuff.hpp:80
bool autorefresh
Does file need to be automaticly.
Definition filebuff.hpp:84
virtual void set_str(std::string str, unsigned int pos)=0
Set string in file.
virtual std::string get_str(unsigned int pos, unsigned int size)=0
Get sequence of characters from file.
virtual void set_char(char ch, unsigned int pos)=0
Set character in file.
virtual void update()=0
Update file/buffer based on internal state variable.
virtual void del_char(unsigned int pos)=0
Remove character from file.
CBuild::buffer_state state
Currennt state of buffer. All operation that change buffer need to set state to CBuild::BUFFER_NEWER ...
Definition filebuff.hpp:91
virtual char get_char(unsigned int pos)=0
Get character from file, return -1 on error.
virtual void save_buffer()=0
Save buffer to file.
void save_buffer()
Save buffer to file.
Definition filebuff.hpp:183
line_filebuff(std::string file, bool refresh=false, bool new_file=false)
Definition filebuff.hpp:192
char get_char(unsigned int pos)
Get character from file, return -1 on error.
Definition filebuff.hpp:198
std::string get_line(unsigned int pos)
Get line from buffer.
Definition filebuff.hpp:390
void set_str(std::string str, unsigned int pos)
Set string in file.
Definition filebuff.hpp:275
void set_line(std::string str, unsigned int pos)
Set line in file.
Definition filebuff.hpp:407
void del_line(unsigned int pos)
Delete line from file.
Definition filebuff.hpp:428
void del_char(unsigned int pos)
Remove character from file.
Definition filebuff.hpp:306
void update()
Update file/buffer based on internal state variable.
Definition filebuff.hpp:442
void del_str(unsigned int pos, unsigned int size)
Remove sequence of characters from file.
Definition filebuff.hpp:331
void set_char(char ch, unsigned int pos)
Set character in file.
Definition filebuff.hpp:249
std::list< std::string > buff
Definition filebuff.hpp:173
std::string get_str(unsigned int pos, unsigned int size)
Get sequence of characters from file.
Definition filebuff.hpp:209
void load_buffer()
Load buffer from file.
Definition filebuff.hpp:174
void save_buffer()
Save buffer to file.
Definition filebuff.hpp:466
void set_char(char ch, unsigned int pos)
Set character in file.
Definition filebuff.hpp:496
void del_char(unsigned int pos)
Remove character from file.
Definition filebuff.hpp:520
void del_str(unsigned int pos, unsigned int size)
Remove sequence of characters from file.
Definition filebuff.hpp:531
void update()
Update file/buffer based on internal state variable.
Definition filebuff.hpp:542
void set_str(std::string str, unsigned int pos)
Set string in file.
Definition filebuff.hpp:509
str_filebuff(std::string file, bool refresh=false, bool new_file=false)
Definition filebuff.hpp:474
std::string get_str(unsigned int pos, unsigned int size)
Get sequence of characters from file.
Definition filebuff.hpp:487
void load_buffer()
Load buffer from file.
Definition filebuff.hpp:459
char get_char(unsigned int pos)
Get character from file, return -1 on error.
Definition filebuff.hpp:480
Filebuffer for CBuild ecosystem.
Definition Build.hpp:34
buffer_state
Represent atate of buffer.
Definition filebuff.hpp:57
@ BUFFER_NEWER
Buffer is newer and file need update.
Definition filebuff.hpp:69
@ FILE_NEWER
File is newer, buffer is not updated.
Definition filebuff.hpp:65
@ MATCH
File match buffer.
Definition filebuff.hpp:61
std::vector< std::string > parse_lines(std::string in)
Parse string to vector of strings.
Definition filebuff.hpp:38