Loading...
Searching...
No Matches
mask.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2026 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef MASK_HPP
#define MASK_HPP
#include "../my_config.h"
extern "C"
{
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_REGEX_H
#include <regex.h>
#endif
} // end extern "C"
#include <string>
#include <deque>
#include "erreurs.hpp"
#include "path.hpp"
namespace libdar
{
class mask
{
public :
mask() {};
mask(const mask & ref) = default;
mask(mask && ref) noexcept = default;
mask & operator = (const mask & ref) = default;
mask & operator = (mask && ref) noexcept = default;
virtual ~mask() = default;
virtual bool is_covered(const std::string &expression) const = 0;
virtual bool is_covered(const path & chemin) const { return is_covered(chemin.display()); };
virtual std::string dump(const std::string & prefix = "") const = 0;
virtual mask *clone() const = 0;
};
class bool_mask : public mask
{
public :
bool_mask(bool always) { val = always; };
bool_mask(const bool_mask & ref) = default;
bool_mask(bool_mask && ref) noexcept = default;
bool_mask & operator = (const bool_mask & ref) = default;
bool_mask & operator = (bool_mask && ref) noexcept = default;
~bool_mask() = default;
bool is_covered(const std::string & expression) const override { return val; };
bool is_covered(const path & chemin) const override { return val; };
std::string dump(const std::string & prefix) const override { return prefix + (val ? gettext("TRUE") : gettext("FALSE")); };
mask *clone() const override { return new (std::nothrow) bool_mask(val); };
private :
bool val;
};
class simple_mask : public mask
{
public :
simple_mask(const std::string & wilde_card_expression, bool case_sensit);
simple_mask(const simple_mask & m) = default;
simple_mask(simple_mask && ref) noexcept = default;
simple_mask & operator = (const simple_mask & m) = default;
simple_mask & operator = (simple_mask && ref) noexcept = default;
~simple_mask() = default;
bool is_covered(const std::string &expression) const override;
std::string dump(const std::string & prefix) const override;
mask *clone() const override { return new (std::nothrow) simple_mask(*this); };
private :
std::string the_mask;
bool case_s;
};
class regular_mask : public mask
{
public :
regular_mask(const std::string & wilde_card_expression,
bool x_case_sensit);
regular_mask(const regular_mask & ref): mask(ref) { copy_from(ref); };
regular_mask(regular_mask && ref) noexcept: mask(std::move(ref)) { move_from(std::move(ref)); };
regular_mask & operator = (const regular_mask & ref);
regular_mask & operator = (regular_mask && ref) noexcept;
virtual ~regular_mask() { detruit(); };
bool is_covered(const std::string & expression) const override;
std::string dump(const std::string & prefix) const override;
mask *clone() const override { return new (std::nothrow) regular_mask(*this); };
private :
regex_t preg;
std::string mask_exp;
bool case_sensit;
void set_preg(const std::string & wilde_card_expression,
bool x_case_sensit);
void copy_from(const regular_mask & ref);
void move_from(regular_mask && ref) noexcept;
void detruit() noexcept { regfree(&preg); };
};
class not_mask : public mask
{
public :
not_mask(const mask &m) { copy_from(m); };
not_mask(const not_mask & m) : mask(m) { copy_from(m); };
not_mask(not_mask && m) noexcept: mask(std::move(m)) { nullifyptr(); move_from(std::move(m)); };
not_mask & operator = (const not_mask & m);
not_mask & operator = (not_mask && m) noexcept { move_from(std::move(m)); return *this; };
~not_mask() { detruit(); };
bool is_covered(const std::string &expression) const override { return !ref->is_covered(expression); };
bool is_covered(const path & chemin) const override { return !ref->is_covered(chemin); };
std::string dump(const std::string & prefix) const override;
mask *clone() const override { return new (std::nothrow) not_mask(*this); };
private :
mask *ref;
void nullifyptr() noexcept { ref = nullptr; };
void copy_from(const not_mask &m);
void copy_from(const mask &m);
void move_from(not_mask && ref) noexcept;
void detruit();
};
class et_mask : public mask
{
public :
et_mask() {}; // field "lst" is an object and initialized by its default constructor
et_mask(const et_mask &m) : mask(m) { copy_from(m); };
et_mask(et_mask && m) noexcept: mask(std::move(m)) { move_from(std::move(m)); };
et_mask & operator = (const et_mask &m);
et_mask & operator = (et_mask && m) noexcept { mask::operator = (std::move(m)); move_from(std::move(m)); return *this; };
~et_mask() { detruit(); };
void add_mask(const mask & toadd);
bool is_covered(const std::string & expression) const override { return t_is_covered(expression); };
bool is_covered(const path & chemin) const override { return t_is_covered(chemin); };
std::string dump(const std::string & prefix) const override { return dump_logical(prefix, gettext("AND")); };
mask *clone() const override { return new (std::nothrow) et_mask(*this); };
U_I size() const { return lst.size(); };
const mask* get_added(U_I index) const { return lst[index]; };
void clear() { detruit(); };
protected :
std::deque<mask *> lst;
std::string dump_logical(const std::string & prefix, const std::string & boolop) const;
private :
void copy_from(const et_mask & m);
void move_from(et_mask && m) noexcept;
void detruit();
template<class T> bool t_is_covered(const T & expression) const
{
std::deque<mask *>::const_iterator it = lst.begin();
if(lst.empty())
throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on"));
while(it != lst.end() && (*it)->is_covered(expression))
++it;
return it == lst.end();
}
};
class ou_mask : public et_mask
{
public:
ou_mask() {};
ou_mask(const ou_mask & ref) = default;
ou_mask(ou_mask && ref) noexcept = default;
ou_mask & operator = (const ou_mask & ref) = default;
ou_mask & operator = (ou_mask && ref) noexcept = default;
~ou_mask() = default;
bool is_covered(const std::string & expression) const override { return t_is_covered(expression); };
bool is_covered(const path & chemin) const override { return t_is_covered(chemin); };
std::string dump(const std::string & prefix) const override { return dump_logical(prefix, gettext("OR")); };
mask *clone() const override { return new (std::nothrow) ou_mask(*this); };
private:
template<class T> bool t_is_covered(const T & expression) const
{
std::deque<mask *>::const_iterator it = lst.begin();
if(lst.empty())
throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask"));
while(it != lst.end() && ! (*it)->is_covered(expression))
it++;
return it != lst.end();
}
};
class simple_path_mask : public mask
{
public :
simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; };
simple_path_mask(const simple_path_mask & ref) = default;
simple_path_mask(simple_path_mask && ref) noexcept = default;
simple_path_mask & operator = (const simple_path_mask & ref) = default;
simple_path_mask & operator = (simple_path_mask && ref) noexcept = default;
~simple_path_mask() = default;
bool is_covered(const std::string & expression) const override { throw SRC_BUG; };
bool is_covered(const path & chemin) const override;
std::string dump(const std::string & prefix) const override;
mask *clone() const override { return new (std::nothrow) simple_path_mask(*this); };
private :
path chemin;
bool case_s;
};
class same_path_mask : public mask
{
public :
same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; };
same_path_mask(const same_path_mask & ref) = default;
same_path_mask(same_path_mask && ref) noexcept = default;
same_path_mask & operator = (const same_path_mask & ref) = default;
same_path_mask & operator = (same_path_mask && ref) noexcept = default;
~same_path_mask() = default;
bool is_covered(const std::string &chemin) const override;
std::string dump(const std::string & prefix) const override;
mask *clone() const override { return new (std::nothrow) same_path_mask(*this); };
private :
std::string chemin;
bool case_s;
};
class exclude_dir_mask : public mask
{
public:
exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;};
exclude_dir_mask(const exclude_dir_mask & ref) = default;
exclude_dir_mask(exclude_dir_mask && ref) noexcept = default;
exclude_dir_mask & operator = (const exclude_dir_mask & ref) = default;
exclude_dir_mask & operator = (exclude_dir_mask && ref) noexcept = default;
~exclude_dir_mask() = default;
bool is_covered(const std::string &expression) const override { throw SRC_BUG; }
bool is_covered(const path &chemin) const override { return chemin.is_subdir_of(chemin, case_s); };
std::string dump(const std::string & prefix) const override;
mask *clone() const override { return new (std::nothrow) exclude_dir_mask(*this); };
private:
std::string chemin;
bool case_s;
};
} // end of namespace
#endif
libdar::Erange
exception used to signal range error
Definition erreurs.hpp:220
libdar::bool_mask
boolean mask, either always true or false
Definition mask.hpp:101
libdar::bool_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:121
libdar::bool_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
Definition mask.hpp:116
libdar::bool_mask::dump
std::string dump(const std::string &prefix) const override
dump in human readable form the nature of the mask
Definition mask.hpp:118
libdar::bool_mask::is_covered
bool is_covered(const path &chemin) const override
check whether the given path is covered by the mask
Definition mask.hpp:117
libdar::bool_mask::bool_mask
bool_mask(bool always)
the constructor
Definition mask.hpp:108
libdar::et_mask
makes an AND operator between two or more masks
Definition mask.hpp:273
libdar::et_mask::is_covered
bool is_covered(const path &chemin) const override
check whether the given path is covered by the mask
Definition mask.hpp:308
libdar::et_mask::operator=
et_mask & operator=(const et_mask &m)
assignment operator
libdar::et_mask::add_mask
void add_mask(const mask &toadd)
add a mask to the operator
libdar::et_mask::clear
void clear()
clear the mask
Definition mask.hpp:328
libdar::et_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
Definition mask.hpp:307
libdar::et_mask::dump
std::string dump(const std::string &prefix) const override
dump in human readable form the nature of the mask
Definition mask.hpp:309
libdar::et_mask::et_mask
et_mask(const et_mask &m)
copy constructor
Definition mask.hpp:284
libdar::et_mask::et_mask
et_mask(et_mask &&m) noexcept
move constructor
Definition mask.hpp:287
libdar::et_mask::get_added
const mask * get_added(U_I index) const
return a pointer by index to composing mask (index start at zero and should be strictly less than wha...
Definition mask.hpp:321
libdar::et_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:312
libdar::et_mask::~et_mask
~et_mask()
destructor
Definition mask.hpp:296
libdar::et_mask::et_mask
et_mask()
the constructor to be used by libdar external programs
Definition mask.hpp:281
libdar::et_mask::size
U_I size() const
the number of mask on which is done the AND operator
Definition mask.hpp:318
libdar::exclude_dir_mask
matches if string is the given constructor string or a sub directory of it
Definition mask.hpp:461
libdar::exclude_dir_mask::dump
std::string dump(const std::string &prefix) const override
dump in human readable form the nature of the mask
libdar::exclude_dir_mask::exclude_dir_mask
exclude_dir_mask(const std::string &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition mask.hpp:467
libdar::exclude_dir_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:480
libdar::exclude_dir_mask::is_covered
bool is_covered(const path &chemin) const override
check whether the given path is covered by the mask
Definition mask.hpp:476
libdar::exclude_dir_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
Definition mask.hpp:475
libdar::mask
the generic class, parent of all masks
Definition mask.hpp:62
libdar::mask::is_covered
virtual bool is_covered(const std::string &expression) const =0
check wether the given string is covered by the mask
libdar::mask::clone
virtual mask * clone() const =0
libdar::mask::dump
virtual std::string dump(const std::string &prefix="") const =0
dump in human readable form the nature of the mask
libdar::mask::is_covered
virtual bool is_covered(const path &chemin) const
check whether the given path is covered by the mask
Definition mask.hpp:84
libdar::not_mask::not_mask
not_mask(const not_mask &m)
copy constructor
Definition mask.hpp:237
libdar::not_mask::dump
std::string dump(const std::string &prefix) const override
dump in human readable form the nature of the mask
libdar::not_mask::not_mask
not_mask(not_mask &&m) noexcept
move constructor
Definition mask.hpp:240
libdar::not_mask::operator=
not_mask & operator=(const not_mask &m)
assignment operator
libdar::not_mask::is_covered
bool is_covered(const path &chemin) const override
check whether the given path is covered by the mask
Definition mask.hpp:253
libdar::not_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
Definition mask.hpp:252
libdar::not_mask::not_mask
not_mask(const mask &m)
the constructor to be used by libdar external programs
Definition mask.hpp:234
libdar::not_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:257
libdar::not_mask::~not_mask
~not_mask()
destructor
Definition mask.hpp:249
libdar::ou_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
Definition mask.hpp:373
libdar::ou_mask::dump
std::string dump(const std::string &prefix) const override
dump in human readable form the nature of the mask
Definition mask.hpp:375
libdar::ou_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:377
libdar::ou_mask::is_covered
bool is_covered(const path &chemin) const override
check whether the given path is covered by the mask
Definition mask.hpp:374
libdar::path
the class path is here to manipulate paths in the Unix notation: using'/'
Definition path.hpp:51
libdar::path::display
std::string display() const
convert back a path to a string
libdar::regular_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:206
libdar::regular_mask::regular_mask
regular_mask(const std::string &wilde_card_expression, bool x_case_sensit)
the constructor to be used by libdar external programs
libdar::regular_mask::dump
std::string dump(const std::string &prefix) const override
inherited from the mask class
libdar::regular_mask::mask_exp
std::string mask_exp
used only by the copy constructor
Definition mask.hpp:210
libdar::regular_mask::regular_mask
regular_mask(regular_mask &&ref) noexcept
the move constructor
Definition mask.hpp:188
libdar::regular_mask::~regular_mask
virtual ~regular_mask()
destructor
Definition mask.hpp:197
libdar::regular_mask::operator=
regular_mask & operator=(const regular_mask &ref)
the assignment operator
libdar::regular_mask::regular_mask
regular_mask(const regular_mask &ref)
the copy constructor
Definition mask.hpp:185
libdar::regular_mask::case_sensit
bool case_sensit
used only by the copy constructor
Definition mask.hpp:211
libdar::regular_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
libdar::same_path_mask
matches if string is exactly the given mask (no wilde card expression)
Definition mask.hpp:430
libdar::same_path_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:450
libdar::same_path_mask::is_covered
bool is_covered(const std::string &chemin) const override
inherited from the mask class
libdar::same_path_mask::same_path_mask
same_path_mask(const std::string &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition mask.hpp:436
libdar::same_path_mask::dump
std::string dump(const std::string &prefix) const override
inherited from the mask class
libdar::simple_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
libdar::simple_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:163
libdar::simple_mask::operator=
simple_mask & operator=(const simple_mask &m)=default
assignment operator
libdar::simple_mask::simple_mask
simple_mask(const std::string &wilde_card_expression, bool case_sensit)
the constructor to use by libdar external programs
libdar::simple_mask::~simple_mask
~simple_mask()=default
default destructor
libdar::simple_mask::simple_mask
simple_mask(simple_mask &&ref) noexcept=default
move constructor
libdar::simple_mask::dump
std::string dump(const std::string &prefix) const override
inherited from the mask class
libdar::simple_mask::simple_mask
simple_mask(const simple_mask &m)=default
copy constructor
libdar::simple_path_mask
string matches if it is subdir of mask or mask is a subdir of expression
Definition mask.hpp:399
libdar::simple_path_mask::clone
mask * clone() const override
inherited from the mask class
Definition mask.hpp:419
libdar::simple_path_mask::dump
std::string dump(const std::string &prefix) const override
dump in human readable form the nature of the mask
libdar::simple_path_mask::simple_path_mask
simple_path_mask(const path &p, bool case_sensit)
the constructor to be used by libdar external programs
Definition mask.hpp:406
libdar::simple_path_mask::is_covered
bool is_covered(const std::string &expression) const override
inherited from the mask class
Definition mask.hpp:414
libdar::simple_path_mask::is_covered
bool is_covered(const path &chemin) const override
check whether the given path is covered by the mask
erreurs.hpp
contains all the excetion class thrown by libdar
libdar::dar_gettext
const char * dar_gettext(const char *)
a routine to change NLS domaine forth and back for inline routines
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47
path.hpp
here is the definition of the path class