mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
LibCore: Add helper to load a possibly-relative path into a Resource
This commit is contained in:
parent
81366b860b
commit
a4c7d9a374
2 changed files with 13 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <LibCore/Resource.h>
|
#include <LibCore/Resource.h>
|
||||||
#include <LibCore/ResourceImplementation.h>
|
#include <LibCore/ResourceImplementation.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
@ -31,6 +32,17 @@ Resource::Resource(String path, Scheme scheme, DirectoryTag)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_filesystem(StringView path)
|
||||||
|
{
|
||||||
|
auto filepath = LexicalPath(path);
|
||||||
|
|
||||||
|
if (filepath.is_absolute())
|
||||||
|
return load_from_uri(TRY(String::formatted("file://{}", path)));
|
||||||
|
|
||||||
|
auto cwd = TRY(Core::System::getcwd());
|
||||||
|
return load_from_uri(TRY(String::formatted("file://{}", filepath.prepend(cwd).string())));
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
|
ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
|
||||||
{
|
{
|
||||||
return ResourceImplementation::the().load_from_uri(uri);
|
return ResourceImplementation::the().load_from_uri(uri);
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
namespace Core {
|
namespace Core {
|
||||||
class Resource : public RefCounted<Resource> {
|
class Resource : public RefCounted<Resource> {
|
||||||
public:
|
public:
|
||||||
|
static ErrorOr<NonnullRefPtr<Resource>> load_from_filesystem(StringView);
|
||||||
static ErrorOr<NonnullRefPtr<Resource>> load_from_uri(StringView);
|
static ErrorOr<NonnullRefPtr<Resource>> load_from_uri(StringView);
|
||||||
|
|
||||||
[[nodiscard]] bool is_file() const { return !m_data.has<DirectoryTag>(); }
|
[[nodiscard]] bool is_file() const { return !m_data.has<DirectoryTag>(); }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue