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 <stdexcept>
36#include <iostream>
37#include <list>
38#include <iterator>
39#include <iostream>
40#include <sstream>
41#include <list>
42#include <sys/stat.h>
43
44
45#include <drain/RegExp.h>
46#include "Path.h"
47
48
49namespace drain {
50
52
58class FilePath {
59
60public:
61
62 typedef Path<std::string,'/',true,false,true> path_t;
63
65 //FilePath(const std::string & s = "");
66 template<typename ...TT>
67 inline
68 FilePath(const TT &... args){
69 set(args...);
70 }
71
72
74 FilePath(const FilePath & s);
75
76 virtual inline
77 ~FilePath(){};
78
79 inline
80 void clear(){
81 this->dir.clear();
82 this->tail.clear();
83 this->extension.clear();
84 }
85
86 inline
87 bool empty() const {
88 return (dir.empty() && tail.empty());
89 // extension may be non-empty - but that would be odd.
90 }
91 //void set(const std::string & s);
92
93 template<typename ...TT>
94 inline
95 void set(const TT &... args){
96 this->clear();
97 append(args...);
98 }
99
100 template<typename T, typename ...TT>
101 void append(const T & arg, const TT &... args){
102 this->dir.append(arg);
103 append(args...);
104 //set(elem);
105 }
106
108 void append(const FilePath & path);
109
111
114 void append(const std::string & s);
115
116
117 inline
118 void append(const char *s){
119 append(std::string(s));
120 }
121
122
123 inline
124 void append(){};
125 /*
126 template<typename T>
127 void append(const T & arg){
128
129 }
130 */
131
132
133 inline
134 bool operator==(const FilePath & p) const {
135
136 if (p.tail != tail)
137 return false;
138
139 if (p.extension != extension)
140 return false;
141
142 if (p.dir != dir)
143 return false;
144
145 return true;
146 }
147
148
149 path_t dir;
150 std::string tail;
151 std::string extension;
152
153 //bool isRooted
154
156 //static
157 //char separator;
158
159 //static const RegExp pathRegExp;
160
161
162 virtual
163 std::ostream & toStream(std::ostream & ostr) const { //, char dirSeparator = 0) const {
164
165 if (!dir.empty()){
166 ostr << dir;
167 if (!dir.back().empty())
168 ostr << dir.separator.character;
169 }
170
171 ostr << tail;
172
173 if (!extension.empty())
174 ostr << '.' << extension;
175
176 return ostr;
177 }
178
179 // Not inhrerited?
180 virtual
181 std::string str() const {
182 std::stringstream sstr;
183 toStream(sstr);
184 return sstr.str();
185 }
186
187 virtual inline
188 operator std::string () const {
189 return str();
190 }
191
192 inline
193 FilePath & operator<<(const FilePath & path){
194 append(path);
195 /*
196 this->dir << path.dir;
197 if (!this->tail.empty())
198 std::cerr << __FILE__ << " warning: dropped:" << this->tail << '\n';
199 this->tail = path.tail;
200 this->extension = path.extension;
201 //this->insert(this->end(), path.begin(), path.end());
202 */
203 return *this;
204 }
205
206 // Consider non-static method?
207
208 static inline
209 int mkdir(const std::string & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH){
210 return mkdir(FilePath::path_t(dirpath), flags);
211 }
212
213 // Note: Path, not FilePath
214 static
215 int mkdir(const FilePath::path_t & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
216
217 //
218 void debug(std::ostream & ostr = std::cout) const;
219
220protected:
221
222 void handleBasename(const std::string & basename);
223
224};
225
226inline
227std::ostream & operator<<(std::ostream & ostr, const FilePath & p) {
228 return p.toStream(ostr);
229}
230
231
232} // drain::
233
234
235#endif
Extracts and stores directory path, base filename and extension.
Definition FilePath.h:58
virtual std::ostream & toStream(std::ostream &ostr) const
Directory path separator.
Definition FilePath.h:163
FilePath(const TT &... args)
Constructor.
Definition FilePath.h:68
Definition Path.h:112
void append(const T2 &arg, const TT &... args)
Append path with element(s), path(s) or string(s).
Definition Path.h:200
Definition DataSelector.cpp:1277