From ba27e132e3019394526c4cfc46b51bde22ff63bc Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 12 Oct 2023 00:22:30 -0700 Subject: [PATCH] Putting Buffers in main to test things out. --- Buffer.cc | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- Buffer.h | 11 +++++++- File.cc | 21 ++++++++++----- File.h | 1 + main.cc | 24 +++++++++++++++--- 5 files changed, 118 insertions(+), 15 deletions(-) diff --git a/Buffer.cc b/Buffer.cc index 1a8b6df..f6dc509 100644 --- a/Buffer.cc +++ b/Buffer.cc @@ -22,10 +22,11 @@ /// +#include +#include #include #include #include -#include #include "Buffer.h" @@ -56,6 +57,17 @@ Buffer::Buffer(std::string fName) } +Buffer::Buffer(std::filesystem::path fPath) + : dirty(false), path(OptString(fPath.string())) +{ + this->name = fPath.filename().string(); + this->Cursor() = Cursor(); + + this->file = OptFile(fPath.string()); +} + + + Buffer::Buffer(std::string fName, std::string fPath) : dirty(false), name(std::move(fName)), path(fPath) { @@ -141,12 +153,23 @@ Buffer::Refresh() return FileStatus::FileStatusIOFailed; } - size_t currentLine = 0; - while (realHandle->good()) { + std::string temp; + while (std::getline(*realHandle, temp)) { + std::vector row(temp.begin(), temp.end()); + row.push_back('\n'); + this->contents.push_back(row); + } } -} + auto status = Buffer::FileStatus::FileStatusOK; + if (!realHandle->eof()) { + status = FileStatus::FileStatusIOFailed; + } + realHandle->close(); + + return status; +} void @@ -178,3 +201,48 @@ Buffer::MarkDirty() } +size_t +Buffer::Size() +{ + size_t size = 0; + + for (const auto& line : this->contents) { + size += line.size(); + } + + return 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(); + + } +} diff --git a/Buffer.h b/Buffer.h index c9763ac..0205a41 100644 --- a/Buffer.h +++ b/Buffer.h @@ -59,6 +59,8 @@ public: FileStatusNonExistent = 5, }; + static std::string FileStatusToString(FileStatus status); + static bool StatusOK(FileStatus status) { return status == FileStatus::FileStatusOK; } @@ -74,11 +76,16 @@ public: /// 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; } bool IsVirtual() - { return this->file.has_value(); } + { return !this->file.has_value(); } Buffer::FileStatus Flush(OptString altPath); @@ -100,6 +107,8 @@ public: bool IsDirty() { return this->dirty; } + size_t Size(); + private: void clearContents(); diff --git a/File.cc b/File.cc index 13f4acc..4d94473 100644 --- a/File.cc +++ b/File.cc @@ -22,9 +22,24 @@ /// #include +#include #include "File.h" +File::File(std::string fPath) + : path(fPath), readOnly(false) +{ + +} + + +File::File(std::filesystem::path fPath) + : path(std::move(fPath)), readOnly(false) +{ + +} + + const std::string File::Path() { return this->path.string(); @@ -37,12 +52,6 @@ void File::SetPath(const std::string &fPath) } -File::File(std::string fPath) - : path(fPath), readOnly(false) -{ - -} - OptOutFileStream File::Flush() { // Exit early if we shouldn't be writing to the file. diff --git a/File.h b/File.h index 2f8e0c6..d6e22ca 100644 --- a/File.h +++ b/File.h @@ -47,6 +47,7 @@ static constexpr std::ios::openmode DefaultMode = class File { public: File(std::string fPath); + File(std::filesystem::path fPath); const std::string Path(); void SetPath(const std::string &fPath); diff --git a/main.cc b/main.cc index eca0e0d..444e48e 100644 --- a/main.cc +++ b/main.cc @@ -26,7 +26,6 @@ #include "Buffer.h" #include "Cursor.h" -#include "LineEnding.h" static void @@ -51,13 +50,30 @@ int main(int argc, char *argv[]) { if ((argc == 2) && (std::string(argv[1]) == "-h")) { - std::cout << "help?\n"; + /// \todo proper command line parsing usage(std::cout, 0); } - Buffer frame; - std::cout << frame.Name() << "\n"; + for (int i = 1; i < argc; i++) { + std::filesystem::path path(argv[i]); + std::cout << "[+] target: " << path << "\n "; + auto buffer = Buffer(path); + std::cout << "\t[+] created buffer " << buffer.Name() << "\n"; + auto status = buffer.Refresh(); + if (!Buffer::StatusOK(status)) { + std::cerr << "[!] failed to read buffer "; + std::cerr << buffer.Name() << "\n"; + std::cerr << "\t[!] reason: "; + std::cerr << Buffer::FileStatusToString(status); + std::cerr << "\n"; + continue; + } + + std::cout << "\t[+] loaded buffer " << buffer.Name() + << " of " << buffer.Size() << " bytes.\n"; + buffer.Close(); + } return 0; }