109 lines
2.0 KiB
C++
109 lines
2.0 KiB
C++
// TJP COPYRIGHT HEADER
|
|
|
|
#pragma once
|
|
|
|
#include "File.h"
|
|
#include "Path.h"
|
|
|
|
namespace tjp::core::file {
|
|
|
|
struct VirtualFileSystem
|
|
{
|
|
virtual ~VirtualFileSystem () {};
|
|
|
|
virtual String pathFor(const StringView &path) const = 0;
|
|
virtual Optional<Contents> get_if(const StringView &path) const = 0;
|
|
virtual Contents get(const StringView &path) const = 0;
|
|
virtual void put(const StringView &path, ContentsView) = 0;
|
|
virtual bool has(const StringView &path) const = 0;
|
|
} ;
|
|
|
|
struct VirtualFileSystemRoot : VirtualFileSystem
|
|
{
|
|
String base;
|
|
|
|
VirtualFileSystemRoot(const StringView &base_) :
|
|
base(base_)
|
|
{}
|
|
|
|
inline
|
|
String pathFor(const StringView &path) const override
|
|
{
|
|
return joinPaths({ base, String(path) });
|
|
}
|
|
|
|
inline
|
|
Optional<Contents> get_if(const StringView &path) const override
|
|
{
|
|
return file::read(pathFor(path));
|
|
}
|
|
|
|
inline
|
|
Contents get(const StringView &path) const override
|
|
{
|
|
return file::require_read(pathFor(path));
|
|
}
|
|
|
|
inline
|
|
void put(const StringView &path, ContentsView contents) override
|
|
{
|
|
file::ensurePath(base, path);
|
|
file::require_write(pathFor(path), contents);
|
|
}
|
|
|
|
inline
|
|
bool has(const StringView &path) const override
|
|
{
|
|
return file::fileExists(pathFor(path));
|
|
}
|
|
} ;
|
|
|
|
struct VirtualFileSystemRelative : VirtualFileSystem
|
|
{
|
|
VirtualFileSystem *to;
|
|
String base;
|
|
|
|
VirtualFileSystemRelative(VirtualFileSystem *to_, const StringView &base_) :
|
|
to(to_),
|
|
base(base_)
|
|
{}
|
|
|
|
String join(const StringView &path) const
|
|
{
|
|
return joinPaths({ base, String(path) });
|
|
}
|
|
|
|
String pathFor(const StringView &path) const override
|
|
{
|
|
return to->pathFor(join(path));
|
|
}
|
|
|
|
inline
|
|
Optional<Contents> get_if(const StringView &path) const override
|
|
{
|
|
return to->get_if(join(path));
|
|
}
|
|
|
|
inline
|
|
Contents get(const StringView &path) const override
|
|
{
|
|
return to->get(join(path));
|
|
}
|
|
|
|
inline
|
|
void put(const StringView &path, ContentsView contents) override
|
|
{
|
|
to->put(join(path), contents);
|
|
}
|
|
|
|
inline
|
|
bool has(const StringView &path) const override
|
|
{
|
|
return to->has(join(path));
|
|
}
|
|
|
|
} ;
|
|
|
|
} // namespace
|
|
|