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