53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <windows.h>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <map>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace ks::files
|
|
{
|
|
class TextDocument
|
|
{
|
|
public:
|
|
static constexpr uint64_t pageBytes = 1024 * 1024;
|
|
static constexpr size_t editLimit = 8 * pageBytes;
|
|
|
|
explicit TextDocument(std::filesystem::path path, bool editable = true);
|
|
~TextDocument();
|
|
TextDocument(const TextDocument &) = delete;
|
|
TextDocument &operator=(const TextDocument &) = delete;
|
|
|
|
uint64_t pages() const;
|
|
uint64_t size() const { return size_; }
|
|
uint64_t boundary(uint64_t page) const;
|
|
std::string readPage(uint64_t page) const;
|
|
void releaseReadHandle();
|
|
void stage(uint64_t page, std::string_view utf8);
|
|
bool modified() const { return !changes_.empty(); }
|
|
bool modifiedWith(uint64_t page, std::string_view current) const;
|
|
bool save();
|
|
const wchar_t *encodingName() const;
|
|
|
|
private:
|
|
enum class Encoding { utf8, utf16le, utf16be };
|
|
struct Change { uint64_t offset; size_t size; };
|
|
|
|
std::filesystem::path path_;
|
|
HANDLE source_ = INVALID_HANDLE_VALUE;
|
|
HANDLE spool_ = INVALID_HANDLE_VALUE;
|
|
uint64_t size_{}, spoolSize_{};
|
|
size_t bom_{};
|
|
Encoding encoding_ = Encoding::utf8;
|
|
bool editable_;
|
|
std::map<uint64_t, Change> changes_;
|
|
|
|
std::string decode(std::string bytes) const;
|
|
uint64_t boundary(uint64_t page,HANDLE source) const;
|
|
std::string readOriginal(uint64_t page) const;
|
|
std::string encode(std::string_view utf8) const;
|
|
};
|
|
}
|