Psi4
path.h
Go to the documentation of this file.
1 //
2 // Created by Justin Turney on 9/1/16.
3 //
4 
5 #ifndef PSI4_CORE_PATH_H
6 #define PSI4_CORE_PATH_H
7 
8 #include <string>
9 #include <vector>
10 
11 namespace psi {
12 namespace filesystem {
13 
18 class path
19 {
20 protected:
21  std::vector<std::string> path_;
22  bool absolute_;
23 
24  static std::vector<std::string> tokenize(const std::string &string, const std::string &delim)
25  {
26  std::string::size_type lastPos = 0, pos = string.find_first_of(delim, lastPos);
27  std::vector<std::string> tokens;
28 
29  while (lastPos != std::string::npos) {
30  if (pos != lastPos)
31  tokens.push_back(string.substr(lastPos, pos - lastPos));
32  lastPos = pos;
33  if (lastPos == std::string::npos || lastPos + 1 == string.length())
34  break;
35  pos = string.find_first_of(delim, ++lastPos);
36  }
37 
38  return tokens;
39  }
40 
41 public:
42 
43  path() : absolute_(false)
44  {}
45 
46  path(const path &path) : path_(path.path_), absolute_(path.absolute_)
47  {}
48 
50  {}
51 
52  path(const std::string &string)
53  { set(string); }
54 
55  void set(const std::string &str);
56 
57  size_t length() const
58  { return path_.size(); }
59 
60  bool empty() const
61  { return path_.empty(); }
62 
63  bool is_absolute() const
64  { return absolute_; }
65 
66  path make_absolute() const;
67 
68  bool exists() const;
69 
70  std::string str() const;
71 
72  bool is_directory() const;
73 
74  bool is_file() const;
75 
76  std::string stem() const;
77 
78  std::string filename() const;
79 
80  std::string extension() const;
81 
82  bool remove_file();
83 
84  bool resize_file(size_t target_length);
85 
86  path parent_path() const;
87 
88  path operator/(const path &other) const;
89 
90  path &operator=(const path &path);
91 
92  path &operator=(path &&path);
93 
94  bool operator==(const path &p) const
95  { return path_ == p.path_; }
96 
97  bool operator!=(const path &p) const
98  { return path_ != p.path_; }
99 
100  static path getcwd();
101 };
102 
103 bool create_directory(const path &p);
104 
105 } // filesystem
106 } // psi
107 
108 #endif //PSI4_CORE_PATH_H
std::string str() const
Definition: path.cc:65
bool is_absolute() const
Definition: path.h:63
bool exists() const
Definition: path.cc:53
bool operator!=(const path &p) const
Definition: path.h:97
bool is_file() const
Definition: path.cc:90
path(const path &path)
Definition: path.h:46
std::string extension() const
Definition: path.cc:121
path & operator=(const path &path)
Definition: path.cc:159
bool absolute_
Definition: path.h:22
static path getcwd()
Definition: path.cc:185
path(const std::string &string)
Definition: path.h:52
path operator/(const path &other) const
Definition: path.cc:146
std::string filename() const
Definition: path.cc:113
bool is_directory() const
Definition: path.cc:82
Class for manipulating paths.
Definition: path.h:18
bool resize_file(size_t target_length)
Definition: path.cc:180
bool remove_file()
Definition: path.cc:175
std::vector< std::string > path_
Definition: path.h:21
bool empty() const
Definition: path.h:60
path parent_path() const
Definition: path.cc:130
path(path &&path)
Definition: path.h:49
static std::vector< std::string > tokenize(const std::string &string, const std::string &delim)
Definition: path.h:24
std::string stem() const
Definition: path.cc:98
bool create_directory(const path &p)
Definition: path.cc:23
path()
Definition: path.h:43
bool operator==(const path &p) const
Definition: path.h:94
path make_absolute() const
Definition: path.cc:28
size_t length() const
Definition: path.h:57
void set(const std::string &str)
Definition: path.cc:59