HAL
project_directory.cpp
Go to the documentation of this file.
2 #include <sstream>
3 #include <iostream>
4 #include <time.h>
5 
6 namespace hal {
7  const std::string ProjectDirectory::s_shadow_dir = "autosave";
8 
9  ProjectDirectory::ProjectDirectory(const std::string& path_)
10  : std::filesystem::path(path_)
11  {
12  replace_extension(); // remove any extension
13  }
14 
16  {
17  srand(time(nullptr));
18 
19  for (int iloop=0; iloop<1000; iloop++)
20  {
21  std::filesystem::path proj = std::filesystem::current_path();
22  std::ostringstream oss;
23  oss << "hal_proj" << (1000 + rand() % 9000);
24  proj.append(oss.str());
25  if (!std::filesystem::exists(proj))
26  return ProjectDirectory(proj.string());
27  }
28  return ProjectDirectory();
29  }
30 
31  std::filesystem::path ProjectDirectory::get_canonical_path() const
32  {
33  if (empty()) return std::filesystem::path();
34  if (!std::filesystem::exists(*this)) return std::filesystem::path();
35  return std::filesystem::canonical(*this);
36  }
37 
38  std::filesystem::path ProjectDirectory::get_default_filename(const std::string& extension) const
39  {
40  std::string name(filename());
41  name += extension.empty() ? std::string(".hal") : extension;
42  std::filesystem::path retval(get_canonical_path());
43  retval.append(name);
44  return retval;
45  }
46 
47  std::filesystem::path ProjectDirectory::get_filename(const std::string &relative_filename) const
48  {
49  std::filesystem::path rel(relative_filename);
50  std::filesystem::path retval(get_canonical_path());
51  if (rel.is_relative())
52  retval.append(relative_filename);
53  else
54  retval.append(rel.filename().string());
55  return retval;
56  }
57 
58  std::filesystem::path ProjectDirectory::get_shadow_filename(const std::string& extension) const
59  {
60  std::string name(filename());
61  name += extension.empty() ? std::string(".hal") : extension;
62  std::filesystem::path retval = get_shadow_dir();
63  retval.append(name);
64  return retval;
65  }
66 
67  std::filesystem::path ProjectDirectory::get_shadow_dir() const
68  {
69  std::filesystem::path retval(get_canonical_path());
70  retval.append(s_shadow_dir);
71  return retval;
72  }
73 
74  std::filesystem::path ProjectDirectory::get_relative_file_path(const std::string &filename) const
75  {
76  std::filesystem::path retval(filename);
77  if (retval.is_relative() || filename.empty() || empty()) return retval;
78  std::string relative = filename;
79  std::string canonical_dir = get_canonical_path().string() + '/';
80  int n = canonical_dir.size();
81  if (!relative.compare(0,n,canonical_dir))
82  return (std::filesystem::path(relative.substr(n)));
83  return retval;
84  }
85 }
std::filesystem::path get_filename(const std::string &relative_filename) const
std::filesystem::path get_relative_file_path(const std::string &filename) const
std::filesystem::path get_canonical_path() const
static const std::string s_shadow_dir
static ProjectDirectory generate_random()
ProjectDirectory(const std::string &path=std::string())
std::filesystem::path get_shadow_filename(const std::string &extension=std::string()) const
std::filesystem::path get_default_filename(const std::string &extension=std::string()) const
std::filesystem::path get_shadow_dir() const
n
Definition: test.py:6
std::string name