Arty
file_path.h
1 #ifndef FILE_PATH_H
2 #define FILE_PATH_H
3 
4 #include <string>
5 #include <algorithm>
6 
8 class FilePath {
9 public:
10  FilePath(const std::string& path)
11  : path_(path)
12  {
13  std::replace(path_.begin(), path_.end(), '\\', '/');
14  auto pos = path_.rfind('/');
15  base_ = (pos != std::string::npos) ? path_.substr(0, pos) : ".";
16  file_ = (pos != std::string::npos) ? path_.substr(pos + 1) : path_;
17  }
18 
19  const std::string& path() const { return path_; }
20  const std::string& base_name() const { return base_; }
21  const std::string& file_name() const { return file_; }
22 
23  std::string extension() const {
24  auto pos = file_.rfind('.');
25  return (pos != std::string::npos) ? file_.substr(pos + 1) : std::string();
26  }
27 
28  std::string remove_extension() const {
29  auto pos = file_.rfind('.');
30  return (pos != std::string::npos) ? file_.substr(0, pos) : file_;
31  }
32 
33  operator const std::string& () const {
34  return path();
35  }
36 
37 private:
38  std::string path_;
39  std::string base_;
40  std::string file_;
41 };
42 
43 #endif // FILE_PATH_H
Represents a path in the file system.
Definition: file_path.h:8