From: Tobias Oetiker Date: Wed, 20 Aug 2014 14:00:17 +0000 (+0200) Subject: cleanup rrd_strtodbl calls X-Git-Tag: v1.5.0-rc1~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fd1aab0c87460b458c40ade11e4396bda7ae2e48;p=thirdparty%2Frrdtool-1.x.git cleanup rrd_strtodbl calls --- diff --git a/doc/librrd.pod b/doc/librrd.pod index f6ecba09..0e5bdf72 100644 --- a/doc/librrd.pod +++ b/doc/librrd.pod @@ -82,6 +82,11 @@ values supplied to the fetch function. If the values are the same, the previous Generates random numbers just like random(). This further ensures that the random number generator is seeded exactly once per process. +=item B + +an rrd aware string to double convertor which sets rrd_error in if there is a problem +and uses the return code exclusively for conversion status reporting. + =item B works like normal strtod, but it is locale independent (and thread safe) diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index ef4ec107..6ff84003 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -1512,7 +1512,7 @@ static int handle_request_update (HANDLER_PROTO) /* {{{ */ /* make sure update time is always moving forward. We use double here since update does support subsecond precision for timestamps ... */ - if ( ( rrd_strtodbl( value, &eostamp, &stamp, "error while parsing time stamp" ) != 1 ) || *eostamp != ':') + if ( ( rrd_strtodbl( value, &eostamp, &stamp, NULL) != 1 ) || *eostamp != ':') { pthread_mutex_unlock(&cache_lock); return send_response(sock, RESP_ERR, diff --git a/src/rrd_graph.c b/src/rrd_graph.c index db122818..b501a450 100644 --- a/src/rrd_graph.c +++ b/src/rrd_graph.c @@ -4583,7 +4583,8 @@ void rrd_graph_options( im->forceleftspace = 1; break; case 'T': - rrd_strtodbl(optarg, 0, &(im->tabwidth), "Function rrd_graph_options, option 'T'"); + if (rrd_strtodbl(optarg, 0, &(im->tabwidth), "option -T") != 2) + return; break; case 'S': im->step = atoi(optarg); @@ -4652,7 +4653,9 @@ void rrd_graph_options( break; }; if (sscanf(optarg, "%[-0-9.e+]:%d", double_str , &im->ylabfact) == 2) { - rrd_strtodbl( double_str, 0, &(im->ygridstep), "Function rrd_graph_options, option 'y'"); + if (rrd_strtodbl( double_str, 0, &(im->ygridstep), "option -y") != 2){ + return; + } if (im->ygridstep <= 0) { rrd_set_error("grid step must be > 0"); return; @@ -4673,10 +4676,11 @@ void rrd_graph_options( "%[-0-9.e+]:%[-0-9.e+]", double_str, double_str2 ) != 2) { - rrd_strtodbl( double_str, 0, &(im->grid_dash_on), "Function rrd_graph_options, option 1008"); - rrd_strtodbl( double_str2, 0, &(im->grid_dash_off), "Function rrd_graph_options, option 1008"); - rrd_set_error("expected grid-dash format float:float"); - return; + if ( rrd_strtodbl( double_str, 0, &(im->grid_dash_on),NULL) !=2 + || rrd_strtodbl( double_str2, 0, &(im->grid_dash_off), NULL) != 2 ){ + rrd_set_error("expected grid-dash format float:float"); + return; + } } break; case 1009: /* enable dynamic labels */ @@ -4690,9 +4694,9 @@ void rrd_graph_options( if(sscanf(optarg, "%[-0-9.e+]:%[-0-9.e+]", double_str, - double_str2 ) == 2) { - rrd_strtodbl( double_str, 0, &(im->second_axis_scale), "Function rrd_graph_options, option 1002"); - rrd_strtodbl( double_str2, 0, &(im->second_axis_shift), "Function rrd_graph_options, option 1002"); + double_str2 ) == 2 + && rrd_strtodbl( double_str, 0, &(im->second_axis_scale),NULL) == 2 + && rrd_strtodbl( double_str2, 0, &(im->second_axis_shift),NULL) == 2){ if(im->second_axis_scale==0){ rrd_set_error("the second_axis_scale must not be 0"); return; @@ -4739,10 +4743,14 @@ void rrd_graph_options( } break; case 'u': - rrd_strtodbl(optarg, 0, &(im->maxval), "Function rrd_graph_options, option 'u'"); + if (rrd_strtodbl(optarg, 0, &(im->maxval), "option -u") != 2){ + return; + } break; case 'l': - rrd_strtodbl(optarg, 0, &(im->minval), "Function rrd_graph_options, option 'l'"); + if (rrd_strtodbl(optarg, 0, &(im->minval), "option -l") != 2){ + return; + } break; case 'b': im->base = atol(optarg); @@ -4854,8 +4862,8 @@ void rrd_graph_options( double size = 1; int end; - if (sscanf(optarg, "%10[A-Z]:%[-0-9.e+]%n", prop, double_str, &end) >= 2) { - rrd_strtodbl( double_str, 0, &size, "Function rrd_graph_options, option 'n'" ); + if (sscanf(optarg, "%10[A-Z]:%[-0-9.e+]%n", prop, double_str, &end) >= 2 + && rrd_strtodbl( double_str, 0, &size, NULL) == 2) { int sindex, propidx; if ((sindex = text_prop_conv(prop)) != -1) { @@ -4890,7 +4898,9 @@ void rrd_graph_options( break; } case 'm': - rrd_strtodbl(optarg, 0, &(im->zoom), "Function rrd_graph_options, option 'm'"); + if (rrd_strtodbl(optarg, 0, &(im->zoom), "option -m") != 2){ + return; + } if (im->zoom <= 0.0) { rrd_set_error("zoom factor must be > 0"); return; @@ -5162,8 +5172,7 @@ int vdef_parse( n = 0; sscanf(str, "%20[-0-9.e+],%29[A-Z]%n", double_str, func, &n); - if ( rrd_strtodbl( str, NULL, ¶m, "Function vdef_parse" ) != 2){ - rrd_clear_error(); + if ( rrd_strtodbl( str, NULL, ¶m, NULL) != 2 ){ n = 0; sscanf(str, "%29[A-Z]%n", func, &n); if (n == (int) strlen(str)) { /* matched */ diff --git a/src/rrd_graph_helper.c b/src/rrd_graph_helper.c index 29325241..1b10f903 100644 --- a/src/rrd_graph_helper.c +++ b/src/rrd_graph_helper.c @@ -144,21 +144,23 @@ int getDouble(const char* v, double *val,char**extra) { unsigned int strtod_ret; *extra=NULL; - if( rrd_strtodbl( v, extra, val, "Function getDouble" ) != 2 ) { - return -1; - } - - strtod_ret = rrd_strtodbl( v, extra, val, "Function getDouble" ); + strtod_ret = rrd_strtodbl( v, extra, val, NULL); /* see rrd_strtodbl's return values for more infromation */ - if( strtod_ret == 0 ) - return -1; - else if( strtod_ret == 1 ) - return 1; - else if( strtod_ret == 2 ) - return 0; - else - return -2; + switch (rrd_strtodbl( v, extra, val, NULL)){ + case 0: + return -1; + break; + case 1: + return 1; + break; + case 2: + return 0; + break; + default: + return -2; + break; + } } int addToArguments(parsedargs_t* pa, char*key, char*value, int cnt) { diff --git a/src/rrd_restore.c b/src/rrd_restore.c index ae5772b1..8fa5c1b6 100644 --- a/src/rrd_restore.c +++ b/src/rrd_restore.c @@ -329,7 +329,7 @@ static int get_xml_double( xmlFree(text); return 0; } - if ( rrd_strtodbl((char *)text,NULL, &temp, "Function xml_get_double") != 2 ){ + if ( rrd_strtodbl((char *)text,NULL, &temp, NULL) != 2 ){ rrd_set_error("ling %d: get_xml_double from '%s' %s", xmlTextReaderGetParserLineNumber(reader), text,rrd_strerror(errno)); diff --git a/src/rrd_strtod.c b/src/rrd_strtod.c index 98aef39e..b6134589 100644 --- a/src/rrd_strtod.c +++ b/src/rrd_strtod.c @@ -54,19 +54,14 @@ unsigned int rrd_strtodbl if ( local_endptr == (char *)str ) { /* no conversion has been done */ /* for inputs like "abcdj", i.e. no number at all */ - if( error == NULL ) { - rrd_set_error("Cannot convert %s to float", str); - } else { + if( error ) { rrd_set_error("%s - Cannot convert %s to float", error, str); } return 0; } else if( local_endptr[0] != '\0' ) { /* conversion has been done, but whole string is not a number */ /* for inputs like "33.343djdjk" */ - if( error == NULL ) { - rrd_set_error("Converted %s to %lf, but cannot convert %s", - str, *dbl, local_endptr); - } else { + if( error ) { rrd_set_error("%s - Converted %s to %lf, but cannot convert %s", error, str, *dbl, local_endptr); } @@ -77,11 +72,9 @@ unsigned int rrd_strtodbl return 2; } else { /* just to be safe */ - if( error == NULL ) - rrd_set_error("Internal error. Something is seriously wrong '%s'", str); - else + if( error ) { rrd_set_error("%s - Internal error. Something is seriously wrong '%s'",error, str); - + } return 3; } } diff --git a/src/rrd_tune.c b/src/rrd_tune.c index 25831c9b..ad41d73f 100644 --- a/src/rrd_tune.c +++ b/src/rrd_tune.c @@ -209,7 +209,7 @@ int rrd_tune( case 'i': matches = sscanf(optarg, DS_NAM_FMT ":%[-0-9.e+]", ds_nam, double_str); if( matches >= 1 ) { - strtod_ret_val = rrd_strtodbl( double_str, 0, &min, "Function rrd_tune, option i" ); + strtod_ret_val = rrd_strtodbl( double_str, NULL, &min, NULL ); } if ((matches < 1) || (strtod_ret_val != 2)) { @@ -229,7 +229,7 @@ int rrd_tune( case 'a': matches = sscanf(optarg, DS_NAM_FMT ":%[-0-9.e+]", ds_nam, double_str); if( matches >= 1 ) { - strtod_ret_val = rrd_strtodbl( double_str, 0, &max, "Function rrd_tune, option i" ); + strtod_ret_val = rrd_strtodbl( double_str, NULL, &max, NULL ); } if ((matches < 1 ) || (strtod_ret_val != 2)) { @@ -430,7 +430,7 @@ int set_hwarg( signed short rra_idx = -1; unsigned int strtod_ret_val; - strtod_ret_val = rrd_strtodbl(arg, 0, ¶m, "Error while parsing Holt-Winters parameter"); + strtod_ret_val = rrd_strtodbl(arg, NULL, ¶m, NULL); /* read the value */ if ((strtod_ret_val == 1 || strtod_ret_val == 2 ) && (param <= 0.0 || param >= 1.0) ) { @@ -469,7 +469,7 @@ int set_hwsmootharg( unsigned int strtod_ret_val; /* read the value */ - strtod_ret_val = rrd_strtodbl(arg, 0, ¶m, "Error while parsing Holt-Winters parameter, function set_hesmootharg"); + strtod_ret_val = rrd_strtodbl(arg, NULL, ¶m, NULL); /* in order to avoid smoothing of SEASONAL or DEVSEASONAL, we need to * the 0.0 value*/ if ( (strtod_ret_val == 1 || strtod_ret_val == 2 ) && @@ -507,7 +507,7 @@ int set_deltaarg( signed short rra_idx = -1; unsigned int strtod_ret_val; - strtod_ret_val = rrd_strtodbl(arg, 0, ¶m, "Function set_deltaarg" ); + strtod_ret_val = rrd_strtodbl(arg, NULL, ¶m, NULL); if ((strtod_ret_val == 1 || strtod_ret_val == 2) && param < 0.1) { rrd_set_error("Parameter specified is too small"); diff --git a/src/rrd_update.c b/src/rrd_update.c index 588c3460..262dc3bd 100644 --- a/src/rrd_update.c +++ b/src/rrd_update.c @@ -1317,7 +1317,7 @@ static int get_time_from_reading( *current_time = tmp_time.tv_sec; *current_time_usec = tmp_time.tv_usec; } else { - if ( rrd_strtodbl( updvals[0], 0, &tmp, "error while parsing time in get_time_from_reading") != 2) { + if ( rrd_strtodbl( updvals[0], NULL, &tmp, "error while parsing time in get_time_from_reading") != 2) { return -1; }; if (tmp < 0.0){ @@ -1422,13 +1422,13 @@ static int update_pdp_prep( } break; case DST_ABSOLUTE: - if( rrd_strtodbl(updvals[ds_idx + 1], 0, &pdp_new[ds_idx], "Function update_pdp_prep, case DST_ABSOLUTE" ) != 2 ) { + if( rrd_strtodbl(updvals[ds_idx + 1], NULL, &pdp_new[ds_idx], "Function update_pdp_prep, case DST_ABSOLUTE" ) != 2 ) { return -1; } rate = pdp_new[ds_idx] / interval; break; case DST_GAUGE: - if( rrd_strtodbl( updvals[ds_idx + 1], 0, &tmp, "Function update_pdp_prep, case DST_GAUGE") == 2 ) { + if( rrd_strtodbl( updvals[ds_idx + 1], NULL, &tmp, "Function update_pdp_prep, case DST_GAUGE") == 2 ) { pdp_new[ds_idx] = tmp * interval; } else { return -1;