VirtualBox

source: vbox/trunk/include/iprt/expreval.h@ 95029

Last change on this file since 95029 was 93176, checked in by vboxsync, 3 years ago

iprt/RTExprEval: Early code for a 'simple' expression evaluator a la /bin/expr and such. [scm+doxygen] bugref:9781

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1/* $Id: expreval.h 93176 2022-01-11 01:15:59Z vboxsync $ */
2/** @file
3 * IPRT - Expression Evaluator.
4 */
5
6/*
7 * Copyright (C) 2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef IPRT_INCLUDED_expreval_h
28#define IPRT_INCLUDED_expreval_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33#include <iprt/types.h>
34
35RT_C_DECLS_BEGIN
36
37/** @defgroup grp_rt_expr_eval RTExprEval - Expression Evaluator
38 * @{ */
39
40/** Handle to an expression evaluator. */
41typedef struct RTEXPREVALINT *RTEXPREVAL;
42/** Pointer to an expression evaluator handle. */
43typedef RTEXPREVAL *PRTEXPREVAL;
44/** NIL expression evaluator handle. */
45#define NIL_RTEXPREVAL ((RTEXPREVAL)~(uintptr_t)0)
46
47/**
48 * Variable getter (supplied by user).
49 *
50 * @returns IPRT status code.
51 * @retval VERR_NOT_FOUND if the variable does not exist.
52 */
53typedef DECLCALLBACKTYPE(int, FNRTEXPREVALQUERYVARIABLE,(const char *pchName, size_t cchName, void *pvUser, char **ppszValue));
54/** Pointer to a variable getter. */
55typedef FNRTEXPREVALQUERYVARIABLE *PFNRTEXPREVALQUERYVARIABLE;
56
57/** @name Expression evaluator flags.
58 * @sa RTExprEvalCreate
59 * @{ */
60/** Default to hexadecimal instead of decimal numbers. */
61#define RTEXPREVAL_F_DEFAULT_BASE_16 RT_BIT_64(0)
62/** Enables C-ish octal style, i.e. 0777 be read as 0x1ff (in hex). */
63#define RTEXPREVAL_F_C_OCTAL RT_BIT_64(1)
64/** Enables the 'exists' operator that can be used to check if a path exists.
65 * @sa RTPathExists */
66#define RTEXPREVAL_F_EXISTS_OP RT_BIT_64(2)
67/** Valid mask. */
68#define RTEXPREVAL_F_VALID_MASK UINT64_MAX(3)
69/** @} */
70
71/**
72 * Creates an expression evaluator.
73 *
74 * @returns IPRT status code.
75 * @param phEval Where to return the handle to the evaluator.
76 * @param fFlags RTEXPREVAL_F_XXX.
77 * @param pszName The evaluator name (for logging).
78 * @param pvUser User argument for callbacks.
79 * @param pfnQueryVariable Callback for querying variables. Optional.
80 */
81RTDECL(int) RTExprEvalCreate(PRTEXPREVAL phEval, uint64_t fFlags, const char *pszName,
82 void *pvUser, PFNRTEXPREVALQUERYVARIABLE pfnQueryVariable);
83
84/**
85 * Retains a reference to the evaluator.
86 *
87 * @returns New reference count, UINT32_MAX if @a hEval is not valid.
88 * @param hEval Handle to the evaluator.
89 */
90RTDECL(uint32_t) RTExprEvalRetain(RTEXPREVAL hEval);
91
92/**
93 * Releases a reference to the evaluator.
94 *
95 * @returns New reference count, UINT32_MAX if @a hEval is not valid. (The
96 * evaluator was destroyed when 0 is returned.)
97 * @param hEval Handle to the evaluator.
98 */
99RTDECL(uint32_t) RTExprEvalRelease(RTEXPREVAL hEval);
100
101/**
102 * Evaluates the given if expression to a boolean result.
103 *
104 * @returns IPRT status code
105 * @param hEval Handle to the evaluator.
106 * @param pch The expression string. Does not need to be zero
107 * terminated.
108 * @param cch The length of the expression. Pass RTSTR_MAX if not
109 * known.
110 * @param pfResult Where to return the result.
111 * @param pErrInfo Where to return additional error info.
112 */
113RTDECL(int) RTExprEvalToBool(RTEXPREVAL hEval, const char *pch, size_t cch, bool *pfResult, PRTERRINFO pErrInfo);
114
115/**
116 * Evaluates the given if expression to an integer (signed 64-bit) result.
117 *
118 * @returns IPRT status code
119 * @param hEval Handle to the evaluator.
120 * @param pch The expression string. Does not need to be zero
121 * terminated.
122 * @param cch The length of the expression. Pass RTSTR_MAX if not
123 * known.
124 * @param piResult Where to return the result.
125 * @param pErrInfo Where to return additional error info.
126 */
127RTDECL(int) RTExprEvalToInteger(RTEXPREVAL hEval, const char *pch, size_t cch, int64_t *piResult, PRTERRINFO pErrInfo);
128
129/**
130 * Evaluates the given if expression to a string result.
131 *
132 * @returns IPRT status code
133 * @param hEval Handle to the evaluator.
134 * @param pch The expression string. Does not need to be zero
135 * terminated.
136 * @param cch The length of the expression. Pass RTSTR_MAX if not
137 * known.
138 * @param ppszResult Where to return the result. This must be freed using
139 * RTStrFree.
140 * @param pErrInfo Where to return additional error info.
141 */
142RTDECL(int) RTExprEvalToString(RTEXPREVAL hEval, const char *pch, size_t cch, char **ppszResult, PRTERRINFO pErrInfo);
143
144/** @} */
145
146RT_C_DECLS_END
147
148#endif /* !IPRT_INCLUDED_expreval_h */
149
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette