From 44b3d15a68b7bb87271f86f8d933aa52d40a6c16 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 22 Jul 2010 13:36:05 +0000 Subject: [PATCH] warn about invalid conversion to int/double/bool in Reader git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1459 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/util/io/reader.cpp | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/util/io/reader.cpp b/src/util/io/reader.cpp index 6a3a6f98..82f1584f 100644 --- a/src/util/io/reader.cpp +++ b/src/util/io/reader.cpp @@ -356,24 +356,39 @@ template <> void Reader::handle(String& s) { } template <> void Reader::handle(int& i) { long l = 0; - getValue().ToLong(&l); + if (!getValue().ToLong(&l)) { + warning(_("Expected integer instead of '") + previous_value + _("'")); + } i = l; } template <> void Reader::handle(unsigned int& i) { long l = 0; - getValue().ToLong(&l); + if (!getValue().ToLong(&l)) { + warning(_("Expected non-negative integer instead of '") + previous_value + _("'")); + } else if (l < 0) { + warning(wxString::Format(_("Expected non-negative integer instead of %d"),(int)l)); + } i = abs(l); // abs, because it will seem strange if -1 comes out as MAX_INT } template <> void Reader::handle(double& d) { - getValue().ToDouble(&d); + if (!getValue().ToDouble(&d)) { + warning(_("Expected floating point number instead of '") + previous_value + _("'")); + } } template <> void Reader::handle(bool& b) { const String& v = getValue(); - b = (v==_("true") || v==_("1") || v==_("yes")); + if (v==_("true") || v==_("1") || v==_("yes")) { + b = true; + } else if (v==_("false") || v==_("0") || v==_("no")) { + b = false; + } else { + warning(_("Expected boolean ('true' or 'false') instead of '") + v + _("'")); + } } -template <> void Reader::handle(tribool& b) { - const String& v = getValue(); - b = (v==_("true") || v==_("1") || v==_("yes")); +template <> void Reader::handle(tribool& tb) { + bool b; + handle(b); + tb = b; } // ----------------------------------------------------------------------------- : Handling less basic util types