From 255742d31162efe64c1efddbafa52d424c1061eb Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 25 Jun 2022 17:58:11 +0100 Subject: [PATCH] Make getEnum use the default instead of throwing. --- include/configreader.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/include/configreader.h b/include/configreader.h index 7a155af9a..8660644e7 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -112,14 +112,24 @@ public: if (val.empty()) return def; - for (const std::pair& enumval : enumvals) - if (stdalgo::string::equalsci(val, enumval.first)) - return enumval.second; + for (const auto& [enumkey, enumval] : enumvals) + if (stdalgo::string::equalsci(val, enumkey)) + return enumval; - std::vector enumkeys; - std::transform(enumvals.begin(), enumvals.end(), std::back_inserter(enumkeys), [](const std::pair& ev) { return ev.first; }); - throw CoreException(val + " is an invalid value for <" + name + ":" + key + ">; acceptable values are " + - stdalgo::string::join(enumkeys, ' ') + ", at " + source.str()); + // Unfortunately we have to iterate this twice. + std::string enumdef = "(unknown)"; + std::string enumkeys; + for (const auto& [enumkey, enumval] : enumvals) + { + enumkeys.append(enumkey).append(", "); + if (enumval == def) + enumdef = enumkey; + } + if (!enumkeys.empty()) + enumkeys.erase(enumkeys.length() - 2); + + LogMalformed(key, val, enumdef, "not one of " + enumkeys); + return def; } /** Get the value of an option