32#ifndef OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP
33#define OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP
61inline double to_double(std::string_view sv,
double def = 0.0) noexcept {
83 if (sv.front() ==
'+') {
85 if (sv.empty() || sv.front() ==
'+' || sv.front() ==
'-')
90 if (ec != std::errc{} || ptr != sv.data() + sv.size())
105inline int to_int(std::string_view sv,
int def = 0) noexcept {
107 auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(), v);
108 if (ec != std::errc{})
128 unsigned m = 0, d = 0,
y = 0;
129 const char* p = sv.data();
130 const char* end = sv.data() + sv.size();
132 auto read_uint = [&](
unsigned& out) ->
bool {
133 auto [np, ec] = std::from_chars(p, end, out);
134 if (ec != std::errc{})
139 auto read_sep = [&]() ->
bool {
140 if (p < end && (*p ==
'/' || *p ==
'-')) {
177 if (ec == std::errc{} && ptr == sv.data() + sv.size()) {
182 unsigned h = 0, m = 0;
184 const char* p = sv.data();
185 const char* end = sv.data() + sv.size();
187 auto read_uint = [&](
unsigned& out) ->
bool {
188 auto [np, nec] = std::from_chars(p, end, out);
189 if (nec != std::errc{})
197 if (p < end && *p ==
':') {
200 return static_cast<double>(h);
201 if (p < end && *p ==
':') {
206 return h * 3600.0 + m * 60.0 + s;
221 const char* p = sv.data();
222 const char* end = sv.data() + sv.size();
232 double dec_hours = 0.0;
234 if (dec == std::errc{} && dp == end)
235 return dec_hours / 24.0;
237 unsigned h = 0, m = 0, s = 0;
238 auto read_uint = [&](
unsigned& out) ->
bool {
239 auto [np, ec] = std::from_chars(p, end, out);
240 if (ec != std::errc{})
248 if (p < end && *p ==
':')
251 if (p < end && *p ==
':')
266inline double parse_datetime(std::string_view date_sv, std::string_view time_sv) {
286 const int idx = reg.
try_add(name);
288 const std::string* canon = reg.
canonical(name);
289 if (canon !=
nullptr && *canon != name) {
292 "(matches existing ID '" + *canon +
"'; names are case-insensitive)"));
DateTime utility functions — numerically identical to legacy datetime.c.
Legacy-compatible error and warning codes with description lookup.
O(1) name-to-index lookup for SWMM objects.
Portable from_chars for floating-point types.
Bidirectional name↔index registry for SWMM objects.
Definition NameIndex.hpp:71
int try_add(const std::string &name)
Copy a new name, returning a duplicate sentinel instead of a duplicate exception.
Definition NameIndex.hpp:115
const std::string * canonical(std::string_view name) const noexcept
Borrow the original spelling of an equivalent registry key.
Definition NameIndex.hpp:169
DateTime encodeTime(int hour, int minute, int second)
Convert nonnegative time components to an elapsed-day fraction.
Definition DateTime.hpp:134
DateTime encodeDate(int year, int month, int day)
Encode a Gregorian calendar date as midnight in SWMM days.
Definition DateTime.hpp:110
std::from_chars_result from_chars_double(const char *first, const char *last, double &value) noexcept
Parse a double prefix using the available numeric backend.
Definition charconv_compat.hpp:76
@ ERR_DUP_NAME
duplicate ID name s
Definition ErrorCodes.hpp:124
double * y
Definition odesolve.c:37