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 : dar.linux@free.fr 00020 /*********************************************************************/ 00021 // $Id: mask.hpp,v 1.17 2005/11/12 17:27:08 edrusb Rel $ 00022 // 00023 /*********************************************************************/ 00024 00030 00031 #ifndef MASK_HPP 00032 #define MASK_HPP 00033 00034 #include "../my_config.h" 00035 00036 extern "C" 00037 { 00038 #if HAVE_UNISTD_H 00039 #include <unistd.h> 00040 #endif 00041 00042 #if HAVE_REGEX_H 00043 #include <regex.h> 00044 #endif 00045 } // end extern "C" 00046 00047 #include <string> 00048 #include <vector> 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 00075 virtual mask *clone() const = 0; 00076 }; 00077 00078 00080 00082 class bool_mask : public mask 00083 { 00084 public : 00086 00090 bool_mask(bool always) { val = always; }; 00091 00093 bool is_covered(const std::string &) const { return val; }; 00095 mask *clone() const { return new bool_mask(val); }; 00096 00097 private : 00098 bool val; 00099 }; 00100 00101 00103 00104 class simple_mask : public mask 00105 { 00106 public : 00107 00109 00112 simple_mask(const std::string & wilde_card_expression, bool case_sensit); 00114 simple_mask(const simple_mask & m) : mask(m) { copy_from(m); }; 00116 simple_mask & operator = (const simple_mask & m); 00118 virtual ~simple_mask() { detruit(); }; 00119 00121 bool is_covered(const std::string &expression) const; 00123 mask *clone() const { return new simple_mask(*this); }; 00124 00125 private : 00126 char *the_mask; 00127 bool case_s; 00128 00129 void copy_from(const simple_mask & m); 00130 void detruit() { if(the_mask != NULL) delete [] the_mask; the_mask = NULL; }; 00131 }; 00132 00133 00135 00136 class regular_mask : public mask 00137 { 00138 public : 00139 00141 00144 regular_mask(const std::string & wilde_card_expression, 00145 bool x_case_sensit); 00147 regular_mask(const regular_mask & ref); 00149 regular_mask & operator= (const regular_mask & ref); 00150 00152 virtual ~regular_mask() { regfree(&preg); }; 00153 00155 bool is_covered(const std::string & expression) const; 00157 mask *clone() const { return new regular_mask(*this); }; 00158 00159 private : 00160 regex_t preg; 00161 std::string mask_exp; //< used only by the copy constructor 00162 bool case_sensit; //< used only by the copy constructor 00163 00164 void set_preg(const std::string & wilde_card_expression, 00165 bool x_case_sensit); 00166 }; 00167 00168 00170 00173 class not_mask : public mask 00174 { 00175 public : 00177 00181 not_mask(const mask &m) { copy_from(m); }; 00183 not_mask(const not_mask & m) : mask(m) { copy_from(m); }; 00185 not_mask & operator = (const not_mask & m); 00187 ~not_mask() { detruit(); }; 00188 00190 bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); }; 00192 mask *clone() const { return new not_mask(*this); }; 00193 00194 private : 00195 mask *ref; 00196 00197 void copy_from(const not_mask &m); 00198 void copy_from(const mask &m); 00199 void detruit(); 00200 }; 00201 00202 00204 00205 class et_mask : public mask 00206 { 00207 public : 00208 00210 00214 et_mask() {}; 00216 et_mask(const et_mask &m) : mask(m) { copy_from(m); }; 00218 et_mask & operator = (const et_mask &m); 00220 ~et_mask() { detruit(); }; 00221 00222 00224 00228 void add_mask(const mask & toadd); 00229 00231 bool is_covered(const std::string & expression) const; 00233 mask *clone() const { return new et_mask(*this); }; 00234 00236 00239 U_I size() const { return lst.size(); }; 00240 00242 00246 void clear() { detruit(); }; 00247 00248 protected : 00249 std::vector<mask *> lst; 00250 00251 private : 00252 void copy_from(const et_mask & m); 00253 void detruit(); 00254 }; 00255 00256 00258 00263 class ou_mask : public et_mask 00264 { 00265 public : 00267 bool is_covered(const std::string & expression) const; 00269 mask *clone() const { return new ou_mask(*this); }; 00270 }; 00271 00272 00274 00275 class simple_path_mask : public mask 00276 { 00277 public : 00279 00283 simple_path_mask(const std::string &p, bool case_sensit) : chemin(p) { case_s = case_sensit; }; 00284 00286 bool is_covered(const std::string &ch) const; 00288 mask *clone() const { return new simple_path_mask(*this); }; 00289 00290 private : 00291 path chemin; 00292 bool case_s; 00293 }; 00294 00295 00297 00298 class same_path_mask : public mask 00299 { 00300 public : 00302 00305 same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; }; 00306 00308 bool is_covered(const std::string &ch) const; 00310 mask *clone() const { return new same_path_mask(*this); }; 00311 00312 private : 00313 std::string chemin; 00314 bool case_s; 00315 }; 00316 00317 00319 00320 class exclude_dir_mask : public mask 00321 { 00322 public: 00324 00327 exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;}; 00328 00330 bool is_covered(const std::string &ch) const { return path(ch).is_subdir_of(chemin, case_s); } 00332 mask *clone() const { return new exclude_dir_mask(*this); }; 00333 00334 private: 00335 std::string chemin; 00336 bool case_s; 00337 }; 00338 00340 00341 } // end of namespace 00342 00343 #endif