Moving Buffer definitions out of the header file.

This commit is contained in:
Kyle Isom 2023-10-14 23:49:35 -07:00
parent 467bb064c7
commit 4b6e70e2c5
2 changed files with 102 additions and 66 deletions

140
Buffer.cc
View File

@ -47,6 +47,36 @@ anonymousName()
} }
std::string
Buffer::FileStatusToString(Buffer::FileStatus status)
{
switch (status) {
case FileStatus::FileStatusOK:
return std::string("OK");
case FileStatus::FileStatusReadOnly:
return std::string("read-only file");
case FileStatus::FileStatusIOFailed:
return std::string("I/O failure");
case FileStatus::FileStatusVirtual:
return std::string("virtual buffer");
case FileStatus::FileStatusInvalidPermissions:
return std::string("invalid permissions");
case FileStatus::FileStatusNonExistent:
return std::string("file does not exist");
default:
abort();
}
}
bool
Buffer::StatusOK(FileStatus status)
{
return status == FileStatus::FileStatusOK;
}
Buffer::Buffer() Buffer::Buffer()
: dirty(false), name(anonymousName()), status(statusOK) : dirty(false), name(anonymousName()), status(statusOK)
{ {
@ -63,9 +93,9 @@ Buffer::Buffer(std::string fName)
Buffer::Buffer(std::filesystem::path fPath) Buffer::Buffer(std::filesystem::path fPath)
: dirty(false), path(OptString(fPath.string())) : dirty(false), path(OptString(fPath.string()))
{ {
this->name = fPath.filename().string(); this->name = fPath.filename().string();
this->cursor = Cursor(); this->cursor = Cursor();
this->file = OptFile(fPath.string()); this->file = OptFile(fPath.string());
// N.B. I am leaving this in to show that I thought about it, but // N.B. I am leaving this in to show that I thought about it, but
// it's the wrong choice. A Frame should call Refresh on a buffer // it's the wrong choice. A Frame should call Refresh on a buffer
// when it's ready to load it. // when it's ready to load it.
@ -77,14 +107,37 @@ Buffer::Buffer(std::filesystem::path fPath)
} }
Buffer::Buffer(std::string fName, std::string fPath) Buffer::Buffer(std::string fName, std::string fPath)
: dirty(false), name(std::move(fName)), path(fPath) : dirty(false), name(std::move(fName)), path(fPath)
{ {
if (this->path) { if (this->path) {
this->file = OptFile (File(fPath)); this->file = OptFile(File(fPath));
} }
}
void
Buffer::Close()
{
this->clearContents();
this->dirty = false;
this->file = std::nullopt;
this->path = std::nullopt;
this->name = std::string("deleted buffer (" + this->name + ")");
}
const std::string
Buffer::Name()
{
return this->name;
}
bool
Buffer::IsVirtual()
{
return !this->file.has_value();
} }
@ -126,7 +179,7 @@ Buffer::Flush(OptString altPath)
return Buffer::FileStatus::FileStatusIOFailed; return Buffer::FileStatus::FileStatusIOFailed;
} }
for (auto line : this->contents) { for (auto line: this->contents) {
std::copy(line.begin(), line.end(), std::copy(line.begin(), line.end(),
std::ostream_iterator<uint8_t>(*realHandle, "")); std::ostream_iterator<uint8_t>(*realHandle, ""));
} }
@ -149,7 +202,7 @@ Buffer::Refresh()
} }
auto realFile = this->file.value(); auto realFile = this->file.value();
auto handle = realFile.Refresh(); auto handle = realFile.Refresh();
if (!handle) { if (!handle) {
return FileStatus::FileStatusNonExistent; return FileStatus::FileStatusNonExistent;
} }
@ -188,6 +241,13 @@ Buffer::Refresh()
} }
void
Buffer::Rename(std::string fName)
{
this->name = fName;
}
void void
Buffer::ChangePath(std::string newPath) Buffer::ChangePath(std::string newPath)
{ {
@ -195,14 +255,10 @@ Buffer::ChangePath(std::string newPath)
} }
void Cursor
Buffer::clearContents() Buffer::Where()
{ {
for (auto & line : this->contents) { return this->cursor;
line.clear();
}
this->contents.clear();
} }
@ -217,12 +273,19 @@ Buffer::MarkDirty()
} }
bool
Buffer::IsDirty()
{
return this->dirty;
}
size_t size_t
Buffer::Size() Buffer::Size()
{ {
size_t size = 0; size_t size = 0;
for (const auto& line : this->contents) { for (const auto &line: this->contents) {
size += line.size(); size += line.size();
} }
@ -230,39 +293,6 @@ Buffer::Size()
} }
void
Buffer::Close()
{
this->clearContents();
this->dirty = false;
this->file = std::nullopt;
this->path = std::nullopt;
this->name = std::string("deleted buffer (" + this->name + ")");
}
std::string
Buffer::FileStatusToString(Buffer::FileStatus status)
{
switch (status) {
case FileStatus::FileStatusOK:
return std::string("OK");
case FileStatus::FileStatusReadOnly:
return std::string("read-only file");
case FileStatus::FileStatusIOFailed:
return std::string("I/O failure");
case FileStatus::FileStatusVirtual:
return std::string("virtual buffer");
case FileStatus::FileStatusInvalidPermissions:
return std::string("invalid permissions");
case FileStatus::FileStatusNonExistent:
return std::string("file does not exist");
default:
abort();
}
}
bool bool
Buffer::Exists() Buffer::Exists()
@ -275,7 +305,6 @@ Buffer::Exists()
} }
void void
Buffer::PrintBufferStatus(std::ostream &os) Buffer::PrintBufferStatus(std::ostream &os)
{ {
@ -319,3 +348,16 @@ Buffer::PrintBufferStatus(std::ostream &os)
os << "\n"; os << "\n";
return; return;
} }
void
Buffer::clearContents()
{
for (auto &line: this->contents) {
line.clear();
}
this->contents.clear();
}

