summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--src/game-server/commandhandler.cpp68
-rw-r--r--src/utils/string.cpp12
-rw-r--r--src/utils/string.hpp1
4 files changed, 33 insertions, 54 deletions
diff --git a/ChangeLog b/ChangeLog
index 83585190..bd5bfaa1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-11-06 David Athay <ko2fan@gmail.com>
+
+ * src/game-server/commandhandler.cpp, src/utils/string.cpp,
+ src/utils/string.hpp: Use a string util for converting strings to
+ integers, as per Bjorns suggestion.
+
2008-11-05 Dennis Friis <peavey@placid.dk>
* src/game-server/commandhandler.cpp: Fix integer arguments randomly
failing due to stream flags not being cleared. To reuse a stream you
diff --git a/src/game-server/commandhandler.cpp b/src/game-server/commandhandler.cpp
index 55610b7c..a9948f9a 100644
--- a/src/game-server/commandhandler.cpp
+++ b/src/game-server/commandhandler.cpp
@@ -36,10 +36,6 @@
#include "../utils/string.hpp"
-#include <sstream>
-
-
-
static void say(const std::string error, Character *player)
{
GameState::sayTo(player, NULL, error);
@@ -115,7 +111,6 @@ static void handleHelp(Character *player, std::string &args)
static void handleWarp(Character *player, std::string &args)
{
- std::stringstream str;
int x, y;
MapComposite *map;
Character *other;
@@ -164,9 +159,7 @@ static void handleWarp(Character *player, std::string &args)
return;
}
- str.str(mapstr);
- str >> id;
- str.clear();
+ id = utils::stringToInt(mapstr);
// get the map
map = MapManager::getMap(id);
@@ -190,12 +183,8 @@ static void handleWarp(Character *player, std::string &args)
}
// change the x and y to integers
- str.str(xstr);
- str >> x;
- str.clear();
- str.str(ystr);
- str >> y;
- str.clear();
+ x = utils::stringToInt(xstr);
+ y = utils::stringToInt(ystr);
// now warp the player
GameState::warp(other, map, x, y);
@@ -207,7 +196,6 @@ static void handleItem(Character *player, std::string &args)
ItemClass *ic;
int value;
int id;
- std::stringstream str;
// get arguments
std::string character = getArgument(args);
@@ -245,9 +233,7 @@ static void handleItem(Character *player, std::string &args)
}
// put the itemclass id into an integer
- str.str(itemclass);
- str >> id;
- str.clear();
+ id = utils::stringToInt(itemclass);
// check for valid item class
ic = ItemManager::getItem(id);
@@ -264,9 +250,7 @@ static void handleItem(Character *player, std::string &args)
return;
}
- str.str(valuestr);
- str >> value;
- str.clear();
+ value = utils::stringToInt(valuestr);
if (value < 0)
{
@@ -282,7 +266,6 @@ static void handleDrop(Character *player, std::string &args)
{
ItemClass *ic;
int value, id;
- std::stringstream str;
// get arguments
std::string itemclass = getArgument(args);
@@ -303,9 +286,7 @@ static void handleDrop(Character *player, std::string &args)
}
// put the item class id into an integer
- str.str(itemclass);
- str >> id;
- str.clear();
+ id = utils::stringToInt(itemclass);
// check for valid item
ic = ItemManager::getItem(id);
@@ -316,9 +297,7 @@ static void handleDrop(Character *player, std::string &args)
}
// put the value into an integer
- str.str(valuestr);
- str >> value;
- str.clear();
+ value = utils::stringToInt(valuestr);
if (value < 0)
{
@@ -337,7 +316,6 @@ static void handleMoney(Character *player, std::string &args)
{
Character *other;
int value;
- std::stringstream str;
// get arguments
std::string character = getArgument(args);
@@ -374,9 +352,7 @@ static void handleMoney(Character *player, std::string &args)
}
// change value into an integer
- str.str(valuestr);
- str >> value;
- str.clear();
+ value = utils::stringToInt(valuestr);
// change how much money the player has
Inventory(other).changeMoney(value);
@@ -388,7 +364,6 @@ static void handleSpawn(Character *player, std::string &args)
MapComposite *map = player->getMap();
Point const &pos = player->getPosition();
int value, id;
- std::stringstream str;
// get the arguments
std::string monsterclass = getArgument(args);
@@ -409,9 +384,7 @@ static void handleSpawn(Character *player, std::string &args)
}
// put the monster class id into an integer
- str.str(monsterclass);
- str >> id;
- str.clear();
+ id = utils::stringToInt(monsterclass);
// check for valid monster
mc = MonsterManager::getMonster(id);
@@ -422,9 +395,7 @@ static void handleSpawn(Character *player, std::string &args)
}
// put the amount into an integer
- str.str(valuestr);
- str >> value;
- str.clear();
+ value = utils::stringToInt(valuestr);
if (value < 0)
{
@@ -522,7 +493,6 @@ static void handleBan(Character *player, std::string &args)
{
Character *other;
int length;
- std::stringstream str;
// get arguments
std::string character = getArgument(args);
@@ -551,9 +521,7 @@ static void handleBan(Character *player, std::string &args)
}
// change the length to an integer
- str.str(valuestr);
- str >> length;
- str.clear();
+ length = utils::stringToInt(valuestr);
if (length < 0)
{
@@ -569,7 +537,6 @@ static void handleLevel(Character *player, std::string &args)
{
Character *other;
int level;
- std::stringstream str;
// get the arguments
std::string character = getArgument(args);
@@ -606,9 +573,7 @@ static void handleLevel(Character *player, std::string &args)
}
// put the amount into an integer
- str.str(valuestr);
- str >> level;
- str.clear();
+ level = utils::stringToInt(valuestr);
if (level < 0)
{
@@ -624,7 +589,6 @@ static void handleAttribute(Character *player, std::string &args)
{
Character *other;
int attr, value;
- std::stringstream str;
// get arguments
std::string character = getArgument(args);
@@ -662,9 +626,7 @@ static void handleAttribute(Character *player, std::string &args)
}
// put the attribute into an integer
- str.str(attrstr);
- str >> attr;
- str.clear();
+ attr = utils::stringToInt(attrstr);
if (attr < 0)
{
@@ -673,9 +635,7 @@ static void handleAttribute(Character *player, std::string &args)
}
// put the value into an integer
- str.str(valuestr);
- str >> value;
- str.clear();
+ value = utils::stringToInt(valuestr);
if (value < 0)
{
diff --git a/src/utils/string.cpp b/src/utils/string.cpp
index abd6eb88..79ca4dc6 100644
--- a/src/utils/string.cpp
+++ b/src/utils/string.cpp
@@ -23,6 +23,7 @@
#include <cctype>
#include <algorithm>
+#include <sstream>
std::string utils::toupper(std::string s)
{
@@ -42,3 +43,14 @@ bool utils::isNumeric(const std::string &s)
return true;
}
+
+int utils::stringToInt(const std::string &s)
+{
+ int value;
+ std::stringstream str(s);
+
+ // put the string into the int
+ str >> value;
+
+ return value;
+}
diff --git a/src/utils/string.hpp b/src/utils/string.hpp
index 2adf31b6..678671d2 100644
--- a/src/utils/string.hpp
+++ b/src/utils/string.hpp
@@ -28,6 +28,7 @@ namespace utils
{
std::string toupper(std::string);
bool isNumeric(const std::string &);
+ int stringToInt(const std::string &);
}
#endif // UTILS_STRING_HPP