OGS 6.1.0-1699-ge946d4c5f
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 <logog/include/logog.hpp>
22 
23 namespace BaseLib
24 {
30 bool IsFileExisting(const std::string &strFilename);
31 
39 template <typename T> void writeValueBinary(std::ostream &out, T const& val)
40 {
41  out.write(reinterpret_cast<const char*>(&val), sizeof(T));
42 }
43 
44 template <typename T>
45 T swapEndianness(T const& v)
46 {
47  union
48  {
49  T v;
50  char c[sizeof(T)];
51  } a, b;
52 
53  a.v = v;
54  for (unsigned short i = 0; i < sizeof(T); i++)
55  b.c[i] = a.c[sizeof(T) - i - 1];
56 
57  return b.v;
58 }
59 
60 double swapEndianness(double const& v);
61 
62 
63 template <typename T>
64 T readBinaryValue(std::istream& in)
65 {
66  T v;
67  in.read(reinterpret_cast<char*>(&v), sizeof(T));
68  return v;
69 }
70 
71 template <typename T>
72 std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n)
73 {
74  std::ifstream in(filename.c_str());
75  if (!in) {
76  ERR("readBinaryArray(): Error while reading from file '%s'.",
77  filename.c_str());
78  ERR("Could not open file '%s' for input.", filename.c_str());
79  in.close();
80  return std::vector<T>();
81  }
82 
83  std::vector<T> result;
84  result.reserve(n);
85 
86  for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
87  result.push_back(BaseLib::readBinaryValue<T>(in));
88 
89  if (result.size() == n)
90  return result;
91 
92  ERR("readBinaryArray(): Error while reading from file '%s'.",
93  filename.c_str());
94  ERR("Read different number of values. Expected %d, got %d.", n, result.size());
95 
96  if (!in.eof())
97  ERR("EOF reached.\n");
98 
99  return std::vector<T>();
100 }
101 
107 void truncateFile( std::string const& file_path);
108 
116 std::string extractBaseName(std::string const& pathname);
117 
123 std::string extractBaseNameWithoutExtension(std::string const& pathname);
124 
128 std::string getFileExtension(std::string const& filename);
129 
134 bool hasFileExtension(std::string const& extension,
135  std::string const& filename);
136 
140 std::string dropFileExtension(std::string const& filename);
141 
146 std::string copyPathToFileName(const std::string &file_name,
147  const std::string &source);
148 
154 std::string extractPath(std::string const& pathname);
155 
159 std::string appendPathSeparator(std::string const& path);
160 
164 std::string joinPaths(std::string const& pathA, std::string const& pathB);
165 
167 std::string const& getProjectDirectory();
168 
170 void setProjectDirectory(std::string const& dir);
171 
174 void removeFiles(std::vector<std::string> const& files);
175 } // end namespace BaseLib
std::string extractBaseName(std::string const &pathname)
Definition: FileTools.cpp:108
std::string copyPathToFileName(const std::string &file_name, const std::string &source)
Definition: FileTools.cpp:143
std::string appendPathSeparator(std::string const &path)
Definition: FileTools.cpp:166
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html. ...
Definition: FileTools.cpp:36
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:116
void removeFiles(std::vector< std::string > const &files)
Definition: FileTools.cpp:207
void setProjectDirectory(std::string const &dir)
Sets the project directory.
Definition: FileTools.cpp:197
T readBinaryValue(std::istream &in)
Definition: FileTools.h:64
Build information.
void truncateFile(std::string const &filename)
truncate a file
Definition: FileTools.cpp:63
std::string const & getProjectDirectory()
Returns the directory where the prj file resides.
Definition: FileTools.cpp:188
std::string extractPath(std::string const &pathname)
Definition: FileTools.cpp:158
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:122
std::vector< T > readBinaryArray(std::string const &filename, std::size_t const n)
Definition: FileTools.h:72
void writeValueBinary(std::ostream &out, T const &val)
write value as binary into the given output stream
Definition: FileTools.h:39
double swapEndianness(double const &v)
Definition: FileTools.cpp:42
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:173
std::string dropFileExtension(std::string const &filename)
Definition: FileTools.cpp:93
bool hasFileExtension(std::string const &extension, std::string const &filename)
Definition: FileTools.cpp:131