17#include <spdlog/fmt/bundled/core.h>
19#include <boost/algorithm/string/predicate.hpp>
23#include <unordered_map>
41 return project_directory_is_set;
49 return std::filesystem::exists(std::filesystem::path(strFilename));
52std::tuple<std::string, std::string::size_type, std::string::size_type>
55 char const close_char,
56 std::string::size_type pos)
58 auto const pos_curly_brace_open = in.find_first_of(open_char, pos);
59 if (pos_curly_brace_open == std::string::npos)
61 return std::make_tuple(
"", std::string::npos, std::string::npos);
63 auto const pos_curly_brace_close =
64 in.find_first_of(close_char, pos_curly_brace_open);
65 if (pos_curly_brace_close == std::string::npos)
67 return std::make_tuple(
"", std::string::npos, std::string::npos);
69 return std::make_tuple(
70 in.substr(pos_curly_brace_open + 1,
71 pos_curly_brace_close - (pos_curly_brace_open + 1)),
72 pos_curly_brace_open, pos_curly_brace_close);
77 auto const position = str.find(keyword);
78 if (position != std::string::npos)
80 return str.substr(0, position);
87 std::string
const& parenthesized_string,
88 std::string::size_type
const begin,
89 std::string::size_type
const end,
90 std::string
const& keyword, T& data)
92 std::string precision_specification =
95 if (precision_specification.empty())
100 std::unordered_map<std::type_index, char> type_specification;
101 type_specification[std::type_index(
typeid(
int))] =
'd';
102 type_specification[std::type_index(
typeid(
double))] =
'f';
103 type_specification[std::type_index(
typeid(std::string))] =
's';
105 auto const& b = precision_specification.back();
107 if (b ==
'e' || b ==
'E' || b ==
'f' || b ==
'F' || b ==
'g' || b ==
'G')
109 type_specification[std::type_index(
typeid(
double))] = b;
110 precision_specification.pop_back();
113 std::string
const generated_fmt_string =
114 "{" + precision_specification +
115 type_specification[std::type_index(
typeid(data))] +
"}";
117 begin, end - begin + 1,
118 fmt::vformat(generated_fmt_string, fmt::make_format_args(data)));
124 std::string
const& mesh_name,
129 char const open_char =
'{';
130 char const close_char =
'}';
131 std::string::size_type begin = 0;
132 std::string::size_type end = std::string::npos;
133 std::string result = format_specification;
135 while (begin != std::string::npos)
137 auto length_before_substitution = result.length();
139 std::string str =
"";
140 std::tie(str, begin, end) =
148 begin = end - (length_before_substitution - result.length());
159 char c[
sizeof(double)];
163 for (
unsigned short i = 0; i <
sizeof(double) / 2; i++)
165 b.c[i] = a.c[
sizeof(double) / 2 - i - 1];
168 for (
unsigned short i =
sizeof(
double) / 2; i <
sizeof(double); i++)
170 b.c[i] = a.c[
sizeof(double) +
sizeof(
double) / 2 - i - 1];
178 auto const filename_path = std::filesystem::path(filename);
179 return (filename_path.parent_path() / filename_path.stem()).string();
184 return std::filesystem::path(pathname).filename().string();
195 return std::filesystem::path(path).extension().string();
205 return std::filesystem::path(pathname).parent_path().string();
208std::string
joinPaths(std::string
const& pathA, std::string
const& pathB)
210 return (std::filesystem::path(pathA) /= std::filesystem::path(pathB))
216 if (!project_directory_is_set)
218 OGS_FATAL(
"The project directory has not yet been set.");
220 return project_directory;
225 if (project_directory_is_set)
227 OGS_FATAL(
"The project directory has already been set.");
231 project_directory = dir;
232 project_directory_is_set =
true;
237 project_directory.clear();
238 project_directory_is_set =
false;
244 std::filesystem::remove(std::filesystem::path(filename));
247 DBUG(
"Removed '{:s}'", filename);
253 for (
auto const& file : files)
263 in.read(
reinterpret_cast<char*
>(&v),
sizeof(T));
274 std::ifstream in(filename.c_str());
277 ERR(
"readBinaryArray(): Error while reading from file '{:s}'.",
279 ERR(
"Could not open file '{:s}' for input.", filename);
281 return std::vector<T>();
284 std::vector<T> result;
287 for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
289 result.push_back(BaseLib::readBinaryValue<T>(in));
292 if (result.size() == n)
297 ERR(
"readBinaryArray(): Error while reading from file '{:s}'.", filename);
298 ERR(
"Read different number of values. Expected {:d}, got {:d}.",
304 ERR(
"EOF reached.\n");
307 return std::vector<T>();
317 out.write(
reinterpret_cast<const char*
>(&val),
sizeof(T));
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
std::vector< T > readBinaryArray(std::string const &filename, std::size_t const n)
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::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::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 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 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.
template std::vector< float > readBinaryArray< float >(std::string const &, std::size_t const)
bool hasFileExtension(std::string const &extension, std::string const &filename)
void removeFiles(std::vector< std::string > const &files)