Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028
00029 #ifndef MASK_HPP
00030 #define MASK_HPP
00031
00032 #include "../my_config.h"
00033
00034 extern "C"
00035 {
00036 #if HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #endif
00039
00040 #if HAVE_REGEX_H
00041 #include <regex.h>
00042 #endif
00043 }
00044
00045 #include <string>
00046 #include <vector>
00047 #include <new>
00048
00049 #include "path.hpp"
00050
00051 namespace libdar
00052 {
00053
00056
00058
00061 class mask
00062 {
00063 public :
00064 virtual ~mask() {};
00065
00067
00071 virtual bool is_covered(const std::string &expression) const = 0;
00072
00074
00079 virtual bool is_covered(const path & chemin) const { return is_covered(chemin.display()); };
00080
00083 virtual mask *clone() const = 0;
00084 };
00085
00086
00088
00090 class bool_mask : public mask
00091 {
00092 public :
00094
00098 bool_mask(bool always) { val = always; };
00099
00101 bool is_covered(const std::string & expression) const { return val; };
00102 bool is_covered(const path & chemin) const { return val; };
00103
00105 mask *clone() const { return new (std::nothrow) bool_mask(val); };
00106
00107 private :
00108 bool val;
00109 };
00110
00111
00113
00114 class simple_mask : public mask
00115 {
00116 public :
00117
00119
00122 simple_mask(const std::string & wilde_card_expression, bool case_sensit);
00124 simple_mask(const simple_mask & m) : mask(m) { copy_from(m); };
00126 const simple_mask & operator = (const simple_mask & m);
00127
00129 bool is_covered(const std::string &expression) const;
00130
00132 mask *clone() const { return new (std::nothrow) simple_mask(*this); };
00133
00134 private :
00135 std::string the_mask;
00136 bool case_s;
00137
00138 void copy_from(const simple_mask & m);
00139 };
00140
00141
00143
00144 class regular_mask : public mask
00145 {
00146 public :
00147
00149
00152 regular_mask(const std::string & wilde_card_expression,
00153 bool x_case_sensit);
00155 regular_mask(const regular_mask & ref);
00157 regular_mask & operator= (const regular_mask & ref);
00158
00160 virtual ~regular_mask() { regfree(&preg); };
00161
00163 bool is_covered(const std::string & expression) const;
00164
00166 mask *clone() const { return new (std::nothrow) regular_mask(*this); };
00167
00168 private :
00169 regex_t preg;
00170 std::string mask_exp;
00171 bool case_sensit;
00172
00173 void set_preg(const std::string & wilde_card_expression,
00174 bool x_case_sensit);
00175 };
00176
00177
00179
00182 class not_mask : public mask
00183 {
00184 public :
00186
00190 not_mask(const mask &m) { copy_from(m); };
00192 not_mask(const not_mask & m) : mask(m) { copy_from(m); };
00194 const not_mask & operator = (const not_mask & m);
00196 ~not_mask() { detruit(); };
00197
00199 bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); };
00200 bool is_covered(const path & chemin) const { return !ref->is_covered(chemin); };
00201
00203 mask *clone() const { return new (std::nothrow) not_mask(*this); };
00204
00205 private :
00206 mask *ref;
00207
00208 void copy_from(const not_mask &m);
00209 void copy_from(const mask &m);
00210 void detruit();
00211 };
00212
00213
00215
00216 class et_mask : public mask
00217 {
00218 public :
00219
00221
00225 et_mask() {};
00227 et_mask(const et_mask &m) : mask(m) { copy_from(m); };
00229 const et_mask & operator = (const et_mask &m);
00231 ~et_mask() { detruit(); };
00232
00233
00235
00239 void add_mask(const mask & toadd);
00240
00242 bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
00243 bool is_covered(const path & chemin) const { return t_is_covered(chemin); };
00244
00246 mask *clone() const { return new (std::nothrow) et_mask(*this); };
00247
00249
00252 U_I size() const { return lst.size(); };
00253
00255
00259 void clear() { detruit(); };
00260
00261 protected :
00262 std::vector<mask *> lst;
00263
00264 private :
00265 void copy_from(const et_mask & m);
00266 void detruit();
00267
00268 template<class T> bool t_is_covered(const T & expression) const
00269 {
00270 std::vector<mask *>::const_iterator it = lst.begin();
00271
00272 if(lst.empty())
00273 throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on"));
00274
00275 while(it != lst.end() && (*it)->is_covered(expression))
00276 ++it;
00277
00278 return it == lst.end();
00279 }
00280
00281 };
00282
00283
00285
00290 class ou_mask : public et_mask
00291 {
00292 public:
00294 bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
00295 bool is_covered(const path & chemin) const { return t_is_covered(chemin); }
00296 ;
00298 mask *clone() const { return new (std::nothrow) ou_mask(*this); };
00299
00300 private:
00301 template<class T> bool t_is_covered(const T & expression) const
00302 {
00303 std::vector<mask *>::const_iterator it = lst.begin();
00304
00305 if(lst.empty())
00306 throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask"));
00307
00308 while(it != lst.end() && ! (*it)->is_covered(expression))
00309 it++;
00310
00311 return it != lst.end();
00312 }
00313
00314 };
00315
00316
00318
00319 class simple_path_mask : public mask
00320 {
00321 public :
00323
00327 simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; };
00328
00330 bool is_covered(const std::string & expression) const { throw SRC_BUG; };
00331 bool is_covered(const path & chemin) const;
00332
00334 mask *clone() const { return new (std::nothrow) simple_path_mask(*this); };
00335
00336 private :
00337 path chemin;
00338 bool case_s;
00339 };
00340
00341
00343
00344 class same_path_mask : public mask
00345 {
00346 public :
00348
00351 same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; };
00352
00354 bool is_covered(const std::string &chemin) const;
00355
00357 mask *clone() const { return new (std::nothrow) same_path_mask(*this); };
00358
00359 private :
00360 std::string chemin;
00361 bool case_s;
00362 };
00363
00364
00366
00367 class exclude_dir_mask : public mask
00368 {
00369 public:
00371
00374 exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;};
00375
00377 bool is_covered(const std::string &expression) const { throw SRC_BUG; }
00378 bool is_covered(const path &chemin) const { return chemin.is_subdir_of(chemin, case_s); };
00379
00381 mask *clone() const { return new (std::nothrow) exclude_dir_mask(*this); };
00382
00383 private:
00384 std::string chemin;
00385 bool case_s;
00386 };
00387
00389
00390 }
00391
00392 #endif