OGS
FileTools.cpp
Go to the documentation of this file.
1
15#include "FileTools.h"
16
17#include <boost/algorithm/string.hpp>
18#include <filesystem>
19#include <typeindex>
20#include <unordered_map>
21
22#include "Error.h"
23
24namespace
25{
28
31} // anonymous namespace
32
33namespace BaseLib
34{
36{
38}
39
43bool IsFileExisting(const std::string& strFilename)
44{
45 return std::filesystem::exists(std::filesystem::path(strFilename));
46}
47
48std::tuple<std::string, std::string::size_type, std::string::size_type>
49getParenthesizedString(std::string const& in,
50 char const open_char,
51 char const close_char,
52 std::string::size_type pos)
53{
54 auto const pos_curly_brace_open = in.find_first_of(open_char, pos);
55 if (pos_curly_brace_open == std::string::npos)
56 {
57 return std::make_tuple("", std::string::npos, std::string::npos);
58 }
59 auto const pos_curly_brace_close =
60 in.find_first_of(close_char, pos_curly_brace_open);
61 if (pos_curly_brace_close == std::string::npos)
62 {
63 return std::make_tuple("", std::string::npos, std::string::npos);
64 }
65 return std::make_tuple(
66 in.substr(pos_curly_brace_open + 1,
67 pos_curly_brace_close - (pos_curly_brace_open + 1)),
68 pos_curly_brace_open, pos_curly_brace_close);
69}
70
71std::string containsKeyword(std::string const& str, std::string const& keyword)
72{
73 auto const position = str.find(keyword);
74 if (position != std::string::npos)
75 {
76 return str.substr(0, position);
77 }
78 return "";
79}
80
81template <typename T>
82bool substituteKeyword(std::string& result, std::string& parenthesized_string,
83 std::string::size_type begin, std::string::size_type end,
84 std::string const& keyword, T& data)
85{
86 std::string precision_specification =
87 containsKeyword(parenthesized_string, keyword);
88
89 if (precision_specification.empty())
90 {
91 return false;
92 }
93
94 std::unordered_map<std::type_index, char> type_specification;
95 type_specification[std::type_index(typeid(int))] = 'd';
96 type_specification[std::type_index(typeid(double))] = 'f'; // default
97 type_specification[std::type_index(typeid(std::string))] = 's';
98
99 auto const& b = precision_specification.back();
100 // see https://fmt.dev/latest/syntax.html#formatspec
101 if (b == 'e' || b == 'E' || b == 'f' || b == 'F' || b == 'g' || b == 'G')
102 {
103 type_specification[std::type_index(typeid(double))] = b;
104 precision_specification = precision_specification.substr(
105 0, precision_specification.length() - 1);
106 }
107
108 std::string const generated_fmt_string =
109 "{" + precision_specification +
110 type_specification[std::type_index(typeid(data))] + "}";
111 result = result.substr(0, begin) + fmt::format(generated_fmt_string, data) +
112 result.substr(end + 1, result.length() - (end + 1));
113 return true;
114}
115
116std::string constructFormattedFileName(std::string const& format_specification,
117 std::string const& mesh_name,
118 int const timestep,
119 double const t,
120 int const iteration)
121{
122 char const open_char = '{';
123 char const close_char = '}';
124 std::string::size_type begin = 0;
125 std::string::size_type end = std::string::npos;
126 std::string result = format_specification;
127
128 while (begin != std::string::npos)
129 {
130 auto length_before_substitution = result.length();
131 // find next parenthesized string
132 std::string str = "";
133 std::tie(str, begin, end) =
134 getParenthesizedString(result, open_char, close_char, begin);
135 if (!substituteKeyword(result, str, begin, end, "timestep", timestep) &&
136 !substituteKeyword(result, str, begin, end, "time", t) &&
137 !substituteKeyword(result, str, begin, end, "iteration", iteration))
138 {
139 substituteKeyword(result, str, begin, end, "meshname", mesh_name);
140 }
141 begin = end - (length_before_substitution - result.length());
142 }
143
144 return result;
145}
146
147double swapEndianness(double const& v)
148{
149 union
150 {
151 double v;
152 char c[sizeof(double)];
153 } a{}, b{};
154
155 a.v = v;
156 for (unsigned short i = 0; i < sizeof(double) / 2; i++)
157 {
158 b.c[i] = a.c[sizeof(double) / 2 - i - 1];
159 }
160
161 for (unsigned short i = sizeof(double) / 2; i < sizeof(double); i++)
162 {
163 b.c[i] = a.c[sizeof(double) + sizeof(double) / 2 - i - 1];
164 }
165
166 return b.v;
167}
168
169std::string dropFileExtension(std::string const& filename)
170{
171 auto const filename_path = std::filesystem::path(filename);
172 return (filename_path.parent_path() / filename_path.stem()).string();
173}
174
175std::string extractBaseName(std::string const& pathname)
176{
177 return std::filesystem::path(pathname).filename().string();
178}
179
180std::string extractBaseNameWithoutExtension(std::string const& pathname)
181{
182 std::string basename = extractBaseName(pathname);
183 return dropFileExtension(basename);
184}
185
186std::string getFileExtension(const std::string& path)
187{
188 return std::filesystem::path(path).extension().string();
189}
190
191bool hasFileExtension(std::string const& extension, std::string const& filename)
192{
193 return boost::iequals(extension, getFileExtension(filename));
194}
195
196std::string copyPathToFileName(const std::string& file_name,
197 const std::string& source)
198{
199 auto filePath = std::filesystem::path(file_name);
200 if (filePath.has_parent_path())
201 {
202 return filePath.string();
203 }
204 return (std::filesystem::path(source) /= filePath).string();
205}
206
207std::string extractPath(std::string const& pathname)
208{
209 return std::filesystem::path(pathname).parent_path().string();
210}
211
212std::string joinPaths(std::string const& pathA, std::string const& pathB)
213{
214 return (std::filesystem::path(pathA) /= std::filesystem::path(pathB))
215 .string();
216}
217
218std::string const& getProjectDirectory()
219{
221 {
222 OGS_FATAL("The project directory has not yet been set.");
223 }
224 return project_directory;
225}
226
227void setProjectDirectory(std::string const& dir)
228{
230 {
231 OGS_FATAL("The project directory has already been set.");
232 }
233 project_directory = dir;
235}
236
237void removeFile(std::string const& filename)
238{
239 bool const success =
240 std::filesystem::remove(std::filesystem::path(filename));
241 if (success)
242 {
243 DBUG("Removed '{:s}'", filename);
244 }
245}
246
247void removeFiles(std::vector<std::string> const& files)
248{
249 for (auto const& file : files)
250 {
251 removeFile(file);
252 }
253}
254} // end namespace BaseLib
#define OGS_FATAL(...)
Definition: Error.h:26
Filename manipulation routines.
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:29
std::string containsKeyword(std::string const &str, std::string const &keyword)
Definition: FileTools.cpp:71
std::string constructFormattedFileName(std::string const &format_specification, std::string const &mesh_name, int const timestep, double const t, int const iteration)
Definition: FileTools.cpp:116
void removeFile(std::string const &filename)
Definition: FileTools.cpp:237
std::string const & getProjectDirectory()
Returns the directory where the prj file resides.
Definition: FileTools.cpp:218
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:186
std::string extractPath(std::string const &pathname)
Definition: FileTools.cpp:207
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)
Definition: FileTools.cpp:82
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)
Definition: FileTools.cpp:49
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition: FileTools.cpp:43
std::string copyPathToFileName(const std::string &file_name, const std::string &source)
Definition: FileTools.cpp:196
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:180
std::string dropFileExtension(std::string const &filename)
Definition: FileTools.cpp:169
bool isProjectDirectorySet()
Returns true if the project directory is set.
Definition: FileTools.cpp:35
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:212
std::string extractBaseName(std::string const &pathname)
Definition: FileTools.cpp:175
double swapEndianness(double const &v)
Definition: FileTools.cpp:147
std::string format(const char *format_str,...)
Definition: StringTools.cpp:85
void setProjectDirectory(std::string const &dir)
Sets the project directory.
Definition: FileTools.cpp:227
bool hasFileExtension(std::string const &extension, std::string const &filename)
Definition: FileTools.cpp:191
void removeFiles(std::vector< std::string > const &files)
Definition: FileTools.cpp:247
static const double v
static const double t
std::string project_directory
The directory where the prj file resides.
Definition: FileTools.cpp:27
bool project_directory_is_set
Whether the project directory has already been set.
Definition: FileTools.cpp:30