ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
fileexpressions.cpp
1using namespace alib::expressions;
2using namespace alib::system;
3
4namespace alib::filetree {
5
6#if !DOXYGEN
7
8//==================================================================================================
9//=== Anonymous expression functions and constant objects
10//==================================================================================================
11namespace
12{
13 #define ES ExpressionScope
14 #define FS dynamic_cast<FileExpressions::FexScope&>(scope)
15 #define AI ArgIterator
16 #define NODE FS.Node
17 #define VAL FS.Node
18 #define INTARG0 args->Unbox<integer>()
19Box getType (ES& scope, AI , AI) { return VAL->Type(); }
20Box isDirectory(ES& scope, AI , AI) { return VAL->IsDirectory(); }
21Box isSymLink (ES& scope, AI , AI) { return VAL->IsSymbolicLink(); }
22Box getSize (ES& scope, AI , AI) { return integer(VAL->Size()); }
23Box getTime (ES& scope, AI , AI) { return VAL->MDate(); }
24Box getBTime (ES& scope, AI , AI) { return VAL->BDate(); }
25Box getCTime (ES& scope, AI , AI) { return VAL->CDate(); }
26Box getATime (ES& scope, AI , AI) { return VAL->ADate(); }
27Box getPerms (ES& scope, AI , AI) { return VAL->Perms(); }
28Box getOwner (ES& scope, AI , AI) { return VAL->Owner(); }
29Box getGroup (ES& scope, AI , AI) { return VAL->Group(); }
30#if ALIB_SYSTEM_FILE_STATUS_IMPL == ALIB_SYSTEM_FILE_POSIX_STATUS
31Box userID (ES& , AI , AI) { return FTValue::TOwnerAndGroupID(getuid()); }
32Box groupID (ES& , AI , AI) { return FTValue::TOwnerAndGroupID(getgid()); }
33#else
34 Box userID (ES& , AI , AI) { return FTValue::UnknownID; }
35 Box groupID (ES& , AI , AI) { return FTValue::UnknownID; }
36#endif
37Box kiloBytes (ES& , AI args, AI) { return INTARG0 * integer(1024); }
38Box megaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024); }
39Box gigaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024); }
40Box teraBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
41Box petaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
42Box exaBytes (ES& , AI args, AI) { return INTARG0 * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024) * integer(1024); }
43
44
45#if ALIB_PATH_CHARACTERS_WIDE == ALIB_CHARACTERS_WIDE
46Box getName (ES& scope, AI , AI) { return NODE.Name(); }
47Box getPath (ES& scope, AI , AI) { return FS.ParentPath; }
48#else
49 // allocate converted name in the scope
50 Box getName (ES& scope, AI , AI) { return String(scope.Allocator, String256(NODE.Name() )); }
51 Box getPath (ES& scope, AI , AI) { return String(scope.Allocator, String256(FS.ParentPath)); }
52#endif
53
54 #undef ES
55 #undef FS
56 #undef AI
57 #undef NODE
58 #undef VAL
59 #undef INTARG0
60
61 extern Box constOwnRead ; Box constOwnRead ;
62 extern Box constOwnWrite ; Box constOwnWrite;
63 extern Box constOwnExec ; Box constOwnExec ;
64 extern Box constGrpRead ; Box constGrpRead ;
65 extern Box constGrpWrite ; Box constGrpWrite;
66 extern Box constGrpExec ; Box constGrpExec ;
67 extern Box constOthRead ; Box constOthRead ;
68 extern Box constOthWrite ; Box constOthWrite;
69 extern Box constOthExec ; Box constOthExec ;
70
71 extern Box constTDirectory ; Box constTDirectory ;
72 extern Box constTSymbolicLinkDir ; Box constTSymbolicLinkDir;
73 extern Box constTRegular ; Box constTRegular ;
74 extern Box constTSymbolicLink ; Box constTSymbolicLink ;
75 extern Box constTBlock ; Box constTBlock ;
76 extern Box constTCharacter ; Box constTCharacter ;
77 extern Box constTFifo ; Box constTFifo ;
78 extern Box constTSocket ; Box constTSocket ;
79
80 extern Box TypeUsrGrpID ; Box TypeUsrGrpID ;
81
82} // anonymous namespace
83#endif //!DOXYGEN
84
85 //================================================================================================
86 //=== FileExpressions::Plugin
87 //================================================================================================
89: Calculus( "Files Plug-in", pCompiler, expressions::CompilePriorities::Custom ) {
90 // Initialize constant static boxes. This must not be done in the C++ bootstrap code.
100
101 constTDirectory = FileStatus::Types::DIRECTORY ;
102 constTSymbolicLinkDir= FileStatus::Types::SYMBOLIC_LINK_DIR ;
103 constTRegular = FileStatus::Types::REGULAR ;
104 constTSymbolicLink = FileStatus::Types::SYMBOLIC_LINK ;
105 constTBlock = FileStatus::Types::BLOCK ;
106 constTCharacter = FileStatus::Types::CHARACTER ;
107 constTFifo = FileStatus::Types::FIFO ;
108 constTSocket = FileStatus::Types::SOCKET ;
109
110 TypeUsrGrpID = FTValue::UnknownID;
111
112#if ALIB_CHARACTERS_WIDE
113 NString256 converter;
114 converter.Reset( FILETREE.GetResource("TFP") ); pCompiler.AddType(constOwnRead , NString(pCompiler.GetAllocator(), converter) );
115 converter.Reset( FILETREE.GetResource("TID") ); pCompiler.AddType(TypeUsrGrpID , NString(pCompiler.GetAllocator(), converter) );
116 converter.Reset( FILETREE.GetResource("TTY") ); pCompiler.AddType(constTDirectory, NString(pCompiler.GetAllocator(), converter) );
117#else
118 pCompiler.AddType(constOwnRead , FILETREE.GetResource("TFP"));
119 pCompiler.AddType(TypeUsrGrpID , FILETREE.GetResource("TID"));
120 pCompiler.AddType(constTDirectory, FILETREE.GetResource("TTY"));
121#endif
122
123 // load identifier/function names from resources
124 constexpr int tableSize= 39;
125 Token functionNames[tableSize];
126 strings::util::LoadResourcedTokens( FILETREE, "CPF", functionNames ALIB_DBG(,tableSize) );
127
128 Token* descriptor= functionNames;
129
130 // Constant identifiers
132 {
133 { *descriptor++, constOwnRead },
134 { *descriptor++, constOwnWrite },
135 { *descriptor++, constOwnExec },
136 { *descriptor++, constGrpRead },
137 { *descriptor++, constGrpWrite },
138 { *descriptor++, constGrpExec },
139 { *descriptor++, constOthRead },
140 { *descriptor++, constOthWrite },
141 { *descriptor++, constOthExec },
142
143 { *descriptor++, constTDirectory },
144 { *descriptor++, constTSymbolicLinkDir },
145 { *descriptor++, constTRegular },
146 { *descriptor++, constTSymbolicLink },
147 { *descriptor++, constTBlock },
148 { *descriptor++, constTCharacter },
149 { *descriptor++, constTFifo },
150 { *descriptor++, constTSocket },
151 };
152
153
154 Functions=
155 {
156 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getName ), &Types::String , ETI },
157 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getType ), &constTDirectory, ETI },
158 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isDirectory), &Types::Boolean , ETI },
159 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(isSymLink ), &Types::Boolean , ETI },
160 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getSize ), &Types::Integer , ETI },
161 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
162 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getTime ), &Types::DateTime, ETI },
163 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getBTime ), &Types::DateTime, ETI },
164 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getCTime ), &Types::DateTime, ETI },
165 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getATime ), &Types::DateTime, ETI },
166 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPerms ), &constOwnRead , ETI },
167 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(kiloBytes ), &Types::Integer , CTI },
168 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(megaBytes ), &Types::Integer , CTI },
169 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(gigaBytes ), &Types::Integer , CTI },
170 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(teraBytes ), &Types::Integer , CTI },
171 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(petaBytes ), &Types::Integer , CTI },
172 { *descriptor++, CALCULUS_SIGNATURE(Signatures::I), CALCULUS_CALLBACK(exaBytes ), &Types::Integer , CTI },
173 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getOwner ), &TypeUsrGrpID , ETI },
174 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getGroup ), &TypeUsrGrpID , ETI },
175 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(userID ), &TypeUsrGrpID , ETI },
176 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(groupID ), &TypeUsrGrpID , ETI },
177 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(getPath ), &Types::String , ETI },
178
179 };
180
181 AutoCasts=
182 {
183 { constOwnRead , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
184 { constTDirectory, nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
185 { TypeUsrGrpID , nullptr, nullptr, CALCULUS_DEFAULT_AUTOCAST , nullptr , nullptr },
186 };
187
188} // FileExpressions::Plugin constructor
189
190//==================================================================================================
191//=== FileExpressions
192//==================================================================================================
194: plugin( compiler ) { compiler.SetupDefaults(); compiler.InsertPlugin( &plugin ); }
195
197{ return SPFileFilter(new FileExpressions::Filter( *this, expressionString )); }
198
199//==================================================================================================
200//=== FileExpressions::Filter
201//==================================================================================================
203: fex ( pFex )
204, scope( pFex.compiler.CfgFormatter ) {
205 expression= fex.compiler.Compile( expressionString );
206 if( !expression->ResultType().IsType<bool>() )
207 throw std::runtime_error( "Expression result type mismatch: expecting boolean result!" );
208}
209
210} // namespace [alib::filetree]
#define ALIB_DBG(...)
void AddType(Type sample, const NString &name)
Definition compiler.cpp:377
MonoAllocator & GetAllocator()
Definition compiler.hpp:465
Expression expression
The compiled expression.
FileExpressions::FexScope scope
The scope used with this filter.
FileExpressions()
Default constructor.
SPFileFilter CreateFilter(const String &expressionString)
Plugin plugin
The file expression Plugin.
Compiler compiler
The expression compiler.
@ CHARACTER
A character special file.
@ BLOCK
A block special file.
@ FIFO
A FIFO (also known as pipe) file.
@ GROUP_READ
Posix S_IRGRP: The file's user group has read permission.
@ GROUP_EXEC
Posix S_IXGRP: The file's user group has execute/search permission.
@ OTHERS_EXEC
Posix S_IXOTH: Other users have execute/search permission.
@ GROUP_WRITE
Posix S_IWGRP: The file's user group has write permission.
@ OWNER_READ
Posix S_IRUSR: File owner has read permission.
@ OWNER_EXEC
Posix S_IXUSR: File owner has execute/search permission.
@ OWNER_WRITE
Posix S_IWUSR: File owner has write permission.
@ OTHERS_READ
Posix S_IROTH: Other users have read permission.
@ OTHERS_WRITE
Posix S_IWOTH: Other users have write permission.
static constexpr TOwnerAndGroupID UnknownID
Constant value for owner and group IDs to denote that the field was not determined.
uint32_t TOwnerAndGroupID
Type definition for owner and group ids.
#define CALCULUS_DEFAULT_AUTOCAST
#define CALCULUS_CALLBACK(func)
#define CALCULUS_SIGNATURE(BoxPointerArray)
std::shared_ptr< FFilter > SPFileFilter
A shared pointer to a filter.
Definition ffilter.hpp:43
void LoadResourcedTokens(camp::Camp &module, const NString &resourceName, strings::util::Token *target, int dbgSizeVerifier, character outerSeparator=',', character innerSeparator=' ')
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
filetree::FilesCamp FILETREE
The singleton instance of ALib Camp class #"FilesCamp".
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
boxing::Box Box
Type alias in namespace #"%alib".
Definition box.hpp:1128
expressions::Compiler Compiler
Type alias in namespace #"%alib".
Definition compiler.hpp:535
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
NLocalString< 256 > NString256
Type alias name for #"TLocalString;TLocalString<nchar,256>".
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
strings::util::Token Token
Type alias in namespace #"%alib".
Definition token.hpp:396
static Box * I[1]
Function accepts one integral argument.
static Box Integer
Sample type-box for integer types. (Precisely for type #"lang::integer".).
static Box Boolean
Sample type-box for C++ type bool.
static Box DateTime
Sample type-box for date and time values of type #"time::DateTime").
static constexpr CTInvokable ETI
Definition calculus.hpp:240
Calculus(const NString &name, Compiler &compiler, CompilePriorities pPriority)
Definition calculus.hpp:250
std::vector< ConstantIdentifierEntry > ConstantIdentifiers
List of identifiers that return constant values to be compiled by this plug-in.
Definition calculus.hpp:277
static constexpr CTInvokable CTI
Definition calculus.hpp:235
std::vector< AutoCastEntry > AutoCasts
List of auto-casts to be compiled by this plug-in.
Definition calculus.hpp:754
std::vector< FunctionEntry > Functions
List of functions to be compiled by this plug-in.
Definition calculus.hpp:336