Disk ARchive  2.5.12
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 #include "tools.hpp"
51 
52 namespace libdar
53 {
54 
57 
59 
62  class mask : public on_pool
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 
83 
85  virtual std::string dump(const std::string & prefix = "") const = 0;
86 
89  virtual mask *clone() const = 0;
90  };
91 
92 
94 
96  class bool_mask : public mask
97  {
98  public :
100 
104  bool_mask(bool always) { val = always; };
105 
107  bool is_covered(const std::string & expression) const { return val; };
108  bool is_covered(const path & chemin) const { return val; };
109  std::string dump(const std::string & prefix) const { return prefix + (val ? gettext("TRUE") : gettext("FALSE")); };
110 
112  mask *clone() const { return new (get_pool()) bool_mask(val); };
113 
114  private :
115  bool val;
116  };
117 
118 
120 
121  class simple_mask : public mask
122  {
123  public :
124 
126 
129  simple_mask(const std::string & wilde_card_expression, bool case_sensit);
131  simple_mask(const simple_mask & m) : mask(m) { copy_from(m); };
133  const simple_mask & operator = (const simple_mask & m);
134 
136  bool is_covered(const std::string &expression) const;
137 
139  std::string dump(const std::string & prefix) const;
140 
142  mask *clone() const { return new (get_pool()) simple_mask(*this); };
143 
144  private :
145  std::string the_mask;
146  bool case_s;
147 
148  void copy_from(const simple_mask & m);
149  };
150 
151 
153 
154  class regular_mask : public mask
155  {
156  public :
157 
159 
162  regular_mask(const std::string & wilde_card_expression,
163  bool x_case_sensit);
165  regular_mask(const regular_mask & ref);
167  regular_mask & operator= (const regular_mask & ref);
168 
170  virtual ~regular_mask() { regfree(&preg); };
171 
173  bool is_covered(const std::string & expression) const;
174 
176  std::string dump(const std::string & prefix) const;
177 
179  mask *clone() const { return new (get_pool()) regular_mask(*this); };
180 
181  private :
182  regex_t preg;
183  std::string mask_exp; //< used only by the copy constructor
184  bool case_sensit; //< used only by the copy constructor
185 
186  void set_preg(const std::string & wilde_card_expression,
187  bool x_case_sensit);
188  };
189 
190 
192 
195  class not_mask : public mask
196  {
197  public :
199 
203  not_mask(const mask &m) { copy_from(m); };
205  not_mask(const not_mask & m) : mask(m) { copy_from(m); };
207  const not_mask & operator = (const not_mask & m);
209  ~not_mask() { detruit(); };
210 
212  bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); };
213  bool is_covered(const path & chemin) const { return !ref->is_covered(chemin); };
214  std::string dump(const std::string & prefix) const;
215 
217  mask *clone() const { return new (get_pool()) not_mask(*this); };
218 
219  private :
220  mask *ref;
221 
222  void copy_from(const not_mask &m);
223  void copy_from(const mask &m);
224  void detruit();
225  };
226 
227 
229 
230  class et_mask : public mask
231  {
232  public :
233 
235 
239  et_mask() {};
241  et_mask(const et_mask &m) : mask(m) { copy_from(m); };
243  const et_mask & operator = (const et_mask &m);
245  ~et_mask() { detruit(); };
246 
247 
249 
253  void add_mask(const mask & toadd);
254 
256  bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
257  bool is_covered(const path & chemin) const { return t_is_covered(chemin); };
258  std::string dump(const std::string & prefix) const { return dump_logical(prefix, gettext("AND")); };
259 
261  mask *clone() const { return new (get_pool()) et_mask(*this); };
262 
264 
267  U_I size() const { return lst.size(); };
268 
270 
274  void clear() { detruit(); };
275 
276  protected :
277  std::vector<mask *> lst;
278 
279  std::string dump_logical(const std::string & prefix, const std::string & boolop) const;
280 
281  private :
282  void copy_from(const et_mask & m);
283  void detruit();
284 
285  template<class T> bool t_is_covered(const T & expression) const
286  {
287  std::vector<mask *>::const_iterator it = lst.begin();
288 
289  if(lst.empty())
290  throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on"));
291 
292  while(it != lst.end() && (*it)->is_covered(expression))
293  ++it;
294 
295  return it == lst.end();
296  }
297 
298  };
299 
300 
302 
307  class ou_mask : public et_mask
308  {
309  public:
311  bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
312  bool is_covered(const path & chemin) const { return t_is_covered(chemin); };
313  std::string dump(const std::string & prefix) const { return dump_logical(prefix, gettext("OR")); };
315  mask *clone() const { return new (get_pool()) ou_mask(*this); };
316 
317  private:
318  template<class T> bool t_is_covered(const T & expression) const
319  {
320  std::vector<mask *>::const_iterator it = lst.begin();
321 
322  if(lst.empty())
323  throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask"));
324 
325  while(it != lst.end() && ! (*it)->is_covered(expression))
326  it++;
327 
328  return it != lst.end();
329  }
330 
331  };
332 
333 
335 
336  class simple_path_mask : public mask
337  {
338  public :
340 
344  simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; };
345 
347  bool is_covered(const std::string & expression) const { throw SRC_BUG; };
348  bool is_covered(const path & chemin) const;
349  std::string dump(const std::string & prefix) const;
350 
352  mask *clone() const { return new (get_pool()) simple_path_mask(*this); };
353 
354  private :
355  path chemin;
356  bool case_s;
357  };
358 
359 
361 
362  class same_path_mask : public mask
363  {
364  public :
366 
369  same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; };
370 
372  bool is_covered(const std::string &chemin) const;
373 
375  std::string dump(const std::string & prefix) const;
376 
378  mask *clone() const { return new (get_pool()) same_path_mask(*this); };
379 
380  private :
381  std::string chemin;
382  bool case_s;
383  };
384 
385 
387 
388  class exclude_dir_mask : public mask
389  {
390  public:
392 
395  exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;};
396 
398  bool is_covered(const std::string &expression) const { throw SRC_BUG; }
399  bool is_covered(const path &chemin) const { return chemin.is_subdir_of(chemin, case_s); };
400  std::string dump(const std::string & prefix) const;
401 
403  mask *clone() const { return new (get_pool()) exclude_dir_mask(*this); };
404 
405  private:
406  std::string chemin;
407  bool case_s;
408  };
409 
411 
412 } // end of namespace
413 
414 #endif
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:398
same_path_mask(const std::string &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition: mask.hpp:369
the generic class, parent of all masks
Definition: mask.hpp:62
~et_mask()
destructor
Definition: mask.hpp:245
mask * clone() const
inherited from the mask class
Definition: mask.hpp:112
makes the OR operator between two or more masks
Definition: mask.hpp:307
memory_pool * get_pool() const
Definition: on_pool.hpp:144
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:212
void clear()
clear the mask
Definition: mask.hpp:274
std::string dump(const std::string &prefix) const
dump in human readable form the nature of the mask
Definition: mask.hpp:258
makes an AND operator between two or more masks
Definition: mask.hpp:230
exclude_dir_mask(const std::string &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition: mask.hpp:395
a set of general purpose routines
const char * dar_gettext(const char *)
a routine to change NLS domaine forth and back for inline routines
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:213
std::string dump(const std::string &prefix) const
dump in human readable form the nature of the mask
Definition: mask.hpp:109
matches regular expressions (see "man 7 regex")
Definition: mask.hpp:154
std::string dump(const std::string &prefix) const
dump in human readable form the nature of the mask
Definition: mask.hpp:313
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:256
mask * clone() const
inherited from the mask class
Definition: mask.hpp:261
not_mask(const mask &m)
the constructor to be used by libdar external programs
Definition: mask.hpp:203
negation of another mask
Definition: mask.hpp:195
boolean mask, either always true or false
Definition: mask.hpp:96
mask * clone() const
inherited from the mask class
Definition: mask.hpp:315
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:399
string matches if it is subdir of mask or mask is a subdir of expression
Definition: mask.hpp:336
matches if string is exactly the given mask (no wilde card expression)
Definition: mask.hpp:362
U_I size() const
the number of mask on which is done the AND operator
Definition: mask.hpp:267
virtual bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:80
virtual ~regular_mask()
destructor
Definition: mask.hpp:170
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:257
et_mask(const et_mask &m)
copy constructor
Definition: mask.hpp:241
mask * clone() const
inherited from the mask class
Definition: mask.hpp:352
matches as done on shell command lines (see "man 7 glob")
Definition: mask.hpp:121
here is the definition of the path classthe path class handle path and provide several operation on t...
virtual mask * clone() const =0
matches if string is the given constructor string or a sub directory of it
Definition: mask.hpp:388
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:347
simple_path_mask(const path &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition: mask.hpp:344
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:311
bool_mask(bool always)
the constructor
Definition: mask.hpp:104
exception used to signal range error
Definition: erreurs.hpp:179
not_mask(const not_mask &m)
copy constructor
Definition: mask.hpp:205
mask * clone() const
inherited from the mask class
Definition: mask.hpp:217
bool is_covered(const std::string &expression) const
inherited from the mask class
Definition: mask.hpp:107
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:239
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:312
mask * clone() const
inherited from the mask class
Definition: mask.hpp:378
bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition: mask.hpp:108
~not_mask()
destructor
Definition: mask.hpp:209
mask * clone() const
inherited from the mask class
Definition: mask.hpp:179
bool is_subdir_of(const path &p, bool case_sensit) const
test whether the current object is a subdir of the method&#39;s argument
mask * clone() const
inherited from the mask class
Definition: mask.hpp:142
virtual std::string dump(const std::string &prefix="") const =0
dump in human readable form the nature of the mask
simple_mask(const simple_mask &m)
copy constructor
Definition: mask.hpp:131
virtual bool is_covered(const std::string &expression) const =0
check wether the given string is covered by the mask
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
std::string display() const
convert back a path to a string
the class path is here to manipulate paths in the Unix notation: using&#39;/&#39;
Definition: path.hpp:50
mask * clone() const
inherited from the mask class
Definition: mask.hpp:403