View File

@ -70,10 +70,7 @@ public:
}; };
static std::string FileStatusToString(FileStatus status); static std::string FileStatusToString(FileStatus status);
static bool StatusOK(FileStatus status);
static bool StatusOK(FileStatus status)
{ return status == FileStatus::FileStatusOK; }
/// The constructor with no arguments generates a new anonymous /// The constructor with no arguments generates a new anonymous
@ -83,19 +80,17 @@ public:
/// A single constructor generates a virtual buffer. /// A single constructor generates a virtual buffer.
Buffer(std::string fName); Buffer(std::string fName);
/// Instantiate a Buffer pointing to fPath.
Buffer(std::string fName, std::string fPath);
void Close();
/// Instantiate a buffer from a filesystem path. /// Instantiate a buffer from a filesystem path.
Buffer(std::filesystem::path fPath); Buffer(std::filesystem::path fPath);
std::string Name() const /// Instantiate a Buffer pointing to fPath.
{ return this->name; } Buffer(std::string fName, std::string fPath);
bool IsVirtual()
{ return !this->file.has_value(); } void Close();
const std::string Name();
bool IsVirtual();
Buffer::FileStatus Flush(OptString altPath); Buffer::FileStatus Flush(OptString altPath);
@ -108,14 +103,13 @@ public:
/// \return A FileStatus indicating whether the read was successful. /// \return A FileStatus indicating whether the read was successful.
Buffer::FileStatus Refresh(); Buffer::FileStatus Refresh();
void Rename(std::string fName);
void ChangePath(std::string newPath); void ChangePath(std::string newPath);
Cursor Where() Cursor Where();
{ return this->cursor; }
void MarkDirty(); void MarkDirty();
bool IsDirty() bool IsDirty();
{ return this->dirty; }
size_t Size(); size_t Size();