OGS
FileTools.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <cstddef>
18#include <iosfwd>
19#include <string>
20#include <tuple>
21#include <vector>
22
23namespace BaseLib
24{
29
35bool IsFileExisting(const std::string& strFilename);
36
43std::tuple<std::string, std::string::size_type, std::string::size_type>
44getParenthesizedString(std::string const& in,
45 char const open_char,
46 char const close_char,
47 std::string::size_type pos);
48
49std::string constructFormattedFileName(std::string const& format_specification,
50 std::string const& mesh_name,
51 int const timestep,
52 double const t,
53 int const iteration);
54
62template <typename T>
63void writeValueBinary(std::ostream& out, T const& val);
64
65template <typename T>
66T swapEndianness(T const& v)
67{
68 union
69 {
70 T v;
71 char c[sizeof(T)];
72 } a, b;
73
74 a.v = v;
75 for (unsigned short i = 0; i < sizeof(T); i++)
76 {
77 b.c[i] = a.c[sizeof(T) - i - 1];
78 }
79
80 return b.v;
81}
82
83double swapEndianness(double const& v);
84
85template <typename T>
86T readBinaryValue(std::istream& in);
87
88extern template float readBinaryValue<float>(std::istream&);
89extern template double readBinaryValue<double>(std::istream&);
90
91template <typename T>
92std::vector<T> readBinaryArray(std::string const& filename,
93 std::size_t const n);
94
102std::string extractBaseName(std::string const& pathname);
103
109std::string extractBaseNameWithoutExtension(std::string const& pathname);
110
114std::string getFileExtension(std::string const& path);
115
120bool hasFileExtension(std::string const& extension,
121 std::string const& filename);
122
126std::string dropFileExtension(std::string const& filename);
127
133std::string extractPath(std::string const& pathname);
134
138std::string joinPaths(std::string const& pathA, std::string const& pathB);
139
141std::string const& getProjectDirectory();
142
144void setProjectDirectory(std::string const& dir);
145
148
151void removeFile(std::string const& filename);
152
155void removeFiles(std::vector<std::string> const& files);
156
157// Creates the given directory. Returns true on success.
158bool createOutputDirectory(std::string const& dir);
159} // end namespace BaseLib
std::vector< T > readBinaryArray(std::string const &filename, std::size_t const n)
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)
Definition FileTools.cpp:53
T readBinaryValue(std::istream &in)
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:47
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.
Definition FileTools.cpp:39
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)
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)