ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
fileexpressions.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
18#if ALIB_FILES_SCANNER_IMPL == ALIB_FILES_SCANNER_POSIX
19# include <unistd.h>
20#endif
21
22// =========================================== Module ==========================================
23#if ALIB_C20_MODULES
25 import ALib.Lang;
27 import ALib.Strings;
28 import ALib.Strings.Token;
29 import ALib.System;
30 import ALib.Expressions;
31#else
32# include "ALib.Lang.H"
34# include "ALib.Strings.H"
35# include "ALib.Strings.Token.H"
36# include "ALib.System.H"
37# include "ALib.Expressions.H"
38# include "ALib.Files.H"
40#endif
41// ====================================== Implementation =======================================
42using namespace alib::expressions;
43using namespace alib::system;
44
45namespace alib::files {
46
47#if !DOXYGEN
48
49//==================================================================================================
50//=== Anonymous expression functions and constant objects
51//==================================================================================================
52namespace
53{
54 #define ES ExpressionScope
55 #define FS dynamic_cast<FileExpressions::FexScope&>(scope)
56 #define AI ArgIterator
57 #define NODE FS.Node
58 #define VAL FS.Node
59 #define INTARG0 args->Unbox<integer>()
60 Box getType (ES& scope, AI , AI) { return VAL->Type(); }
61 Box isDirectory(ES& scope, AI , AI) { return VAL->IsDirectory(); }
62 Box isSymLink (ES& scope, AI , AI) { return VAL->IsSymbolicLink(); }
63 Box getSize (ES& scope, AI , AI) { return integer(VAL->Size()); }
64 Box getTime (ES& scope, AI , AI) { return VAL->MDate(); }
65 Box getBTime (ES& scope, AI , AI) { return VAL->BDate(); }
66 Box getCTime (ES& scope, AI , AI) { return VAL->CDate(); }
67 Box getATime (ES& scope, AI , AI) { return VAL->ADate(); }
68 Box getPerms (ES& scope, AI , AI) { return VAL->Perms(); }
69 Box getOwner (ES& scope, AI , AI) { return VAL->Owner(); }
70 Box getGroup (ES& scope, AI , AI) { return VAL->Group(); }
71#if ALIB_FILES_SCANNER_IMPL == ALIB_FILES_SCANNER_POSIX
72 Box userID (ES& , AI , AI) { return FInfo::TOwnerAndGroupID(getuid()); }
73 Box groupID (ES& , AI , AI) { return FInfo::TOwnerAndGroupID(getgid()); }
74#else
75 Box userID (ES& , AI , AI) { return FInfo::UnknownID; }
76 Box groupID (ES& , AI , AI) { return FInfo::UnknownID; }
77#endif
78 Box kiloBytes (ES& , AI args, AI) { return INTARG0 * integer(1024); }
79 Box megaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024); }
80 Box gigaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024); }
81 Box teraBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
82 Box petaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
83 Box exaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
84
85
86#if ALIB_PATH_CHARACTERS_WIDE == ALIB_CHARACTERS_WIDE
87 Box getName (ES& scope, AI , AI) { return NODE.Name(); }
88 Box getPath (ES& scope, AI , AI) { return FS.ParentPath; }
89#else
90 // allocate converted name in the scope
91 Box getName (ES& scope, AI , AI) { return String(scope.Allocator, String256(NODE.Name() )); }
92 Box getPath (ES& scope, AI , AI) { return String(scope.Allocator, String256(FS.ParentPath)); }
93#endif
94
95 #undef ES
96 #undef FS
97 #undef AI
98 #undef NODE
99 #undef VAL
100 #undef INTARG0
101
102 extern Box constOwnRead ; Box constOwnRead ;
103 extern Box constOwnWrite ; Box constOwnWrite;
104 extern Box constOwnExec ; Box constOwnExec ;
105 extern Box constGrpRead ; Box constGrpRead ;
106 extern Box constGrpWrite ; Box constGrpWrite;
107 extern Box constGrpExec ; Box constGrpExec ;
108 extern Box constOthRead ; Box constOthRead ;
109 extern Box constOthWrite ; Box constOthWrite;
110 extern Box constOthExec ; Box constOthExec ;
111
112 extern Box constTDirectory ; Box constTDirectory ;
113 extern Box constTSymbolicLinkDir ; Box constTSymbolicLinkDir;
114 extern Box constTRegular ; Box constTRegular ;
115 extern Box constTSymbolicLink ; Box constTSymbolicLink ;
116 extern Box constTBlock ; Box constTBlock ;
117 extern Box constTCharacter ; Box constTCharacter ;
118 extern Box constTFifo ; Box constTFifo ;
119 extern Box constTSocket ; Box constTSocket ;
120
121 extern Box TypeUsrGrpID ; Box TypeUsrGrpID ;
122
123#if ALIB_CHARACTERS_WIDE
124 NString32 TypeNameConverterTFP;
125 NString32 TypeNameConverterTID;
126 NString32 TypeNameConverterTTY;
127 NString32 TypeNameConverterTPT;
128#endif
129} // anonymous namespace
130#endif //!DOXYGEN
131
132//==================================================================================================
133//=== FileExpressions::Plugin
134//==================================================================================================
136: Calculus( "Files Plug-in", pCompiler, expressions::CompilePriorities::Custom )
137{
138 // Initialize constant static boxes. This must not be done in the C++ bootstrap code.
139 constOwnRead = FInfo::Permissions::OWNER_READ ;
140 constOwnWrite = FInfo::Permissions::OWNER_WRITE ;
141 constOwnExec = FInfo::Permissions::OWNER_EXEC ;
142 constGrpRead = FInfo::Permissions::GROUP_READ ;
143 constGrpWrite = FInfo::Permissions::GROUP_WRITE ;
144 constGrpExec = FInfo::Permissions::GROUP_EXEC ;
145 constOthRead = FInfo::Permissions::OTHERS_READ ;
146 constOthWrite = FInfo::Permissions::OTHERS_WRITE;
147 constOthExec = FInfo::Permissions::OTHERS_EXEC ;
148
149 constTDirectory = FInfo::Types::DIRECTORY ;
150 constTSymbolicLinkDir= FInfo::Types::SYMBOLIC_LINK_DIR ;
151 constTRegular = FInfo::Types::REGULAR ;
152 constTSymbolicLink = FInfo::Types::SYMBOLIC_LINK ;
153 constTBlock = FInfo::Types::BLOCK ;
154 constTCharacter = FInfo::Types::CHARACTER ;
155 constTFifo = FInfo::Types::FIFO ;
156 constTSocket = FInfo::Types::SOCKET ;
157
158 TypeUsrGrpID = FInfo::UnknownID;
159
160 // load identifier/function names from resources
161 constexpr int tableSize= 39;
162 Token functionNames[tableSize];
163 strings::util::LoadResourcedTokens( FILES, "CPF", functionNames ALIB_DBG(,tableSize) );
164
165 Token* descriptor= functionNames;
166
167#if ALIB_CHARACTERS_WIDE
168 TypeNameConverterTFP= FILES.GetResource("TFP"); pCompiler.AddType(constOwnRead , TypeNameConverterTFP);
169 TypeNameConverterTID= FILES.GetResource("TID"); pCompiler.AddType(TypeUsrGrpID , TypeNameConverterTID);
170 TypeNameConverterTTY= FILES.GetResource("TTY"); pCompiler.AddType(constTDirectory, TypeNameConverterTTY);
171#else
172 pCompiler.AddType(constOwnRead , FILES.GetResource("TFP"));
173 pCompiler.AddType(TypeUsrGrpID , FILES.GetResource("TID"));
174 pCompiler.AddType(constTDirectory, FILES.GetResource("TTY"));
175#endif
176
177 // Constant identifiers
179 {
180 { *descriptor++, constOwnRead },
181 { *descriptor++, constOwnWrite },
182 { *descriptor++, constOwnExec },
183 { *descriptor++, constGrpRead },
184 { *descriptor++, constGrpWrite },
185 { *descriptor++, constGrpExec },
186 { *descriptor++, constOthRead },
187 { *descriptor++, constOthWrite },
188 { *descriptor++, constOthExec },
189
190 { *descriptor++, constTDirectory },
191 { *descriptor++, constTSymbolicLinkDir },
192 { *descriptor++, constTRegular },
193 { *descriptor++, constTSymbolicLink },
194 { *descriptor++, constTBlock },
195 { *descriptor++, constTCharacter },
196 { *descriptor++, constTFifo },
197 { *descriptor++, constTSocket },
198 };
199
200
201 Functions=
202 {
203 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getName ), &Types::String , ETI },
204 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getType ), &constTDirectory, ETI },
205 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isDirectory), &Types::Boolean , ETI },
206 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isSymLink ), &Types::Boolean , ETI },
207 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getSize ), &Types::Integer , ETI },
208 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
209 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
210 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getBTime ), &Types::DateTime, ETI },
211 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getCTime ), &Types::DateTime, ETI },
212 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getATime ), &Types::DateTime, ETI },
213 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPerms ), &constOwnRead , ETI },
214 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(kiloBytes ), &Types::Integer , CTI },
215 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(megaBytes ), &Types::Integer , CTI },
216 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(gigaBytes ), &Types::Integer , CTI },
217 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(teraBytes ), &Types::Integer , CTI },
218 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(petaBytes ), &Types::Integer , CTI },
219 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(exaBytes ), &Types::Integer , CTI },
220 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getOwner ), &TypeUsrGrpID , ETI },
221 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getGroup ), &TypeUsrGrpID , ETI },
222 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(userID ), &TypeUsrGrpID , ETI },
223 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(groupID ), &TypeUsrGrpID , ETI },
224 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPath ), &Types::String , ETI },
225
226 };
227
228 AutoCasts=
229 {
230 { constOwnRead , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
231 { constTDirectory, nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
232 { TypeUsrGrpID , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
233 };
234
235} // FileExpressions::Plugin constructor
236
237//==================================================================================================
238//=== FileExpressions
239//==================================================================================================
241: compiler()
242, plugin( compiler )
243{
244 compiler.SetupDefaults();
245 compiler.InsertPlugin( &plugin );
246}
247
249{
250 return SPFileFilter(new FileExpressions::Filter( *this, expressionString ));
251}
252
253//==================================================================================================
254//=== FileExpressions::Filter
255//==================================================================================================
257: fex ( pFex )
258, scope( pFex.compiler.CfgFormatter )
259{
260 expression= fex.compiler.Compile( expressionString );
261 if( !expression->ResultType().IsType<bool>() )
262 throw std::runtime_error( "Expression result type mismatch: expecting boolean result!" );
263}
264
265} // namespace [alib::files]
ALIB_DLL void AddType(Type sample, const NString &name)
Definition compiler.cpp:428
uint32_t TOwnerAndGroupID
Type definition for owner and group ids.
Definition finfo.inl:116
static constexpr TOwnerAndGroupID UnknownID
Constant value for owner and group IDs to denote that the field was not determined.
Definition finfo.inl:119
@ DIRECTORY
Directory/folder.
Definition finfo.inl:26
@ CHARACTER
A character special file.
Definition finfo.inl:33
@ BLOCK
A block special file.
Definition finfo.inl:32
@ SOCKET
A socket file.
Definition finfo.inl:35
@ REGULAR
Regular file.
Definition finfo.inl:29
@ FIFO
A FIFO (also known as pipe) file.
Definition finfo.inl:34
@ GROUP_READ
Posix S_IRGRP: The file's user group has read permission.
Definition finfo.inl:100
@ GROUP_EXEC
Posix S_IXGRP: The file's user group has execute/search permission.
Definition finfo.inl:102
@ OTHERS_EXEC
Posix S_IXOTH: Other users have execute/search permission.
Definition finfo.inl:107
@ GROUP_WRITE
Posix S_IWGRP: The file's user group has write permission.
Definition finfo.inl:101
@ OWNER_READ
Posix S_IRUSR: File owner has read permission.
Definition finfo.inl:95
@ OWNER_EXEC
Posix S_IXUSR: File owner has execute/search permission.
Definition finfo.inl:97
@ OWNER_WRITE
Posix S_IWUSR: File owner has write permission.
Definition finfo.inl:96
@ OTHERS_READ
Posix S_IROTH: Other users have read permission.
Definition finfo.inl:105
@ OTHERS_WRITE
Posix S_IWOTH: Other users have write permission.
Definition finfo.inl:106
FileExpressions & fex
The scope used with this filter.
friend class FileExpressions
The outer class is our friend.
Expression expression
The compiled expression.
FileExpressions::FexScope scope
The scope used with this filter.
ALIB_DLL SPFileFilter CreateFilter(const String &expressionString)
ALIB_DLL FileExpressions()
Default constructor.
Plugin plugin
The file expression Plugin.
Compiler compiler
The expression compiler.
#define CALCULUS_DEFAULT_AUTOCAST
#define CALCULUS_CALLBACK(func)
#define CALCULUS_SIGNATURE(BoxPointerArray)
#define ALIB_DBG(...)
Definition alib.inl:836
std::shared_ptr< FFilter > SPFileFilter
A shared pointer to a filter.
Definition ffilter.inl:44
void LoadResourcedTokens(camp::Camp &module, const NString &resourceName, strings::util::Token *target, int dbgSizeVerifier, character outerSeparator=',', character innerSeparator=' ')
strings::util::Token Token
Type alias in namespace alib.
Definition token.inl:401
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
files::FilesCamp FILES
The singleton instance of ALib Camp class FilesCamp.
Definition filescamp.cpp:47
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.inl:574
NLocalString< 32 > NString32
Type alias name for TLocalString<nchar,32>.
static ALIB_DLL Box * I[1]
Function accepts one integral argument.
static ALIB_DLL Box Integer
Sample type-box for integer types. (Precisely for type integer.)
static ALIB_DLL Box Boolean
Sample type-box for C++ type bool.
static ALIB_DLL Box DateTime
Sample type-box for date and time values of type DateTime).
static ALIB_DLL Box String
static constexpr CTInvokable ETI
Definition calculus.inl:239
Calculus(const NString &name, Compiler &compiler, CompilePriorities pPriority)
Definition calculus.inl:251
std::vector< ConstantIdentifierEntry > ConstantIdentifiers
List of identifiers that return constant values to be compiled by this plug-in.
Definition calculus.inl:282
static constexpr CTInvokable CTI
Definition calculus.inl:234
std::vector< AutoCastEntry > AutoCasts
List of auto-casts to be compiled by this plug-in.
Definition calculus.inl:798
std::vector< FunctionEntry > Functions
List of functions to be compiled by this plug-in.
Definition calculus.inl:342