Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
mask.hpp
Go to the documentation of this file.
1 //*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: mask.hpp,v 1.25 2011/05/20 10:23:07 edrusb Rel $
22 //
23 /*********************************************************************/
24 
31 
32 #ifndef MASK_HPP
33 #define MASK_HPP
34 
35 #include "../my_config.h"
36 
37 extern "C"
38 {
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 
43 #if HAVE_REGEX_H
44 #include <regex.h>
45 #endif
46 } // end extern "C"
47 
48 #include <string>
49 #include <vector>
50 #include "path.hpp"
51 
52 namespace libdar
53 {
54 
57 
59 
62  class mask
63  {
64  public :
65  virtual ~mask() {};
66 
68 
72  virtual bool is_covered(const std::string &expression) const = 0;
73 
75 
80  virtual bool is_covered(const path & chemin) const { return is_covered(chemin.display()); };
81 
84  virtual mask *clone() const = 0;
85  };
86 
87 
89 
91  class bool_mask : public mask
92  {
93  public :
95 
99  bool_mask(bool always) { val = always; };
100 
102  bool is_covered(const std::string & expression) const { return val; };
103  bool is_covered(const path & chemin) const { return val; };
104 
106  mask *clone() const { return new bool_mask(val); };
107 
108  private :
109  bool val;
110  };
111 
112 
114 
115  class simple_mask : public mask
116  {
117  public :
118 
120 
123  simple_mask(const std::string & wilde_card_expression, bool case_sensit);
125  simple_mask(const simple_mask & m) : mask(m) { copy_from(m); };
127  const simple_mask & operator = (const simple_mask & m);
128 
130  bool is_covered(const std::string &expression) const;
131 
133  mask *clone() const { return new simple_mask(*this); };
134 
135  private :
136  std::string the_mask;
137  bool case_s;
138 
139  void copy_from(const simple_mask & m);
140  };
141 
142 
144 
145  class regular_mask : public mask
146  {
147  public :
148 
150 
153  regular_mask(const std::string & wilde_card_expression,
154  bool x_case_sensit);
156  regular_mask(const regular_mask & ref);
158  regular_mask & operator= (const regular_mask & ref);
159 
161  virtual ~regular_mask() { regfree(&preg); };
162 
164  bool is_covered(const std::string & expression) const;
165 
167  mask *clone() const { return new regular_mask(*this); };
168 
169  private :
170  regex_t preg;
171  std::string mask_exp; //< used only by the copy constructor
172  bool case_sensit; //< used only by the copy constructor
173 
174  void set_preg(const std::string & wilde_card_expression,
175  bool x_case_sensit);
176  };
177 
178 
180 
183  class not_mask : public mask
184  {
185  public :
187 
191  not_mask(const mask &m) { copy_from(m); };
193  not_mask(const not_mask & m) : mask(m) { copy_from(m); };
195  const not_mask & operator = (const not_mask & m);
197  ~not_mask() { detruit(); };
198 
200  bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); };
201  bool is_covered(const path & chemin) const { return !ref->is_covered(chemin); };
202 
204  mask *clone() const { return new not_mask(*this); };
205 
206  private :
207  mask *ref;
208 
209  void copy_from(const not_mask &m);
210  void copy_from(const mask &m);
211  void detruit();
212  };
213 
214 
216 
217  class et_mask : public mask
218  {
219  public :
220 
222 
226  et_mask() {};
228  et_mask(const et_mask &m) : mask(m) { copy_from(m); };
230  const et_mask & operator = (const et_mask &m);
232  ~et_mask() { detruit(); };
233 
234 
236 
240  void add_mask(const mask & toadd);
241 
243  bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
244  bool is_covered(const path & chemin) const { return t_is_covered(chemin); };
245 
247  mask *clone() const { return new et_mask(*this); };
248 
250 
253  U_I size() const { return lst.size(); };
254 
256 
260  void clear() { detruit(); };
261 
262  protected :
263  std::vector<mask *> lst;
264 
265  private :
266  void copy_from(const et_mask & m);
267  void detruit();
268 
269  template<class T> bool t_is_covered(const T & expression) const
270  {
271  std::vector<mask *>::const_iterator it = lst.begin();
272 
273  if(lst.empty())
274  throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on"));
275 
276  while(it != lst.end() && (*it)->is_covered(expression))
277  ++it;
278 
279  return it == lst.end();
280  }
281 
282  };
283 
284 
286 
291  class ou_mask : public et_mask
292  {
293  public:
295  bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
296  bool is_covered(const path & chemin) const { return t_is_covered(chemin); }
297 ;
299  mask *clone() const { return new ou_mask(*this); };
300 
301  private:
302  template<class T> bool t_is_covered(const T & expression) const
303  {
304  std::vector<mask *>::const_iterator it = lst.begin();
305 
306  if(lst.empty())
307  throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask"));
308 
309  while(it != lst.end() && ! (*it)->is_covered(expression))
310  it++;
311 
312  return it != lst.end();
313  }
314 
315  };
316 
317 
319 
320  class simple_path_mask : public mask
321  {
322  public :
324 
328  simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; };
329 
331  bool is_covered(const std::string & expression) const { throw SRC_BUG; };
332  bool is_covered(const path & chemin) const;
333 
335  mask *clone() const { return new simple_path_mask(*this); };
336 
337  private :
338  path chemin;
339  bool case_s;
340  };
341 
342 
344 
345  class same_path_mask : public mask
346  {
347  public :
349 
352  same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; };
353 
355  bool is_covered(const std::string &chemin) const;
356 
358  mask *clone() const { return new same_path_mask(*this); };
359 
360  private :
361  std::string chemin;
362  bool case_s;
363  };
364 
365 
367 
368  class exclude_dir_mask : public mask
369  {
370  public:
372 
375  exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;};
376 
378  bool is_covered(const std::string &expression) const { throw SRC_BUG; }
379  bool is_covered(const path &chemin) const { return chemin.is_subdir_of(chemin, case_s); };
380 
382  mask *clone() const { return new exclude_dir_mask(*this); };
383 
384  private:
385  std::string chemin;
386  bool case_s;
387  };
388 
390 
391 } // end of namespace
392 
393 #endif