more generic solution for badge units
Costa Tsaousis (ktsaou) committed
Jul 11, 2018 at 03:43 UTC
74fd1025a0cdf83cfac8743897b1c0e8ea48021f
1 file changed
+80
-64
src/web_buffer_svg.c
+80
-64
@@ -429,55 +429,81 @@ static inline char *format_value_with_precision_and_unit(char *value_string, siz
429
return value_string;
430
}
431
432
+typedef enum badge_units_format {
433
+ UNITS_FORMAT_NONE,
434
+ UNITS_FORMAT_SECONDS,
435
+ UNITS_FORMAT_SECONDS_AGO,
436
+ UNITS_FORMAT_MINUTES,
437
+ UNITS_FORMAT_MINUTES_AGO,
438
+ UNITS_FORMAT_HOURS,
439
+ UNITS_FORMAT_HOURS_AGO,
440
+ UNITS_FORMAT_ONOFF,
441
+ UNITS_FORMAT_UPDOWN,
442
+ UNITS_FORMAT_OKERROR,
443
+ UNITS_FORMAT_OKFAILED,
444
+ UNITS_FORMAT_EMPTY,
445
+ UNITS_FORMAT_PERCENT
446
+} UNITS_FORMAT;
447
+
448
+
449
+struct units_formatter {
450
+ const char *units;
451
+ uint32_t hash;
452
+ UNITS_FORMAT format;
453
+} badge_units_formatters[] = {
454
+ { "seconds", 0, UNITS_FORMAT_SECONDS },
455
+ { "seconds ago", 0, UNITS_FORMAT_SECONDS_AGO },
456
+ { "minutes", 0, UNITS_FORMAT_MINUTES },
457
+ { "minutes ago", 0, UNITS_FORMAT_MINUTES_AGO },
458
+ { "hours", 0, UNITS_FORMAT_HOURS },
459
+ { "hours ago", 0, UNITS_FORMAT_HOURS_AGO },
460
+ { "on/off", 0, UNITS_FORMAT_ONOFF },
461
+ { "on-off", 0, UNITS_FORMAT_ONOFF },
462
+ { "onoff", 0, UNITS_FORMAT_ONOFF },
463
+ { "up/down", 0, UNITS_FORMAT_UPDOWN },
464
+ { "up-down", 0, UNITS_FORMAT_UPDOWN },
465
+ { "updown", 0, UNITS_FORMAT_UPDOWN },
466
+ { "ok/error", 0, UNITS_FORMAT_OKERROR },
467
+ { "ok-error", 0, UNITS_FORMAT_OKERROR },
468
+ { "okerror", 0, UNITS_FORMAT_OKERROR },
469
+ { "ok/failed", 0, UNITS_FORMAT_OKFAILED },
470
+ { "ok-failed", 0, UNITS_FORMAT_OKFAILED },
471
+ { "okfailed", 0, UNITS_FORMAT_OKFAILED },
472
+ { "empty", 0, UNITS_FORMAT_EMPTY },
473
+ { "null", 0, UNITS_FORMAT_EMPTY },
474
+ { "percentage", 0, UNITS_FORMAT_PERCENT },
475
+ { "percent", 0, UNITS_FORMAT_PERCENT },
476
+ { "pcent", 0, UNITS_FORMAT_PERCENT },
477
+
478
+ // terminator
479
+ { NULL, 0, UNITS_FORMAT_NONE }
480
+};
481
+
482
inline char *format_value_and_unit(char *value_string, size_t value_string_len, calculated_number value, const char *units, int precision) {
433
- static uint32_t
434
- hash_seconds = 0,
435
- hash_seconds_ago = 0,
436
- hash_minutes = 0,
437
- hash_minutes_ago = 0,
438
- hash_hours = 0,
439
- hash_hours_ago = 0,
440
- hash_onoff = 0,
441
- hash_onoff2 = 0,
442
- hash_updown = 0,
443
- hash_updown2 = 0,
444
- hash_okerror = 0,
445
- hash_okerror2 = 0,
446
- hash_okfailed = 0,
447
- hash_okfailed2 = 0,
448
- hash_empty = 0,
449
- hash_null = 0,
450
- hash_percentage = 0,
451
- hash_percent = 0,
452
- hash_pcent = 0;
453
-
454
- if(unlikely(!hash_seconds)) {
455
- hash_seconds = simple_hash("seconds");
456
- hash_seconds_ago = simple_hash("seconds ago");
457
- hash_minutes = simple_hash("minutes");
458
- hash_minutes_ago = simple_hash("minutes ago");
459
- hash_hours = simple_hash("hours");
460
- hash_hours_ago = simple_hash("hours ago");
461
- hash_onoff = simple_hash("on/off");
462
- hash_onoff2 = simple_hash("on-off");
463
- hash_updown = simple_hash("up/down");
464
- hash_updown2 = simple_hash("up-down");
465
- hash_okerror = simple_hash("ok/error");
466
- hash_okerror2 = simple_hash("ok-error");
467
- hash_okfailed = simple_hash("ok/failed");
468
- hash_okfailed2 = simple_hash("ok-failed");
469
- hash_empty = simple_hash("empty");
470
- hash_null = simple_hash("null");
471
- hash_percentage = simple_hash("percentage");
472
- hash_percent = simple_hash("percent");
473
- hash_pcent = simple_hash("pcent");
483
+ static int max = -1;
484
+ int i;
485
+
486
+ if(unlikely(max == -1)) {
487
+ for(i = 0; badge_units_formatters[i].units; i++)
488
+ badge_units_formatters[i].hash = simple_hash(badge_units_formatters[i].units);
489
+
490
+ max = i;
491
}
492
493
if(unlikely(!units)) units = "";
477
-
494
uint32_t hash_units = simple_hash(units);
495
480
- if(unlikely((hash_units == hash_seconds && !strcmp(units, "seconds")) || (hash_units == hash_seconds_ago && !strcmp(units, "seconds ago")))) {
496
+ UNITS_FORMAT format = UNITS_FORMAT_NONE;
497
+ for(i = 0; i < max; i++) {
498
+ struct units_formatter *ptr = &badge_units_formatters[i];
499
+
500
+ if(hash_units == ptr->hash && !strcmp(units, ptr->units)) {
501
+ format = ptr->format;
502
+ break;
503
+ }
504
+ }
505
+
506
+ if(unlikely(format == UNITS_FORMAT_SECONDS || format == UNITS_FORMAT_SECONDS_AGO)) {
507
if(value == 0.0) {
508
snprintfz(value_string, value_string_len, "%s", "now");
509
return value_string;
@@ -487,7 +513,7 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
513
return value_string;
514
}
515
490
- const char *suffix = (hash_units == hash_seconds_ago)?" ago":"";
516
+ const char *suffix = (format == UNITS_FORMAT_SECONDS_AGO)?" ago":"";
517
518
size_t s = (size_t)value;
519
size_t d = s / 86400;
@@ -507,7 +533,7 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
533
return value_string;
534
}
535
510
- else if(unlikely((hash_units == hash_minutes && !strcmp(units, "minutes")) || (hash_units == hash_minutes_ago && !strcmp(units, "minutes ago")))) {
536
+ else if(unlikely(format == UNITS_FORMAT_MINUTES || format == UNITS_FORMAT_MINUTES_AGO)) {
537
if(value == 0.0) {
538
snprintfz(value_string, value_string_len, "%s", "now");
539
return value_string;
@@ -517,7 +543,7 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
543
return value_string;
544
}
545
520
- const char *suffix = (hash_units == hash_minutes_ago)?" ago":"";
546
+ const char *suffix = (format == UNITS_FORMAT_MINUTES_AGO)?" ago":"";
547
548
size_t m = (size_t)value;
549
size_t d = m / (60 * 24);
@@ -534,7 +560,7 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
560
return value_string;
561
}
562
537
- else if(unlikely((hash_units == hash_hours && !strcmp(units, "hours")) || (hash_units == hash_hours_ago && !strcmp(units, "hours ago")))) {
563
+ else if(unlikely(format == UNITS_FORMAT_HOURS || format == UNITS_FORMAT_HOURS_AGO)) {
564
if(value == 0.0) {
565
snprintfz(value_string, value_string_len, "%s", "now");
566
return value_string;
@@ -544,7 +570,7 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
570
return value_string;
571
}
572
547
- const char *suffix = (hash_units == hash_hours_ago)?" ago":"";
573
+ const char *suffix = (format == UNITS_FORMAT_HOURS_AGO)?" ago":"";
574
575
size_t h = (size_t)value;
576
size_t d = h / 24;
@@ -558,42 +584,32 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
584
return value_string;
585
}
586
561
- else if(unlikely((hash_units == hash_onoff && !strcmp(units, "on/off")) || (hash_units == hash_onoff2 && !strcmp(units, "on-off")))) {
587
+ else if(unlikely(format == UNITS_FORMAT_ONOFF)) {
588
snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"on":"off");
589
return value_string;
590
}
591
566
- else if(unlikely((hash_units == hash_updown && !strcmp(units, "up/down")) || (hash_units == hash_updown2 && !strcmp(units, "up-down")))) {
592
+ else if(unlikely(format == UNITS_FORMAT_UPDOWN)) {
593
snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"up":"down");
594
return value_string;
595
}
596
571
- else if(unlikely((hash_units == hash_okerror && !strcmp(units, "ok/error")) || (hash_units == hash_okerror2 && !strcmp(units, "ok-error")))) {
597
+ else if(unlikely(format == UNITS_FORMAT_OKERROR)) {
598
snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"ok":"error");
599
return value_string;
600
}
601
576
- else if(unlikely((hash_units == hash_okfailed && !strcmp(units, "ok/failed")) || (hash_units == hash_okfailed2 && !strcmp(units, "ok-failed")))) {
602
+ else if(unlikely(format == UNITS_FORMAT_OKFAILED)) {
603
snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"ok":"failed");
604
return value_string;
605
}
606
581
- else if(unlikely(hash_units == hash_empty && !strcmp(units, "empty")))
582
- units = "";
583
-
584
- else if(unlikely(hash_units == hash_null && !strcmp(units, "null")))
607
+ else if(unlikely(format == UNITS_FORMAT_EMPTY))
608
units = "";
609
587
- else if(unlikely(hash_units == hash_percentage && !strcmp(units, "percentage")))
588
- units = "%";
589
-
590
- else if(unlikely(hash_units == hash_percent && !strcmp(units, "percent")))
591
- units = "%";
592
-
593
- else if(unlikely(hash_units == hash_pcent && !strcmp(units, "pcent")))
610
+ else if(unlikely(format == UNITS_FORMAT_PERCENT))
611
units = "%";
612
596
-
613
if(unlikely(isnan(value) || isinf(value))) {
614
strcpy(value_string, "-");
615
return value_string;