diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-02-18 20:14:23 +0100 |
---|---|---|
committer | Ira Rice <irarice@gmail.com> | 2009-02-18 13:19:11 -0700 |
commit | c3f7f72f37adad5103587069ff549798fc9d652d (patch) | |
tree | df268b0bfd478cb3f4d740b1bce9a882bc70c538 | |
parent | b8f0e1755ae8c6acf9bc681a83f18ef6ff0a1172 (diff) | |
download | mana-c3f7f72f37adad5103587069ff549798fc9d652d.tar.gz mana-c3f7f72f37adad5103587069ff549798fc9d652d.tar.bz2 mana-c3f7f72f37adad5103587069ff549798fc9d652d.tar.xz mana-c3f7f72f37adad5103587069ff549798fc9d652d.zip |
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.
47 files changed, 150 insertions, 91 deletions
diff --git a/aethyra.cbp b/aethyra.cbp index 08ec74fd..29bc7227 100644 --- a/aethyra.cbp +++ b/aethyra.cbp @@ -420,10 +420,10 @@ <Unit filename="src\utils\fastsqrt.h" /> <Unit filename="src\utils\gettext.h" /> <Unit filename="src\utils\mutex.h" /> + <Unit filename="src\utils\stringutils.cpp" /> + <Unit filename="src\utils\stringutils.h" /> <Unit filename="src\utils\strprintf.cpp" /> <Unit filename="src\utils\strprintf.h" /> - <Unit filename="src\utils\tostring.h" /> - <Unit filename="src\utils\trim.h" /> <Unit filename="src\utils\xml.cpp" /> <Unit filename="src\utils\xml.h" /> <Unit filename="src\vector.cpp" /> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 25bcbfb0..51d6b937 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 71862994..6b5dda8b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -246,10 +246,10 @@ aethyra_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 34b042e8..3f34948e 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -50,9 +50,8 @@ #include "gui/speechbubble.h" #include "utils/dtor.h" -#include "utils/gettext.h" -#include "utils/tostring.h" -#include "utils/trim.h" +#include "utils/gettext.h" +#include "utils/stringutils.h" #include "utils/xml.h" int Being::instances = 0; diff --git a/src/configuration.cpp b/src/configuration.cpp index 2224a6c7..0df666ab 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -24,7 +24,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 c1ca3c2c..b229ec70 100644 --- a/src/emoteshortcut.cpp +++ b/src/emoteshortcut.cpp @@ -25,7 +25,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 d491b3ff..cc19bd72 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -40,9 +40,7 @@ #include "resources/mapreader.h" #include "resources/resourcemanager.h" -#include "utils/tostring.h" - -char itemCurrenyQ[10] = "0"; +#include "utils/stringutils.h" Engine::Engine(Network *network): mCurrentMap(NULL), diff --git a/src/gui/char_select.cpp b/src/gui/char_select.cpp index bad6d8de..fa0594d6 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 60f8f5dd..6977a104 100644 --- a/src/gui/char_server.cpp +++ b/src/gui/char_server.cpp @@ -30,7 +30,7 @@ #include "../serverinfo.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" extern SERVER_INFO **server_info; @@ -102,7 +102,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 0e8810ac..91e410ce 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -47,8 +47,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) @@ -336,15 +335,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; @@ -405,14 +397,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 aa249077..b68bf68b 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 2d8bb826..c41e0a5e 100644 --- a/src/gui/debugwindow.cpp +++ b/src/gui/debugwindow.cpp @@ -34,7 +34,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 721a0ebd..c4cace55 100644 --- a/src/gui/emotecontainer.cpp +++ b/src/gui/emotecontainer.cpp @@ -39,7 +39,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 af83a743..e828df8b 100644 --- a/src/gui/emoteshortcutcontainer.cpp +++ b/src/gui/emoteshortcutcontainer.cpp @@ -39,7 +39,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 8cf1d730..30129941 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 fae517a9..6be55e1c 100644 --- a/src/gui/equipmentwindow.cpp +++ b/src/gui/equipmentwindow.cpp @@ -41,7 +41,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 adade3b9..40bbd887 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 ec825ae3..e8457337 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 e84c79ca..79aeb227 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 d13cd1c2..88afcd5b 100644 --- a/src/gui/itempopup.cpp +++ b/src/gui/itempopup.cpp @@ -36,7 +36,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 324e1b6c..10ced157 100644 --- a/src/gui/itemshortcutcontainer.cpp +++ b/src/gui/itemshortcutcontainer.cpp @@ -36,7 +36,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 159b7f6f..cd331322 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -39,7 +39,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 629cf45c..6537b48e 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 982d808d..ff8825ef 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 fd7df18d..13928e41 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -40,7 +40,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 f3fa3c65..31b56b51 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 1fb0b443..0f79e3c6 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 34dfde0f..8b21582e 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -47,7 +47,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 cc559def..13ff042d 100644 --- a/src/gui/shortcutcontainer.cpp +++ b/src/gui/shortcutcontainer.cpp @@ -26,7 +26,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 42a48b2f..49af3ae2 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 c57cac59..ba2e2462 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 a25d57d4..81dcb047 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -45,7 +45,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 79a37227..9a7f6173 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 fd32c4f5..0627ccda 100644 --- a/src/itemshortcut.cpp +++ b/src/itemshortcut.cpp @@ -26,7 +26,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 0470fefc..608c25b8 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -45,7 +45,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 a6ddd597..06ec2f75 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 <CoreFoundation/CFBundle.h> @@ -964,7 +964,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 5e38aee2..b780b5cc 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -37,7 +37,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 5bf45bb4..95555300 100644 --- a/src/net/charserverhandler.cpp +++ b/src/net/charserverhandler.cpp @@ -34,7 +34,7 @@ #include "../gui/ok_dialog.h" #include "../utils/gettext.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" CharServerHandler::CharServerHandler(): mCharCreateDialog(0) @@ -166,7 +166,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 14566b39..6717f515 100644 --- a/src/net/chathandler.cpp +++ b/src/net/chathandler.cpp @@ -35,8 +35,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 ac5f94d3..8ea0071d 100644 --- a/src/net/inventoryhandler.cpp +++ b/src/net/inventoryhandler.cpp @@ -38,7 +38,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 e173252f..cf45dbd4 100644 --- a/src/net/loginhandler.cpp +++ b/src/net/loginhandler.cpp @@ -31,7 +31,7 @@ #include "../utils/gettext.h" #include "../utils/strprintf.h" -#include "../utils/tostring.h" +#include "../utils/stringutils.h" extern SERVER_INFO **server_info; @@ -111,7 +111,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 02d8bf4f..0126ad6d 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -27,7 +27,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 */ @@ -327,7 +327,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 9c34cec6..bce53ae9 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 ceaf0395..0031bbd0 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -30,7 +30,7 @@ #include "../utils/dtor.h" #include "../utils/gettext.h" -#include "../utils/trim.h" +#include "../utils/stringutils.h" #include "../utils/xml.h" namespace @@ -125,12 +125,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 840f0677..901a2561 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -34,7 +34,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/utils/trim.h b/src/utils/stringutils.cpp index 0602842f..d414d665 100644 --- a/src/utils/trim.h +++ b/src/utils/stringutils.cpp @@ -20,17 +20,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef UTILS_TRIM_H -#define UTILS_TRIM_H +#include "stringutils.h" -#include <string> +#include <algorithm> -/** - * 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 &trim(std::string &str) { std::string::size_type pos = str.find_last_not_of(' '); if (pos != std::string::npos) @@ -47,6 +41,24 @@ inline void trim(std::string &str) // 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; } -#endif +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..7d8df748 --- /dev/null +++ b/src/utils/stringutils.h @@ -0,0 +1,69 @@ +/* + * Aethyra + * Copyright (C) 2007 The Mana World Development Team + * + * This file is part of Aethyra based on original code + * from 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 <string> +#include <sstream> + +/** + * 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<typename T> 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 |