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::size_type begin, std::string::size_type end,
88 std::string
const& keyword, T& data)
90 std::string precision_specification =
93 if (precision_specification.empty())
98 std::unordered_map<std::type_index, char> type_specification;
99 type_specification[std::type_index(
typeid(
int))] =
'd';
100 type_specification[std::type_index(
typeid(
double))] =
'f';
101 type_specification[std::type_index(
typeid(std::string))] =
's';
103 auto const& b = precision_specification.back();
105 if (b ==
'e' || b ==
'E' || b ==
'f' || b ==
'F' || b ==
'g' || b ==
'G')
107 type_specification[std::type_index(
typeid(
double))] = b;
108 precision_specification.pop_back();
111 std::string
const generated_fmt_string =
112 "{" + precision_specification +
113 type_specification[std::type_index(
typeid(data))] +
"}";
115 begin, end - begin + 1,
116 fmt::vformat(generated_fmt_string, fmt::make_format_args(data)));
122 std::string
const& mesh_name,
127 char const open_char =
'{';
128 char const close_char =
'}';
129 std::string::size_type begin = 0;
130 std::string::size_type end = std::string::npos;
131 std::string result = format_specification;
133 while (begin != std::string::npos)
135 auto length_before_substitution = result.length();
137 std::string str =
"";
138 std::tie(str, begin, end) =
146 begin = end - (length_before_substitution - result.length());
157 char c[
sizeof(double)];
161 for (
unsigned short i = 0; i <
sizeof(double) / 2; i++)
163 b.c[i] = a.c[
sizeof(double) / 2 - i - 1];
166 for (
unsigned short i =
sizeof(
double) / 2; i <
sizeof(double); i++)
168 b.c[i] = a.c[
sizeof(double) +
sizeof(
double) / 2 - i - 1];
176 auto const filename_path = std::filesystem::path(filename);
177 return (filename_path.parent_path() / filename_path.stem()).string();
182 return std::filesystem::path(pathname).filename().string();
193 return std::filesystem::path(path).extension().string();
202 const std::string& source)
204 auto filePath = std::filesystem::path(file_name);
205 if (filePath.has_parent_path())
207 return filePath.string();
209 return (std::filesystem::path(source) /= filePath).string();
214 return std::filesystem::path(pathname).parent_path().string();
217std::string
joinPaths(std::string
const& pathA, std::string
const& pathB)
219 return (std::filesystem::path(pathA) /= std::filesystem::path(pathB))
225 if (!project_directory_is_set)
227 OGS_FATAL(
"The project directory has not yet been set.");
229 return project_directory;
234 if (project_directory_is_set)
236 OGS_FATAL(
"The project directory has already been set.");
240 project_directory = dir;
241 project_directory_is_set =
true;
246 project_directory.clear();
247 project_directory_is_set =
false;
253 std::filesystem::remove(std::filesystem::path(filename));
256 DBUG(
"Removed '{:s}'", filename);
262 for (
auto const& file : files)
272 in.read(
reinterpret_cast<char*
>(&v),
sizeof(T));
283 std::ifstream in(filename.c_str());
286 ERR(
"readBinaryArray(): Error while reading from file '{:s}'.",
288 ERR(
"Could not open file '{:s}' for input.", filename);
290 return std::vector<T>();
293 std::vector<T> result;
296 for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
298 result.push_back(BaseLib::readBinaryValue<T>(in));
301 if (result.size() == n)
306 ERR(
"readBinaryArray(): Error while reading from file '{:s}'.", filename);
307 ERR(
"Read different number of values. Expected {:d}, got {:d}.",
313 ERR(
"EOF reached.\n");
316 return std::vector<T>();
326 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)
bool substituteKeyword(std::string &result, std::string &parenthesized_string, std::string::size_type begin, std::string::size_type end, std::string const &keyword, T &data)
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.
std::string copyPathToFileName(const std::string &file_name, const std::string &source)
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)
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)