OGS 6.2.1-97-g73d1aeda3
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 std::string project_directory;
26 
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  {
53  b.c[i] = a.c[sizeof(double) / 2 - i - 1];
54  }
55 
56  for (unsigned short i = sizeof(double) / 2; i < sizeof(double); i++)
57  {
58  b.c[i] = a.c[sizeof(double) + sizeof(double) / 2 - i - 1];
59  }
60 
61  return b.v;
62 }
63 
67 void truncateFile( std::string const& filename)
68 {
69  std::ofstream ofs(filename.c_str(), std::ios_base::trunc);
70  ofs.close();
71 }
72 
73 namespace
74 {
75 
82 std::string::size_type findLastPathSeparator(std::string const& path)
83 {
84  return path.find_last_of("/\\");
85 }
86 
91 std::string::size_type findLastDot(std::string const& path)
92 {
93  return path.find_last_of('.');
94 }
95 } // end namespace
96 
97 std::string dropFileExtension(std::string const& filename)
98 {
99  // Look for dots in filename.
100  auto const p = findLastDot(filename);
101  if (p == std::string::npos)
102  {
103  return filename;
104  }
105 
106  // Check position of the last path separator.
107  auto const s = findLastPathSeparator(filename);
108  if (s != std::string::npos && p < s)
109  {
110  return filename;
111  }
112 
113  return filename.substr(0, p);
114 }
115 
116 std::string extractBaseName(std::string const& pathname)
117 {
118  auto const p = findLastPathSeparator(pathname);
119  if (p == std::string::npos)
120  {
121  return pathname;
122  }
123  return pathname.substr(p + 1);
124 }
125 
126 std::string extractBaseNameWithoutExtension(std::string const& pathname)
127 {
128  std::string basename = extractBaseName(pathname);
129  return dropFileExtension(basename);
130 }
131 
132 std::string getFileExtension(const std::string &path)
133 {
134  const std::string str = extractBaseName(path);
135  auto const p = findLastDot(str);
136  if (p == std::string::npos)
137  {
138  return std::string();
139  }
140  return str.substr(p + 1);
141 }
142 
143 bool hasFileExtension(std::string const& extension, std::string const& filename)
144 {
145  return boost::iequals(extension, getFileExtension(filename));
146 }
147 
148 static const char pathSeparator =
149 #ifdef _WIN32
150  '\\';
151 #else
152  '/';
153 #endif
154 
155 std::string copyPathToFileName(const std::string &file_name,
156  const std::string &source)
157 {
158  // check if file_name already contains a full path
159  auto const pos = findLastPathSeparator(file_name);
160  if (pos != std::string::npos)
161  {
162  return file_name;
163  }
164 
165  if (source.empty())
166  {
167  return file_name;
168  }
169  if (source.back() != pathSeparator)
170  {
171  return BaseLib::extractPath(source + pathSeparator).append(file_name);
172  }
173  return BaseLib::extractPath(source).append(file_name);
174 }
175 
176 std::string extractPath(std::string const& pathname)
177 {
178  auto const pos = findLastPathSeparator(pathname);
179  if (pos == std::string::npos)
180  {
181  return "";
182  }
183  return pathname.substr(0, pos + 1);
184 }
185 
186 std::string appendPathSeparator(std::string const& path)
187 {
188  if (findLastPathSeparator(path) == path.length() - 1)
189  {
190  return path;
191  }
192  return path + pathSeparator;
193 }
194 
195 std::string joinPaths(std::string const& pathA, std::string const& pathB)
196 {
197  if (pathA.empty())
198  {
199  return pathB;
200  }
201 
202  if (pathB.empty())
203  {
204  return pathA;
205  }
206 
207  if (pathB.front() == pathSeparator) {
208  auto const tmpB = pathB.substr(1);
209  return appendPathSeparator(pathA) + tmpB;
210  }
211  return appendPathSeparator(pathA) + pathB;
212 }
213 
214 std::string const& getProjectDirectory()
215 {
217  {
218  OGS_FATAL("The project directory has not yet been set.");
219  }
220  return project_directory;
221 }
222 
223 void setProjectDirectory(std::string const& dir)
224 {
226  {
227  OGS_FATAL("The project directory has already been set.");
228  }
229  project_directory = dir;
231 }
232 
233 void removeFiles(std::vector<std::string> const& files)
234 {
235  for (auto const& file : files)
236  {
237  int const success = std::remove(file.c_str());
238  if (success == 0)
239  {
240  DBUG("Removed '%s'", file.c_str());
241  }
242  else
243  {
244  if (errno == ENOENT) // File does not exists
245  {
246  continue;
247  }
248  ERR("Removing file '%s' failed with error %d.", file.c_str(),
249  errno);
250  std::perror("Error: ");
251  OGS_FATAL("Unrecoverable error happened while removing a file.");
252  }
253  }
254 }
255 } // end namespace BaseLib
static const char pathSeparator
Definition: FileTools.cpp:148
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:116
std::string copyPathToFileName(const std::string &file_name, const std::string &source)
Definition: FileTools.cpp:155
std::string appendPathSeparator(std::string const &path)
Definition: FileTools.cpp:186
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:126
void removeFiles(std::vector< std::string > const &files)
Definition: FileTools.cpp:233
std::string::size_type findLastDot(std::string const &path)
Definition: FileTools.cpp:91
void setProjectDirectory(std::string const &dir)
Sets the project directory.
Definition: FileTools.cpp:223
Definition of the quicksort function.
void truncateFile(std::string const &filename)
truncate a file
Definition: FileTools.cpp:67
std::string const & getProjectDirectory()
Returns the directory where the prj file resides.
Definition: FileTools.cpp:214
std::string extractPath(std::string const &pathname)
Definition: FileTools.cpp:176
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:132
std::string::size_type findLastPathSeparator(std::string const &path)
Definition: FileTools.cpp:82
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
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:195
std::string dropFileExtension(std::string const &filename)
Definition: FileTools.cpp:97
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:143