OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
FileIO.hpp
Go to the documentation of this file.
1
34
35#ifndef OPENSWMM_ENGINE_CORE_FILEIO_HPP
36#define OPENSWMM_ENGINE_CORE_FILEIO_HPP
37
38#include <concepts>
39#include <cstdio>
40#include <filesystem>
41#include <string>
42#include <type_traits>
43
44namespace openswmm::io {
45
55inline std::filesystem::path utf8_path(const std::string& utf8) {
56#ifdef _WIN32
57 return std::filesystem::path(
58 std::u8string(reinterpret_cast<const char8_t*>(utf8.data()), utf8.size()));
59#else
60 return std::filesystem::path(utf8);
61#endif
62}
63
85template <class P>
86 requires std::same_as<std::remove_cvref_t<P>, std::filesystem::path>
87std::filesystem::path utf8_path(P&&) = delete;
88
99inline std::string path_utf8(const std::filesystem::path& p, bool generic = false) {
100#ifdef _WIN32
101 const std::u8string s = generic ? p.generic_u8string() : p.u8string();
102 return std::string(reinterpret_cast<const char*>(s.data()), s.size());
103#else
104 // Mirror of utf8_path's POSIX branch: bytes through unchanged, never via
105 // u8string, because a POSIX path need not be valid UTF-8.
106 return generic ? p.generic_string() : p.string();
107#endif
108}
109
119template <class S>
120 requires(!std::same_as<std::remove_cvref_t<S>, std::filesystem::path>)
121std::string path_utf8(S&&, bool = false) = delete;
122
135inline std::FILE* fopen_path(const std::filesystem::path& p, const char* mode) {
136#ifdef _WIN32
137 // _wfopen is the only fopen that takes a wide path. The mode is ASCII by
138 // construction, so widening it character-by-character is exact.
139 const std::wstring wmode(mode, mode + std::char_traits<char>::length(mode));
140 return ::_wfopen(p.c_str(), wmode.c_str());
141#else
142 return std::fopen(p.c_str(), mode);
143#endif
144}
145
155inline std::FILE* fopen_utf8(const std::string& utf8, const char* mode) {
156 return fopen_path(utf8_path(utf8), mode);
157}
158
159} // namespace openswmm::io
160
161#endif // OPENSWMM_ENGINE_CORE_FILEIO_HPP
Definition AtomicOutputFile.hpp:25
std::filesystem::path utf8_path(const std::string &utf8)
Convert borrowed path bytes into an owned native filesystem path.
Definition FileIO.hpp:55
std::string path_utf8(const std::filesystem::path &p, bool generic=false)
Copy a native path into the engine byte-string representation.
Definition FileIO.hpp:99
std::FILE * fopen_utf8(const std::string &utf8, const char *mode)
Convert engine path bytes and open a C stdio stream.
Definition FileIO.hpp:155
std::FILE * fopen_path(const std::filesystem::path &p, const char *mode)
Open a borrowed native path using platform C stdio.
Definition FileIO.hpp:135