ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
fileexpressions.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_files of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_FILES_FILEEXPRESSIONS
9#define HPP_ALIB_FILES_FILEEXPRESSIONS 1
10#pragma once
11#include "alib/files/ftree.hpp"
13
14ALIB_ASSERT_MODULE(EXPRESSIONS)
15#include "alib/expressions/plugins/calculus.hpp"
16
17namespace alib{ namespace files {
18
19/// This class implements an \ref alib_mod_expressions "ALib Expression Compiler Plugin"
20/// and thus enables "run-time expressions" to work with nodes of \alib{files;FTree}
21///
22/// Unless multithreaded use is planned, an application should create one singleton of this class.
23///
24/// Method #CreateFilter creates an object of inner type \alib{files;FileExpressions::Filter}, which
25/// implements the \b FFilter interface used with scan parameters of function
26/// \alib{files;ScanFiles}. This allows run-time evaluation of filter rules, for example rules
27/// coming from the command line, from configuration files or from interactive end-user input
28/// (hence from an UX/UI).
29///
30/// Of course the expression compiler can also be used directly without using the inner
31/// \b Filter class for evaluation and expressions do not need return a boolean "yes/no" result.
32///
33/// @see
34/// For a quick tutorial, see the \ref alib_mod_files "Programmer's Manual".
35///
36/// # Implemented Expression Features #
37/// %Compiler plug-in that provides expressions on filesystem entries, stored in nodes of
38/// class \alib{files;FTree}.
39///
40/// All identifier and function names are defined case insensitive.
41/// Furthermore, the tokens are not hard-coded but resourced with \alibcamp singleton
42/// \ref alib::FILES. With that, for example language translations might be performed.
43///
44/// ## Types: ##
45/// <br>
46/// This plug-in introduces the following types to the expression compiler:
47/// - \alib{files;FInfo::Types}
48/// - \alib{files;FInfo::Permissions}
49/// - \alib{files;FInfo::TOwnerAndGroupID}
50///
51/// Each types are auto-cast to \b Integer to allow all common operators, especially bitwise
52/// boolean operators.
53///
54/// ## Constants: ##
55/// <br>
56/// Type | Name |Min. Abbreviation| Description
57/// --------------------------------|------------------|-----------------|-------------
58/// \alib{files::FInfo;Permissions} |\b OwnerRead | ore | Used test result of function \p{Permission}.
59/// \alib{files::FInfo;Permissions} |\b OwnerWrite | ow | Used test result of function \p{Permission}.
60/// \alib{files::FInfo;Permissions} |\b OwnerExecute | oe | Used test result of function \p{Permission}.
61/// \alib{files::FInfo;Permissions} |\b GroupRead | gr | Used test result of function \p{Permission}.
62/// \alib{files::FInfo;Permissions} |\b GroupWrite | gw | Used test result of function \p{Permission}.
63/// \alib{files::FInfo;Permissions} |\b GroupExecute | ge | Used test result of function \p{Permission}.
64/// \alib{files::FInfo;Permissions} |\b OthersRead | otr | Used test result of function \p{Permission}.
65/// \alib{files::FInfo;Permissions} |\b OthersWrite | otw | Used test result of function \p{Permission}.
66/// \alib{files::FInfo;Permissions} |\b OthersExecute | ote | Used test result of function \p{Permission}.
67/// \alib{files::FInfo;Types} |\b Directory | dir | Used to compare the result of function \p{Type}.
68/// \alib{files::FInfo;Types} |\b SymbolicLinkDir| sld | Used to compare the result of function \p{Type}.
69/// \alib{files::FInfo;Types} |\b Regular | reg | Used to compare the result of function \p{Type}.
70/// \alib{files::FInfo;Types} |\b SymbolicLink | sl | Used to compare the result of function \p{Type}.
71/// \alib{files::FInfo;Types} |\b Block | block | Used to compare the result of function \p{Type}.
72/// \alib{files::FInfo;Types} |\b Character | character | Used to compare the result of function \p{Type}.
73/// \alib{files::FInfo;Types} |\b Fifo | fifo | Used to compare the result of function \p{Type}.
74/// \alib{files::FInfo;Types} |\b Socket | socket | Used to compare the result of function \p{Type}.
75///
76///
77/// <br>
78/// ## Constant Constructor Functions: ##
79/// <br>
80/// Return Type| Name |Min. Abbreviation| Signature | Description |
81/// -----------|-------------|-----------------|-----------|-------------|
82/// Integer |\b KiloBytes | KB | int | Returns the given number multiplied with 2^10.
83/// Integer |\b MegaBytes | MB | int | Returns the given number multiplied with 2^20.
84/// Integer |\b GigaBytes | GB | int | Returns the given number multiplied with 2^30.
85/// Integer |\b TeraBytes | TB | int | Returns the given number multiplied with 2^40.
86/// Integer |\b PetaBytes | PB | int | Returns the given number multiplied with 2^50.
87/// Integer |\b ExaBytes | EB | int | Returns the given number multiplied with 2^60.
88///
89/// <br>
90/// ## Functions: ##
91/// <br>
92///
93/// Return Type | Name | Min. Abbreviation | Signature | Description |
94/// ----------------------------------- | ----------- | ------------------| -----------| ------------- |
95/// String | \b Name | name | ./. | Returns the file name.
96/// \alib{files::FInfo;Types} | \b Type | ty | ./. | Returns the result of \alib{files::FInfo;Type}.
97/// Boolean | \b IsDirectory | isdir | ./. | Returns the result of \alib{files::FInfo;IsDirectory}.
98/// Boolean | \b IsSymbolicLink| issl | ./. | Returns the result of \alib{files::FInfo;IsSymbolicLink}.
99/// Integer | \b Size | size | ./. | Returns the result of \alib{files::FInfo;Size}.
100/// DateTime | \b Date | timestamp | ./. | Returns the result of \alib{files::FInfo;MDate}.
101/// DateTime | \b MDate | md | ./. | Returns the result of \alib{files::FInfo;MDate}.
102/// DateTime | \b BDate | bd | ./. | Returns the result of \alib{files::FInfo;BDate}.
103/// DateTime | \b CDate | cd | ./. | Returns the result of \alib{files::FInfo;CDate}.
104/// DateTime | \b ATime | ad | ./. | Returns the result of \alib{files::FInfo;ADate}.
105/// \alib{files::FInfo;Permissions} | \b Permissions | perm | ./. | Returns the result of \alib{files::FInfo;Permissions}.
106/// \alib{files::FInfo;TOwnerAndGroupID}| \b Owner | owner | ./. | Returns the result of \alib{files::FInfo;Owner}.
107/// \alib{files::FInfo;TOwnerAndGroupID}| \b Group | group | ./. | Returns the result of \alib{files::FInfo;Group}.
108/// \alib{files::FInfo;TOwnerAndGroupID}| \b UserID | uid | ./. | Returns the current user's ID (calls posix <b>getuid()</b>).
109/// \alib{files::FInfo;TOwnerAndGroupID}| \b GroupID | gid | ./. | Returns the current user's group ID (calls posix <b>getgid()</b>).
110///
112{
113 protected:
114 /// The internal compiler plug-in which adds file-related functions to the expression
115 /// compiler as documented with the outer class.
117 {
118 /// Constructor
119 /// @param compiler The compiler that this plugin will be attached to.
121 };
122
123 public:
124 /// The expression scope used with file expressions. Contains an \b FTree cursor and
125 /// the string of the path the file or folder is located in.
127 {
128 File Node; ///< A node of a \alib{files;FTree}.
129 lang::system::PathString ParentPath; ///< The absolute path to the parent directory.
130
131 /// Constructor. Passes the formatter to the parent constructor.
132 /// @param formatter A reference to a \c std::shared_ptr holding a formatter.
133 /// Usually \alib{expressions;Compiler::CfgFormatter}.
134 FexScope( SPFormatter& formatter )
135 : expressions::Scope(formatter) {}
136 };
137
138 /// Implementation of abstract class \alib{files;FFilter} which uses run-time
139 /// compiled and evaluated expressions to determine inclusion of files or directories.
140 /// Instances of this type can be created with outer classes' method
141 /// \alib{files;FileExpressions::CreateFilter} and for example be attached to fields
142 /// \alib{files::ScanParameters;FileFilter},
143 /// \alib{files::ScanParameters;DirectoryFilterPreRecursion} and
144 /// \alib{files::ScanParameters;DirectoryFilterPostRecursion} of class
145 /// \b ScanParameters.
146 class Filter : public FFilter
147 {
148 protected:
149 friend class FileExpressions; ///< The outer class is our friend.
150 FileExpressions& fex; ///< The scope used with this filter.
151 FileExpressions::FexScope scope; ///< The scope used with this filter.
152 Expression expression; ///< The compiled expression.
153
154
155 /// Protected constructor. Compiles the given expression.
156 /// To create an instance of this class, use
157 /// \alib{files;FileExpressions::CreateFilter}.
158 /// @param pFex The expression compiler to use.
159 /// @param expressionString The expression string.
161 Filter( FileExpressions& pFex, const String& expressionString );
162
163
164 public:
165 /// The filter method.
166 /// @param node The node in the file-tree to examine.
167 /// @param parentPath The absolute path to the parent directory that the file resides in.
168 /// @return The result determines, whether the given file or directory has passed the filter
169 /// or not. Consequently, \c true means "passed" and \c false means
170 /// "filtered out".
171 virtual bool Includes( const File& node, const lang::system::PathString& parentPath) override
172 {
173 scope.Node = node;
174 scope.ParentPath = parentPath;
175 return expression->Evaluate( scope ).Unbox<bool>();
176 }
177 }; // class Filter
178
179
180 Compiler compiler; ///< The expression compiler.
181 Plugin plugin; ///< The file expression Plugin
182
183
184 /// Default constructor.
187
188 /// Creates a file filter using the expression compiler of this instance.
189 /// @param expressionString The expression string to filter files and directories.
190 /// @return A shared pointer to the dynamically allocated filter object.
192 SPFileFilter CreateFilter( const String& expressionString );
193
194}; // class FileExpressions
195
196} // namespace alib[::files]
197
198
199/// Type alias in namespace \b alib.
201
202} // namespace [alib]
203
204#endif // HPP_ALIB_FILES_FILEEXPRESSIONS
205
ALIB_API Filter(FileExpressions &pFex, const String &expressionString)
FileExpressions & fex
The scope used with this filter.
FileExpressions::FexScope scope
The scope used with this filter.
Expression expression
The compiled expression.
virtual bool Includes(const File &node, const lang::system::PathString &parentPath) override
Plugin plugin
The file expression Plugin.
Compiler compiler
The expression compiler.
ALIB_API FileExpressions()
Default constructor.
ALIB_API SPFileFilter CreateFilter(const String &expressionString)
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_API
Definition alib.hpp:639
std::shared_ptr< FFilter > SPFileFilter
A shared pointer to a filter.
Definition ffilter.hpp:49
Definition alib.cpp:69
lang::system::PathString ParentPath
The absolute path to the parent directory.
Plugin(Compiler &compiler)
DOXYGEN.