jes/src/core/TextLoader.cc
Josh Holtrop 7093560a94 Support loading files that do not end with a EOL sequence
- do not drop the last line of such files from TextLoader
- add a flag to TextLoader to indicate if the loaded file ended with a
  EOL sequence
2016-07-24 12:22:03 -04:00

83 lines
2.3 KiB
C++

#include "TextLoader.h"
#include <stdint.h>
/** Create a TextLoader. */
TextLoader::TextLoader()
{
m_line_endings = LINE_ENDING_LF;
m_lines = NULL;
m_eol_at_eof = true;
}
/**
* Scan text to detect line endings and record their positions.
*
* @param buffer Buffer containing the text to import.
* @param size Size of the buffer.
*/
void TextLoader::load_buffer(uint8_t * buffer, size_t size)
{
LineIndexPairListRef lines[LINE_ENDING_COUNT];
size_t line_start[LINE_ENDING_COUNT] = {0};
unsigned int n_cr = 0;
unsigned int n_lf = 0;
bool crlf = true;
for (size_t i = 0; i < LINE_ENDING_COUNT; i++)
{
lines[i] = std::make_shared<LineIndexPairList>();
}
for (size_t i = 0; i < size; i++)
{
if (buffer[i] == '\r')
{
lines[LINE_ENDING_CR]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_CR]], i - line_start[LINE_ENDING_CR]));
n_cr++;
line_start[LINE_ENDING_CR] = i + 1;
if (crlf)
{
if ((i < (size - 1)) && (buffer[i + 1] == '\n'))
{
lines[LINE_ENDING_CRLF]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_CRLF]], i - line_start[LINE_ENDING_CRLF]));
n_lf++;
i++;
line_start[LINE_ENDING_CRLF] = i + 1;
}
else
{
crlf = false;
}
}
}
else if (buffer[i] == '\n')
{
lines[LINE_ENDING_LF]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF]));
crlf = false;
n_lf++;
line_start[LINE_ENDING_LF] = i + 1;
}
}
if (crlf && (n_lf > 0u))
{
m_line_endings = LINE_ENDING_CRLF;
}
else if ((n_cr > 0u) && (n_lf == 0u))
{
m_line_endings = LINE_ENDING_CR;
}
else
{
m_line_endings = LINE_ENDING_LF;
}
m_lines = lines[m_line_endings];
/* Check if there is a line that was not terminated by a EOL sequence at
* the end of the file. */
if (line_start[m_line_endings] < size)
{
m_lines->push_back(LineIndexPair(&buffer[line_start[m_line_endings]], size - line_start[m_line_endings]));
m_eol_at_eof = false;
}
}