21#ifndef __FILEBUFF_HPP__
22#define __FILEBUFF_HPP__
39 std::vector<std::string> ret;
49 if (buff.length() > 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;
157 virtual void del_str(
unsigned int pos,
unsigned int size) = 0;
175 std::ifstream file(this->
path);
177 while (std::getline(file, line)) {
178 this->buff.push_back(line);
184 std::ofstream file(this->
path);
185 for (
auto line : this->
buff) {
186 file << line <<
"\n";
192 line_filebuff(std::string file,
bool refresh =
false,
bool new_file =
false)
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);
204 tmp_pos = tmp_pos - (
signed long)line.length();
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();
215 std::string line = *it;
217 if ((tmp_pos - (
signed long)line.length()) < 0) {
218 signed long size_tmp = size;
221 if ((size_tmp - (
signed long)line.length()) < 0) {
223 ret += line.substr(tmp_pos, size_tmp);
226 ret += line.substr(0, size_tmp);
231 ret += line.substr(tmp_pos);
236 auto it = this->buff.begin();
241 size_tmp = size_tmp - line.length();
244 tmp_pos = tmp_pos - (
signed long)line.length();
247 return std::string(
"");
250 signed long tmp_pos = pos;
251 for (
unsigned int i = 0; i < this->buff.size(); i++) {
252 auto it = this->buff.begin();
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);
265 tmp_pos = tmp_pos - (
signed long)line.length();
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();
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);
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++) {
291 this->buff.insert(it, processed_str.at(j));
296 tmp_pos = tmp_pos - (
signed long)line.length();
307 signed long tmp_pos = pos;
308 for (
unsigned int i = 0; i < this->buff.size(); i++) {
309 auto it = this->buff.begin();
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);
321 tmp_pos = tmp_pos - (
signed long)line.length();
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();
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);
347 del_size = line.substr(tmp_pos).length();
353 signed long tmp_size = size - del_size;
356 auto it = this->buff.begin();
358 std::string line = *it;
359 if ((tmp_size - (
signed long)line.length()) < 0) {
360 std::string end = line.substr(tmp_size);
365 this->buff.erase(it);
367 tmp_size = tmp_size - line.length();
372 tmp_pos = tmp_pos - (
signed long)line.length();
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()));
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()));
414 this->buff.insert(it, str);
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()));
435 this->buff.erase(it);
460 std::ifstream file(this->
path);
461 std::stringstream str;
463 this->buff = str.str();
467 std::ofstream file(this->
path);
468 file.write(this->buff.c_str(), this->buff.length());
474 str_filebuff(std::string file,
bool refresh =
false,
bool new_file =
false)
481 if (pos < this->
buff.length()) {
482 return this->buff.at(pos);
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);
493 return std::string(
"");
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;
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;
521 std::string start, end;
522 start = this->buff.substr(0, pos);
523 end = this->buff.substr(pos + 1);
524 this->buff = start + end;
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;
Base class of filebuffer.
virtual void load_buffer()=0
Load buffer from file.
filebuff(std::string file, bool autorefresh=true)
Create filebuffer, simply store some vars.
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.
std::string path
Path to file.
bool autorefresh
Does file need to be automaticly.
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 ...
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.
line_filebuff(std::string file, bool refresh=false, bool new_file=false)
char get_char(unsigned int pos)
Get character from file, return -1 on error.
std::string get_line(unsigned int pos)
Get line from buffer.
void set_str(std::string str, unsigned int pos)
Set string in file.
void set_line(std::string str, unsigned int pos)
Set line in file.
void del_line(unsigned int pos)
Delete line from file.
void del_char(unsigned int pos)
Remove character from file.
void update()
Update file/buffer based on internal state variable.
void del_str(unsigned int pos, unsigned int size)
Remove sequence of characters from file.
void set_char(char ch, unsigned int pos)
Set character in file.
std::list< std::string > buff
std::string get_str(unsigned int pos, unsigned int size)
Get sequence of characters from file.
void load_buffer()
Load buffer from file.
void save_buffer()
Save buffer to file.
void set_char(char ch, unsigned int pos)
Set character in file.
void del_char(unsigned int pos)
Remove character from file.
void del_str(unsigned int pos, unsigned int size)
Remove sequence of characters from file.
void update()
Update file/buffer based on internal state variable.
void set_str(std::string str, unsigned int pos)
Set string in file.
str_filebuff(std::string file, bool refresh=false, bool new_file=false)
std::string get_str(unsigned int pos, unsigned int size)
Get sequence of characters from file.
void load_buffer()
Load buffer from file.
char get_char(unsigned int pos)
Get character from file, return -1 on error.
Filebuffer for CBuild ecosystem.
buffer_state
Represent atate of buffer.
@ BUFFER_NEWER
Buffer is newer and file need update.
@ FILE_NEWER
File is newer, buffer is not updated.
@ MATCH
File match buffer.
std::vector< std::string > parse_lines(std::string in)
Parse string to vector of strings.