ALib C++ Library
Library Version: 2511 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
24 module ALib.Files.Expressions;
25 import ALib.Lang;
26 import ALib.Characters.Functions;
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>()
60Box getType (ES& scope, AI , AI) { return VAL->Type(); }
61Box isDirectory(ES& scope, AI , AI) { return VAL->IsDirectory(); }
62Box isSymLink (ES& scope, AI , AI) { return VAL->IsSymbolicLink(); }
63Box getSize (ES& scope, AI , AI) { return integer(VAL->Size()); }
64Box getTime (ES& scope, AI , AI) { return VAL->MDate(); }
65Box getBTime (ES& scope, AI , AI) { return VAL->BDate(); }
66Box getCTime (ES& scope, AI , AI) { return VAL->CDate(); }
67Box getATime (ES& scope, AI , AI) { return VAL->ADate(); }
68Box getPerms (ES& scope, AI , AI) { return VAL->Perms(); }
69Box getOwner (ES& scope, AI , AI) { return VAL->Owner(); }
70Box getGroup (ES& scope, AI , AI) { return VAL->Group(); }
71#if ALIB_FILES_SCANNER_IMPL == ALIB_FILES_SCANNER_POSIX
72Box userID (ES& , AI , AI) { return FInfo::TOwnerAndGroupID(getuid()); }
73Box 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
78Box kiloBytes (ES& , AI args, AI) { return INTARG0 * integer(1024); }
79Box megaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024); }
80Box gigaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024); }
81Box teraBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
82Box petaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
83Box 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
87Box getName (ES& scope, AI , AI) { return NODE.Name(); }
88Box 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} // anonymous namespace
124#endif //!DOXYGEN
125
126 //================================================================================================
127 //=== FileExpressions::Plugin
128 //================================================================================================
130: Calculus( "Files Plug-in", pCompiler, expressions::CompilePriorities::Custom ) {
131 // Initialize constant static boxes. This must not be done in the C++ bootstrap code.
132 constOwnRead = FInfo::Permissions::OWNER_READ ;
133 constOwnWrite = FInfo::Permissions::OWNER_WRITE ;
134 constOwnExec = FInfo::Permissions::OWNER_EXEC ;
135 constGrpRead = FInfo::Permissions::GROUP_READ ;
136 constGrpWrite = FInfo::Permissions::GROUP_WRITE ;
137 constGrpExec = FInfo::Permissions::GROUP_EXEC ;
138 constOthRead = FInfo::Permissions::OTHERS_READ ;
139 constOthWrite = FInfo::Permissions::OTHERS_WRITE;
140 constOthExec = FInfo::Permissions::OTHERS_EXEC ;
141
142 constTDirectory = FInfo::Types::DIRECTORY ;
143 constTSymbolicLinkDir= FInfo::Types::SYMBOLIC_LINK_DIR ;
144 constTRegular = FInfo::Types::REGULAR ;
145 constTSymbolicLink = FInfo::Types::SYMBOLIC_LINK ;
146 constTBlock = FInfo::Types::BLOCK ;
147 constTCharacter = FInfo::Types::CHARACTER ;
148 constTFifo = FInfo::Types::FIFO ;
149 constTSocket = FInfo::Types::SOCKET ;
150
151 TypeUsrGrpID = FInfo::UnknownID;
152
153 // load identifier/function names from resources
154 constexpr int tableSize= 39;
155 Token functionNames[tableSize];
156 strings::util::LoadResourcedTokens( FILES, "CPF", functionNames ALIB_DBG(,tableSize) );
157
158 Token* descriptor= functionNames;
159
160#if ALIB_CHARACTERS_WIDE
161 NString256 converter;
162 converter.Reset( FILES.GetResource("TFP") ); pCompiler.AddType(constOwnRead , NString(pCompiler.GetAllocator(), converter) );
163 converter.Reset( FILES.GetResource("TID") ); pCompiler.AddType(TypeUsrGrpID , NString(pCompiler.GetAllocator(), converter) );
164 converter.Reset( FILES.GetResource("TTY") ); pCompiler.AddType(constTDirectory, NString(pCompiler.GetAllocator(), converter) );
165#else
166 pCompiler.AddType(constOwnRead , FILES.GetResource("TFP"));
167 pCompiler.AddType(TypeUsrGrpID , FILES.GetResource("TID"));
168 pCompiler.AddType(constTDirectory, FILES.GetResource("TTY"));
169#endif
170
171 // Constant identifiers
173 {
174 { *descriptor++, constOwnRead },
175 { *descriptor++, constOwnWrite },
176 { *descriptor++, constOwnExec },
177 { *descriptor++, constGrpRead },
178 { *descriptor++, constGrpWrite },
179 { *descriptor++, constGrpExec },
180 { *descriptor++, constOthRead },
181 { *descriptor++, constOthWrite },
182 { *descriptor++, constOthExec },
183
184 { *descriptor++, constTDirectory },
185 { *descriptor++, constTSymbolicLinkDir },
186 { *descriptor++, constTRegular },
187 { *descriptor++, constTSymbolicLink },
188 { *descriptor++, constTBlock },
189 { *descriptor++, constTCharacter },
190 { *descriptor++, constTFifo },
191 { *descriptor++, constTSocket },
192 };
193
194
195 Functions=
196 {
197 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getName ), &Types::String , ETI },
198 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getType ), &constTDirectory, ETI },
199 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isDirectory), &Types::Boolean , ETI },
200 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isSymLink ), &Types::Boolean , ETI },
201 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getSize ), &Types::Integer , ETI },
202 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
203 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
204 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getBTime ), &Types::DateTime, ETI },
205 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getCTime ), &Types::DateTime, ETI },
206 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getATime ), &Types::DateTime, ETI },
207 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPerms ), &constOwnRead , ETI },
208 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(kiloBytes ), &Types::Integer , CTI },
209 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(megaBytes ), &Types::Integer , CTI },
210 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(gigaBytes ), &Types::Integer , CTI },
211 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(teraBytes ), &Types::Integer , CTI },
212 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(petaBytes ), &Types::Integer , CTI },
213 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(exaBytes ), &Types::Integer , CTI },
214 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getOwner ), &TypeUsrGrpID , ETI },
215 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getGroup ), &TypeUsrGrpID , ETI },
216 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(userID ), &TypeUsrGrpID , ETI },
217 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(groupID ), &TypeUsrGrpID , ETI },
218 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPath ), &Types::String , ETI },
219
220 };
221
222 AutoCasts=
223 {
224 { constOwnRead , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
225 { constTDirectory, nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
226 { TypeUsrGrpID , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
227 };
228
229} // FileExpressions::Plugin constructor
230
231//==================================================================================================
232//=== FileExpressions
233//==================================================================================================
235: plugin( compiler ) { compiler.SetupDefaults(); compiler.InsertPlugin( &plugin ); }
236
238{ return SPFileFilter(new FileExpressions::Filter( *this, expressionString )); }
239
240//==================================================================================================
241//=== FileExpressions::Filter
242//==================================================================================================
244: fex ( pFex )
245, scope( pFex.compiler.CfgFormatter ) {
246 expression= fex.compiler.Compile( expressionString );
247 if( !expression->ResultType().IsType<bool>() )
248 throw std::runtime_error( "Expression result type mismatch: expecting boolean result!" );
249}
250
251} // namespace [alib::files]
ALIB_DLL void AddType(Type sample, const NString &name)
Definition compiler.cpp:408
MonoAllocator & GetAllocator()
Definition compiler.inl:470
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:853
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:398
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2198
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1149
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:2189
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.inl:540
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256>.
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:249
std::vector< ConstantIdentifierEntry > ConstantIdentifiers
List of identifiers that return constant values to be compiled by this plug-in.
Definition calculus.inl:277
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:763
std::vector< FunctionEntry > Functions
List of functions to be compiled by this plug-in.
Definition calculus.inl:337