SPH
FileManager.cpp
Go to the documentation of this file.
1 #include "io/FileManager.h"
2 #include "io/FileSystem.h"
3 #include <algorithm>
4 #include <iomanip>
5 
7 
8 class PathError : public std::exception {
9 public:
10  virtual const char* what() const noexcept {
11  return "Cannot generate more paths";
12  }
13 };
14 
16  auto iter = std::find(usedPaths.begin(), usedPaths.end(), expected);
17  if (iter == usedPaths.end()) {
18  usedPaths.insert(expected);
19  return expected;
20  } else {
21  std::stringstream ss;
22  // since std::set is sorted, we actually do not need to search the whole container for other paths
23  for (Size i = 1; i < 999; ++i) {
24  Path path = expected;
25  path.removeExtension();
26  ss.str("");
27  ss << std::setw(3) << std::setfill('0') << i;
28  path = Path(path.native() + "_" + ss.str());
29  if (!expected.extension().empty()) {
30  // add back previously removed extension
31  // note that replaceExtension would remove any other extensions the file might have
32  path = Path(path.native() + "." + expected.extension().native());
33  }
34 
35  // starting from i==2, the path are always consecutive, so we don't really have to find the path;
36  // the speed different is quite small, though
37  iter = std::find(iter, usedPaths.end(), path);
38  if (iter == usedPaths.end()) {
39  usedPaths.insert(path);
40  return path;
41  }
42  }
43  }
44  throw PathError();
45 }
46 
47 char RandomPathManager::chars[] = "abcdefghijklmnopqrstuvwxyz0123456789";
48 
49 Path RandomPathManager::getPath(const std::string extension) {
50  while (true) {
51  std::string name;
52  for (Size i = 0; i < 8; ++i) {
53  // -1 for terminating zero, -2 is the last indexable position
54  const Size index = min(Size(rng() * (sizeof(chars) - 1)), Size(sizeof(chars) - 2));
55  name += chars[index];
56  }
57  Path path(name);
58  if (!extension.empty()) {
59  path.replaceExtension(extension);
60  }
61  if (!FileSystem::pathExists(path)) {
62  return path;
63  }
64  }
65 }
66 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
#define NAMESPACE_SPH_END
Definition: Object.h:12
virtual const char * what() const noexcept
Definition: FileManager.cpp:10
Object representing a path on a filesystem.
Definition: Path.h:17
Path & replaceExtension(const std::string &newExtension)
Changes the extension of the file.
Definition: Path.cpp:76
Path & removeExtension()
Removes the extension from the path.
Definition: Path.cpp:100
std::string native() const
Returns the native version of the path.
Definition: Path.cpp:71
bool empty() const
Checks if the path is empty.
Definition: Path.cpp:10
Path extension() const
Returns the extension of the filename.
Definition: Path.cpp:59
Path getPath(const std::string extension="")
Generates a new random path.
Definition: FileManager.cpp:49
Path getPath(const Path &expected)
Generates a unique path, based on given input path.
Definition: FileManager.cpp:15
bool pathExists(const Path &path)
Checks if a file or directory exists (or more precisely, if a file or directory is accessible).
Definition: FileSystem.cpp:21