Disk ARchive 2.4.2
|
00001 //*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : http://dar.linux.free.fr/email.html 00020 /*********************************************************************/ 00021 // $Id: mask.hpp,v 1.25 2011/05/20 10:23:07 edrusb Rel $ 00022 // 00023 /*********************************************************************/ 00024 00031 00032 #ifndef MASK_HPP 00033 #define MASK_HPP 00034 00035 #include "../my_config.h" 00036 00037 extern "C" 00038 { 00039 #if HAVE_UNISTD_H 00040 #include <unistd.h> 00041 #endif 00042 00043 #if HAVE_REGEX_H 00044 #include <regex.h> 00045 #endif 00046 } // end extern "C" 00047 00048 #include <string> 00049 #include <vector> 00050 #include "path.hpp" 00051 00052 namespace libdar 00053 { 00054 00057 00059 00062 class mask 00063 { 00064 public : 00065 virtual ~mask() {}; 00066 00068 00072 virtual bool is_covered(const std::string &expression) const = 0; 00073 00075 00080 virtual bool is_covered(const path & chemin) const { return is_covered(chemin.display()); }; 00081 00084 virtual mask *clone() const = 0; 00085 }; 00086 00087 00089 00091 class bool_mask : public mask 00092 { 00093 public : 00095 00099 bool_mask(bool always) { val = always; }; 00100 00102 bool is_covered(const std::string & expression) const { return val; }; 00103 bool is_covered(const path & chemin) const { return val; }; 00104 00106 mask *clone() const { return new bool_mask(val); }; 00107 00108 private : 00109 bool val; 00110 }; 00111 00112 00114 00115 class simple_mask : public mask 00116 { 00117 public : 00118 00120 00123 simple_mask(const std::string & wilde_card_expression, bool case_sensit); 00125 simple_mask(const simple_mask & m) : mask(m) { copy_from(m); }; 00127 const simple_mask & operator = (const simple_mask & m); 00128 00130 bool is_covered(const std::string &expression) const; 00131 00133 mask *clone() const { return new simple_mask(*this); }; 00134 00135 private : 00136 std::string the_mask; 00137 bool case_s; 00138 00139 void copy_from(const simple_mask & m); 00140 }; 00141 00142 00144 00145 class regular_mask : public mask 00146 { 00147 public : 00148 00150 00153 regular_mask(const std::string & wilde_card_expression, 00154 bool x_case_sensit); 00156 regular_mask(const regular_mask & ref); 00158 regular_mask & operator= (const regular_mask & ref); 00159 00161 virtual ~regular_mask() { regfree(&preg); }; 00162 00164 bool is_covered(const std::string & expression) const; 00165 00167 mask *clone() const { return new regular_mask(*this); }; 00168 00169 private : 00170 regex_t preg; 00171 std::string mask_exp; //< used only by the copy constructor 00172 bool case_sensit; //< used only by the copy constructor 00173 00174 void set_preg(const std::string & wilde_card_expression, 00175 bool x_case_sensit); 00176 }; 00177 00178 00180 00183 class not_mask : public mask 00184 { 00185 public : 00187 00191 not_mask(const mask &m) { copy_from(m); }; 00193 not_mask(const not_mask & m) : mask(m) { copy_from(m); }; 00195 const not_mask & operator = (const not_mask & m); 00197 ~not_mask() { detruit(); }; 00198 00200 bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); }; 00201 bool is_covered(const path & chemin) const { return !ref->is_covered(chemin); }; 00202 00204 mask *clone() const { return new not_mask(*this); }; 00205 00206 private : 00207 mask *ref; 00208 00209 void copy_from(const not_mask &m); 00210 void copy_from(const mask &m); 00211 void detruit(); 00212 }; 00213 00214 00216 00217 class et_mask : public mask 00218 { 00219 public : 00220 00222 00226 et_mask() {}; 00228 et_mask(const et_mask &m) : mask(m) { copy_from(m); }; 00230 const et_mask & operator = (const et_mask &m); 00232 ~et_mask() { detruit(); }; 00233 00234 00236 00240 void add_mask(const mask & toadd); 00241 00243 bool is_covered(const std::string & expression) const { return t_is_covered(expression); }; 00244 bool is_covered(const path & chemin) const { return t_is_covered(chemin); }; 00245 00247 mask *clone() const { return new et_mask(*this); }; 00248 00250 00253 U_I size() const { return lst.size(); }; 00254 00256 00260 void clear() { detruit(); }; 00261 00262 protected : 00263 std::vector<mask *> lst; 00264 00265 private : 00266 void copy_from(const et_mask & m); 00267 void detruit(); 00268 00269 template<class T> bool t_is_covered(const T & expression) const 00270 { 00271 std::vector<mask *>::const_iterator it = lst.begin(); 00272 00273 if(lst.empty()) 00274 throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on")); 00275 00276 while(it != lst.end() && (*it)->is_covered(expression)) 00277 ++it; 00278 00279 return it == lst.end(); 00280 } 00281 00282 }; 00283 00284 00286 00291 class ou_mask : public et_mask 00292 { 00293 public: 00295 bool is_covered(const std::string & expression) const { return t_is_covered(expression); }; 00296 bool is_covered(const path & chemin) const { return t_is_covered(chemin); } 00297 ; 00299 mask *clone() const { return new ou_mask(*this); }; 00300 00301 private: 00302 template<class T> bool t_is_covered(const T & expression) const 00303 { 00304 std::vector<mask *>::const_iterator it = lst.begin(); 00305 00306 if(lst.empty()) 00307 throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask")); 00308 00309 while(it != lst.end() && ! (*it)->is_covered(expression)) 00310 it++; 00311 00312 return it != lst.end(); 00313 } 00314 00315 }; 00316 00317 00319 00320 class simple_path_mask : public mask 00321 { 00322 public : 00324 00328 simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; }; 00329 00331 bool is_covered(const std::string & expression) const { throw SRC_BUG; }; 00332 bool is_covered(const path & chemin) const; 00333 00335 mask *clone() const { return new simple_path_mask(*this); }; 00336 00337 private : 00338 path chemin; 00339 bool case_s; 00340 }; 00341 00342 00344 00345 class same_path_mask : public mask 00346 { 00347 public : 00349 00352 same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; }; 00353 00355 bool is_covered(const std::string &chemin) const; 00356 00358 mask *clone() const { return new same_path_mask(*this); }; 00359 00360 private : 00361 std::string chemin; 00362 bool case_s; 00363 }; 00364 00365 00367 00368 class exclude_dir_mask : public mask 00369 { 00370 public: 00372 00375 exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;}; 00376 00378 bool is_covered(const std::string &expression) const { throw SRC_BUG; } 00379 bool is_covered(const path &chemin) const { return chemin.is_subdir_of(chemin, case_s); }; 00380 00382 mask *clone() const { return new exclude_dir_mask(*this); }; 00383 00384 private: 00385 std::string chemin; 00386 bool case_s; 00387 }; 00388 00390 00391 } // end of namespace 00392 00393 #endif