X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrd_rpncalc.c;h=5bb5e4056ac21c5beb203c973d29962479ba5725;hb=734d64e1e4744aa0994cc7956263ac72e3d42eb1;hp=d1ab0dea2a95b2942659055a76239dd92f8a61be;hpb=2a6a270edfda89b04722b42b57992907f871c671;p=rrdtool.git diff --git a/src/rrd_rpncalc.c b/src/rrd_rpncalc.c index d1ab0de..5bb5e40 100644 --- a/src/rrd_rpncalc.c +++ b/src/rrd_rpncalc.c @@ -1,13 +1,14 @@ /**************************************************************************** - * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007 + * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008 **************************************************************************** * rrd_rpncalc.c RPN calculator functions ****************************************************************************/ #include "rrd_tool.h" #include "rrd_rpncalc.h" -#include "rrd_graph.h" +// #include "rrd_graph.h" #include +#include short addop2str( enum op_en op, @@ -19,8 +20,8 @@ int tzoffset( time_t); /* used to implement LTIME */ short rpn_compact( - rpnp_t * rpnp, - rpn_cdefds_t ** rpnc, + rpnp_t *rpnp, + rpn_cdefds_t **rpnc, short *count) { short i; @@ -61,7 +62,7 @@ short rpn_compact( } rpnp_t *rpn_expand( - rpn_cdefds_t * rpnc) + rpn_cdefds_t *rpnc) { short i; rpnp_t *rpnp; @@ -93,8 +94,8 @@ rpnp_t *rpn_expand( * str: out string, memory is allocated by the function, must be freed by the * the caller */ void rpn_compact2str( - rpn_cdefds_t * rpnc, - ds_def_t * ds_def, + rpn_cdefds_t *rpnc, + ds_def_t *ds_def, char **str) { unsigned short i, offset = 0; @@ -172,10 +173,12 @@ void rpn_compact2str( add_op(OP_SORT, SORT) add_op(OP_REV, REV) add_op(OP_TREND, TREND) + add_op(OP_TRENDNAN, TRENDNAN) add_op(OP_RAD2DEG, RAD2DEG) add_op(OP_DEG2RAD, DEG2RAD) add_op(OP_AVG, AVG) add_op(OP_ABS, ABS) + add_op(OP_ADDNAN, ADDNAN) #undef add_op } (*str)[offset] = '\0'; @@ -278,14 +281,17 @@ long lookup_DS( rpnp_t *rpn_parse( void *key_hash, const char *const expr_const, - long (*lookup) (void *, - char *)) + long (*lookup) (void *, + char *)) { int pos = 0; char *expr; long steps = -1; rpnp_t *rpnp; char vname[MAX_VNAME_LEN + 10]; + char *old_locale; + + old_locale = setlocale(LC_NUMERIC, "C"); rpnp = NULL; expr = (char *) expr_const; @@ -293,6 +299,7 @@ rpnp_t *rpn_parse( while (*expr) { if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) * sizeof(rpnp_t))) == NULL) { + setlocale(LC_NUMERIC, old_locale); return NULL; } @@ -305,8 +312,7 @@ rpnp_t *rpn_parse( else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \ rpnp[steps].op = VV; \ expr+=strlen(#VVV); \ - } - + } #define match_op_param(VV,VVV) \ else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \ @@ -364,10 +370,12 @@ rpnp_t *rpn_parse( match_op(OP_SORT, SORT) match_op(OP_REV, REV) match_op(OP_TREND, TREND) + match_op(OP_TRENDNAN, TRENDNAN) match_op(OP_RAD2DEG, RAD2DEG) match_op(OP_DEG2RAD, DEG2RAD) match_op(OP_AVG, AVG) match_op(OP_ABS, ABS) + match_op(OP_ADDNAN, ADDNAN) #undef match_op else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1) && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) != @@ -377,24 +385,28 @@ rpnp_t *rpn_parse( } else { + setlocale(LC_NUMERIC, old_locale); free(rpnp); return NULL; } + if (*expr == 0) break; if (*expr == ',') expr++; else { + setlocale(LC_NUMERIC, old_locale); free(rpnp); return NULL; } } rpnp[steps + 1].op = OP_END; + setlocale(LC_NUMERIC, old_locale); return rpnp; } void rpnstack_init( - rpnstack_t * rpnstack) + rpnstack_t *rpnstack) { rpnstack->s = NULL; rpnstack->dc_stacksize = 0; @@ -402,7 +414,7 @@ void rpnstack_init( } void rpnstack_free( - rpnstack_t * rpnstack) + rpnstack_t *rpnstack) { if (rpnstack->s != NULL) free(rpnstack->s); @@ -434,10 +446,10 @@ static int rpn_compare_double( * 0 on success */ short rpn_calc( - rpnp_t * rpnp, - rpnstack_t * rpnstack, + rpnp_t *rpnp, + rpnstack_t *rpnstack, long data_idx, - rrd_value_t * output, + rrd_value_t *output, int output_idx) { int rpi; @@ -533,6 +545,20 @@ short rpn_calc( + rpnstack->s[stptr]; stptr--; break; + case OP_ADDNAN: + stackunderflow(1); + if (isnan(rpnstack->s[stptr - 1])) { + rpnstack->s[stptr - 1] = rpnstack->s[stptr]; + } else if (isnan(rpnstack->s[stptr])) { + /* NOOP */ + /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */ + } else { + rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] + + rpnstack->s[stptr]; + } + + stptr--; + break; case OP_SUB: stackunderflow(1); rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] @@ -680,8 +706,10 @@ short rpn_calc( break; case OP_IF: stackunderflow(2); - rpnstack->s[stptr - 2] = rpnstack->s[stptr - 2] != 0.0 ? - rpnstack->s[stptr - 1] : rpnstack->s[stptr]; + rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2]) + || rpnstack->s[stptr - 2] == + 0.0) ? rpnstack->s[stptr] : rpnstack-> + s[stptr - 1]; stptr--; stptr--; break; @@ -757,6 +785,7 @@ short rpn_calc( } break; case OP_TREND: + case OP_TRENDNAN: stackunderflow(1); if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) { rrd_set_error("malformed trend arguments"); @@ -766,16 +795,24 @@ short rpn_calc( time_t step = (time_t) rpnp[rpi - 2].step; if (output_idx > (int) ceil((float) dur / (float) step)) { + int ignorenan = (rpnp[rpi].op == OP_TREND); double accum = 0.0; int i = 0; + int count = 0; do { - accum += + double val = rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--]; + if (ignorenan || !isnan(val)) { + accum += val; + ++count; + } + dur -= step; } while (dur > 0); - rpnstack->s[--stptr] = (accum / -i); + rpnstack->s[--stptr] = + (count == 0) ? DNAN : (accum / count); } else rpnstack->s[--stptr] = DNAN; }