OGS
FileTools.cpp
Go to the documentation of this file.
1 
15 #include "FileTools.h"
16 
17 #include <boost/algorithm/string.hpp>
18 #include <typeindex>
19 #include <unordered_map>
20 
21 #include "Error.h"
22 #include "filesystem.h"
23 
24 namespace
25 {
27 std::string project_directory;
28 
31 } // anonymous namespace
32 
33 namespace BaseLib
34 {
36 {
38 }
39 
43 bool IsFileExisting(const std::string& strFilename)
44 {
45  return fs::exists(fs::path(strFilename));
46 }
47 
48 std::tuple<std::string, std::string::size_type, std::string::size_type>
49 getParenthesizedString(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 
71 std::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 
81 template <typename T>
82 bool 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 
116 std::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 
147 double 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 
169 std::string dropFileExtension(std::string const& filename)
170 {
171  auto const filename_path = fs::path(filename);
172  return (filename_path.parent_path() / filename_path.stem()).string();
173 }
174 
175 std::string extractBaseName(std::string const& pathname)
176 {
177  return fs::path(pathname).filename().string();
178 }
179 
180 std::string extractBaseNameWithoutExtension(std::string const& pathname)
181 {
182  std::string basename = extractBaseName(pathname);
183  return dropFileExtension(basename);
184 }
185 
186 std::string getFileExtension(const std::string& path)
187 {
188  return fs::path(path).extension().string();
189 }
190 
191 bool hasFileExtension(std::string const& extension, std::string const& filename)
192 {
193  return boost::iequals(extension, getFileExtension(filename));
194 }
195 
196 std::string copyPathToFileName(const std::string& file_name,
197  const std::string& source)
198 {
199  auto filePath = fs::path(file_name);
200  if (filePath.has_parent_path())
201  {
202  return filePath.string();
203  }
204  return (fs::path(source) /= filePath).string();
205 }
206 
207 std::string extractPath(std::string const& pathname)
208 {
209  return fs::path(pathname).parent_path().string();
210 }
211 
212 std::string joinPaths(std::string const& pathA, std::string const& pathB)
213 {
214  return (fs::path(pathA) /= fs::path(pathB)).string();
215 }
216 
217 std::string const& getProjectDirectory()
218 {
220  {
221  OGS_FATAL("The project directory has not yet been set.");
222  }
223  return project_directory;
224 }
225 
226 void setProjectDirectory(std::string const& dir)
227 {
229  {
230  OGS_FATAL("The project directory has already been set.");
231  }
232  project_directory = dir;
234 }
235 
236 void removeFile(std::string const& filename)
237 {
238  bool const success = fs::remove(fs::path(filename));
239  if (success)
240  {
241  DBUG("Removed '{:s}'", filename);
242  }
243 }
244 
245 void removeFiles(std::vector<std::string> const& files)
246 {
247  for (auto const& file : files)
248  {
249  removeFile(file);
250  }
251 }
252 } // end namespace BaseLib
#define OGS_FATAL(...)
Definition: Error.h:26
Filename manipulation routines.
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
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:236
std::string const & getProjectDirectory()
Returns the directory where the prj file resides.
Definition: FileTools.cpp:217
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:226
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:245
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