Clean up some tests.

This commit is contained in:
James Grogan 2022-12-01 10:52:48 +00:00
parent b17ba8b3a7
commit c102ebb6da
64 changed files with 615 additions and 541 deletions

View file

@ -4,32 +4,29 @@
#include "ByteUtils.h"
#include <streambuf>
#include <fstream>
#include <sstream>
File::File(std::filesystem::path path)
: mFullPath(path),
mInHandle(),
mOutHandle(),
mAccessMode(AccessMode::Read)
mOutHandle()
{
}
std::string File::GetExtension() const
File::~File()
{
close();
}
std::string File::getExtension() const
{
return mFullPath.extension().string();
}
void File::SetAccessMode(AccessMode mode)
{
mAccessMode = mode;
}
std::string File::dumpBinary()
{
mAccessMode = AccessMode::Read;
Open();
open(AccessMode::Read);
std::stringstream sstr;
sstr << "Count | Binary | Decimal | ASCII \n";
@ -46,22 +43,30 @@ std::string File::dumpBinary()
count++;
}
const auto out = sstr.str();
Close();
close();
return out;
}
std::ifstream* File::GetInHandle() const
std::ifstream* File::getInHandle() const
{
return mInHandle.get();
}
std::ofstream* File::GetOutHandle() const
std::ofstream* File::getOutHandle() const
{
return mOutHandle.get();
}
std::optional<unsigned char> File::readNextByte()
{
if (!mInHandle)
{
if (!open(AccessMode::Read))
{
return std::nullopt;
}
}
if (mInHandle->good())
{
if (auto val = mInHandle->get(); val == EOF)
@ -79,31 +84,39 @@ std::optional<unsigned char> File::readNextByte()
}
}
void File::Open(bool asBinary)
bool File::open(AccessMode accessMode)
{
if(mAccessMode == AccessMode::Read)
if (mFullPath.is_absolute() && !std::filesystem::exists(mFullPath.parent_path()))
{
std::filesystem::create_directories(mFullPath.parent_path());
}
if(accessMode == AccessMode::Read)
{
auto flags = std::ifstream::in;
if (asBinary)
{
//flags |= std::ifstream::binary;
}
mInHandle = std::make_unique<std::ifstream>();
mInHandle->open(mFullPath, flags);
if (!mInHandle->is_open())
{
MLOG_ERROR("Failed to open file at :" << mFullPath);
return false;
}
}
else
{
auto flags = std::ofstream::out;
if (asBinary)
{
//flags |= std::ofstream::binary;
}
mOutHandle = std::make_unique<std::ofstream>();
mOutHandle->open(mFullPath, flags);
if (!mOutHandle->is_open())
{
MLOG_ERROR("Failed to open file at :" << mFullPath);
return false;
}
}
return true;
}
void File::Close()
void File::close()
{
if(mOutHandle)
{
@ -115,27 +128,29 @@ void File::Close()
}
}
FileFormat::Format File::InferFormat() const
FileFormat::Format File::inferFormat() const
{
const auto extension = GetExtension();
const auto extension = getExtension();
return FileFormat::InferFormat(extension);
}
void File::WriteText(const std::string& text)
void File::writeText(const std::string& text)
{
bool had_to_open{ false };
bool had_to_open{false};
if (!mOutHandle)
{
had_to_open = true;
SetAccessMode(File::AccessMode::Write);
Open();
if (!open(File::AccessMode::Write))
{
return;
}
}
(*mOutHandle) << text;
if (had_to_open)
{
Close();
close();
}
}
@ -143,12 +158,15 @@ std::vector<std::string> File::readLines()
{
std::vector<std::string> content;
if (!PathExists())
if (!pathExists())
{
return content;
return {};
}
Open(false);
if(!open(AccessMode::Read))
{
return {};
}
std::string str;
while(std::getline(*mInHandle, str))
@ -156,23 +174,26 @@ std::vector<std::string> File::readLines()
content.push_back(str);
}
Close();
close();
return content;
}
std::string File::read()
{
if (!PathExists())
if (!pathExists())
{
return {};
}
Open(false);
if(!open(AccessMode::Read))
{
return {};
}
std::stringstream buffer;
buffer << mInHandle->rdbuf();
Close();
close();
return buffer.str();
}
@ -181,14 +202,24 @@ std::string File::getBaseFilename(const Path& path)
return path.stem().string();
}
std::string File::ReadText()
std::string File::readText()
{
if (!pathExists())
{
return {};
}
if(!open(AccessMode::Read))
{
return {};
}
std::string str((std::istreambuf_iterator<char>(*mInHandle)),
std::istreambuf_iterator<char>());
return str;
}
bool File::PathExists() const
bool File::pathExists() const
{
return std::filesystem::exists(mFullPath);
}

