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