summaryrefslogtreecommitdiffstats
path: root/lib/perfdata.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/perfdata.c')
-rw-r--r--lib/perfdata.c83
1 files changed, 82 insertions, 1 deletions
diff --git a/lib/perfdata.c b/lib/perfdata.c
index 661756c5..f425ffcf 100644
--- a/lib/perfdata.c
+++ b/lib/perfdata.c
@@ -33,7 +33,7 @@ char *pd_value_to_string(const mp_perfdata_value pd) {
33char *pd_to_string(mp_perfdata pd) { 33char *pd_to_string(mp_perfdata pd) {
34 assert(pd.label != NULL); 34 assert(pd.label != NULL);
35 char *result = NULL; 35 char *result = NULL;
36 asprintf(&result, "%s=", pd.label); 36 asprintf(&result, "'%s'=", pd.label);
37 37
38 asprintf(&result, "%s%s", result, pd_value_to_string(pd.value)); 38 asprintf(&result, "%s%s", result, pd_value_to_string(pd.value));
39 39
@@ -514,3 +514,84 @@ perfdata_value_parser_wrapper parse_pd_value(const char *input) {
514 } 514 }
515 return result; 515 return result;
516} 516}
517
518mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value) {
519 perfdata.max = value;
520 perfdata.max_present = true;
521 return perfdata;
522}
523
524mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value) {
525 perfdata.min = value;
526 perfdata.min_present = true;
527 return perfdata;
528}
529
530double mp_get_pd_value(mp_perfdata_value value) {
531 assert(value.type != PD_TYPE_NONE);
532 switch (value.type) {
533 case PD_TYPE_DOUBLE:
534 return value.pd_double;
535 case PD_TYPE_INT:
536 return (double)value.pd_int;
537 case PD_TYPE_UINT:
538 return (double)value.pd_uint;
539 default:
540 return 0; // just to make the compiler happy
541 }
542}
543
544mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right) {
545 if (left.type == right.type) {
546 switch (left.type) {
547 case PD_TYPE_DOUBLE:
548 left.pd_double *= right.pd_double;
549 return left;
550 case PD_TYPE_INT:
551 left.pd_int *= right.pd_int;
552 return left;
553 case PD_TYPE_UINT:
554 left.pd_uint *= right.pd_uint;
555 return left;
556 default:
557 // what to here?
558 return left;
559 }
560 }
561
562 // Different types, oh boy, just do the lazy thing for now and switch to double
563 switch (left.type) {
564 case PD_TYPE_INT:
565 left.pd_double = (double)left.pd_int;
566 left.type = PD_TYPE_DOUBLE;
567 break;
568 case PD_TYPE_UINT:
569 left.pd_double = (double)left.pd_uint;
570 left.type = PD_TYPE_DOUBLE;
571 break;
572 }
573
574 switch (right.type) {
575 case PD_TYPE_INT:
576 right.pd_double = (double)right.pd_int;
577 right.type = PD_TYPE_DOUBLE;
578 break;
579 case PD_TYPE_UINT:
580 right.pd_double = (double)right.pd_uint;
581 right.type = PD_TYPE_DOUBLE;
582 break;
583 }
584
585 left.pd_double *= right.pd_double;
586 return left;
587}
588
589mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor) {
590 if (!range.end_infinity) {
591 range.end = mp_pd_value_multiply(range.end, factor);
592 }
593 if (!range.start_infinity) {
594 range.start = mp_pd_value_multiply(range.start, factor);
595 }
596 return range;
597}