1 /* Path.hpp
2  * Copyright (C) 2021  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT PATH WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BUTILITIES_PATH_HPP_
19 #define BUTILITIES_PATH_HPP_
20 
21 #include <string>
22 
23 #ifdef _WIN32
24 #define BUTILITIES_PATH_SLASH "\\"
25 #else
26 #define BUTILITIES_PATH_SLASH "/"
27 #endif
28 
29 namespace BUtilities
30 {
31 
32 class Path
33 {
34 public:
Path()35         Path () : dir_(), file_(), ext_() {}
Path(const std::string & path)36         Path (const std::string& path) {split (path);}
37 
operator =(const std::string & path)38         Path& operator= (const std::string& path)
39         {
40                 split (path);
41                 return *this;
42         }
43 
operator std::string() const44         operator std::string() const {return (dir().empty() || (dir() == BUTILITIES_PATH_SLASH) ? dir() : dir() + BUTILITIES_PATH_SLASH) + filename();}
45 
dir() const46         std::string dir() const {return dir_;}
filename() const47         std::string filename() const {return file_ + (ext_.empty() ? "" : "." + ext_);}
ext() const48         std::string ext() const {return ext_;}
49 
50 protected:
51         std::string dir_;
52         std::string file_;
53         std::string ext_;
54 
split(const std::string & path)55         void split (const std::string& path)
56         {
57                 const size_t spos = path.find_last_of (BUTILITIES_PATH_SLASH);
58                 if (spos == std::string::npos) dir_ = "";
59                 else if (spos == 0) dir_ = BUTILITIES_PATH_SLASH;
60                 else dir_ = path.substr (0, spos);
61 
62                 file_ = path.substr (spos + 1);
63 
64                 if (file_ == "") ext_ = "";
65 
66                 else if ((file_ == ".") || (file_ == ".."))
67                 {
68                         dir_ = (dir_.empty() || (dir_ == BUTILITIES_PATH_SLASH) ? dir_ : dir_ + BUTILITIES_PATH_SLASH) + file_;
69                         file_ = "";
70                         ext_ = "";
71                 }
72 
73                 else
74                 {
75                         const size_t dpos = file_.find_last_of (".");
76                         if ((dpos != std::string::npos) && (dpos != 0))
77                         {
78                                 ext_ = file_.substr (dpos + 1);
79                                 file_ = file_.substr (0, dpos);
80                         }
81 
82                         else ext_ = "";
83 
84                 }
85         }
86 
87 
88 };
89 
90 }
91 
92 #endif /* BUTILITIES_PATH_HPP_ */
93