handling saving issues in certain contexts/new shortcuts
This commit is contained in:
@@ -1,19 +1,48 @@
|
||||
#include "working_directory.h"
|
||||
|
||||
#include <format>
|
||||
|
||||
#include "log.h"
|
||||
#include "path_.h"
|
||||
|
||||
namespace anm2ed::util
|
||||
{
|
||||
WorkingDirectory::WorkingDirectory(const std::filesystem::path& path, Type type)
|
||||
{
|
||||
previous = std::filesystem::current_path();
|
||||
std::error_code ec{};
|
||||
previous = std::filesystem::current_path(ec);
|
||||
if (ec)
|
||||
{
|
||||
logger.warning(std::format("Could not query current directory: {}", ec.message()));
|
||||
previous.clear();
|
||||
ec.clear();
|
||||
}
|
||||
|
||||
if (type == FILE)
|
||||
{
|
||||
std::filesystem::path filePath = path;
|
||||
std::filesystem::path parentPath = filePath.parent_path();
|
||||
std::filesystem::current_path(parentPath);
|
||||
std::filesystem::current_path(parentPath, ec);
|
||||
if (ec)
|
||||
logger.warning(
|
||||
std::format("Could not set current directory to {}: {}", path::to_utf8(parentPath), ec.message()));
|
||||
}
|
||||
else
|
||||
std::filesystem::current_path(path);
|
||||
{
|
||||
std::filesystem::current_path(path, ec);
|
||||
if (ec)
|
||||
logger.warning(std::format("Could not set current directory to {}: {}", path::to_utf8(path), ec.message()));
|
||||
}
|
||||
}
|
||||
|
||||
WorkingDirectory::~WorkingDirectory() { std::filesystem::current_path(previous); }
|
||||
}
|
||||
WorkingDirectory::~WorkingDirectory()
|
||||
{
|
||||
if (previous.empty()) return;
|
||||
|
||||
std::error_code ec{};
|
||||
std::filesystem::current_path(previous, ec);
|
||||
if (ec)
|
||||
logger.warning(
|
||||
std::format("Could not restore current directory to {}: {}", path::to_utf8(previous), ec.message()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user