Virtual.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "Virtual.h"
  2. #include "WallpaperEngine/Assets/AssetLoadException.h"
  3. #include <cstring>
  4. using namespace WallpaperEngine::FileSystem;
  5. using namespace WallpaperEngine::FileSystem::Adapters;
  6. ReadStreamSharedPtr VirtualAdapter::open (const std::filesystem::path& path) const {
  7. const auto file = this->files.find (path);
  8. if (file == this->files.end ()) {
  9. throw std::filesystem::filesystem_error ("Cannot find file", path, std::error_code ());
  10. }
  11. return file->second;
  12. }
  13. bool VirtualAdapter::exists (const std::filesystem::path& path) const { return this->files.contains (path); }
  14. std::filesystem::path VirtualAdapter::physicalPath (const std::filesystem::path& path) const {
  15. throw std::filesystem::filesystem_error ("Virtual adapter does not support realpath", path, std::error_code ());
  16. }
  17. void VirtualAdapter::add (const std::filesystem::path& path, const char* data) {
  18. size_t length = strlen (data);
  19. auto buffer = std::make_unique<char[]> (length);
  20. std::memcpy (buffer.get (), data, length);
  21. this->add (path, std::make_shared<MemoryStream> (std::move (buffer), length));
  22. }
  23. void VirtualAdapter::add (const std::filesystem::path& path, const JSON& data) { this->add (path, data.dump ()); }
  24. void VirtualAdapter::add (const std::filesystem::path& path, const std::string& data) {
  25. auto buffer = std::make_unique<char[]> (data.size ());
  26. std::memcpy (buffer.get (), data.data (), data.size ());
  27. this->add (path, std::make_shared<MemoryStream> (std::move (buffer), data.size ()));
  28. }
  29. void VirtualAdapter::add (const std::filesystem::path& path, MemoryStreamSharedPtr stream) {
  30. this->files.insert_or_assign (path, stream);
  31. }
  32. bool VirtualFactory::handlesMountpoint (const std::filesystem::path& path) const { return path.string () == "virtual"; }
  33. AdapterSharedPtr VirtualFactory::create (const std::filesystem::path& path) const {
  34. return std::make_unique<VirtualAdapter> ();
  35. }