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