From a4c7d9a37444e15f8b19d1775af07f5c62cf7431 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 18 Oct 2023 14:08:44 -0600 Subject: [PATCH] LibCore: Add helper to load a possibly-relative path into a Resource --- Userland/Libraries/LibCore/Resource.cpp | 12 ++++++++++++ Userland/Libraries/LibCore/Resource.h | 1 + 2 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibCore/Resource.cpp b/Userland/Libraries/LibCore/Resource.cpp index c86bcf3c9a..b1967d1757 100644 --- a/Userland/Libraries/LibCore/Resource.cpp +++ b/Userland/Libraries/LibCore/Resource.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace Core { @@ -31,6 +32,17 @@ Resource::Resource(String path, Scheme scheme, DirectoryTag) { } +ErrorOr> 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> Resource::load_from_uri(StringView uri) { return ResourceImplementation::the().load_from_uri(uri); diff --git a/Userland/Libraries/LibCore/Resource.h b/Userland/Libraries/LibCore/Resource.h index a9b434b0a8..69c12a3bc9 100644 --- a/Userland/Libraries/LibCore/Resource.h +++ b/Userland/Libraries/LibCore/Resource.h @@ -20,6 +20,7 @@ namespace Core { class Resource : public RefCounted { public: + static ErrorOr> load_from_filesystem(StringView); static ErrorOr> load_from_uri(StringView); [[nodiscard]] bool is_file() const { return !m_data.has(); }