FilePath.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
49 namespace drain {
50 
52 
58 class FilePath {
59 
60 public:
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  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 
143  //static
144  //char separator;
145 
146  //static const RegExp pathRegExp;
147 
148 
149  virtual
150  std::ostream & toStream(std::ostream & ostr) const { //, char dirSeparator = 0) const {
151 
152  if (!dir.empty()){
153  ostr << dir;
154  if (!dir.back().empty())
155  ostr << dir.separator.character;
156  }
157 
158  ostr << basename;
159 
160  if (!extension.empty())
161  ostr << '.' << extension;
162 
163  return ostr;
164  }
165 
166  // Not inhrerited?
167  virtual
168  std::string str() const {
169  std::stringstream sstr;
170  toStream(sstr);
171  return sstr.str();
172  }
173 
174  inline
175  FilePath & operator<<(const FilePath & path){
176  this->dir << path.dir;
177  if (!this->basename.empty())
178  std::cerr << __FILE__ << " warning: dropped:" << this->basename << '\n';
179  this->basename = path.basename;
180  this->extension = path.extension;
181  //this->insert(this->end(), path.begin(), path.end());
182  return *this;
183  }
184 
185  static inline
186  int mkdir(const std::string & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH){
187  return mkdir(FilePath::path_t(dirpath), flags);
188  }
189 
190  // Note: Path, not FilePath
191  static
192  int mkdir(const FilePath::path_t & dirpath, int flags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
193 
194 
195 };
196 
197 inline
198 std::ostream & operator<<(std::ostream & ostr, const FilePath & p) {
199  return p.toStream(ostr);
200 }
201 
202 
203 } // drain::
204 
205 
206 #endif
Extracts and stores directory path, base filename and extension.
Definition: FilePath.h:58
FilePath(const TT &... args)
Constructor.
Definition: FilePath.h:68
virtual std::ostream & toStream(std::ostream &ostr) const
Directory path separator.
Definition: FilePath.h:150
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