mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
Include filesize when opening a file with searchpaths
This commit is contained in:
17
src/ObjLoading/SearchPath/ISearchPath.cpp
Normal file
17
src/ObjLoading/SearchPath/ISearchPath.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include "ISearchPath.h"
|
||||
|
||||
bool SearchPathOpenFile::IsOpen() const
|
||||
{
|
||||
return m_stream != nullptr;
|
||||
}
|
||||
|
||||
SearchPathOpenFile::SearchPathOpenFile()
|
||||
: m_length(0)
|
||||
{
|
||||
}
|
||||
|
||||
SearchPathOpenFile::SearchPathOpenFile(std::unique_ptr<std::istream> stream, const int64_t length)
|
||||
: m_stream(std::move(stream)),
|
||||
m_length(length)
|
||||
{
|
||||
}
|
@ -3,21 +3,40 @@
|
||||
#include <functional>
|
||||
#include <istream>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
#include "Utils/ObjStream.h"
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "SearchPathSearchOptions.h"
|
||||
|
||||
class SearchPathOpenFile
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<std::istream> m_stream;
|
||||
int64_t m_length;
|
||||
|
||||
_NODISCARD bool IsOpen() const;
|
||||
|
||||
SearchPathOpenFile();
|
||||
SearchPathOpenFile(std::unique_ptr<std::istream> stream, int64_t length);
|
||||
};
|
||||
|
||||
class ISearchPath
|
||||
{
|
||||
public:
|
||||
ISearchPath() = default;
|
||||
virtual ~ISearchPath() = default;
|
||||
|
||||
ISearchPath(const ISearchPath& other) = default;
|
||||
ISearchPath(ISearchPath&& other) noexcept = default;
|
||||
ISearchPath& operator=(const ISearchPath& other) = default;
|
||||
ISearchPath& operator=(ISearchPath&& other) noexcept = default;
|
||||
|
||||
/**
|
||||
* \brief Opens a file relative to the search path.
|
||||
* \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 std::unique_ptr<std::istream> Open(const std::string& fileName) = 0;
|
||||
virtual SearchPathOpenFile Open(const std::string& fileName) = 0;
|
||||
|
||||
/**
|
||||
* \brief Returns the path to the search path.
|
||||
|
@ -17,16 +17,17 @@ std::string SearchPathFilesystem::GetPath()
|
||||
return m_path;
|
||||
}
|
||||
|
||||
std::unique_ptr<std::istream> SearchPathFilesystem::Open(const std::string& fileName)
|
||||
SearchPathOpenFile SearchPathFilesystem::Open(const std::string& fileName)
|
||||
{
|
||||
auto file = std::make_unique<std::ifstream>(fs::path(m_path).append(fileName).string(), std::fstream::in | std::fstream::binary);
|
||||
const auto filePath = fs::path(m_path).append(fileName);
|
||||
std::ifstream file(filePath.string(), std::fstream::in | std::fstream::binary);
|
||||
|
||||
if (file->is_open())
|
||||
if (file.is_open())
|
||||
{
|
||||
return std::move(file);
|
||||
return SearchPathOpenFile(std::make_unique<std::ifstream>(std::move(file)), static_cast<int64_t>(file_size(filePath)));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return SearchPathOpenFile();
|
||||
}
|
||||
|
||||
void SearchPathFilesystem::Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback)
|
||||
|
@ -11,7 +11,7 @@ class SearchPathFilesystem final : public ISearchPath
|
||||
public:
|
||||
explicit SearchPathFilesystem(std::string path);
|
||||
|
||||
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
|
||||
SearchPathOpenFile Open(const std::string& fileName) override;
|
||||
std::string GetPath() override;
|
||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||
};
|
@ -2,19 +2,19 @@
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
std::unique_ptr<std::istream> SearchPaths::Open(const std::string& fileName)
|
||||
SearchPathOpenFile SearchPaths::Open(const std::string& fileName)
|
||||
{
|
||||
for(auto* searchPathEntry : m_search_paths)
|
||||
{
|
||||
auto file = searchPathEntry->Open(fileName);
|
||||
|
||||
if(file)
|
||||
if(file.IsOpen())
|
||||
{
|
||||
return std::move(file);
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return SearchPathOpenFile();
|
||||
}
|
||||
|
||||
std::string SearchPaths::GetPath()
|
||||
|
@ -15,7 +15,7 @@ public:
|
||||
SearchPaths() = default;
|
||||
~SearchPaths() override = default;
|
||||
|
||||
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
|
||||
SearchPathOpenFile Open(const std::string& fileName) override;
|
||||
std::string GetPath() override;
|
||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||
|
||||
|
Reference in New Issue
Block a user