Putting Buffers in main to test things out.

This commit is contained in:
Kyle Isom 2023-10-12 00:22:30 -07:00
parent 2dcc577f57
commit ba27e132e3
5 changed files with 118 additions and 15 deletions

View File

@ -22,10 +22,11 @@
///
#include <filesystem>
#include <iterator>
#include <optional>
#include <random>
#include <sstream>
#include <iterator>
#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<uint8_t> 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();
}
}

View File

@ -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();

21
File.cc
View File

@ -22,9 +22,24 @@
///
#include <filesystem>
#include <utility>
#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.

1
File.h
View File

@ -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);

24
main.cc
View File

@ -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;
}