|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-14 18:43:23
|
Module: performous
Branch: dance
Commit: 3611554238c2940c4ca8c91fd12f04f32daeaf0c
Author: Markus Raab <un...@ma...>
Date: Wed Nov 11 10:40:43 2009 +0100
improved error checking
---
game/database.hh | 8 ++++++++
game/hiscore.cc | 12 ++++++++----
game/hiscore.hh | 8 ++------
game/players.cc | 2 ++
game/players.hh | 7 +++++++
5 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/game/database.hh b/game/database.hh
index 64a57b7..8ceb4d7 100644
--- a/game/database.hh
+++ b/game/database.hh
@@ -18,6 +18,14 @@ public:
Database (fs::path filename);
~Database ();
+ /**Loads the whole database from xml.
+ @exception bad_cast may be thrown if xml element is not of correct type
+ @exception xmlpp exceptions may be thrown on any parse errors
+ @exception PlayersException if some conditions of players fail (e.g. no id)
+ @exception HiscoreException if some hiscore conditions fail (e.g. score too high)
+ @exception SongsExceptions if some songs conditions fail (e.g. no id)
+ @post filled database
+ */
void load();
void save();
diff --git a/game/hiscore.cc b/game/hiscore.cc
index 2082a60..326e3c3 100644
--- a/game/hiscore.cc
+++ b/game/hiscore.cc
@@ -4,7 +4,6 @@
#include <algorithm>
#include <boost/lexical_cast.hpp>
-#include <boost/numeric/conversion/cast.hpp>
#include <libxml++/libxml++.h>
@@ -30,18 +29,23 @@ void Hiscore::load(xmlpp::NodeSet const& n)
{
xmlpp::Element& element = dynamic_cast<xmlpp::Element&>(**it);
xmlpp::Attribute* a_playerid = element.get_attribute("playerid");
+ if (!a_playerid) throw HiscoreException("Attribute playerid not found");
xmlpp::Attribute* a_songid = element.get_attribute("songid");
+ if (!a_songid) throw HiscoreException("Attribute songid not found");
xmlpp::Attribute* a_track = element.get_attribute("track");
int playerid = boost::lexical_cast<int>(a_playerid->get_value());
int songid = boost::lexical_cast<int>(a_songid->get_value());
xmlpp::TextNode* tn = element.get_child_text();
+ if (!tn) throw HiscoreException("Score not found");
int score = boost::lexical_cast<int>(tn->get_content());
- if (score < 0) throw boost::numeric::negative_overflow();
- if (score > 10000) throw boost::numeric::positive_overflow();
+ if (score < 0) throw HiscoreException("Score negativ overflow");
+ if (score > 10000) throw HiscoreException("Score positive overflow");
- std::string track = a_track->get_value();
+ std::string track;
+ if (!a_track) track = "VOCALS";
+ else track = a_track->get_value();
addHiscore(score, playerid, songid, track);
}
diff --git a/game/hiscore.hh b/game/hiscore.hh
index a7cbe89..88261c4 100644
--- a/game/hiscore.hh
+++ b/game/hiscore.hh
@@ -9,13 +9,9 @@ namespace xmlpp { class Node; class Element; typedef std::vector<Node*>NodeSet;
/**Exception which will be thrown when loading or
saving a SongHiscore fails.*/
struct HiscoreException: public std::runtime_error {
- HiscoreException (std::string const& msg, unsigned int linenum) :
- runtime_error(msg), m_linenum(linenum)
+ HiscoreException (std::string const& msg) :
+ runtime_error(msg)
{}
- /**Line information where the problem occured.*/
- unsigned int line() const {return m_linenum;}
- private:
- unsigned int m_linenum;
};
/**This struct holds together information for a
diff --git a/game/players.cc b/game/players.cc
index 51eedae..c1d4b50 100644
--- a/game/players.cc
+++ b/game/players.cc
@@ -29,7 +29,9 @@ void Players::load(xmlpp::NodeSet const& n) {
{
xmlpp::Element& element = dynamic_cast<xmlpp::Element&>(**it);
xmlpp::Attribute* a_name = element.get_attribute("name");
+ if (!a_name) throw PlayersException("Attribute name not found");
xmlpp::Attribute* a_id = element.get_attribute("id");
+ if (!a_id) throw PlayersException("Attribute id not found");
int id = -1;
try {id = boost::lexical_cast<int>(a_id->get_value());} catch (boost::bad_lexical_cast const&) { }
xmlpp::NodeSet n2 = element.find("picture");
diff --git a/game/players.hh b/game/players.hh
index d8a64d1..17cc6d3 100644
--- a/game/players.hh
+++ b/game/players.hh
@@ -13,6 +13,13 @@
namespace xmlpp { class Node; class Element; typedef std::vector<Node*>NodeSet; }
+/**Exception which will be thrown when loading or
+ saving Players fails.*/
+struct PlayersException: public std::runtime_error {
+ PlayersException (std::string const& msg) :
+ runtime_error(msg)
+ {}
+};
/**A collection of all Players.
|