jes/src/core/Buffer.h

118 lines
3.1 KiB
C++

#ifndef BUFFER_H
#define BUFFER_H
#include <list>
#include <memory>
#include "LineEndings.h"
#include "Encoding.h"
#include "GapBuffer.h"
class Buffer
{
public:
class Iterator
{
public:
Iterator(Buffer * buffer)
{
m_buffer = buffer;
m_offset = 0u;
m_line = 0u;
}
bool valid() const
{
return m_offset < m_buffer->size();
}
bool is_start_of_line();
bool is_end_of_line(bool eol_only);
bool go_back();
bool go_forward();
bool go_start_of_line();
bool go_end_of_line(bool allow_eol);
bool go_left_in_line();
bool go_right_in_line(bool allow_eol);
bool go_previous_line();
bool go_next_line();
void warp(ssize_t offset_offset, ssize_t line_offset)
{
m_offset += offset_offset;
m_line += line_offset;
}
uint8_t * address() const
{
return m_buffer->address(m_offset);
}
uint32_t operator*() const
{
if (valid())
{
return Encoding::decode(m_buffer->encoding(), address());
}
else
{
return 0xFFFFFFFFu;
}
}
bool operator==(const Iterator & other) const
{
return m_offset == other.m_offset;
}
bool operator!=(const Iterator & other) const
{
return m_offset != other.m_offset;
}
bool operator<(const Iterator & other) const
{
return m_offset < other.m_offset;
}
bool operator<=(const Iterator & other) const
{
return m_offset <= other.m_offset;
}
bool operator>(const Iterator & other) const
{
return m_offset > other.m_offset;
}
bool operator>=(const Iterator & other) const
{
return m_offset >= other.m_offset;
}
size_t line() const { return m_line; }
protected:
Buffer * m_buffer;
size_t m_offset;
size_t m_line;
};
Buffer();
Buffer(const char * filename);
Buffer(const uint8_t * data, size_t data_length);
bool write_to_file(const char * filename);
std::shared_ptr<Iterator> add_iterator()
{
return std::make_shared<Iterator>(this);
}
auto get_string() { return m_gap_buffer->get_string(); }
size_t size() const { return m_gap_buffer->size(); }
uint8_t * address(size_t offset) const { return m_gap_buffer->address(offset); }
Encoding::Type encoding() const { return m_encoding; }
uint8_t tabstop() const { return m_tabstop; }
protected:
bool m_eol_at_eof;
LineEndings::Type m_line_endings;
std::shared_ptr<GapBuffer> m_gap_buffer;
Encoding::Type m_encoding;
uint8_t m_tabstop;
void common_initialization();
bool load_from_file(const char * filename);
void load_empty_buffer();
bool load_from_memory(const uint8_t * data, size_t data_length);
void load_text_in_buffer(uint8_t * buffer, size_t buffer_size, size_t data_length);
};
#endif