Replace FileAPI with c++ streams and std::filesystem

This commit is contained in:
Jan
2021-03-03 14:04:35 +01:00
parent b6b0a57232
commit 1cd06668e0
96 changed files with 1355 additions and 1061 deletions

View File

@ -1,8 +1,11 @@
#pragma once
#include "SearchPathSearchOptions.h"
#include "Utils/FileAPI.h"
#include <functional>
#include <istream>
#include <memory>
#include "Utils/ObjStream.h"
#include "SearchPathSearchOptions.h"
class ISearchPath
{
@ -14,7 +17,7 @@ public:
* \param fileName The relative path to the file to open.
* \return A pointer to an \c IFile object to read the found file or \c nullptr when no file could be found.
*/
virtual FileAPI::IFile* Open(const std::string& fileName) = 0;
virtual std::unique_ptr<std::istream> Open(const std::string& fileName) = 0;
/**
* \brief Returns the path to the search path.

View File

@ -1,7 +1,11 @@
#include "SearchPathFilesystem.h"
#include "Utils/PathUtils.h"
#include <filesystem>
#include <fstream>
#include "Utils/ObjFileStream.h"
namespace fs = std::filesystem;
SearchPathFilesystem::SearchPathFilesystem(std::string path)
{
@ -13,20 +17,19 @@ std::string SearchPathFilesystem::GetPath()
return m_path;
}
FileAPI::IFile* SearchPathFilesystem::Open(const std::string& fileName)
std::unique_ptr<std::istream> SearchPathFilesystem::Open(const std::string& fileName)
{
FileAPI::File file = FileAPI::Open(utils::Path::Combine(m_path, fileName), FileAPI::Mode::MODE_READ);
auto file = std::make_unique<std::ifstream>(fs::path(m_path).append(fileName).string(), std::fstream::in | std::fstream::binary);
if (file.IsOpen())
if (file->is_open())
{
return new FileAPI::File(std::move(file));
return std::move(file);
}
return nullptr;
}
void SearchPathFilesystem::Find(const SearchPathSearchOptions& options,
const std::function<void(const std::string&)>& callback)
void SearchPathFilesystem::Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback)
{
try
{

View File

@ -1,8 +1,9 @@
#pragma once
#include "ISearchPath.h"
#include <string>
#include "ISearchPath.h"
class SearchPathFilesystem final : public ISearchPath
{
std::string m_path;
@ -10,7 +11,7 @@ class SearchPathFilesystem final : public ISearchPath
public:
explicit SearchPathFilesystem(std::string path);
FileAPI::IFile* Open(const std::string& fileName) override;
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
std::string GetPath() override;
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
};

View File

@ -5,15 +5,7 @@
SearchPaths::SearchPaths() = default;
SearchPaths::~SearchPaths()
{
for(auto searchPathToFree : m_to_free)
{
delete searchPathToFree;
}
m_to_free.clear();
m_search_paths.clear();
}
= default;
SearchPaths::SearchPaths(const SearchPaths& other)
: m_search_paths(other.m_search_paths)
@ -41,15 +33,15 @@ SearchPaths& SearchPaths::operator=(SearchPaths&& other) noexcept
return *this;
}
FileAPI::IFile* SearchPaths::Open(const std::string& fileName)
std::unique_ptr<std::istream> SearchPaths::Open(const std::string& fileName)
{
for(auto searchPathEntry : m_search_paths)
for(auto* searchPathEntry : m_search_paths)
{
auto* file = searchPathEntry->Open(fileName);
auto file = searchPathEntry->Open(fileName);
if(file != nullptr)
if(file)
{
return file;
return std::move(file);
}
}
@ -63,16 +55,16 @@ std::string SearchPaths::GetPath()
void SearchPaths::Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback)
{
for (auto searchPathEntry : m_search_paths)
for (auto* searchPathEntry : m_search_paths)
{
searchPathEntry->Find(options, callback);
}
}
void SearchPaths::CommitSearchPath(ISearchPath* searchPath)
void SearchPaths::CommitSearchPath(std::unique_ptr<ISearchPath> searchPath)
{
m_search_paths.push_back(searchPath);
m_to_free.push_back(searchPath);
m_search_paths.push_back(searchPath.get());
m_owned_search_paths.emplace_back(std::move(searchPath));
}
void SearchPaths::IncludeSearchPath(ISearchPath* searchPath)

View File

@ -1,12 +1,13 @@
#pragma once
#include "ISearchPath.h"
#include <vector>
#include "ISearchPath.h"
class SearchPaths final : public ISearchPath
{
std::vector<ISearchPath*> m_search_paths;
std::vector<ISearchPath*> m_to_free;
std::vector<std::unique_ptr<ISearchPath>> m_owned_search_paths;
public:
using iterator = std::vector<ISearchPath*>::iterator;
@ -14,7 +15,7 @@ public:
SearchPaths();
~SearchPaths() override;
FileAPI::IFile* Open(const std::string& fileName) override;
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
std::string GetPath() override;
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
@ -27,7 +28,7 @@ public:
* \brief Adds a search path that gets deleted upon destruction of the \c SearchPaths object.
* \param searchPath The search path to add.
*/
void CommitSearchPath(ISearchPath* searchPath);
void CommitSearchPath(std::unique_ptr<ISearchPath> searchPath);
/**
* \brief Adds a search path that does \b NOT get deleted upon destruction of the \c SearchPaths object.