Files
anm2ed/src/working_directory.cpp
T
shweet 7afdf709c3 Format like smack-a-brother, and write comments as sentences
Four-space indents, braces on their own lines, and one-statement bodies without braces.
Semicolons in comments became full stops.
Designated initializers may leave fields out without a warning.
2026-11-22 17:30:03 -05:00

47 lines
1.3 KiB
C++

#include "working_directory.h"
#include <format>
#include "log.h"
#include "path.h"
WorkingDirectory::WorkingDirectory(const std::filesystem::path& path, Type type)
{
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, 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, ec);
if (ec)
logger.warning(std::format("Could not set current directory to {}: {}", path_to_utf8(path), ec.message()));
}
}
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()));
}