OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
InputParseUtils.hpp
Go to the documentation of this file.
1
16#ifndef OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP
17#define OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP
18
19#include <string_view>
20#include <string>
21#include <charconv>
22#include "../core/charconv_compat.hpp"
23#include "../core/DateTime.hpp"
24
25namespace openswmm::input {
26
27// ============================================================================
28// Numeric parsing
29// ============================================================================
30
34inline double to_double(std::string_view sv, double def = 0.0) noexcept {
35 double v = def;
36 openswmm::from_chars_double(sv.data(), sv.data() + sv.size(), v);
37 return v;
38}
39
43inline int to_int(std::string_view sv, int def = 0) noexcept {
44 int v = def;
45 auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(), v);
46 if (ec != std::errc{}) return def;
47 return v;
48}
49
50// ============================================================================
51// Date/time parsing
52// ============================================================================
53
57inline double parse_date(std::string_view sv) {
58 unsigned m = 0, d = 0, y = 0;
59 const char* p = sv.data();
60 const char* end = sv.data() + sv.size();
61
62 auto read_uint = [&](unsigned& out) -> bool {
63 auto [np, ec] = std::from_chars(p, end, out);
64 if (ec != std::errc{}) return false;
65 p = np;
66 return true;
67 };
68
69 if (!read_uint(m)) return 0.0;
70 if (p < end && *p == '/') ++p; else return 0.0;
71 if (!read_uint(d)) return 0.0;
72 if (p < end && *p == '/') ++p; else return 0.0;
73 if (!read_uint(y)) return 0.0;
74
75 return datetime::encodeDate(static_cast<int>(y),
76 static_cast<int>(m),
77 static_cast<int>(d));
78}
79
88inline double parse_time_seconds(std::string_view sv) {
89 // Try plain number first
90 double val = 0.0;
91 auto [ptr, ec] = openswmm::from_chars_double(sv.data(), sv.data() + sv.size(), val);
92 if (ec == std::errc{} && ptr == sv.data() + sv.size()) {
93 return val; // plain number = seconds
94 }
95
96 // Try HH:MM:SS or HH:MM
97 unsigned h = 0, m = 0;
98 double s = 0.0;
99 const char* p = sv.data();
100 const char* end = sv.data() + sv.size();
101
102 auto read_uint = [&](unsigned& out) -> bool {
103 auto [np, nec] = std::from_chars(p, end, out);
104 if (nec != std::errc{}) return false;
105 p = np;
106 return true;
107 };
108
109 if (!read_uint(h)) return 0.0;
110 if (p < end && *p == ':') {
111 ++p;
112 if (!read_uint(m)) return static_cast<double>(h);
113 if (p < end && *p == ':') {
114 ++p;
116 }
117 }
118 return h * 3600.0 + m * 60.0 + s;
119}
120
126inline double parse_time_day_fraction(std::string_view sv) {
127 unsigned h = 0, m = 0, s = 0;
128 const char* p = sv.data();
129 const char* end = sv.data() + sv.size();
130
131 auto read_uint = [&](unsigned& out) -> bool {
132 auto [np, ec] = std::from_chars(p, end, out);
133 if (ec != std::errc{}) return false;
134 p = np;
135 return true;
136 };
137
138 if (!read_uint(h)) return 0.0;
139 if (p < end && *p == ':') ++p;
140 read_uint(m);
141 if (p < end && *p == ':') ++p;
142 read_uint(s);
143
144 return datetime::encodeTime(static_cast<int>(h),
145 static_cast<int>(m),
146 static_cast<int>(s));
147}
148
156inline double parse_datetime(std::string_view date_sv, std::string_view time_sv) {
157 return parse_date(date_sv) + parse_time_day_fraction(time_sv);
158}
159
160} /* namespace openswmm::input */
161
162#endif /* OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP */
DateTime encodeTime(int hour, int minute, int second)
Encode hour:minute:second to fractional day.
Definition DateTime.hpp:94
DateTime encodeDate(int year, int month, int day)
Encode year-month-day to DateTime.
Definition DateTime.hpp:76
Definition CatchmentHandler.cpp:45
double parse_datetime(std::string_view date_sv, std::string_view time_sv)
Parse a combined date + time pair to a DateTime.
Definition InputParseUtils.hpp:156
double parse_time_day_fraction(std::string_view sv)
Parse a time string HH:MM:SS to a fractional day (DateTime).
Definition InputParseUtils.hpp:126
double to_double(std::string_view sv, double def=0.0) noexcept
Parse a double from a string_view, returning a default on failure.
Definition InputParseUtils.hpp:34
double parse_time_seconds(std::string_view sv)
Parse a time string to seconds.
Definition InputParseUtils.hpp:88
int to_int(std::string_view sv, int def=0) noexcept
Parse an int from a string_view, returning a default on failure.
Definition InputParseUtils.hpp:43
double parse_date(std::string_view sv)
Parse a date string in MM/DD/YYYY format to a DateTime (decimal days).
Definition InputParseUtils.hpp:57
std::from_chars_result from_chars_double(const char *first, const char *last, double &value) noexcept
Locale-independent parse of a double from a character range.
Definition charconv_compat.hpp:29
double * y
Definition odesolve.c:28