add Buffer::write_to_file()
This commit is contained in:
parent
1730838921
commit
a416a47b29
@ -3,6 +3,7 @@
|
|||||||
#include "System.h"
|
#include "System.h"
|
||||||
#include "File.h"
|
#include "File.h"
|
||||||
#include "TextLoader.h"
|
#include "TextLoader.h"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
Buffer::Buffer()
|
Buffer::Buffer()
|
||||||
{
|
{
|
||||||
@ -10,6 +11,7 @@ Buffer::Buffer()
|
|||||||
m_file_buffer = nullptr;
|
m_file_buffer = nullptr;
|
||||||
m_file_buffer_size = 0u;
|
m_file_buffer_size = 0u;
|
||||||
m_eol_at_eof = true;
|
m_eol_at_eof = true;
|
||||||
|
m_line_endings = TextLoader::LINE_ENDING_LF;
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer::~Buffer()
|
Buffer::~Buffer()
|
||||||
@ -58,6 +60,55 @@ bool Buffer::load_from_file(const char * filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_eol_at_eof = text_loader.get_eol_at_eof();
|
m_eol_at_eof = text_loader.get_eol_at_eof();
|
||||||
|
m_line_endings = text_loader.get_line_endings();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Buffer::write_to_file(const char * filename)
|
||||||
|
{
|
||||||
|
File file;
|
||||||
|
const char * eol_seq;
|
||||||
|
size_t eol_len;
|
||||||
|
switch (m_line_endings)
|
||||||
|
{
|
||||||
|
case TextLoader::LINE_ENDING_LF:
|
||||||
|
eol_seq = "\n";
|
||||||
|
eol_len = 1u;
|
||||||
|
break;
|
||||||
|
case TextLoader::LINE_ENDING_CR:
|
||||||
|
eol_seq = "\r";
|
||||||
|
eol_len = 1u;
|
||||||
|
break;
|
||||||
|
case TextLoader::LINE_ENDING_CRLF:
|
||||||
|
eol_seq = "\r\n";
|
||||||
|
eol_len = 2u;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!file.open(filename, true))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto pd = piece_table->start_descriptor->next;
|
||||||
|
pd != piece_table->end_descriptor;
|
||||||
|
pd = pd->next)
|
||||||
|
{
|
||||||
|
if (!file.write(pd->start, pd->length))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pd->eol())
|
||||||
|
{
|
||||||
|
if (!file.write((const uint8_t *)eol_seq, eol_len))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,13 @@ public:
|
|||||||
Buffer();
|
Buffer();
|
||||||
~Buffer();
|
~Buffer();
|
||||||
bool load_from_file(const char * filename);
|
bool load_from_file(const char * filename);
|
||||||
|
bool write_to_file(const char * filename);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint8_t * m_file_buffer;
|
uint8_t * m_file_buffer;
|
||||||
unsigned long m_file_buffer_size;
|
unsigned long m_file_buffer_size;
|
||||||
bool m_eol_at_eof;
|
bool m_eol_at_eof;
|
||||||
|
uint8_t m_line_endings;
|
||||||
|
|
||||||
void free_file_buffer();
|
void free_file_buffer();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user