32#ifndef OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP
33#define OPENSWMM_ENGINE_INPUT_PARSE_UTILS_HPP
53inline double to_double(std::string_view sv,
double def = 0.0) noexcept {
62inline int to_int(std::string_view sv,
int def = 0) noexcept {
64 auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(), v);
65 if (ec != std::errc{})
return def;
77 unsigned m = 0, d = 0,
y = 0;
78 const char* p = sv.data();
79 const char* end = sv.data() + sv.size();
81 auto read_uint = [&](
unsigned& out) ->
bool {
82 auto [np, ec] = std::from_chars(p, end, out);
83 if (ec != std::errc{})
return false;
88 if (!read_uint(m))
return 0.0;
89 if (p < end && *p ==
'/') ++p;
else return 0.0;
90 if (!read_uint(d))
return 0.0;
91 if (p < end && *p ==
'/') ++p;
else return 0.0;
92 if (!read_uint(
y))
return 0.0;
111 if (ec == std::errc{} && ptr == sv.data() + sv.size()) {
116 unsigned h = 0, m = 0;
118 const char* p = sv.data();
119 const char* end = sv.data() + sv.size();
121 auto read_uint = [&](
unsigned& out) ->
bool {
122 auto [np, nec] = std::from_chars(p, end, out);
123 if (nec != std::errc{})
return false;
128 if (!read_uint(h))
return 0.0;
129 if (p < end && *p ==
':') {
131 if (!read_uint(m))
return static_cast<double>(h);
132 if (p < end && *p ==
':') {
137 return h * 3600.0 + m * 60.0 + s;
146 unsigned h = 0, m = 0, s = 0;
147 const char* p = sv.data();
148 const char* end = sv.data() + sv.size();
150 auto read_uint = [&](
unsigned& out) ->
bool {
151 auto [np, ec] = std::from_chars(p, end, out);
152 if (ec != std::errc{})
return false;
157 if (!read_uint(h))
return 0.0;
158 if (p < end && *p ==
':') ++p;
160 if (p < end && *p ==
':') ++p;
165 static_cast<int>(s));
175inline double parse_datetime(std::string_view date_sv, std::string_view time_sv) {
201 std::vector<std::string>& errors) {
202 const int idx = reg.
try_add(name);
204 const std::string* canon = reg.
canonical(name);
205 if (canon !=
nullptr && *canon != name) {
207 "(matches existing ID '" + *canon +
208 "'; 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)
Non-throwing add.
Definition NameIndex.hpp:104
const std::string * canonical(std::string_view name) const noexcept
Stored (original) spelling for a case-insensitive match.
Definition NameIndex.hpp:144
DateTime encodeTime(int hour, int minute, int second)
Encode hour:minute:second to fractional day.
Definition DateTime.hpp:110
DateTime encodeDate(int year, int month, int day)
Encode year-month-day to DateTime.
Definition DateTime.hpp:92
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:70
@ ERR_DUP_NAME
duplicate ID name s
Definition ErrorCodes.hpp:122
double * y
Definition odesolve.c:28