OGS
FileTools.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <fstream>
18#include <string>
19#include <vector>
20
21#include "Logging.h"
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{
65 out.write(reinterpret_cast<const char*>(&val), sizeof(T));
66}
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{
91 T v;
92 in.read(reinterpret_cast<char*>(&v), sizeof(T));
93 return v;
94}
95
96template <typename T>
97std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n)
98{
99 std::ifstream in(filename.c_str());
100 if (!in)
101 {
102 ERR("readBinaryArray(): Error while reading from file '{:s}'.",
103 filename);
104 ERR("Could not open file '{:s}' for input.", filename);
105 in.close();
106 return std::vector<T>();
107 }
108
109 std::vector<T> result;
110 result.reserve(n);
111
112 for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
113 {
114 result.push_back(BaseLib::readBinaryValue<T>(in));
115 }
116
117 if (result.size() == n)
118 {
119 return result;
120 }
121
122 ERR("readBinaryArray(): Error while reading from file '{:s}'.", filename);
123 ERR("Read different number of values. Expected {:d}, got {:d}.",
124 n,
125 result.size());
126
127 if (!in.eof())
128 {
129 ERR("EOF reached.\n");
130 }
131
132 return std::vector<T>();
133}
134
142std::string extractBaseName(std::string const& pathname);
143
149std::string extractBaseNameWithoutExtension(std::string const& pathname);
150
154std::string getFileExtension(std::string const& path);
155
160bool hasFileExtension(std::string const& extension,
161 std::string const& filename);
162
167std::string copyPathToFileName(const std::string& file_name,
168 const std::string& source);
169
173std::string dropFileExtension(std::string const& filename);
174
180std::string extractPath(std::string const& pathname);
181
185std::string joinPaths(std::string const& pathA, std::string const& pathB);
186
188std::string const& getProjectDirectory();
189
191void setProjectDirectory(std::string const& dir);
192
195void removeFile(std::string const& filename);
196
199void removeFiles(std::vector<std::string> const& files);
200} // end namespace BaseLib
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:44
std::vector< T > readBinaryArray(std::string const &filename, std::size_t const n)
Definition: FileTools.h:97
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
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
T readBinaryValue(std::istream &in)
Definition: FileTools.h:89
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
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
void writeValueBinary(std::ostream &out, T const &val)
write value as binary into the given output stream
Definition: FileTools.h:63
static const double v
static const double t