ALib C++ Library
Library Version: 2402 R1
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// #################################################################################################
8
9#if !defined(HPP_ALIB_FILES_FILEEXPRESSIONS)
11#endif
12
14
15
16#if ( defined(__GLIBCXX__) \
17 || defined(__APPLE__) \
18 || defined(__ANDROID_NDK__) ) && !defined(ALIB_TESTSTDFS)
19# include <unistd.h>
20#endif
21
22using namespace alib::expressions;
23
24///////////////////////////////////// Expressions ///////////////////////////////////////////////////
25namespace alib::files {
26
27#if !defined(ALIB_DOX)
28
29//==================================================================================================
30//=== Anonymous expression functions and constant objects
31//==================================================================================================
32namespace
33{
34 #define ES ExpressionScope
35 #define FS dynamic_cast<FileExpressions::FexScope&>(scope)
36 #define AI ArgIterator
37 #define NODE FS.Node
38 #define VAL FS.Node.Value()
39 #define INTARG0 args->Unbox<integer>()
40 Box getName (ES& scope, AI , AI) { return MAString(scope.Allocator, NODE.Name(), 0); }
41 Box getType (ES& scope, AI , AI) { return VAL.Type(); }
42 Box isDirectory(ES& scope, AI , AI) { return VAL.IsDirectory(); }
43 Box isSymLink (ES& scope, AI , AI) { return VAL.IsSymbolicLink(); }
44 Box getSize (ES& scope, AI , AI) { return integer(VAL.Size()); }
45 Box getTime (ES& scope, AI , AI) { return VAL.MTime(); }
46 Box getCTime (ES& scope, AI , AI) { return VAL.CTime(); }
47 Box getATime (ES& scope, AI , AI) { return VAL.ATime(); }
48 Box getPerms (ES& scope, AI , AI) { return VAL.Perms(); }
49 Box getOwner (ES& scope, AI , AI) { return VAL.Owner(); }
50 Box getGroup (ES& scope, AI , AI) { return VAL.Group(); }
51#if ( (defined(__GLIBCXX__) && !defined(__MINGW32__)) \
52 || defined(__APPLE__) \
53 || defined(__ANDROID_NDK__) ) && !defined(ALIB_TESTSTDFS)
54 Box userID (ES& , AI , AI) { return FInfo::TOwnerAndGroupID(getuid()); }
55 Box groupID (ES& , AI , AI) { return FInfo::TOwnerAndGroupID(getgid()); }
56#else
57 Box userID (ES& , AI , AI) { return FInfo::UnknownID; }
58 Box groupID (ES& , AI , AI) { return FInfo::UnknownID; }
59#endif
60 Box kiloBytes (ES& , AI args, AI) { return INTARG0 * integer(1024); }
61 Box megaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024); }
62 Box gigaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024); }
63 Box teraBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
64 Box petaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
65 Box exaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
66 Box getPath (ES& scope, AI , AI) { return MAString(scope.Allocator, FS.ParentPath, 0); }
67 #undef ES
68 #undef FS
69 #undef AI
70 #undef NODE
71 #undef VAL
72 #undef INTARG0
73
74 extern Box constOwnRead ; Box constOwnRead ;
75 extern Box constOwnWrite ; Box constOwnWrite;
76 extern Box constOwnExec ; Box constOwnExec ;
77 extern Box constGrpRead ; Box constGrpRead ;
78 extern Box constGrpWrite ; Box constGrpWrite;
79 extern Box constGrpExec ; Box constGrpExec ;
80 extern Box constOthRead ; Box constOthRead ;
81 extern Box constOthWrite ; Box constOthWrite;
82 extern Box constOthExec ; Box constOthExec ;
83
84 extern Box constTDirectory ; Box constTDirectory ;
85 extern Box constTSymbolicLinkDir ; Box constTSymbolicLinkDir;
86 extern Box constTRegular ; Box constTRegular ;
87 extern Box constTSymbolicLink ; Box constTSymbolicLink ;
88 extern Box constTBlock ; Box constTBlock ;
89 extern Box constTCharacter ; Box constTCharacter ;
90 extern Box constTFifo ; Box constTFifo ;
91 extern Box constTSocket ; Box constTSocket ;
92
93 extern Box TypeUsrGrpID ; Box TypeUsrGrpID ;
94
95 NString32 TypeNameConverterTFP;
96 NString32 TypeNameConverterTID;
97 NString32 TypeNameConverterTTY;
98} // anonymous namespace
99#endif //!defined(ALIB_DOX)
100
101//==================================================================================================
102//=== FileExpressions::Plugin
103//==================================================================================================
105: Calculus( "Files Plug-in", pCompiler )
106{
107 // Initialize constant static boxes. This must not be done in the C++ bootstrap code.
108 constOwnRead = FInfo::Permissions::OWNER_READ ;
109 constOwnWrite = FInfo::Permissions::OWNER_WRITE ;
110 constOwnExec = FInfo::Permissions::OWNER_EXEC ;
111 constGrpRead = FInfo::Permissions::GROUP_READ ;
112 constGrpWrite = FInfo::Permissions::GROUP_WRITE ;
113 constGrpExec = FInfo::Permissions::GROUP_EXEC ;
114 constOthRead = FInfo::Permissions::OTHERS_READ ;
115 constOthWrite = FInfo::Permissions::OTHERS_WRITE;
116 constOthExec = FInfo::Permissions::OTHERS_EXEC ;
117
118 constTDirectory = FInfo::Types::DIRECTORY ;
119 constTSymbolicLinkDir= FInfo::Types::SYMBOLIC_LINK_DIR ;
120 constTRegular = FInfo::Types::REGULAR ;
121 constTSymbolicLink = FInfo::Types::SYMBOLIC_LINK ;
122 constTBlock = FInfo::Types::BLOCK ;
123 constTCharacter = FInfo::Types::CHARACTER ;
124 constTFifo = FInfo::Types::FIFO ;
125 constTSocket = FInfo::Types::SOCKET ;
126
127
128
129 TypeUsrGrpID= FInfo::UnknownID;
130
131 // load identifier/function names from resources
132 constexpr int tableSize= 38;
133 Token functionNames[tableSize];
134 Token::LoadResourcedTokens( FILES, "CPF", functionNames ALIB_DBG(,tableSize) );
135
137 Token* descriptor= functionNames;
138
139#if defined(_WIN32)
140 TypeNameConverterTFP= FILES.GetResource("TFP"); pCompiler.AddType(constOwnRead , TypeNameConverterTFP);
141 TypeNameConverterTID= FILES.GetResource("TID"); pCompiler.AddType(TypeUsrGrpID , TypeNameConverterTID);
142 TypeNameConverterTTY= FILES.GetResource("TTY"); pCompiler.AddType(constTDirectory, TypeNameConverterTTY);
143#else
144 pCompiler.AddType(constOwnRead, FILES.GetResource("TFP"));
145 pCompiler.AddType(TypeUsrGrpID, FILES.GetResource("TID"));
146 pCompiler.AddType(constTDirectory, FILES.GetResource("TTY"));
147#endif
148 // Constant identifiers
150 {
151 { *descriptor++, constOwnRead },
152 { *descriptor++, constOwnWrite },
153 { *descriptor++, constOwnExec },
154 { *descriptor++, constGrpRead },
155 { *descriptor++, constGrpWrite },
156 { *descriptor++, constGrpExec },
157 { *descriptor++, constOthRead },
158 { *descriptor++, constOthWrite },
159 { *descriptor++, constOthExec },
160
161 { *descriptor++, constTDirectory },
162 { *descriptor++, constTSymbolicLinkDir },
163 { *descriptor++, constTRegular },
164 { *descriptor++, constTSymbolicLink },
165 { *descriptor++, constTBlock },
166 { *descriptor++, constTCharacter },
167 { *descriptor++, constTFifo },
168 { *descriptor++, constTSocket },
169 };
170
171
172 Functions= // todo: ETI/CTI has to be checked. I had just copy/pasted it 8-/
173 { // but it is all set to ETI. The kiloBytes, megabytes, etc can at least be set to CTI
174 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getName ), &Types::String , ETI },
175 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getType ), &constTDirectory, ETI },
176 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isDirectory), &Types::Boolean , ETI },
177 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isSymLink ), &Types::Boolean , ETI },
178 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getSize ), &Types::Integer , ETI },
179 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
180 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
181 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getCTime ), &Types::DateTime, ETI },
182 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getATime ), &Types::DateTime, ETI },
183 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPerms ), &constOwnRead , ETI },
184 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(kiloBytes ), &Types::Integer , ETI },
185 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(megaBytes ), &Types::Integer , ETI },
186 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(gigaBytes ), &Types::Integer , ETI },
187 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(teraBytes ), &Types::Integer , ETI },
188 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(petaBytes ), &Types::Integer , ETI },
189 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(exaBytes ), &Types::Integer , ETI },
190 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getOwner ), &TypeUsrGrpID , ETI },
191 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getGroup ), &TypeUsrGrpID , ETI },
192 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(userID ), &TypeUsrGrpID , ETI },
193 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(groupID ), &TypeUsrGrpID , ETI },
194 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPath ), &Types::String , ETI },
195
196 };
197
198 AutoCasts=
199 {
200 { constOwnRead , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST, nullptr, nullptr },
201 { constTDirectory, nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST, nullptr, nullptr },
202 { TypeUsrGrpID , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST, nullptr, nullptr },
203 };
204} // FileExpressions::Plugin constructor
205
206//==================================================================================================
207//=== FileExpressions
208//==================================================================================================
216
218{
219 return SPFileFilter(new FileExpressions::Filter( *this, expressionString ));
220}
221
222//==================================================================================================
223//=== FileExpressions::Filter
224//==================================================================================================
226: fex ( pFex )
227, scope( pFex.compiler.CfgFormatter )
228{
229 expression= fex.compiler.Compile( expressionString );
230 if( !expression->ResultType().IsType<bool>() )
231 throw std::runtime_error( "Expression result type mismatch: expecting boolean result!" );
232}
233
234} // namespace [alib::files]
virtual ALIB_API SPExpression Compile(const String &expressionString)
Definition compiler.cpp:251
ALIB_API void AddType(Type sample, const NString &name)
Definition compiler.cpp:424
ALIB_API void SetupDefaults()
Definition compiler.cpp:160
@ 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.
static constexpr TOwnerAndGroupID UnknownID
Definition finfo.hpp:98
uint32_t TOwnerAndGroupID
Definition finfo.hpp:95
@ 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.
SPExpression expression
The compiled expression.
FileExpressions & fex
The scope used with this filter.
Plugin plugin
The file expression Plugin.
Compiler compiler
The expression compiler.
ALIB_API SPFileFilter CreateFilter(const String &expressionString)
const String & GetResource(const NString &name)
void InsertPlugin(TPlugin *plugin, TPriorities priority, 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:35
#define CALCULUS_CALLBACK(func)
Definition calculus.hpp:34
#define CALCULUS_SIGNATURE(BoxPointerArray)
Definition calculus.hpp:41
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
#define ALIB_DBG(...)
Definition alib.hpp:457
std::shared_ptr< FFilter > SPFileFilter
Definition ffilter.hpp:54
files::Files FILES
Definition filescamp.cpp:30
monomem::TMAString< character > MAString
Type alias in namespace alib.
Definition mastring.hpp:105
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
static ALIB_API Box * I[1]
Function accepts one integral argument.
static ALIB_API Box DateTime
static ALIB_API Box Integer
static ALIB_API Box Boolean
static ALIB_API Box String
std::vector< ConstantIdentifierEntry > ConstantIdentifiers
Definition calculus.hpp:315
std::vector< FunctionEntry > Functions
Definition calculus.hpp:386
static constexpr CTInvokable ETI
Definition calculus.hpp:265
std::vector< AutoCastEntry > AutoCasts
Definition calculus.hpp:865