replace FileLoader with TextLoader
This commit is contained in:
parent
cae0b581a2
commit
a2bd7504b5
@ -2,7 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include "System.h"
|
||||
#include "File.h"
|
||||
#include "FileLoader.h"
|
||||
#include "TextLoader.h"
|
||||
|
||||
Buffer::Buffer()
|
||||
{
|
||||
@ -45,12 +45,11 @@ bool Buffer::load_from_file(const char * filename)
|
||||
return false;
|
||||
}
|
||||
|
||||
/* TODO: remove FileLoader */
|
||||
FileLoader fl;
|
||||
fl.load_buf(m_file_buffer, file_size);
|
||||
for (size_t i = 0, num_lines = fl.num_lines(); i < num_lines; i++)
|
||||
TextLoader text_loader;
|
||||
text_loader.load_buffer(m_file_buffer, file_size);
|
||||
for (auto it = text_loader.begin(); it != text_loader.end(); it++)
|
||||
{
|
||||
m_lines.push_back(fl.get_line(i));
|
||||
m_lines.push_back(std::make_shared<Text>(it->first, it->second));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -1,43 +0,0 @@
|
||||
#ifndef JES_FILELOADER_H
|
||||
#define JES_FILELOADER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "Text.h"
|
||||
|
||||
class FileLoader
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
LINE_ENDING_LF,
|
||||
LINE_ENDING_CR,
|
||||
LINE_ENDING_CRLF,
|
||||
LINE_ENDING_COUNT
|
||||
};
|
||||
|
||||
FileLoader();
|
||||
void load_buf(uint8_t * buf, size_t size);
|
||||
size_t num_lines()
|
||||
{
|
||||
if (m_lines == NULL)
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_lines->size();
|
||||
}
|
||||
}
|
||||
int get_line_endings() { return m_line_endings; }
|
||||
std::shared_ptr<Text> get_line(size_t index);
|
||||
protected:
|
||||
typedef std::pair<const uint8_t *, size_t> LineIndexPair;
|
||||
typedef std::vector<LineIndexPair> LineIndexPairVector;
|
||||
typedef std::shared_ptr<LineIndexPairVector> LineIndexPairVectorRef;
|
||||
int m_line_endings;
|
||||
LineIndexPairVectorRef m_lines;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,36 +1,42 @@
|
||||
#include "FileLoader.h"
|
||||
#include "TextLoader.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/** Create a FileLoader. */
|
||||
FileLoader::FileLoader()
|
||||
/** Create a TextLoader. */
|
||||
TextLoader::TextLoader()
|
||||
{
|
||||
m_line_endings = LINE_ENDING_LF;
|
||||
m_lines = NULL;
|
||||
}
|
||||
|
||||
void FileLoader::load_buf(uint8_t * buf, size_t size)
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
LineIndexPairVectorRef lines[LINE_ENDING_COUNT];
|
||||
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<LineIndexPairVector>();
|
||||
lines[i] = std::make_shared<LineIndexPairList>();
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
if (buf[i] == '\r')
|
||||
if (buffer[i] == '\r')
|
||||
{
|
||||
lines[LINE_ENDING_CR]->push_back(LineIndexPair(&buf[line_start[LINE_ENDING_CR]], i - line_start[LINE_ENDING_CR]));
|
||||
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)) && (buf[i + 1] == '\n'))
|
||||
if ((i < (size - 1)) && (buffer[i + 1] == '\n'))
|
||||
{
|
||||
lines[LINE_ENDING_CRLF]->push_back(LineIndexPair(&buf[line_start[LINE_ENDING_CRLF]], i - line_start[LINE_ENDING_CRLF]));
|
||||
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;
|
||||
@ -41,9 +47,9 @@ void FileLoader::load_buf(uint8_t * buf, size_t size)
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (buf[i] == '\n')
|
||||
else if (buffer[i] == '\n')
|
||||
{
|
||||
lines[LINE_ENDING_LF]->push_back(LineIndexPair(&buf[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF]));
|
||||
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;
|
||||
@ -66,14 +72,3 @@ void FileLoader::load_buf(uint8_t * buf, size_t size)
|
||||
m_lines = lines[LINE_ENDING_LF];
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Text> FileLoader::get_line(size_t index)
|
||||
{
|
||||
if (index > num_lines())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const LineIndexPair & line_index_pair = (*m_lines)[index];
|
||||
return std::make_shared<Text>(line_index_pair.first, line_index_pair.second);
|
||||
}
|
44
src/core/TextLoader.h
Normal file
44
src/core/TextLoader.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef TEXTLOADER_H
|
||||
#define TEXTLOADER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
|
||||
class TextLoader
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
LINE_ENDING_LF,
|
||||
LINE_ENDING_CR,
|
||||
LINE_ENDING_CRLF,
|
||||
LINE_ENDING_COUNT
|
||||
};
|
||||
|
||||
TextLoader();
|
||||
void load_buffer(uint8_t * buffer, size_t size);
|
||||
size_t num_lines()
|
||||
{
|
||||
if (m_lines == nullptr)
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_lines->size();
|
||||
}
|
||||
}
|
||||
int get_line_endings() { return m_line_endings; }
|
||||
auto begin() { return m_lines->begin(); }
|
||||
auto end() { return m_lines->end(); }
|
||||
|
||||
protected:
|
||||
typedef std::pair<const uint8_t *, uint32_t> LineIndexPair;
|
||||
typedef std::list<LineIndexPair> LineIndexPairList;
|
||||
typedef std::shared_ptr<LineIndexPairList> LineIndexPairListRef;
|
||||
int m_line_endings;
|
||||
LineIndexPairListRef m_lines;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,3 +1,4 @@
|
||||
#if 0
|
||||
#include "gtest/gtest.h"
|
||||
#include "FileLoader.h"
|
||||
|
||||
@ -7,7 +8,6 @@ TEST(FileLoaderTest, num_lines_defaults_to_0)
|
||||
EXPECT_EQ(0u, fr.num_lines());
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST(FileLoaderTest, load_returns_false_for_nonexistent_file)
|
||||
{
|
||||
FileLoader fr;
|
||||
|
Loading…
x
Reference in New Issue
Block a user