OGS 6.1.0-1721-g6382411ad
FileTools.cpp
Go to the documentation of this file.
1 
15 #include "FileTools.h"
16 #include "Error.h"
17 #include "StringTools.h"
18 
19 #include <sys/stat.h>
20 #include <boost/algorithm/string.hpp>
21 
22 namespace
23 {
25 static std::string project_directory = "";
26 
28 static bool project_directory_is_set = false;
29 } // anonymous namespace
30 
31 namespace BaseLib
32 {
36 bool IsFileExisting(const std::string &strFilename)
37 {
38  struct stat buffer {};
39  return (stat (strFilename.c_str(), &buffer) == 0);
40 }
41 
42 double swapEndianness(double const& v)
43 {
44  union
45  {
46  double v;
47  char c[sizeof(double)];
48  } a {}, b {};
49 
50  a.v = v;
51  for (unsigned short i = 0; i < sizeof(double)/2; i++)
52  b.c[i] = a.c[sizeof(double)/2 - i - 1];
53 
54  for (unsigned short i = sizeof(double)/2; i < sizeof(double); i++)
55  b.c[i] = a.c[sizeof(double)+sizeof(double)/2 - i - 1];
56 
57  return b.v;
58 }
59 
63 void truncateFile( std::string const& filename)
64 {
65  std::ofstream ofs(filename.c_str(), std::ios_base::trunc);
66  ofs.close();
67 }
68 
69 namespace
70 {
71 
77 static
78 std::string::size_type findLastPathSeparator(std::string const& path)
79 {
80  return path.find_last_of("/\\");
81 }
82 
86 static
87 std::string::size_type findLastDot(std::string const& path)
88 {
89  return path.find_last_of('.');
90 }
91 } // end namespace
92 
93 std::string dropFileExtension(std::string const& filename)
94 {
95  // Look for dots in filename.
96  auto const p = findLastDot(filename);
97  if (p == std::string::npos)
98  return filename;
99 
100  // Check position of the last path separator.
101  auto const s = findLastPathSeparator(filename);
102  if (s != std::string::npos && p < s)
103  return filename;
104 
105  return filename.substr(0, p);
106 }
107 
108 std::string extractBaseName(std::string const& pathname)
109 {
110  auto const p = findLastPathSeparator(pathname);
111  if (p == std::string::npos)
112  return pathname;
113  return pathname.substr(p + 1);
114 }
115 
116 std::string extractBaseNameWithoutExtension(std::string const& pathname)
117 {
118  std::string basename = extractBaseName(pathname);
119  return dropFileExtension(basename);
120 }
121 
122 std::string getFileExtension(const std::string &path)
123 {
124  const std::string str = extractBaseName(path);
125  auto const p = findLastDot(str);
126  if (p == std::string::npos)
127  return std::string();
128  return str.substr(p + 1);
129 }
130 
131 bool hasFileExtension(std::string const& extension, std::string const& filename)
132 {
133  return boost::iequals(extension, getFileExtension(filename));
134 }
135 
136 static const char pathSeparator =
137 #ifdef _WIN32
138  '\\';
139 #else
140  '/';
141 #endif
142 
143 std::string copyPathToFileName(const std::string &file_name,
144  const std::string &source)
145 {
146  // check if file_name already contains a full path
147  auto const pos = findLastPathSeparator(file_name);
148  if (pos != std::string::npos)
149  return file_name;
150 
151  if (source.empty())
152  return file_name;
153  if (source.back() != pathSeparator)
154  return BaseLib::extractPath(source + pathSeparator).append(file_name);
155  return BaseLib::extractPath(source).append(file_name);
156 }
157 
158 std::string extractPath(std::string const& pathname)
159 {
160  auto const pos = findLastPathSeparator(pathname);
161  if (pos == std::string::npos)
162  return "";
163  return pathname.substr(0, pos + 1);
164 }
165 
166 std::string appendPathSeparator(std::string const& path)
167 {
168  if(findLastPathSeparator(path) == path.length() - 1)
169  return path;
170  return path + pathSeparator;
171 }
172 
173 std::string joinPaths(std::string const& pathA, std::string const& pathB)
174 {
175  if (pathA.empty())
176  return pathB;
177 
178  if (pathB.empty())
179  return pathA;
180 
181  if (pathB.front() == pathSeparator) {
182  auto const tmpB = pathB.substr(1);
183  return appendPathSeparator(pathA) + tmpB;
184  }
185  return appendPathSeparator(pathA) + pathB;
186 }
187 
188 std::string const& getProjectDirectory()
189 {
191  {
192  OGS_FATAL("The project directory has not yet been set.");
193  }
194  return project_directory;
195 }
196 
197 void setProjectDirectory(std::string const& dir)
198 {
200  {
201  OGS_FATAL("The project directory has already been set.");
202  }
203  project_directory = dir;
205 }
206 
207 void removeFiles(std::vector<std::string> const& files)
208 {
209  for (auto const& file : files)
210  {
211  int const success = std::remove(file.c_str());
212  if (success == 0)
213  {
214  DBUG("Removed '%s'", file.c_str());
215  }
216  else
217  {
218  if (errno == ENOENT) // File does not exists
219  {
220  continue;
221  }
222  ERR("Removing file '%s' failed with error %d.", file.c_str(),
223  errno);
224  std::perror("Error: ");
225  OGS_FATAL("Unrecoverable error happened while removing a file.");
226  }
227  }
228 }
229 } // end namespace BaseLib
static const char pathSeparator
Definition: FileTools.cpp:136
static bool project_directory_is_set
Whether the project directory has already been set.
Definition: FileTools.cpp:28
Definition of string helper functions.
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
static std::string::size_type findLastDot(std::string const &path)
Definition: FileTools.cpp:87
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
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
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
static std::string::size_type findLastPathSeparator(std::string const &path)
Definition: FileTools.cpp:78
double swapEndianness(double const &v)
Definition: FileTools.cpp:42
Filename manipulation routines.
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
static std::string project_directory
The directory where the prj file resides.
Definition: FileTools.cpp:25
bool hasFileExtension(std::string const &extension, std::string const &filename)
Definition: FileTools.cpp:131