mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-12-16 03:08:52 +08:00
feat: support parse(std::filesystem::path) #113
This commit is contained in:
@@ -12,6 +12,13 @@
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
#if __has_include(<filesystem>)
|
||||
#define TOML11_HAS_STD_FILESYSTEM
|
||||
#include <filesystem>
|
||||
#endif // has_include(<string_view>)
|
||||
#endif // cplusplus >= C++17
|
||||
|
||||
namespace toml
|
||||
{
|
||||
namespace detail
|
||||
@@ -2102,5 +2109,39 @@ basic_value<Comment, Table, Array> parse(const std::string& fname)
|
||||
return parse<Comment, Table, Array>(ifs, fname);
|
||||
}
|
||||
|
||||
#ifdef TOML11_HAS_STD_FILESYSTEM
|
||||
// This function just forwards `parse("filename.toml")` to std::string version
|
||||
// to avoid the ambiguity in overload resolution.
|
||||
//
|
||||
// Both std::string and std::filesystem::path are convertible from const char[].
|
||||
// Without this, both parse(std::string) and parse(std::filesystem::path)
|
||||
// matches to parse("filename.toml"). This breaks the existing code.
|
||||
//
|
||||
// This function exactly matches to the invokation with string literal.
|
||||
// So this function is preferred than others and the ambiguity disappears.
|
||||
template<typename Comment = ::toml::discard_comments,
|
||||
template<typename ...> class Table = std::unordered_map,
|
||||
template<typename ...> class Array = std::vector,
|
||||
std::size_t N>
|
||||
basic_value<Comment, Table, Array> parse(const char (&fname)[N])
|
||||
{
|
||||
return parse<Comment, Table, Array>(std::string(fname));
|
||||
}
|
||||
|
||||
template<typename Comment = ::toml::discard_comments,
|
||||
template<typename ...> class Table = std::unordered_map,
|
||||
template<typename ...> class Array = std::vector>
|
||||
basic_value<Comment, Table, Array> parse(const std::filesystem::path& fpath)
|
||||
{
|
||||
std::ifstream ifs(fpath, std::ios_base::binary);
|
||||
if(!ifs.good())
|
||||
{
|
||||
throw std::runtime_error("toml::parse: file open error -> " +
|
||||
fpath.string());
|
||||
}
|
||||
return parse<Comment, Table, Array>(ifs, fpath.string());
|
||||
}
|
||||
#endif // TOML11_HAS_STD_FILESYSTEM
|
||||
|
||||
} // toml
|
||||
#endif// TOML11_PARSER_HPP
|
||||
|
||||
Reference in New Issue
Block a user