Support empty file-backed buffers.
This commit is contained in:
29
Buffer.cpp
29
Buffer.cpp
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
Buffer::Buffer() = default;
|
||||
@@ -17,11 +18,29 @@ Buffer::Buffer(const std::string &path)
|
||||
bool
|
||||
Buffer::OpenFromFile(const std::string &path, std::string &err)
|
||||
{
|
||||
std::ifstream in(path, std::ios::in | std::ios::binary);
|
||||
if (!in) {
|
||||
err = "Failed to open file: " + path;
|
||||
return false;
|
||||
}
|
||||
// If the file doesn't exist, initialize an empty, non-file-backed buffer
|
||||
// with the provided filename. Do not touch the filesystem until Save/SaveAs.
|
||||
if (!std::filesystem::exists(path)) {
|
||||
rows_.clear();
|
||||
nrows_ = 0;
|
||||
filename_ = path;
|
||||
is_file_backed_ = false;
|
||||
dirty_ = false;
|
||||
|
||||
// Reset cursor/viewport state
|
||||
curx_ = cury_ = rx_ = 0;
|
||||
rowoffs_ = coloffs_ = 0;
|
||||
mark_set_ = false;
|
||||
mark_curx_ = mark_cury_ = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::ifstream in(path, std::ios::in | std::ios::binary);
|
||||
if (!in) {
|
||||
err = "Failed to open file: " + path;
|
||||
return false;
|
||||
}
|
||||
|
||||
rows_.clear();
|
||||
std::string line;
|
||||
|
||||
Reference in New Issue
Block a user