From 7e43b82e3b7cda034fab34c15ecfaa97c1a99146 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Wed, 18 Feb 2009 20:14:23 +0100 Subject: Introduced a toLower method and grouped string utils The string utility methods are now grouped together in the stringutils.h header. Also, a toLower method was added for convenience. --- src/CMakeLists.txt | 4 +-- src/Makefile.am | 4 +-- src/being.cpp | 3 +- src/configuration.cpp | 2 +- src/emoteshortcut.cpp | 2 +- src/engine.cpp | 2 +- src/gui/char_select.cpp | 2 +- src/gui/char_server.cpp | 4 +-- src/gui/chat.cpp | 23 +++---------- src/gui/color.cpp | 2 +- src/gui/debugwindow.cpp | 2 +- src/gui/emotecontainer.cpp | 2 +- src/gui/emoteshortcutcontainer.cpp | 2 +- src/gui/emotewindow.cpp | 2 +- src/gui/equipmentwindow.cpp | 2 +- src/gui/inttextfield.cpp | 2 +- src/gui/inventorywindow.cpp | 2 +- src/gui/itemcontainer.cpp | 2 +- src/gui/itempopup.cpp | 2 +- src/gui/itemshortcutcontainer.cpp | 2 +- src/gui/login.cpp | 2 +- src/gui/ministatus.cpp | 2 +- src/gui/recorder.cpp | 2 +- src/gui/register.cpp | 2 +- src/gui/setup_colors.cpp | 2 +- src/gui/setup_keyboard.cpp | 2 +- src/gui/setup_video.cpp | 2 +- src/gui/shortcutcontainer.cpp | 2 +- src/gui/status.cpp | 2 +- src/gui/trade.cpp | 2 +- src/gui/updatewindow.cpp | 2 +- src/gui/viewport.cpp | 2 +- src/itemshortcut.cpp | 2 +- src/localplayer.cpp | 2 +- src/main.cpp | 4 +-- src/map.cpp | 2 +- src/net/charserverhandler.cpp | 4 +-- src/net/chathandler.cpp | 3 +- src/net/inventoryhandler.cpp | 2 +- src/net/loginhandler.cpp | 4 +-- src/net/network.cpp | 4 +-- src/net/playerhandler.cpp | 2 +- src/resources/itemdb.cpp | 9 ++--- src/resources/mapreader.cpp | 2 +- src/units.cpp | 3 +- src/utils/stringutils.cpp | 63 +++++++++++++++++++++++++++++++++++ src/utils/stringutils.h | 68 ++++++++++++++++++++++++++++++++++++++ src/utils/tostring.h | 48 --------------------------- src/utils/trim.h | 51 ---------------------------- 49 files changed, 188 insertions(+), 177 deletions(-) create mode 100644 src/utils/stringutils.cpp create mode 100644 src/utils/stringutils.h delete mode 100644 src/utils/tostring.h delete mode 100644 src/utils/trim.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 25f1a4bc..38d3d82e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -296,10 +296,10 @@ SET(SRCS utils/dtor.h utils/fastsqrt.h utils/gettext.h + utils/stringutils.cpp + utils/stringutils.h utils/strprintf.cpp utils/strprintf.h - utils/tostring.h - utils/trim.h utils/xml.cpp utils/xml.h animatedsprite.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 2777f38b..e8e9a695 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -246,10 +246,10 @@ tmw_SOURCES = gui/widgets/dropdown.cpp \ utils/dtor.h \ utils/fastsqrt.h \ utils/gettext.h \ + utils/stringutils.cpp \ + utils/stringutils.h \ utils/strprintf.cpp \ utils/strprintf.h \ - utils/tostring.h \ - utils/trim.h \ utils/mutex.h \ utils/xml.cpp \ utils/xml.h \ diff --git a/src/being.cpp b/src/being.cpp index 45029a01..43480eba 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -49,8 +49,7 @@ #include "utils/dtor.h" #include "utils/gettext.h" -#include "utils/tostring.h" -#include "utils/trim.h" +#include "utils/stringutils.h" #include "utils/xml.h" #include diff --git a/src/configuration.cpp b/src/configuration.cpp index 7a1ecf3c..3c3ae1d5 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -23,7 +23,7 @@ #include "configuration.h" #include "log.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" #include "utils/xml.h" void ConfigurationObject::setValue(const std::string &key, std::string value) diff --git a/src/emoteshortcut.cpp b/src/emoteshortcut.cpp index 807fa3a2..46e13782 100644 --- a/src/emoteshortcut.cpp +++ b/src/emoteshortcut.cpp @@ -24,7 +24,7 @@ #include "configuration.h" #include "localplayer.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" EmoteShortcut::EmoteShortcut *emoteShortcut; diff --git a/src/engine.cpp b/src/engine.cpp index 39b9b3ec..a67d379f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -39,7 +39,7 @@ #include "resources/mapreader.h" #include "resources/resourcemanager.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" char itemCurrenyQ[10] = "0"; diff --git a/src/gui/char_select.cpp b/src/gui/char_select.cpp index 3b088d46..34218477 100644 --- a/src/gui/char_select.cpp +++ b/src/gui/char_select.cpp @@ -46,7 +46,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/trim.h" +#include "../utils/stringutils.h" // Defined in main.cpp, used here for setting the char create dialog extern CharServerHandler charServerHandler; diff --git a/src/gui/char_server.cpp b/src/gui/char_server.cpp index 8e7b0dbc..89bfa93a 100644 --- a/src/gui/char_server.cpp +++ b/src/gui/char_server.cpp @@ -29,7 +29,7 @@ #include "../serverinfo.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" extern SERVER_INFO **server_info; @@ -101,7 +101,7 @@ void ServerSelectDialog::action(const gcn::ActionEvent &event) { mOkButton->setEnabled(false); const SERVER_INFO *si = server_info[mServerList->getSelected()]; - mLoginData->hostname = iptostring(si->address); + mLoginData->hostname = ipToString(si->address); mLoginData->port = si->port; mLoginData->updateHost = si->updateHost; state = mNextState; diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 89588cee..ac13355c 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -46,8 +46,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" -#include "../utils/trim.h" +#include "../utils/stringutils.h" ChatWindow::ChatWindow(Network * network): Window(""), mNetwork(network), mTmpVisible(false) @@ -338,15 +337,8 @@ void ChatWindow::whisper(const std::string &nick, std::string msg) std::string playerName = player_node->getName(); std::string tempNick = recvnick; - for (unsigned int i = 0; i < playerName.size(); i++) - { - playerName[i] = (char) tolower(playerName[i]); - } - - for (unsigned int i = 0; i < tempNick.size(); i++) - { - tempNick[i] = (char) tolower(tempNick[i]); - } + toLower(playerName); + toLower(tempNick); if (tempNick.compare(playerName) == 0 || msg.empty()) return; @@ -407,14 +399,9 @@ void ChatWindow::chatSend(const std::string &nick, std::string msg) start = msg.find('[', start + 1); } - std::string temp = msg.substr(start+1, end - start - 1); - - trim(temp); + std::string temp = msg.substr(start + 1, end - start - 1); - for (unsigned int i = 0; i < temp.size(); i++) - { - temp[i] = (char) tolower(temp[i]); - } + toLower(trim(temp)); const ItemInfo itemInfo = ItemDB::get(temp); if (itemInfo.getName() != _("Unknown item")) diff --git a/src/gui/color.cpp b/src/gui/color.cpp index 31fa8ea4..f9b89857 100644 --- a/src/gui/color.cpp +++ b/src/gui/color.cpp @@ -24,7 +24,7 @@ #include "../configuration.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" Color::Color() { diff --git a/src/gui/debugwindow.cpp b/src/gui/debugwindow.cpp index 5a5acfad..2ed891db 100644 --- a/src/gui/debugwindow.cpp +++ b/src/gui/debugwindow.cpp @@ -33,7 +33,7 @@ #include "../particle.h" #include "../map.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" DebugWindow::DebugWindow(): Window("Debug") diff --git a/src/gui/emotecontainer.cpp b/src/gui/emotecontainer.cpp index 94ce9736..e22b031b 100644 --- a/src/gui/emotecontainer.cpp +++ b/src/gui/emotecontainer.cpp @@ -38,7 +38,7 @@ #include "../utils/dtor.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" const int EmoteContainer::gridWidth = 34; // emote icon width + 4 const int EmoteContainer::gridHeight = 36; // emote icon height + 4 diff --git a/src/gui/emoteshortcutcontainer.cpp b/src/gui/emoteshortcutcontainer.cpp index b66592c1..a0739723 100644 --- a/src/gui/emoteshortcutcontainer.cpp +++ b/src/gui/emoteshortcutcontainer.cpp @@ -38,7 +38,7 @@ #include "../utils/dtor.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" static const int MAX_ITEMS = 12; diff --git a/src/gui/emotewindow.cpp b/src/gui/emotewindow.cpp index 745d10a9..48635720 100644 --- a/src/gui/emotewindow.cpp +++ b/src/gui/emotewindow.cpp @@ -30,7 +30,7 @@ #include "../localplayer.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" EmoteWindow::EmoteWindow(): Window(_("Emote")) diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp index 7361f7ab..02a0729b 100644 --- a/src/gui/equipmentwindow.cpp +++ b/src/gui/equipmentwindow.cpp @@ -40,7 +40,7 @@ #include "../resources/resourcemanager.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" // Positions of the boxes, 2nd dimension is X and Y respectively. static const int boxPosition[][2] = { diff --git a/src/gui/inttextfield.cpp b/src/gui/inttextfield.cpp index 9f633965..d3fe448b 100644 --- a/src/gui/inttextfield.cpp +++ b/src/gui/inttextfield.cpp @@ -22,7 +22,7 @@ #include "inttextfield.h" #include "sdlinput.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" IntTextField::IntTextField(int def): TextField(toString(def)), diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index 880994d7..fea86b81 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -44,7 +44,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" InventoryWindow::InventoryWindow(int invSize): Window(_("Inventory")), diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index 6e73731b..eed31f7a 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -38,7 +38,7 @@ #include "../resources/image.h" #include "../resources/resourcemanager.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" const int ItemContainer::gridWidth = 36; // item icon width + 4 const int ItemContainer::gridHeight = 42; // item icon height + 10 diff --git a/src/gui/itempopup.cpp b/src/gui/itempopup.cpp index 9e7cb8a9..48aa7c8c 100644 --- a/src/gui/itempopup.cpp +++ b/src/gui/itempopup.cpp @@ -37,7 +37,7 @@ #include "../resources/iteminfo.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" ItemPopup::ItemPopup(): Window() diff --git a/src/gui/itemshortcutcontainer.cpp b/src/gui/itemshortcutcontainer.cpp index 9d1b6375..e5990a28 100644 --- a/src/gui/itemshortcutcontainer.cpp +++ b/src/gui/itemshortcutcontainer.cpp @@ -35,7 +35,7 @@ #include "../resources/image.h" #include "../resources/resourcemanager.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" ItemShortcutContainer::ItemShortcutContainer(): ShortcutContainer(), diff --git a/src/gui/login.cpp b/src/gui/login.cpp index e9964f2e..e91f7616 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -38,7 +38,7 @@ #include "../configuration.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" static const int MAX_SERVER_LIST_SIZE = 5; static const int LOGIN_DIALOG_WIDTH = 220; diff --git a/src/gui/ministatus.cpp b/src/gui/ministatus.cpp index 7058d572..25f17bd3 100644 --- a/src/gui/ministatus.cpp +++ b/src/gui/ministatus.cpp @@ -28,7 +28,7 @@ #include "../graphics.h" #include "../localplayer.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" MiniStatusWindow::MiniStatusWindow(): Window("") diff --git a/src/gui/recorder.cpp b/src/gui/recorder.cpp index 8177f108..ce097db2 100644 --- a/src/gui/recorder.cpp +++ b/src/gui/recorder.cpp @@ -28,7 +28,7 @@ #include "widgets/layout.h" -#include "../utils/trim.h" +#include "../utils/stringutils.h" Recorder::Recorder(ChatWindow *chat, const std::string &title, const std::string &buttonTxt) : diff --git a/src/gui/register.cpp b/src/gui/register.cpp index e17f5902..c2190501 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -39,7 +39,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" /** * Listener used while dealing with wrong data. It is used to direct the focus diff --git a/src/gui/setup_colors.cpp b/src/gui/setup_colors.cpp index d8053ae6..49c99996 100644 --- a/src/gui/setup_colors.cpp +++ b/src/gui/setup_colors.cpp @@ -40,7 +40,7 @@ #include "../configuration.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" Setup_Colors::Setup_Colors() : mSelected(-1) diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp index ab85900a..aba8cf35 100644 --- a/src/gui/setup_keyboard.cpp +++ b/src/gui/setup_keyboard.cpp @@ -36,7 +36,7 @@ #include "../keyboardconfig.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" /** * The list model for key function list. diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp index fd7481a5..aa39dd36 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -46,7 +46,7 @@ #include "../particle.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" extern Graphics *graphics; diff --git a/src/gui/shortcutcontainer.cpp b/src/gui/shortcutcontainer.cpp index 282349dc..74609fa1 100644 --- a/src/gui/shortcutcontainer.cpp +++ b/src/gui/shortcutcontainer.cpp @@ -25,7 +25,7 @@ #include "../resources/image.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" float ShortcutContainer::mAlpha = config.getValue("guialpha", 0.8); diff --git a/src/gui/status.cpp b/src/gui/status.cpp index f6ca9698..b1a973fe 100644 --- a/src/gui/status.cpp +++ b/src/gui/status.cpp @@ -33,7 +33,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" StatusWindow::StatusWindow(LocalPlayer *player): Window(player->getName()), diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index 529d06b0..2f6007a9 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -44,7 +44,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" TradeWindow::TradeWindow(Network *network): Window(_("Trade: You")), diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 76a2d626..6ec62337 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -44,7 +44,7 @@ #include "../resources/resourcemanager.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" /** * Calculates the Alder-32 checksum for the given file. diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index 9b86a536..a1a02e1e 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -37,7 +37,7 @@ #include "../resources/monsterinfo.h" #include "../resources/resourcemanager.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" extern volatile int tick_time; diff --git a/src/itemshortcut.cpp b/src/itemshortcut.cpp index 3404b0e3..b1dfb618 100644 --- a/src/itemshortcut.cpp +++ b/src/itemshortcut.cpp @@ -25,7 +25,7 @@ #include "itemshortcut.h" #include "localplayer.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" ItemShortcut::ItemShortcut *itemShortcut; diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 70e42f88..44899069 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -46,7 +46,7 @@ #include "resources/imageset.h" #include "resources/resourcemanager.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" LocalPlayer *player_node = NULL; diff --git a/src/main.cpp b/src/main.cpp index 500a46b4..bdc3941c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,7 +81,7 @@ #include "resources/resourcemanager.h" #include "utils/gettext.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" #ifdef __APPLE__ #include @@ -976,7 +976,7 @@ int main(int argc, char *argv[]) if (n_server == 1) { SERVER_INFO *si = *server_info; - loginData.hostname = iptostring(si->address); + loginData.hostname = ipToString(si->address); loginData.port = si->port; loginData.updateHost = si->updateHost; state = UPDATE_STATE; diff --git a/src/map.cpp b/src/map.cpp index 4bbde186..814b4225 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -36,7 +36,7 @@ #include "resources/resourcemanager.h" #include "utils/dtor.h" -#include "utils/tostring.h" +#include "utils/stringutils.h" extern volatile int tick_time; diff --git a/src/net/charserverhandler.cpp b/src/net/charserverhandler.cpp index 932cf705..8e743bf0 100644 --- a/src/net/charserverhandler.cpp +++ b/src/net/charserverhandler.cpp @@ -33,7 +33,7 @@ #include "../gui/ok_dialog.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" CharServerHandler::CharServerHandler(): mCharCreateDialog(0) @@ -165,7 +165,7 @@ void CharServerHandler::handleMessage(MessageIn *msg) slot = mCharInfo->getPos(); msg->skip(4); // CharID, must be the same as player_node->charID map_path = msg->readString(16); - mLoginData->hostname = iptostring(msg->readInt32()); + mLoginData->hostname = ipToString(msg->readInt32()); mLoginData->port = msg->readInt16(); mCharInfo->unlock(); mCharInfo->select(0); diff --git a/src/net/chathandler.cpp b/src/net/chathandler.cpp index 25877907..a3ccc4fb 100644 --- a/src/net/chathandler.cpp +++ b/src/net/chathandler.cpp @@ -34,8 +34,7 @@ #include "../gui/chat.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" -#include "../utils/trim.h" +#include "../utils/stringutils.h" extern Being *player_node; diff --git a/src/net/inventoryhandler.cpp b/src/net/inventoryhandler.cpp index 46f03e28..d5ac0c29 100644 --- a/src/net/inventoryhandler.cpp +++ b/src/net/inventoryhandler.cpp @@ -37,7 +37,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" InventoryHandler::InventoryHandler() { diff --git a/src/net/loginhandler.cpp b/src/net/loginhandler.cpp index 4fd0f373..2695fc7b 100644 --- a/src/net/loginhandler.cpp +++ b/src/net/loginhandler.cpp @@ -30,7 +30,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" extern SERVER_INFO **server_info; @@ -110,7 +110,7 @@ void LoginHandler::handleMessage(MessageIn *msg) logger->log("Network: Server: %s (%s:%d)", server_info[i]->name.c_str(), - iptostring(server_info[i]->address), + ipToString(server_info[i]->address), server_info[i]->port); } state = CHAR_SERVER_STATE; diff --git a/src/net/network.cpp b/src/net/network.cpp index 059da779..3fa046c4 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -26,7 +26,7 @@ #include "network.h" #include "../log.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" /** Warning: buffers and other variables are shared, so there can be only one connection active at a time */ @@ -326,7 +326,7 @@ bool Network::realConnect() } logger->log("Network::Started session with %s:%i", - iptostring(ipAddress.host), ipAddress.port); + ipToString(ipAddress.host), ipAddress.port); mState = CONNECTED; diff --git a/src/net/playerhandler.cpp b/src/net/playerhandler.cpp index 74e959da..fc3506fb 100644 --- a/src/net/playerhandler.cpp +++ b/src/net/playerhandler.cpp @@ -39,7 +39,7 @@ #include "../gui/skill.h" #include "../gui/viewport.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" #include "../utils/gettext.h" // TODO Move somewhere else diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index 577b9947..fec52150 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -29,7 +29,7 @@ #include "../utils/dtor.h" #include "../utils/gettext.h" -#include "../utils/trim.h" +#include "../utils/stringutils.h" #include "../utils/xml.h" namespace @@ -124,12 +124,7 @@ void ItemDB::load() if (itr == mNamedItemInfos.end()) { std::string temp = name; - trim(temp); - - for (unsigned int i = 0; i < temp.size(); i++) - { - temp[i] = (char) tolower(temp[i]); - } + toLower(trim(temp)); mNamedItemInfos[temp] = itemInfo; } diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 53df40c3..5fa47261 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -33,7 +33,7 @@ #include "../tileset.h" #include "../utils/base64.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" #include "../utils/xml.h" const unsigned int DEFAULT_TILE_WIDTH = 32; diff --git a/src/units.cpp b/src/units.cpp index f4bc01b7..4f8b95f0 100644 --- a/src/units.cpp +++ b/src/units.cpp @@ -28,8 +28,7 @@ #include "log.h" #include "utils/strprintf.h" -#include "utils/tostring.h" -#include "utils/trim.h" +#include "utils/stringutils.h" #include "utils/xml.h" struct UnitLevel { diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp new file mode 100644 index 00000000..04a54149 --- /dev/null +++ b/src/utils/stringutils.cpp @@ -0,0 +1,63 @@ +/* + * The Mana World + * Copyright (C) 2007 The Mana World Development Team + * + * This file is part of The Mana World. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "stringutils.h" + +#include + +std::string &trim(std::string &str) +{ + std::string::size_type pos = str.find_last_not_of(' '); + if (pos != std::string::npos) + { + str.erase(pos + 1); + pos = str.find_first_not_of(' '); + if (pos != std::string::npos) + { + str.erase(0, pos); + } + } + else + { + // There is nothing else but whitespace in the string + str.clear(); + } + return str; +} + +std::string &toLower(std::string &str) +{ + std::transform(str.begin(), str.end(), str.begin(), tolower); + return str; +} + +const char *ipToString(int address) +{ + static char asciiIP[16]; + + sprintf(asciiIP, "%i.%i.%i.%i", + (unsigned char)(address), + (unsigned char)(address >> 8), + (unsigned char)(address >> 16), + (unsigned char)(address >> 24)); + + return asciiIP; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h new file mode 100644 index 00000000..8b8b7bc2 --- /dev/null +++ b/src/utils/stringutils.h @@ -0,0 +1,68 @@ +/* + * The Mana World + * Copyright (C) 2007 The Mana World Development Team + * + * This file is part of The Mana World. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef UTILS_STRINGUTILS_H +#define UTILS_STRINGUTILS_H + +#include +#include + +/** + * Trims spaces off the end and the beginning of the given string. + * + * @param str the string to trim spaces off + * @return a reference to the trimmed string + */ +std::string &trim(std::string &str); + +/** + * Converts the given string to lower case. + * + * @param str the string to convert to lower case + * @return a reference to the given string converted to lower case + */ +std::string &toLower(std::string &str); + +/** + * Converts the given value to a string using std::stringstream. + * + * @param arg the value to convert to a string + * @return the string representation of arg + */ +template std::string toString(const T &arg) +{ + std::stringstream ss; + ss << arg; + return ss.str(); +} + +/** + * Converts the given IP address to a string. + * + * The returned string is statically allocated, and shouldn't be freed. It is + * changed upon the next use of this method. + * + * @param address the address to convert to a string + * @return the string representation of the address + */ +const char *ipToString(int address); + +#endif // UTILS_STRINGUTILS_H diff --git a/src/utils/tostring.h b/src/utils/tostring.h deleted file mode 100644 index 28d8c521..00000000 --- a/src/utils/tostring.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * The Mana World - * Copyright (C) 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef UTILS_TOSTRING_H -#define UTILS_TOSTRING_H - -#include - -template -std::string toString(const T &arg) -{ - std::stringstream ss; - ss << arg; - return ss.str(); -} - -inline char *iptostring(int address) -{ - static char asciiIP[16]; - - sprintf(asciiIP, "%i.%i.%i.%i", - (unsigned char)(address), - (unsigned char)(address >> 8), - (unsigned char)(address >> 16), - (unsigned char)(address >> 24)); - - return asciiIP; -} - -#endif diff --git a/src/utils/trim.h b/src/utils/trim.h deleted file mode 100644 index b7474ac7..00000000 --- a/src/utils/trim.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * The Mana World - * Copyright (C) 2007 The Mana World Development Team - * - * This file is part of The Mana World. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef UTILS_TRIM_H -#define UTILS_TRIM_H - -#include - -/** - * Trims spaces off the end and the beginning of the given string. - * - * @param str the string to trim spaces off - */ -inline void trim(std::string &str) -{ - std::string::size_type pos = str.find_last_not_of(' '); - if (pos != std::string::npos) - { - str.erase(pos + 1); - pos = str.find_first_not_of(' '); - if (pos != std::string::npos) - { - str.erase(0, pos); - } - } - else - { - // There is nothing else but whitespace in the string - str.clear(); - } -} - -#endif -- cgit v1.2.3-70-g09d2