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-2018 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  protected:
48  std::vector<std::string> path_;
49  bool absolute_;
50 
51  static std::vector<std::string> tokenize(const std::string &string, const std::string &delim) {
52  std::string::size_type lastPos = 0, pos = string.find_first_of(delim, lastPos);
53  std::vector<std::string> tokens;
54 
55  while (lastPos != std::string::npos) {
56  if (pos != lastPos) tokens.push_back(string.substr(lastPos, pos - lastPos));
57  lastPos = pos;
58  if (lastPos == std::string::npos || lastPos + 1 == string.length()) break;
59  pos = string.find_first_of(delim, ++lastPos);
60  }
61 
62  return tokens;
63  }
64 
65  public:
66  path() : absolute_(false) {}
67 
68  path(const path &path) : path_(path.path_), absolute_(path.absolute_) {}
69 
71 
72  path(const std::string &string) { set(string); }
73 
74  void set(const std::string &str);
75 
76  size_t length() const { return path_.size(); }
77 
78  bool empty() const { return path_.empty(); }
79 
80  bool is_absolute() const { return absolute_; }
81 
82  path make_absolute() const;
83 
84  bool exists() const;
85 
86  std::string str() const;
87 
88  bool is_directory() const;
89 
90  bool is_file() const;
91 
92  std::string stem() const;
93 
94  std::string filename() const;
95 
96  std::string extension() const;
97 
98  bool remove_file();
99 
100  bool resize_file(size_t target_length);
101 
102  path parent_path() const;
103 
104  path operator/(const path &other) const;
105 
106  path &operator=(const path &path);
107 
108  path &operator=(path &&path);
109 
110  bool operator==(const path &p) const { return path_ == p.path_; }
111 
112  bool operator!=(const path &p) const { return path_ != p.path_; }
113 
114  static path getcwd();
115 };
116 
117 bool create_directory(const path &p);
118 
119 } // filesystem
120 } // psi
121 
122 #endif // PSI4_CORE_PATH_H
std::string str() const
Definition: path.cc:115
bool is_absolute() const
Definition: path.h:80
bool exists() const
Definition: path.cc:101
bool operator!=(const path &p) const
Definition: path.h:112
bool is_file() const
Definition: path.cc:136
path(const path &path)
Definition: path.h:68
std::string extension() const
Definition: path.cc:153
path & operator=(const path &path)
Definition: path.cc:183
bool absolute_
Definition: path.h:49
static path getcwd()
Definition: path.cc:201
path(const std::string &string)
Definition: path.h:72
path operator/(const path &other) const
Definition: path.cc:173
std::string filename() const
Definition: path.cc:147
bool is_directory() const
Definition: path.cc:130
Class for manipulating paths.
Definition: path.h:46
bool resize_file(size_t target_length)
Definition: path.cc:199
bool remove_file()
Definition: path.cc:197
std::vector< std::string > path_
Definition: path.h:48
bool empty() const
Definition: path.h:78
path parent_path() const
Definition: path.cc:160
path(path &&path)
Definition: path.h:70
static std::vector< std::string > tokenize(const std::string &string, const std::string &delim)
Definition: path.h:51
std::string stem() const
Definition: path.cc:142
bool create_directory(const path &p)
Definition: path.cc:77
path()
Definition: path.h:66
bool operator==(const path &p) const
Definition: path.h:110
path make_absolute() const
Definition: path.cc:79
size_t length() const
Definition: path.h:76
void set(const std::string &str)
Definition: path.cc:106