Psi4
path.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 //
30 // Created by Justin Turney on 9/1/16.
31 //
32 
33 #ifndef PSI4_CORE_PATH_H
34 #define PSI4_CORE_PATH_H
35 
36 #include <string>
37 #include <vector>
38 
39 namespace psi {
40 namespace filesystem {
41 
46 class path
47 {
48 protected:
49  std::vector<std::string> path_;
50  bool absolute_;
51 
52  static std::vector<std::string> tokenize(const std::string &string, const std::string &delim)
53  {
54  std::string::size_type lastPos = 0, pos = string.find_first_of(delim, lastPos);
55  std::vector<std::string> tokens;
56 
57  while (lastPos != std::string::npos) {
58  if (pos != lastPos)
59  tokens.push_back(string.substr(lastPos, pos - lastPos));
60  lastPos = pos;
61  if (lastPos == std::string::npos || lastPos + 1 == string.length())
62  break;
63  pos = string.find_first_of(delim, ++lastPos);
64  }
65 
66  return tokens;
67  }
68 
69 public:
70 
71  path() : absolute_(false)
72  {}
73 
74  path(const path &path) : path_(path.path_), absolute_(path.absolute_)
75  {}
76 
78  {}
79 
80  path(const std::string &string)
81  { set(string); }
82 
83  void set(const std::string &str);
84 
85  size_t length() const
86  { return path_.size(); }
87 
88  bool empty() const
89  { return path_.empty(); }
90 
91  bool is_absolute() const
92  { return absolute_; }
93 
94  path make_absolute() const;
95 
96  bool exists() const;
97 
98  std::string str() const;
99 
100  bool is_directory() const;
101 
102  bool is_file() const;
103 
104  std::string stem() const;
105 
106  std::string filename() const;
107 
108  std::string extension() const;
109 
110  bool remove_file();
111 
112  bool resize_file(size_t target_length);
113 
114  path parent_path() const;
115 
116  path operator/(const path &other) const;
117 
118  path &operator=(const path &path);
119 
120  path &operator=(path &&path);
121 
122  bool operator==(const path &p) const
123  { return path_ == p.path_; }
124 
125  bool operator!=(const path &p) const
126  { return path_ != p.path_; }
127 
128  static path getcwd();
129 };
130 
131 bool create_directory(const path &p);
132 
133 } // filesystem
134 } // psi
135 
136 #endif //PSI4_CORE_PATH_H
std::string str() const
Definition: path.cc:93
bool is_absolute() const
Definition: path.h:91
bool exists() const
Definition: path.cc:81
bool operator!=(const path &p) const
Definition: path.h:125
bool is_file() const
Definition: path.cc:118
path(const path &path)
Definition: path.h:74
std::string extension() const
Definition: path.cc:149
path & operator=(const path &path)
Definition: path.cc:187
bool absolute_
Definition: path.h:50
static path getcwd()
Definition: path.cc:213
path(const std::string &string)
Definition: path.h:80
path operator/(const path &other) const
Definition: path.cc:174
std::string filename() const
Definition: path.cc:141
bool is_directory() const
Definition: path.cc:110
Class for manipulating paths.
Definition: path.h:46
bool resize_file(size_t target_length)
Definition: path.cc:208
bool remove_file()
Definition: path.cc:203
std::vector< std::string > path_
Definition: path.h:49
bool empty() const
Definition: path.h:88
path parent_path() const
Definition: path.cc:158
path(path &&path)
Definition: path.h:77
static std::vector< std::string > tokenize(const std::string &string, const std::string &delim)
Definition: path.h:52
std::string stem() const
Definition: path.cc:126
bool create_directory(const path &p)
Definition: path.cc:51
path()
Definition: path.h:71
bool operator==(const path &p) const
Definition: path.h:122
path make_absolute() const
Definition: path.cc:56
size_t length() const
Definition: path.h:85
void set(const std::string &str)
Definition: path.cc:87