Fix the hack for File::c_str to work (#1242)

This commit is contained in:
Rangi
2023-11-14 07:38:18 -05:00
committed by GitHub
parent cf62ff772f
commit 232416b30d
4 changed files with 33 additions and 28 deletions

View File

@@ -12,6 +12,7 @@
#include <ios>
#include <iostream>
#include <streambuf>
#include <string>
#include <string.h>
#include <string_view>
#include <variant>
@@ -85,17 +86,11 @@ public:
: nullptr;
}
char const *c_str(std::filesystem::path const &path) const {
// FIXME: This is a hack to prevent the path string from being destroyed until
// `.c_str(path)` is called again. It's necessary because just `return path.c_str()`
// fails on Windows, where paths use `wchar_t`.
static std::string path_string;
return std::visit(Visitor{[&path](std::filebuf const &) {
path_string = path.string();
return path_string.c_str();
},
std::string string(std::filesystem::path const &path) const {
return std::visit(Visitor{[&path](std::filebuf const &) { return path.string(); },
[](std::streambuf const *buf) {
return buf == std::cin.rdbuf() ? "<stdin>" : "<stdout>";
return std::string{buf == std::cin.rdbuf()
? "<stdin>" : "<stdout>"};
}},
_file);
}