Disk ARchive  2.5.10
Full featured and portable backup and archiving tool
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 
28 
29 #ifndef MASK_HPP
30 #define MASK_HPP
31 
32 #include "../my_config.h"
33 
34 extern "C"
35 {
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 
40 #if HAVE_REGEX_H
41 #include <regex.h>
42 #endif
43 } // end extern "C"
44 
45 #include <string>
46 #include <vector>
47 
48 #include "path.hpp"
49 #include "on_pool.hpp"
50 
51 namespace libdar
52 {
53 
56 
58 
61  class mask : public on_pool
62  {
63  public :
64  virtual ~mask() {};
65 
67 
71  virtual bool is_covered(const std::string &expression) const = 0;
72 
74 
79  virtual bool is_covered(const path & chemin) const { return is_covered(chemin.display()); };
80 
83  virtual mask *clone() const = 0;
84  };
85 
86 
88 
90  class bool_mask : public mask
91  {
92  public :
94 
98  bool_mask(bool always) { val = always; };
99 
101  bool is_covered(const std::string & expression) const { return val; };
102  bool is_covered(const path & chemin) const { return val; };
103 
105  mask *clone() const { return new (get_pool()) bool_mask(val); };
106 
107  private :
108  bool val;
109  };
110 
111 
113 
114  class simple_mask : public mask
115  {
116  public :
117 
119 
122  simple_mask(const std::string & wilde_card_expression, bool case_sensit);
124  simple_mask(const simple_mask & m) : mask(m) { copy_from(m); };
126  const simple_mask & operator = (const simple_mask & m);
127 
129  bool is_covered(const std::string &expression) const;
130 
132  mask *clone() const { return new (get_pool()) simple_mask(*this); };
133 
134  private :
135  std::string the_mask;
136  bool case_s;
137 
138  void copy_from(const simple_mask & m);
139  };
140 
141 
143 
144  class regular_mask : public mask
145  {
146  public :
147 
149 
152  regular_mask(const std::string & wilde_card_expression,
153  bool x_case_sensit);
155  regular_mask(const regular_mask & ref);
157  regular_mask & operator= (const regular_mask & ref);
158 
160  virtual ~regular_mask() { regfree(&preg); };
161 
163  bool is_covered(const std::string & expression) const;
164 
166  mask *clone() const { return new (get_pool()) regular_mask(*this); };
167 
168  private :
169  regex_t preg;
170  std::string mask_exp; //< used only by the copy constructor
171  bool case_sensit; //< used only by the copy constructor
172 
173  void set_preg(const std::string & wilde_card_expression,
174  bool x_case_sensit);
175  };
176 
177 
179 
182  class not_mask : public mask
183  {
184  public :
186 
190  not_mask(const mask &m) { copy_from(m); };
192  not_mask(const not_mask & m) : mask(m) { copy_from(m); };
194  const not_mask & operator = (const not_mask & m);
196  ~not_mask() { detruit(); };
197 
199  bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); };
200  bool is_covered(const path & chemin) const { return !ref->is_covered(chemin); };
201 
203  mask *clone() const { return new (get_pool()) not_mask(*this); };
204 
205  private :
206  mask *ref;
207 
208  void copy_from(const not_mask &m);
209  void copy_from(const mask &m);
210  void detruit();
211  };
212 
213 
215 
216  class et_mask : public mask
217  {
218  public :
219 
221 
225  et_mask() {};
227  et_mask(const et_mask &m) : mask(m) { copy_from(m); };
229  const et_mask & operator = (const et_mask &m);
231  ~et_mask() { detruit(); };
232 
233 
235 
239  void add_mask(const mask & toadd);
240 
242  bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
243  bool is_covered(const path & chemin) const { return t_is_covered(chemin); };
244 
246  mask *clone() const { return new (get_pool()) et_mask(*this); };
247 
249 
252  U_I size() const { return lst.size(); };
253 
255 
259  void clear() { detruit(); };
260 
261  protected :
262  std::vector<mask *> lst;
263 
264  private :
265  void copy_from(const et_mask & m);
266  void detruit();
267 
268  template<class T> bool t_is_covered(const T & expression) const
269  {
270  std::vector<mask *>::const_iterator it = lst.begin();
271 
272  if(lst.empty())
273  throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on"));
274 
275  while(it != lst.end() && (*it)->is_covered(expression))
276  ++it;
277 
278  return it == lst.end();
279  }
280 
281  };
282 
283 
285 
290  class ou_mask : public et_mask
291  {
292  public:
294  bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
295  bool is_covered(const path & chemin) const { return t_is_covered(chemin); }
296  ;
298  mask *clone() const { return new (get_pool()) ou_mask(*this); };
299 
300  private:
301  template<class T> bool t_is_covered(const T & expression) const
302  {
303  std::vector<mask *>::const_iterator it = lst.begin();
304 
305  if(lst.empty())
306  throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask"));
307 
308  while(it != lst.end() && ! (*it)->is_covered(expression))
309  it++;
310 
311  return it != lst.end();
312  }
313 
314  };
315 
316 
318 
319  class simple_path_mask : public mask
320  {
321  public :
323 
327  simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; };
328 
330  bool is_covered(const std::string & expression) const { throw SRC_BUG; };
331  bool is_covered(const path & chemin) const;
332 
334  mask *clone() const { return new (get_pool()) simple_path_mask(*this); };
335 
336  private :
337  path chemin;
338  bool case_s;
339  };
340 
341 
343 
344  class same_path_mask : public mask
345  {
346  public :
348 
351  same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; };
352 
354  bool is_covered(const std::string &chemin) const;
355 
357  mask *clone() const { return new (get_pool()) same_path_mask(*this); };
358 
359  private :
360  std::string chemin;
361  bool case_s;
362  };
363 
364 
366 
367  class exclude_dir_mask : public mask
368  {
369  public:
371 
374  exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;};
375 
377  bool is_covered(const std::string &expression) const { throw SRC_BUG; }
378  bool is_covered(const path &chemin) const { return chemin.is_subdir_of(chemin, case_s); };
379 
381  mask *clone() const { return new (get_pool()) exclude_dir_mask(*this); };
382 
383  private:
384  std::string chemin;
385  bool case_s;
386  };
387 
389 
390 } // end of namespace
391 
392 #endif
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:295
mask * clone() const
inherited from the mask class
Definition: mask.hpp:166
same_path_mask(const std::string &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition: mask.hpp:351
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:200
bool is_covered(const std::string &expression) const
inherited from the mask class
the generic class, parent of all masks
Definition: mask.hpp:61
~et_mask()
destructor
Definition: mask.hpp:231
makes the OR operator between two or more masks
Definition: mask.hpp:290
U_I size() const
the number of mask on which is done the AND operator
Definition: mask.hpp:252
void clear()
clear the mask
Definition: mask.hpp:259
makes an AND operator between two or more masks
Definition: mask.hpp:216
exclude_dir_mask(const std::string &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition: mask.hpp:374
const char * dar_gettext(const char *)
a routine to change NLS domaine forth and back for inline routines
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:242
matches regular expressions (see "man 7 regex")
Definition: mask.hpp:144
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:102
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:330
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:199
not_mask(const mask &m)
the constructor to be used by libdar external programs
Definition: mask.hpp:190
negation of another mask
Definition: mask.hpp:182
boolean mask, either always true or false
Definition: mask.hpp:90
mask * clone() const
inherited from the mask class
Definition: mask.hpp:132
mask * clone() const
inherited from the mask class
Definition: mask.hpp:298
regular_mask & operator=(const regular_mask &ref)
the assignment operator
string matches if it is subdir of mask or mask is a subdir of expression
Definition: mask.hpp:319
matches if string is exactly the given mask (no wilde card expression)
Definition: mask.hpp:344
const not_mask & operator=(const not_mask &m)
assignment operator
bool is_subdir_of(const path &p, bool case_sensit) const
test whether the current object is a subdir of the method's argument
memory_pool * get_pool() const
Definition: on_pool.hpp:144
virtual ~regular_mask()
destructor
Definition: mask.hpp:160
et_mask(const et_mask &m)
copy constructor
Definition: mask.hpp:227
matches as done on shell command lines (see "man 7 glob")
Definition: mask.hpp:114
here is the definition of the path classthe path class handle path and provide several operation on t...
virtual mask * clone() const =0
bool is_covered(const std::string &chemin) const
inherited from the mask class
mask * clone() const
inherited from the mask class
Definition: mask.hpp:203
matches if string is the given constructor string or a sub directory of it
Definition: mask.hpp:367
mask * clone() const
inherited from the mask class
Definition: mask.hpp:334
mask * clone() const
inherited from the mask class
Definition: mask.hpp:246
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:101
virtual bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:79
simple_path_mask(const path &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition: mask.hpp:327
bool_mask(bool always)
the constructor
Definition: mask.hpp:98
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:243
exception used to signal range error
Definition: erreurs.hpp:179
not_mask(const not_mask &m)
copy constructor
Definition: mask.hpp:192
const et_mask & operator=(const et_mask &m)
assignment operator
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:378
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:377
bool is_covered(const std::string &expression) const
inherited from the mask class
this is the base class of object that can be allocated on a memory pool
et_mask()
the constructor to be used by libdar external programs
Definition: mask.hpp:225
std::string display() const
convert back a path to a string
~not_mask()
destructor
Definition: mask.hpp:196
mask * clone() const
inherited from the mask class
Definition: mask.hpp:357
simple_mask(const simple_mask &m)
copy constructor
Definition: mask.hpp:124
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:294
regular_mask(const std::string &wilde_card_expression, bool x_case_sensit)
the constructor to be used by libdar external programs
virtual bool is_covered(const std::string &expression) const =0
check wether the given string is covered by the mask
simple_mask(const std::string &wilde_card_expression, bool case_sensit)
the constructor to use by libdar external programs
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
mask * clone() const
inherited from the mask class
Definition: mask.hpp:381
mask * clone() const
inherited from the mask class
Definition: mask.hpp:105
const simple_mask & operator=(const simple_mask &m)
assignment operator
void add_mask(const mask &toadd)
add a mask to the operator
the class path is here to manipulate paths in the Unix notation: using'/'
Definition: path.hpp:50