diff --git a/Buffer.cc b/Buffer.cc index a2e89c7..9cdde2e 100644 --- a/Buffer.cc +++ b/Buffer.cc @@ -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() : dirty(false), name(anonymousName()), status(statusOK) { @@ -63,9 +93,9 @@ Buffer::Buffer(std::string fName) Buffer::Buffer(std::filesystem::path fPath) : dirty(false), path(OptString(fPath.string())) { - this->name = fPath.filename().string(); + this->name = fPath.filename().string(); 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 // it's the wrong choice. A Frame should call Refresh on a buffer // 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) : dirty(false), name(std::move(fName)), path(fPath) { 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; } - for (auto line : this->contents) { + for (auto line: this->contents) { std::copy(line.begin(), line.end(), std::ostream_iterator(*realHandle, "")); } @@ -149,7 +202,7 @@ Buffer::Refresh() } auto realFile = this->file.value(); - auto handle = realFile.Refresh(); + auto handle = realFile.Refresh(); if (!handle) { return FileStatus::FileStatusNonExistent; } @@ -188,6 +241,13 @@ Buffer::Refresh() } +void +Buffer::Rename(std::string fName) +{ + this->name = fName; +} + + void Buffer::ChangePath(std::string newPath) { @@ -195,14 +255,10 @@ Buffer::ChangePath(std::string newPath) } -void -Buffer::clearContents() +Cursor +Buffer::Where() { - for (auto & line : this->contents) { - line.clear(); - } - - this->contents.clear(); + return this->cursor; } @@ -217,12 +273,19 @@ Buffer::MarkDirty() } +bool +Buffer::IsDirty() +{ + return this->dirty; +} + + size_t 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(); } @@ -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 Buffer::Exists() @@ -275,7 +305,6 @@ Buffer::Exists() } - void Buffer::PrintBufferStatus(std::ostream &os) { @@ -319,3 +348,16 @@ Buffer::PrintBufferStatus(std::ostream &os) os << "\n"; return; } + + +void +Buffer::clearContents() +{ + for (auto &line: this->contents) { + line.clear(); + } + + this->contents.clear(); +} + + diff --git a/Buffer.h b/Buffer.h index 0f053b6..977dc8a 100644 --- a/Buffer.h +++ b/Buffer.h @@ -70,10 +70,7 @@ public: }; static std::string FileStatusToString(FileStatus status); - - - static bool StatusOK(FileStatus status) - { return status == FileStatus::FileStatusOK; } + static bool StatusOK(FileStatus status); /// The constructor with no arguments generates a new anonymous @@ -83,19 +80,17 @@ public: /// A single constructor generates a virtual buffer. 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. Buffer(std::filesystem::path fPath); - std::string Name() const - { return this->name; } + /// Instantiate a Buffer pointing to fPath. + 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); @@ -108,14 +103,13 @@ public: /// \return A FileStatus indicating whether the read was successful. Buffer::FileStatus Refresh(); + void Rename(std::string fName); void ChangePath(std::string newPath); - Cursor Where() - { return this->cursor; } + Cursor Where(); void MarkDirty(); - bool IsDirty() - { return this->dirty; } + bool IsDirty(); size_t Size();