17#include <boost/algorithm/string/predicate.hpp>
18#include <boost/endian/conversion.hpp>
19#include <boost/interprocess/file_mapping.hpp>
20#include <boost/interprocess/mapped_region.hpp>
24#include <unordered_map>
42 return project_directory_is_set;
50 return std::filesystem::exists(std::filesystem::path(strFilename));
53std::tuple<std::string, std::string::size_type, std::string::size_type>
56 char const close_char,
57 std::string::size_type pos)
59 auto const pos_curly_brace_open = in.find_first_of(open_char, pos);
60 if (pos_curly_brace_open == std::string::npos)
62 return std::make_tuple(
"", std::string::npos, std::string::npos);
64 auto const pos_curly_brace_close =
65 in.find_first_of(close_char, pos_curly_brace_open);
66 if (pos_curly_brace_close == std::string::npos)
68 return std::make_tuple(
"", std::string::npos, std::string::npos);
70 return std::make_tuple(
71 in.substr(pos_curly_brace_open + 1,
72 pos_curly_brace_close - (pos_curly_brace_open + 1)),
73 pos_curly_brace_open, pos_curly_brace_close);
78 auto const position = str.find(keyword);
79 if (position != std::string::npos)
81 return str.substr(0, position);
88 std::string
const& parenthesized_string,
89 std::string::size_type
const begin,
90 std::string::size_type
const end,
91 std::string
const& keyword, T& data)
93 std::string precision_specification =
96 if (precision_specification.empty())
101 if constexpr (std::is_same_v<std::remove_cvref_t<T>,
bool>)
103 if (keyword ==
"converged")
105 std::string r = data ?
"" :
"_not_converged";
106 result.replace(begin, end - begin + 1, r);
111 std::unordered_map<std::type_index, char> type_specification;
112 type_specification[std::type_index(
typeid(
int))] =
'd';
113 type_specification[std::type_index(
typeid(
double))] =
'f';
114 type_specification[std::type_index(
typeid(std::string))] =
's';
116 auto const& b = precision_specification.back();
118 if (b ==
'e' || b ==
'E' || b ==
'f' || b ==
'F' || b ==
'g' || b ==
'G')
120 type_specification[std::type_index(
typeid(
double))] = b;
121 precision_specification.pop_back();
124 std::string
const generated_fmt_string =
125 "{" + precision_specification +
126 type_specification[std::type_index(
typeid(data))] +
"}";
128 begin, end - begin + 1,
129 fmt::vformat(generated_fmt_string, fmt::make_format_args(data)));
135 std::string
const& mesh_name,
139 bool const converged)
141 char const open_char =
'{';
142 char const close_char =
'}';
143 std::string::size_type begin = 0;
144 std::string::size_type end = std::string::npos;
145 std::string result = format_specification;
147 while (begin != std::string::npos)
149 auto length_before_substitution = result.length();
151 std::string str =
"";
152 std::tie(str, begin, end) =
162 begin = end - (length_before_substitution - result.length());
173 char c[
sizeof(double)];
177 for (
unsigned short i = 0; i <
sizeof(double) / 2; i++)
179 b.c[i] = a.c[
sizeof(double) / 2 - i - 1];
182 for (
unsigned short i =
sizeof(
double) / 2; i <
sizeof(double); i++)
184 b.c[i] = a.c[
sizeof(double) +
sizeof(
double) / 2 - i - 1];
192 auto const filename_path = std::filesystem::path(filename);
193 return (filename_path.parent_path() / filename_path.stem()).string();
198 return std::filesystem::path(pathname).filename().string();
209 return std::filesystem::path(path).extension().string();
219 return std::filesystem::path(pathname).parent_path().string();
222std::string
joinPaths(std::string
const& pathA, std::string
const& pathB)
224 return (std::filesystem::path(pathA) /= std::filesystem::path(pathB))
222std::string
joinPaths(std::string
const& pathA, std::string
const& pathB) {
…}
230 if (!project_directory_is_set)
232 OGS_FATAL(
"The project directory has not yet been set.");
234 return project_directory;
239 if (project_directory_is_set)
241 OGS_FATAL(
"The project directory has already been set.");
245 project_directory = dir;
246 project_directory_is_set =
true;
251 project_directory.clear();
252 project_directory_is_set =
false;
258 std::filesystem::remove(std::filesystem::path(filename));
261 DBUG(
"Removed '{:s}'", filename);
267 for (
auto const& file : files)
280 std::error_code mkdir_err;
281 if (std::filesystem::create_directories(dir, mkdir_err))
283 INFO(
"Output directory {:s} created.", dir);
285 else if (mkdir_err.value() != 0)
287 WARN(
"Could not create output directory {:s}. Error code {:d}, {:s}",
288 dir, mkdir_err.value(), mkdir_err.message());
299 if (file_extension !=
".bin")
302 "Currently only binary files with extension '.bin' supported. The "
303 "specified file has extension {:s}.",
313 in.read(
reinterpret_cast<char*
>(&v),
sizeof(T));
323 std::size_t
const start_element,
324 std::size_t
const num_elements)
328 OGS_FATAL(
"File {:s} not found", filename);
332 std::uintmax_t file_size = std::filesystem::file_size(filename);
333 std::size_t total_elements = file_size /
sizeof(T);
335 if (start_element >= total_elements)
337 OGS_FATAL(
"Start element is beyond file size");
341 std::size_t
const elements_to_read =
342 std::min(num_elements, total_elements - start_element);
345 std::size_t
const offset = start_element *
sizeof(T);
346 std::size_t
const size_to_map = elements_to_read *
sizeof(T);
349 boost::interprocess::file_mapping file(filename.c_str(),
350 boost::interprocess::read_only);
353 boost::interprocess::mapped_region region(
354 file, boost::interprocess::read_only, offset, size_to_map);
357 auto* addr = region.get_address();
360 std::vector<T> result(elements_to_read);
361 std::memcpy(result.data(), addr, size_to_map);
363 if constexpr (std::endian::native != std::endian::little)
365 boost::endian::endian_reverse_inplace(result);
382 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)
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 constructFormattedFileName(std::string const &format_specification, std::string const &mesh_name, int const timestep, double const t, int const iteration, bool const converged)
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)