|
7 | 7 |
|
8 | 8 | #pragma once |
9 | 9 |
|
| 10 | +#include <chrono> |
| 11 | +#include <regex> |
| 12 | +#include <stdexcept> |
| 13 | + |
10 | 14 | #include <jansson.h> |
11 | 15 |
|
12 | 16 | #include <villas/node/config.hpp> |
13 | 17 | #include <villas/sample.hpp> |
14 | 18 |
|
| 19 | +#include "villas/exceptions.hpp" |
| 20 | + |
15 | 21 | #ifdef WITH_CONFIG |
16 | 22 | #include <libconfig.h> |
17 | 23 | #endif |
@@ -42,5 +48,93 @@ int json_object_extend(json_t *orig, json_t *merge); |
42 | 48 |
|
43 | 49 | json_t *json_load_cli(int argc, const char *argv[]); |
44 | 50 |
|
| 51 | +template <typename Duration = std::chrono::nanoseconds> |
| 52 | +Duration parse_duration(std::string_view input) { |
| 53 | + using namespace std::literals::chrono_literals; |
| 54 | + |
| 55 | + // Map unit strings to their corresponding chrono durations |
| 56 | + static const std::unordered_map<std::string, std::chrono::nanoseconds> |
| 57 | + unit_map = { |
| 58 | + {"d", 24h}, // days |
| 59 | + {"h", 1h}, // hours |
| 60 | + {"m", 1min}, // minutes |
| 61 | + {"s", 1s}, // seconds |
| 62 | + {"ms", 1ms}, // milliseconds |
| 63 | + {"us", 1us}, // microseconds |
| 64 | + {"ns", 1ns} // nanoseconds |
| 65 | + }; |
| 66 | + |
| 67 | + std::regex token_re(R"((\d+)([a-z]+))"); |
| 68 | + auto begin = std::regex_iterator(input.begin(), input.end(), token_re); |
| 69 | + auto end = std::regex_iterator<std::string_view::const_iterator>(); |
| 70 | + |
| 71 | + std::chrono::nanoseconds total_duration{0}; |
| 72 | + |
| 73 | + for (auto i = begin; i != end; ++i) { |
| 74 | + auto match = *i; |
| 75 | + auto number_str = match[1]; |
| 76 | + auto unit_str = match[2]; |
| 77 | + |
| 78 | + auto it = unit_map.find(unit_str); |
| 79 | + if (it == unit_map.end()) { |
| 80 | + throw RuntimeError("Unknown duration unit: {}", unit_str.str()); |
| 81 | + } |
| 82 | + |
| 83 | + auto unit = it->second; |
| 84 | + |
| 85 | + int64_t number; |
| 86 | + try { |
| 87 | + number = std::stoul(number_str); |
| 88 | + } catch (const std::invalid_argument &e) { |
| 89 | + throw RuntimeError("Invalid number in duration: {}", match.str()); |
| 90 | + } catch (const std::out_of_range &e) { |
| 91 | + throw RuntimeError("Duration overflows maximum representable value: {}", |
| 92 | + match.str()); |
| 93 | + } |
| 94 | + |
| 95 | + auto duration = unit * number; |
| 96 | + |
| 97 | + if (duration > Duration::zero() && duration < Duration(1)) |
| 98 | + throw RuntimeError("Duration underflows minimum representable value"); |
| 99 | + |
| 100 | + if (unit.count() != 0 && |
| 101 | + duration.count() / unit.count() != number) // Check for overflow. |
| 102 | + throw RuntimeError("Duration overflows maximum representable value: {}", |
| 103 | + match.str()); |
| 104 | + ; |
| 105 | + |
| 106 | + total_duration += duration; |
| 107 | + } |
| 108 | + |
| 109 | + return std::chrono::duration_cast<Duration>(total_duration); |
| 110 | +} |
| 111 | + |
| 112 | +template <typename Duration = std::chrono::nanoseconds> |
| 113 | +Duration parse_duration(json_t *json) { |
| 114 | + switch (json_typeof(json)) { |
| 115 | + case JSON_INTEGER: { |
| 116 | + int64_t value = json_integer_value(json); |
| 117 | + if (value < 0) { |
| 118 | + throw ConfigError(json, "duration-negative", "Negative duration value"); |
| 119 | + } |
| 120 | + |
| 121 | + return Duration(value); |
| 122 | + } |
| 123 | + |
| 124 | + case JSON_STRING: { |
| 125 | + try { |
| 126 | + return parse_duration<Duration>( |
| 127 | + std::string_view(json_string_value(json), json_string_length(json))); |
| 128 | + } catch (const RuntimeError &e) { |
| 129 | + throw ConfigError(json, "duration", "{}", e.what()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + default: |
| 134 | + throw ConfigError(json, "duration", |
| 135 | + "Expected a string or integer for duration"); |
| 136 | + } |
| 137 | +} |
| 138 | + |
45 | 139 | } // namespace node |
46 | 140 | } // namespace villas |
0 commit comments