17#include <spdlog/fmt/bundled/core.h>
19#include <boost/algorithm/string/predicate.hpp>
20#include <boost/endian/conversion.hpp>
21#include <boost/interprocess/file_mapping.hpp>
22#include <boost/interprocess/mapped_region.hpp>
26#include <unordered_map>
44 return project_directory_is_set;
52 return std::filesystem::exists(std::filesystem::path(strFilename));
55std::tuple<std::string, std::string::size_type, std::string::size_type>
58 char const close_char,
59 std::string::size_type pos)
61 auto const pos_curly_brace_open = in.find_first_of(open_char, pos);
62 if (pos_curly_brace_open == std::string::npos)
64 return std::make_tuple(
"", std::string::npos, std::string::npos);
66 auto const pos_curly_brace_close =
67 in.find_first_of(close_char, pos_curly_brace_open);
68 if (pos_curly_brace_close == std::string::npos)
70 return std::make_tuple(
"", std::string::npos, std::string::npos);
72 return std::make_tuple(
73 in.substr(pos_curly_brace_open + 1,
74 pos_curly_brace_close - (pos_curly_brace_open + 1)),
75 pos_curly_brace_open, pos_curly_brace_close);
80 auto const position = str.find(keyword);
81 if (position != std::string::npos)
83 return str.substr(0, position);
90 std::string
const& parenthesized_string,
91 std::string::size_type
const begin,
92 std::string::size_type
const end,
93 std::string
const& keyword, T& data)
95 std::string precision_specification =
98 if (precision_specification.empty())
103 std::unordered_map<std::type_index, char> type_specification;
104 type_specification[std::type_index(
typeid(
int))] =
'd';
105 type_specification[std::type_index(
typeid(
double))] =
'f';
106 type_specification[std::type_index(
typeid(std::string))] =
's';
108 auto const& b = precision_specification.back();
110 if (b ==
'e' || b ==
'E' || b ==
'f' || b ==
'F' || b ==
'g' || b ==
'G')
112 type_specification[std::type_index(
typeid(
double))] = b;
113 precision_specification.pop_back();
116 std::string
const generated_fmt_string =
117 "{" + precision_specification +
118 type_specification[std::type_index(
typeid(data))] +
"}";
120 begin, end - begin + 1,
121 fmt::vformat(generated_fmt_string, fmt::make_format_args(data)));
127 std::string
const& mesh_name,
132 char const open_char =
'{';
133 char const close_char =
'}';
134 std::string::size_type begin = 0;
135 std::string::size_type end = std::string::npos;
136 std::string result = format_specification;
138 while (begin != std::string::npos)
140 auto length_before_substitution = result.length();
142 std::string str =
"";
143 std::tie(str, begin, end) =
151 begin = end - (length_before_substitution - result.length());
162 char c[
sizeof(double)];
166 for (
unsigned short i = 0; i <
sizeof(double) / 2; i++)
168 b.c[i] = a.c[
sizeof(double) / 2 - i - 1];
171 for (
unsigned short i =
sizeof(
double) / 2; i <
sizeof(double); i++)
173 b.c[i] = a.c[
sizeof(double) +
sizeof(
double) / 2 - i - 1];
181 auto const filename_path = std::filesystem::path(filename);
182 return (filename_path.parent_path() / filename_path.stem()).string();
187 return std::filesystem::path(pathname).filename().string();
198 return std::filesystem::path(path).extension().string();
208 return std::filesystem::path(pathname).parent_path().string();
211std::string
joinPaths(std::string
const& pathA, std::string
const& pathB)
213 return (std::filesystem::path(pathA) /= std::filesystem::path(pathB))
219 if (!project_directory_is_set)
221 OGS_FATAL(
"The project directory has not yet been set.");
223 return project_directory;
228 if (project_directory_is_set)
230 OGS_FATAL(
"The project directory has already been set.");
234 project_directory = dir;
235 project_directory_is_set =
true;
240 project_directory.clear();
241 project_directory_is_set =
false;
247 std::filesystem::remove(std::filesystem::path(filename));
250 DBUG(
"Removed '{:s}'", filename);
256 for (
auto const& file : files)
269 std::error_code mkdir_err;
270 if (std::filesystem::create_directories(dir, mkdir_err))
272 INFO(
"Output directory {:s} created.", dir);
274 else if (mkdir_err.value() != 0)
276 WARN(
"Could not create output directory {:s}. Error code {:d}, {:s}",
277 dir, mkdir_err.value(), mkdir_err.message());
288 if (file_extension !=
".bin")
291 "Currently only binary files with extension '.bin' supported. The "
292 "specified file has extension {:s}.",
302 in.read(
reinterpret_cast<char*
>(&v),
sizeof(T));
312 std::size_t
const start_element,
313 std::size_t
const num_elements)
317 OGS_FATAL(
"File {:s} not found", filename);
321 std::uintmax_t file_size = std::filesystem::file_size(filename);
322 std::size_t total_elements = file_size /
sizeof(T);
324 if (start_element >= total_elements)
326 OGS_FATAL(
"Start element is beyond file size");
330 std::size_t
const elements_to_read =
331 std::min(num_elements, total_elements - start_element);
334 std::size_t
const offset = start_element *
sizeof(T);
335 std::size_t
const size_to_map = elements_to_read *
sizeof(T);
338 boost::interprocess::file_mapping file(filename.c_str(),
339 boost::interprocess::read_only);
342 boost::interprocess::mapped_region region(
343 file, boost::interprocess::read_only, offset, size_to_map);
346 auto* addr = region.get_address();
349 std::vector<T> result(elements_to_read);
350 std::memcpy(result.data(), addr, size_to_map);
352 if constexpr (std::endian::native != std::endian::little)
354 boost::endian::endian_reverse_inplace(result);
371 out.write(
reinterpret_cast<const char*
>(&val),
sizeof(T));
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
std::string containsKeyword(std::string const &str, std::string const &keyword)
std::string constructFormattedFileName(std::string const &format_specification, std::string const &mesh_name, int const timestep, double const t, int const iteration)
void removeFile(std::string const &filename)
std::vector< T > readBinaryVector(std::string const &filename, std::size_t const start_element, std::size_t const num_elements)
std::string const & getProjectDirectory()
Returns the directory where the prj file resides.
std::string getFileExtension(const std::string &path)
void writeValueBinary(std::ostream &out, T const &val)
write value as binary into the given output stream
std::string extractPath(std::string const &pathname)
std::vector< double > readDoublesFromBinaryFile(const std::string &filename)
std::tuple< std::string, std::string::size_type, std::string::size_type > getParenthesizedString(std::string const &in, char const open_char, char const close_char, std::string::size_type pos)
T readBinaryValue(std::istream &in)
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
template void writeValueBinary< std::size_t >(std::ostream &, std::size_t const &)
template std::vector< double > readBinaryVector< double >(std::string const &, std::size_t const, std::size_t const)
template float readBinaryValue< float >(std::istream &)
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::string dropFileExtension(std::string const &filename)
bool isProjectDirectorySet()
Returns true if the project directory is set.
std::string joinPaths(std::string const &pathA, std::string const &pathB)
void unsetProjectDirectory()
Unsets the project directory.
template double readBinaryValue< double >(std::istream &)
std::string extractBaseName(std::string const &pathname)
double swapEndianness(double const &v)
bool createOutputDirectory(std::string const &dir)
bool substituteKeyword(std::string &result, std::string const &parenthesized_string, std::string::size_type const begin, std::string::size_type const end, std::string const &keyword, T &data)
void setProjectDirectory(std::string const &dir)
Sets the project directory.
bool hasFileExtension(std::string const &extension, std::string const &filename)
void removeFiles(std::vector< std::string > const &files)
template std::vector< float > readBinaryVector< float >(std::string const &, std::size_t const, std::size_t const)