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->basename.clear();
83 this->extension.clear();
84 }
85
86 //void set(const std::string & s);
87
88 template<typename ...TT>
89 inline
90 void set(const TT &... args){
91 this->clear();
92 append(args...);
93 }
94
95 template<typename T, typename ...TT>
96 void append(const T & arg, const TT &... args){
97 this->dir.append(arg);
98 append(args...);
99 //set(elem);
100 }
101
103 void append(const FilePath & path);
104
106
109 void append(const std::string & s);
110
111
112 inline
113 void append(){};
114 /*
115 template<typename T>
116 void append(const T & arg){
117
118 }
119 */
120
121
122 inline
123 bool operator==(const FilePath & p) const {
124
125 if (p.basename != basename)
126 return false;
127
128 if (p.extension != extension)
129 return false;
130
131 if (p.dir != dir)
132 return false;
133
134 return true;
135 }
136
137
138 path_t dir;
139 std::string basename;
140 std::string extension;
141
142 //bool isRooted
143
145 //static
146 //char separator;
147
148 //static const RegExp pathRegExp;
149
150
151 virtual
152 std::ostream & toStream(std::ostream & ostr) const { //, char dirSeparator = 0) const {
153
154 if (!dir.empty()){
155 ostr << dir;
156 if (!dir.back().empty())
157 ostr << dir.separator.character;
158 }
159
160 ostr << basename;
161
162 if (!extension.empty())
163 ostr << '.' << extension;
164
165 return ostr;
166 }
167
168 // Not inhrerited?
169 virtual
170 std::string str() const {
171 std::stringstream sstr;
172 toStream(sstr);
173 return sstr.str();
174 }
175
176 inline
177 FilePath & operator<<(const FilePath & path){
178 this->dir << path.dir;
179 if (!this->basename.empty())
180 std::cerr << __FILE__ << " warning: dropped:" << this->basename << '\n';
181 this->basename = path.basename;
182 this->extension = path.extension;
183 //this->insert(this->end(), path.begin(), path.end());
184 return *this;
185 }
186
187 static inline
188 int mkdir(const std::string & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH){
189 return mkdir(FilePath::path_t(dirpath), flags);
190 }
191
192 // Note: Path, not FilePath
193 static
194 int mkdir(const FilePath::path_t & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
195
196
197};
198
199inline
200std::ostream & operator<<(std::ostream & ostr, const FilePath & p) {
201 return p.toStream(ostr);
202}
203
204
205} // drain::
206
207
208#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:152
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:196
Definition DataSelector.cpp:1277