CFG: print errors for enums

This commit is contained in:
Nekotekina 2017-08-13 18:30:44 +03:00
parent 24f9f06279
commit 2047cb8d1d

View File

@ -87,6 +87,8 @@ bool cfg::try_to_int64(s64* out, const std::string& value, s64 min, s64 max)
bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, const std::string& value) bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, const std::string& value)
{ {
u64 max = -1;
for (u64 i = 0;; i++) for (u64 i = 0;; i++)
{ {
std::string var; std::string var;
@ -104,6 +106,8 @@ bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) f
{ {
break; break;
} }
max = i;
} }
try try
@ -113,14 +117,22 @@ bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) f
if (pos != value.size()) if (pos != value.size())
{ {
if (out) cfg.error("cfg::try_to_enum_value('%s'): unexpected characters (pos=%zu)", value, pos);
return false;
}
if (val > max)
{
if (out) cfg.error("cfg::try_to_enum_value('%s'): out of bounds(0..%u)", value, max);
return false; return false;
} }
if (out) *out = val; if (out) *out = val;
return true; return true;
} }
catch (...) catch (const std::exception& e)
{ {
if (out) cfg.error("cfg::try_to_enum_value('%s'): invalid enum value: %s", value, e.what());
return false; return false;
} }
} }