View file

@ -23,42 +23,38 @@ public:
File(std::filesystem::path fullPath);
std::string GetExtension() const;
~File();
FileFormat::Format InferFormat() const;
void close();
std::ifstream* GetInHandle() const;
std::string dumpBinary();
std::ofstream* GetOutHandle() const;
static std::string getBaseFilename(const Path& path);
void WriteText(const std::string& text);
std::string getExtension() const;
std::string ReadText();
std::ifstream* getInHandle() const;
std::ofstream* getOutHandle() const;
FileFormat::Format inferFormat() const;
std::string readText();
std::vector<std::string> readLines();
std::string read();
static std::string getBaseFilename(const Path& path);
bool pathExists() const;
bool PathExists() const;
void SetAccessMode(AccessMode mode);
void Open(bool asBinary = false);
void Close();
bool open(AccessMode mode);
std::optional<unsigned char> readNextByte();
std::string dumpBinary();
void writeText(const std::string& text);
private:
std::filesystem::path mFullPath;
std::unique_ptr<std::ifstream> mInHandle;
std::unique_ptr<std::ofstream> mOutHandle;
AccessMode mAccessMode;
};

View file

@ -119,17 +119,15 @@ void TomlReader::processLine(const std::string& line)
if (in_comment)
{
mWorkingTable->addComment({ mLastSectionOffset, working_string });
}
}
else if (in_header)
{
onHeader(working_string);
}
else if (found_key)
{
std::locale locale;
key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), key_string.end());
working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), working_string.end());
key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [](char c) {return std::isspace(c); }), key_string.end());
working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [](char c) {return std::isspace(c); }), working_string.end());
if (working_string.size()>2 && working_string[0] == '"' && working_string[working_string.size() - 1] == '"')
{
working_string = working_string.substr(1, working_string.size() - 2);

View file

@ -12,61 +12,61 @@ using Path = std::filesystem::path;
class TomlTable
{
public:
using Comment = std::pair<unsigned, std::string>;
using KeyValuePairs = std::unordered_map<std::string, std::string>;
using Comment = std::pair<unsigned, std::string>;
using KeyValuePairs = std::unordered_map<std::string, std::string>;
TomlTable(const std::string& header);
TomlTable(const std::string& header);
void addComment(const Comment& comment);
void addComment(const Comment& comment);
void addTable(std::unique_ptr<TomlTable> table);
void addTable(std::unique_ptr<TomlTable> table);
void addKeyValuePair(const std::string& key, const std::string& value);
void addKeyValuePair(const std::string& key, const std::string& value);
std::string getHeader() const;
std::string getHeader() const;
TomlTable* getTable(const std::string& path);
TomlTable* getTable(const std::string& path);
KeyValuePairs getKeyValuePairs() const;
KeyValuePairs getKeyValuePairs() const;
private:
unsigned mLineOffset{ 0 };
std::string mHeader;
std::unordered_map<std::string, std::unique_ptr<TomlTable> > mTables;
KeyValuePairs mMap;
std::vector<Comment> mComments;
unsigned mLineOffset{ 0 };
std::string mHeader;
std::unordered_map<std::string, std::unique_ptr<TomlTable> > mTables;
KeyValuePairs mMap;
std::vector<Comment> mComments;
};
class TomlContent
{
public:
TomlContent();
TomlContent();
TomlTable* getRootTable() const;
TomlTable* getRootTable() const;
TomlTable* getTable(const std::string& path) const;
TomlTable* getTable(const std::string& path) const;
private:
std::unique_ptr<TomlTable> mRootTable;
std::unique_ptr<TomlTable> mRootTable;
};
class TomlReader
{
public:
TomlReader();
TomlReader();
TomlContent* getContent() const;
TomlContent* getContent() const;
void read(const Path& input_path);
void read(const Path& input_path);
void processLine(const std::string& line);
void processLine(const std::string& line);
void onHeader(const std::string& header);
void onHeader(const std::string& header);
void onKeyValuePair(const std::string key, const std::string value);
void onKeyValuePair(const std::string key, const std::string value);
private:
unsigned mLastSectionOffset{ 0 };
std::unique_ptr<TomlContent> mContent;
TomlTable* mWorkingTable{nullptr};
};
unsigned mLastSectionOffset{ 0 };
std::unique_ptr<TomlContent> mContent;
TomlTable* mWorkingTable{nullptr};
};