diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/mutex.h | 91 | ||||
-rw-r--r-- | src/utils/specialfolder.cpp | 78 | ||||
-rw-r--r-- | src/utils/specialfolder.h | 30 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 177 | ||||
-rw-r--r-- | src/utils/stringutils.h | 142 |
5 files changed, 518 insertions, 0 deletions
diff --git a/src/utils/mutex.h b/src/utils/mutex.h new file mode 100644 index 0000000..80569e2 --- /dev/null +++ b/src/utils/mutex.h @@ -0,0 +1,91 @@ +/* + * The Mana Client + * Copyright (C) 2008-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MUTEX_H +#define MUTEX_H + +#include <SDL_thread.h> + +/** + * A mutex provides mutual exclusion of access to certain data that is + * accessed by multiple threads. + */ +class Mutex +{ +public: + Mutex(); + ~Mutex(); + + void lock(); + void unlock(); + +private: + Mutex(const Mutex&); // prevent copying + Mutex& operator=(const Mutex&); + + SDL_mutex *mMutex; +}; + +/** + * A convenience class for locking a mutex. + */ +class MutexLocker +{ +public: + MutexLocker(Mutex *mutex); + ~MutexLocker(); + +private: + Mutex *mMutex; +}; + + +inline Mutex::Mutex() +{ + mMutex = SDL_CreateMutex(); +} + +inline Mutex::~Mutex() +{ + SDL_DestroyMutex(mMutex); +} + +inline void Mutex::lock() +{ +} + +inline void Mutex::unlock() +{ +} + + +inline MutexLocker::MutexLocker(Mutex *mutex): + mMutex(mutex) +{ + mMutex->lock(); +} + +inline MutexLocker::~MutexLocker() +{ + mMutex->unlock(); +} + +#endif // MUTEX_H diff --git a/src/utils/specialfolder.cpp b/src/utils/specialfolder.cpp new file mode 100644 index 0000000..6460771 --- /dev/null +++ b/src/utils/specialfolder.cpp @@ -0,0 +1,78 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifdef WIN32 +#include "specialfolder.h" +#include <windows.h> + +#ifdef _SPECIALFOLDERLOCATION_TEST_ +// compile with -D_SPECIALFOLDERLOCATION_TEST_ to get a standalone +// binary for testing +#include <iostream> +#endif + +/* + * Retrieve the pathname of special folders on win32, or an empty string + * on error / if the folder does not exist. + * See http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx for + * a list of folder ids + */ +std::string getSpecialFolderLocation(int folderId) +{ + std::string ret; + LPITEMIDLIST pItemIdList; + LPMALLOC pMalloc; + char szPath[_MAX_PATH]; + + // get the item ID list for folderId + HRESULT hr = SHGetSpecialFolderLocation(NULL, folderId, &pItemIdList); + if (hr != S_OK) + return ret; + + // convert the ID list into a file system path + if (SHGetPathFromIDList(pItemIdList, szPath) == FALSE) + return ret; + + // get the IMalloc pointer and free all resources we used + hr = SHGetMalloc(&pMalloc); + pMalloc->Free(pItemIdList); + pMalloc->Release(); + + ret = szPath; + return ret; +} + +#ifdef _SPECIALFOLDERLOCATION_TEST_ +int main() +{ + std::cout << "APPDATA " << getSpecialFolderLocation(CSIDL_APPDATA) + << std::endl; + std::cout << "DESKTOP " << getSpecialFolderLocation(CSIDL_DESKTOP) + << std::endl; + std::cout << "LOCAL_APPDATA " + << getSpecialFolderLocation(CSIDL_LOCAL_APPDATA) + << std::endl; + std::cout << "MYPICTURES " << getSpecialFolderLocation(CSIDL_MYPICTURES) + << std::endl; + std::cout << "PERSONAL " << getSpecialFolderLocation(CSIDL_PERSONAL) + << std::endl; +} +#endif +#endif diff --git a/src/utils/specialfolder.h b/src/utils/specialfolder.h new file mode 100644 index 0000000..c2c2e0b --- /dev/null +++ b/src/utils/specialfolder.h @@ -0,0 +1,30 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _SPECIALFOLDER_H +#define _SPECIALFOLDER_H + +#ifdef WIN32 +#include <shlobj.h> +#include <string> +std::string getSpecialFolderLocation(int folderId); +#endif + +#endif diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp new file mode 100644 index 0000000..c7f83c3 --- /dev/null +++ b/src/utils/stringutils.cpp @@ -0,0 +1,177 @@ +/* + * The Mana Client + * Copyright (C) 2007-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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, see <http://www.gnu.org/licenses/>. + */ + +#include "stringutils.h" + +#include <string.h> +#include <algorithm> +#include <cstdarg> +#include <cstdio> + +const int UTF8_MAX_SIZE = 10; + +std::string &utils_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; +} + +std::string &toUpper(std::string &str) +{ + std::transform(str.begin(), str.end(), str.begin(), toupper); + return str; +} + +unsigned int atox(const std::string &str) +{ + unsigned int value; + sscanf(str.c_str(), "0x%06x", &value); + + return value; +} + +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; +} + +std::string strprintf(char const *format, ...) +{ + char buf[256]; + va_list(args); + va_start(args, format); + int nb = vsnprintf(buf, 256, format, args); + va_end(args); + if (nb < 256) + { + return buf; + } + // The static size was not big enough, try again with a dynamic allocation. + ++nb; + char *buf2 = new char[nb]; + va_start(args, format); + vsnprintf(buf2, nb, format, args); + va_end(args); + std::string res(buf2); + delete [] buf2; + return res; +} + +std::string &removeBadChars(std::string &str) +{ + std::string::size_type pos; + do + { + pos = str.find_first_of("@#[]"); + if (pos != std::string::npos) + str.erase(pos, 1); + } while (pos != std::string::npos); + + return str; +} + +std::string removeColors(std::string msg) +{ + for (unsigned int f = 0; f < msg.length() - 2 && msg.length() > 2; f++) + { + while (msg.length() > f + 2 && msg.at(f) == '#' && msg.at(f + 1) == '#') + { + msg = msg.erase(f, 3); + } + } + return msg; +} + +int compareStrI(const std::string &a, const std::string &b) +{ + std::string::const_iterator itA = a.begin(); + std::string::const_iterator endA = a.end(); + std::string::const_iterator itB = b.begin(); + std::string::const_iterator endB = b.end(); + + for (; itA < endA, itB < endB; ++itA, ++itB) + { + int comp = tolower(*itA) - tolower(*itB); + if (comp) + return comp; + } + + // Check string lengths + if (itA == endA && itB == endB) + return 0; + else if (itA == endA) + return -1; + else + return 1; +} + +bool isWordSeparator(char chr) +{ + return (chr == ' ' || chr == ',' || chr == '.' || chr == '"'); +} + +const std::string findSameSubstring(const std::string &str1, const std::string &str2) +{ + int minLength = str1.length() > str2.length() ? str2.length() : str1.length(); + for (int f = 0; f < minLength; f ++) + { + if (str1.at(f) != str2.at(f)) + { + return str1.substr(0, f); + } + } + return str1.substr(0, minLength); +} + +const char* getSafeUtf8String(std::string text) +{ + char* buf = new char[text.size() + UTF8_MAX_SIZE]; + memcpy(buf, text.c_str(), text.size()); + memset(buf + text.size(), 0, UTF8_MAX_SIZE); + return buf; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h new file mode 100644 index 0000000..5f79f5b --- /dev/null +++ b/src/utils/stringutils.h @@ -0,0 +1,142 @@ +/* + * The Mana Client + * Copyright (C) 2007-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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, see <http://www.gnu.org/licenses/>. + */ + +#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 &utils_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 string to upper case. + * + * @param str the string to convert to upper case + * @return a reference to the given string converted to upper case + */ +std::string &toUpper(std::string &str); + + +/** + * Converts an ascii hexidecimal string to an integer + * + * @param str the hex string to convert to an int + * @return the integer representation of the hex string + */ +unsigned int atox(const 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 value to a int using std::stringstream. + * + * @param arg the value to convert to a int + * @return the int representation of arg + */ + +template<typename T> int toInt(const T &arg) +{ + std::stringstream ss(arg); + int out; + ss >> out; + return out; +} + +/** + * 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); + +/** + * A safe version of sprintf that returns a std::string of the result. + */ +std::string strprintf(char const *, ...) +#ifdef __GNUC__ + /* This attribute is nice: it even works through gettext invokation. For + example, gcc will complain that strprintf(_("%s"), 42) is ill-formed. */ + __attribute__((__format__(__printf__, 1, 2))) +#endif +; + +/** + * Removes bad characters from a string + * + * @param str the string to remove the bad chars from + * @return a reference to the string without bad chars + */ +std::string &removeBadChars(std::string &str); + +/** + * Removes colors from a string + * + * @param msg the string to remove the colors from + * @return string without colors + */ +std::string removeColors(std::string msg); + +/** + * Compares the two strings case-insensitively. + * + * @param a the first string in the comparison + * @param b the second string in the comparison + * @return 0 if the strings are equal, positive if the first is greater, + * negative if the second is greater + */ +int compareStrI(const std::string &a, const std::string &b); + +bool isWordSeparator(char chr); + +const std::string findSameSubstring(const std::string &str1, const std::string &str2); + +const char* getSafeUtf8String(std::string text); + +#endif // UTILS_STRINGUTILS_H |