detect file encoding when loading it

This commit is contained in:
Josh Holtrop 2016-08-11 20:11:13 -04:00
parent c4253078a9
commit 490505faa1
4 changed files with 10 additions and 0 deletions

View File

@ -12,6 +12,7 @@ Buffer::Buffer()
m_file_buffer_size = 0u; m_file_buffer_size = 0u;
m_eol_at_eof = true; m_eol_at_eof = true;
m_line_endings = LineEndings::LF; m_line_endings = LineEndings::LF;
m_encoding = Encoding::UTF_8;
} }
Buffer::~Buffer() Buffer::~Buffer()
@ -63,6 +64,7 @@ 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(); m_line_endings = text_loader.get_line_endings();
m_encoding = text_loader.get_encoding();
return true; return true;
} }

View File

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include "PieceTable.h" #include "PieceTable.h"
#include "LineEndings.h" #include "LineEndings.h"
#include "Encoding.h"
class Buffer class Buffer
{ {
@ -21,6 +22,7 @@ protected:
unsigned long m_file_buffer_size; unsigned long m_file_buffer_size;
bool m_eol_at_eof; bool m_eol_at_eof;
LineEndings::Type m_line_endings; LineEndings::Type m_line_endings;
Encoding::Type m_encoding;
void free_file_buffer(); void free_file_buffer();
}; };

View File

@ -5,6 +5,7 @@
TextLoader::TextLoader() TextLoader::TextLoader()
{ {
m_line_endings = LineEndings::LF; m_line_endings = LineEndings::LF;
m_encoding = Encoding::UTF_8;
m_lines = NULL; m_lines = NULL;
m_eol_at_eof = true; m_eol_at_eof = true;
} }
@ -79,4 +80,6 @@ void TextLoader::load_buffer(uint8_t * buffer, size_t size)
m_lines->push_back(Span(&buffer[line_start[m_line_endings]], size - line_start[m_line_endings])); m_lines->push_back(Span(&buffer[line_start[m_line_endings]], size - line_start[m_line_endings]));
m_eol_at_eof = false; m_eol_at_eof = false;
} }
m_encoding = Encoding::detect_encoding(buffer, size);
} }

View File

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include "Span.h" #include "Span.h"
#include "LineEndings.h" #include "LineEndings.h"
#include "Encoding.h"
class TextLoader class TextLoader
{ {
@ -24,12 +25,14 @@ public:
} }
} }
LineEndings::Type get_line_endings() { return m_line_endings; } LineEndings::Type get_line_endings() { return m_line_endings; }
Encoding::Type get_encoding() { return m_encoding; }
auto begin() { return m_lines->begin(); } auto begin() { return m_lines->begin(); }
auto end() { return m_lines->end(); } auto end() { return m_lines->end(); }
bool get_eol_at_eof() { return m_eol_at_eof; } bool get_eol_at_eof() { return m_eol_at_eof; }
protected: protected:
LineEndings::Type m_line_endings; LineEndings::Type m_line_endings;
Encoding::Type m_encoding;
bool m_eol_at_eof; bool m_eol_at_eof;
std::shared_ptr<std::list<Span>> m_lines; std::shared_ptr<std::list<Span>> m_lines;
}; };