OGS
FileTools.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <cstddef>
18#include <iosfwd>
19#include <limits>
20#include <string>
21#include <tuple>
22#include <vector>
23
24namespace BaseLib
25{
30
36bool IsFileExisting(const std::string& strFilename);
37
44std::tuple<std::string, std::string::size_type, std::string::size_type>
45getParenthesizedString(std::string const& in,
46 char const open_char,
47 char const close_char,
48 std::string::size_type pos);
49
50std::string constructFormattedFileName(std::string const& format_specification,
51 std::string const& mesh_name,
52 int const timestep,
53 double const t,
54 int const iteration);
55
56std::vector<double> readDoublesFromBinaryFile(const std::string& filename);
57
65template <typename T>
66void writeValueBinary(std::ostream& out, T const& val);
67
68template <typename T>
69T swapEndianness(T const& v)
70{
71 union
72 {
73 T v;
74 char c[sizeof(T)];
75 } a, b;
76
77 a.v = v;
78 for (unsigned short i = 0; i < sizeof(T); i++)
79 {
80 b.c[i] = a.c[sizeof(T) - i - 1];
81 }
82
83 return b.v;
84}
85
86double swapEndianness(double const& v);
87
88template <typename T>
89T readBinaryValue(std::istream& in);
90
91extern template float readBinaryValue<float>(std::istream&);
92extern template double readBinaryValue<double>(std::istream&);
93
94template <typename T>
95std::vector<T> readBinaryVector(
96 std::string const& filename, std::size_t const start_element = 0,
97 std::size_t const num_elements = std::numeric_limits<std::size_t>::max());
98
106std::string extractBaseName(std::string const& pathname);
107
113std::string extractBaseNameWithoutExtension(std::string const& pathname);
114
118std::string getFileExtension(std::string const& path);
119
124bool hasFileExtension(std::string const& extension,
125 std::string const& filename);
126
130std::string dropFileExtension(std::string const& filename);
131
137std::string extractPath(std::string const& pathname);
138
142std::string joinPaths(std::string const& pathA, std::string const& pathB);
143
145std::string const& getProjectDirectory();
146
148void setProjectDirectory(std::string const& dir);
149
152
155void removeFile(std::string const& filename);
156
159void removeFiles(std::vector<std::string> const& files);
160
161// Creates the given directory. Returns true on success.
162bool createOutputDirectory(std::string const& dir);
163} // end namespace BaseLib
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::vector< T > readBinaryVector(std::string const &filename, std::size_t const start_element, std::size_t const num_elements)
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::vector< double > readDoublesFromBinaryFile(const std::string &filename)
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:56
T readBinaryValue(std::istream &in)
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:50
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:42
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)