port forward of fixes from the stable version of rrd_cgi
[rrdtool.git] / src / rrd_rpncalc.h
1 /****************************************************************************
2  * RRDtool 1.0.33  Copyright Tobias Oetiker, 1997 - 2000
3  ****************************************************************************
4  * rrd_rpncalc.h  RPN calculator functions
5  ****************************************************************************/
6 #ifndef _RRD_RPNCALC_H
7 #define _RRD_RPNCALC_H
8
9 /* WARNING: if new operators are added, they MUST be added at the very end of the list.
10  * This is because COMPUTE (CDEF) DS store OP nodes by number (name is not
11  * an option due to limited par array size). OP nodes must have the same
12  * numeric values, otherwise the stored numbers will mean something different. */
13 enum op_en {OP_NUMBER=0,OP_VARIABLE,OP_INF,OP_PREV,OP_NEGINF,
14             OP_UNKN,OP_NOW,OP_TIME,OP_ADD,OP_MOD,OP_SUB,OP_MUL,
15             OP_DIV,OP_SIN, OP_DUP, OP_EXC, OP_POP,
16             OP_COS,OP_LOG,OP_EXP,OP_LT,OP_LE,OP_GT,OP_GE,OP_EQ,OP_IF,
17             OP_MIN,OP_MAX,OP_LIMIT, OP_FLOOR, OP_CEIL,
18             OP_UN,OP_END,OP_LTIME,OP_NE,OP_ISINF,OP_PREV_OTHER,OP_COUNT,OP_ATAN};
19
20 typedef struct rpnp_t {
21     enum op_en   op;
22     double val; /* value for a OP_NUMBER */
23     long ptr; /* pointer into the gdes array for OP_VAR */
24     double *data; /* pointer to the current value from OP_VAR DAS*/
25     long ds_cnt;   /* data source count for data pointer */
26     long step; /* time step for OP_VAR das */
27 } rpnp_t;
28
29 /* a compact representation of rpnp_t for computed data sources */
30 typedef struct rpn_cdefds_t {
31     char op;  /* rpn operator type */
32     short val; /* used by OP_NUMBER and OP_VARIABLE */
33 } rpn_cdefds_t;
34
35 /* limit imposed by sizeof(rpn_cdefs_t) and rrd.ds_def.par */
36 #define DS_CDEF_MAX_RPN_NODES 26 
37
38 typedef struct rpnstack_t {
39     double *s;
40     long dc_stacksize;
41     long dc_stackblock;
42 } rpnstack_t;
43
44 void rpnstack_init(rpnstack_t *rpnstack);
45 void rpnstack_free(rpnstack_t *rpnstack);
46
47 void parseCDEF_DS(char *def, rrd_t *rrd, int ds_idx);
48 long lookup_DS(void *rrd_vptr, char *ds_name);
49
50 short rpn_compact(rpnp_t *rpnp,rpn_cdefds_t **rpnc,short *count);
51 rpnp_t * rpn_expand(rpn_cdefds_t *rpnc);
52 void rpn_compact2str(rpn_cdefds_t *rpnc,ds_def_t *ds_def,char **str);
53 rpnp_t * rpn_parse(void *key_hash,char *expr, long (*lookup)(void *,char *));
54 short rpn_calc(rpnp_t *rpnp, rpnstack_t *rpnstack, long data_idx, rrd_value_t *output, int output_idx);
55
56 #endif