Loading...
Searching...
No Matches
FilePath.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef DRAIN_FILES_H_
33#define DRAIN_FILES_H_
34
35#include <iostream>
36#include <iostream>
37#include <sstream>
38#include <sys/stat.h>
39
40
41#include <drain/RegExp.h>
42#include "Path.h"
43
44
45namespace drain {
46
48
54class FilePath {
55
56public:
57
58 typedef Path<std::string,'/',true,false,true> path_t;
59
61 //FilePath(const std::string & s = "");
62 template<typename ...TT>
63 inline
64 FilePath(const TT &... args){
65 set(args...);
66 }
67
68
70 FilePath(const FilePath & s);
71
72 virtual inline
73 ~FilePath(){};
74
75 inline
76 void clear(){
77 this->dir.clear();
78 this->tail.clear();
79 this->extension.clear();
80 }
81
82 inline
83 bool empty() const {
84 return (dir.empty() && tail.empty());
85 // extension may be non-empty - but that would be odd.
86 }
87 //void set(const std::string & s);
88
89 template<typename ...TT>
90 inline
91 void set(const TT &... args){
92 this->clear();
93 append(args...);
94 }
95
96 template<typename T, typename ...TT>
97 void append(const T & arg, const TT &... args){
98 this->dir.append(arg);
99 append(args...);
100 //set(elem);
101 }
102
104 void append(const FilePath & path);
105
107
110 void append(const std::string & s);
111
112
113 inline
114 void append(const char *s){
115 append(std::string(s));
116 }
117
118
119 inline
120 void append(){};
121 /*
122 template<typename T>
123 void append(const T & arg){
124
125 }
126 */
127
128
129 inline
130 bool operator==(const FilePath & p) const {
131
132 if (p.tail != tail)
133 return false;
134
135 if (p.extension != extension)
136 return false;
137
138 if (p.dir != dir)
139 return false;
140
141 return true;
142 }
143
144
145 path_t dir;
146 std::string tail;
147 std::string extension;
148
149 //bool isRooted
150
152 //static
153 //char separator;
154
155 //static const RegExp pathRegExp;
156
157
158 virtual
159 std::ostream & toStream(std::ostream & ostr) const { //, char dirSeparator = 0) const {
160
161 if (!dir.empty()){
162 ostr << dir;
163 if (!dir.back().empty())
164 ostr << dir.separator.character;
165 }
166
167 ostr << tail;
168
169 if (!extension.empty())
170 ostr << '.' << extension;
171
172 return ostr;
173 }
174
175 // Not inhrerited?
176 virtual
177 std::string str() const {
178 std::stringstream sstr;
179 toStream(sstr);
180 return sstr.str();
181 }
182
183 virtual inline
184 operator std::string () const {
185 return str();
186 }
187
188 inline
189 FilePath & operator<<(const FilePath & path){
190 append(path);
191 /*
192 this->dir << path.dir;
193 if (!this->tail.empty())
194 std::cerr << __FILE__ << " warning: dropped:" << this->tail << '\n';
195 this->tail = path.tail;
196 this->extension = path.extension;
197 //this->insert(this->end(), path.begin(), path.end());
198 */
199 return *this;
200 }
201
202 // Consider non-static method?
203
204 static inline
205 int mkdir(const std::string & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH){
206 return mkdir(FilePath::path_t(dirpath), flags);
207 }
208
209 // Note: Path, not FilePath
210 static
211 int mkdir(const FilePath::path_t & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
212
213 //
214 void debug(std::ostream & ostr = std::cout) const;
215
216protected:
217
218 void handleBasename(const std::string & basename);
219
220};
221
222inline
223std::ostream & operator<<(std::ostream & ostr, const FilePath & p) {
224 return p.toStream(ostr);
225}
226
227
228} // drain::
229
230
231#endif
Extracts and stores directory path, base filename and extension.
Definition FilePath.h:54
virtual std::ostream & toStream(std::ostream &ostr) const
Directory path separator.
Definition FilePath.h:159
FilePath(const TT &... args)
Constructor.
Definition FilePath.h:64
Definition Path.h:135
void append(const T2 &arg, const TT &... args)
Append path with element(s), path(s) or string(s).
Definition Path.h:223
Definition DataSelector.cpp:1277