diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/mutex.h | 4 | ||||
-rw-r--r-- | src/utils/sha256.cpp | 2 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 37 | ||||
-rw-r--r-- | src/utils/stringutils.h | 21 | ||||
-rw-r--r-- | src/utils/xml.cpp | 16 | ||||
-rw-r--r-- | src/utils/xml.h | 21 | ||||
-rw-r--r-- | src/utils/zlib.cpp | 8 |
7 files changed, 77 insertions, 32 deletions
diff --git a/src/utils/mutex.h b/src/utils/mutex.h index a0c72e95..b6c4e88d 100644 --- a/src/utils/mutex.h +++ b/src/utils/mutex.h @@ -75,13 +75,13 @@ inline Mutex::~Mutex() inline void Mutex::lock() { if (SDL_mutexP(mMutex) == -1) - logger->log("Mutex locking failed: %s", SDL_GetError()); + Log::info("Mutex locking failed: %s", SDL_GetError()); } inline void Mutex::unlock() { if (SDL_mutexV(mMutex) == -1) - logger->log("Mutex unlocking failed: %s", SDL_GetError()); + Log::info("Mutex unlocking failed: %s", SDL_GetError()); } diff --git a/src/utils/sha256.cpp b/src/utils/sha256.cpp index 0e44af7a..a1339d14 100644 --- a/src/utils/sha256.cpp +++ b/src/utils/sha256.cpp @@ -3,7 +3,7 @@ * Copyright (C) 2008-2009 The Mana World Development Team * Copyright (C) 2009-2012 The Mana Developers * - * This file has been slighly modified as part of The Mana Client. + * This file has been slightly modified as 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 version 2, as diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index ba5f575b..7b05b365 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -111,14 +111,13 @@ std::string &replaceCharacters(std::string &str, return str; } -std::string removeColors(std::string msg) +std::string &removeColors(std::string &msg) { - for (unsigned int f = 0; f < msg.length() - 2 && msg.length() > 2; f++) + auto pos = msg.find("##"); + while (pos != std::string::npos && msg.length() - pos >= 3) { - while (msg.length() > f + 2 && msg.at(f) == '#' && msg.at(f + 1) == '#') - { - msg = msg.erase(f, 3); - } + msg.erase(pos, 3); + pos = msg.find("##", pos); } return msg; } @@ -153,6 +152,32 @@ bool getBoolFromString(std::string text, bool def) return def; } +// Overload for std::vector<int> to parse comma-separated integers +void fromString(const char *str, std::vector<int> &value) +{ + value.clear(); + + const char *p = str; + while (*p) + { + while (*p == ' ' || *p == ',') + ++p; // skip spaces and commas + if (!*p) + break; + char *end = nullptr; + int v = static_cast<int>(strtol(p, &end, 10)); + if (end != p) + { + value.push_back(v); + p = end; + } + else + { + ++p; // skip invalid character to avoid infinite loop + } + } +} + std::string autocomplete(const std::vector<std::string> &candidates, std::string base) { diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 1fa8e1e6..885ddcb8 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -107,19 +107,28 @@ std::string &replaceCharacters(std::string &str, char replacement = '_'); /** - * Removes colors from a string + * Removes colors from a string. * * @param msg the string to remove the colors from - * @return string without colors + * @return reference to the modified string */ -std::string removeColors(std::string msg); +std::string &removeColors(std::string &msg); /** * Returns whether a string starts with a given prefix. */ -inline bool startsWith(const std::string &str, const char *prefix) +inline bool startsWith(std::string_view str, std::string_view prefix) { - return str.rfind(prefix, 0) == 0; + return str.substr(0, prefix.size()) == prefix; +} + +/** + * Returns whether a string ends with a given suffix. + */ +inline bool endsWith(std::string_view str, std::string_view suffix) +{ + return str.size() >= suffix.size() && + str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } /** @@ -193,6 +202,8 @@ inline void fromString(const char *str, bool &value) value = getBoolFromString(str); } +void fromString(const char *str, std::vector<int> &value); + template<typename T> struct FromString<T, std::enable_if_t<std::is_enum_v<T>>> { diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index c408c9c2..34bcc52b 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -43,13 +43,13 @@ namespace XML auto *context = static_cast<XMLContext*>(ctx); if (context) - logger->log("Error in XML file '%s' on line %d", - context->file.c_str(), error->line); + Log::info("Error in XML file '%s' on line %d", + context->file.c_str(), error->line); else - logger->log("Error in unknown XML file on line %d", - error->line); + Log::info("Error in unknown XML file on line %d", + error->line); - logger->log("%s", error->message); + Log::info("%s", error->message); // No need to keep errors around xmlCtxtResetLastError(error->ctxt); @@ -76,11 +76,11 @@ namespace XML SDL_free(data); if (!mDoc) - logger->log("Error parsing XML file %s", filename.c_str()); + Log::info("Error parsing XML file %s", filename.c_str()); } else { - logger->log("Error loading %s: %s", filename.c_str(), SDL_GetError()); + Log::info("Error loading %s: %s", filename.c_str(), SDL_GetError()); } xmlSetStructuredErrorFunc(nullptr, xmlLogger); @@ -109,7 +109,7 @@ namespace XML mWriter = xmlNewTextWriterFilename(fileName.c_str(), 0); if (!mWriter) { - logger->log("Error creating XML writer for file %s", fileName.c_str()); + Log::info("Error creating XML writer for file %s", fileName.c_str()); return; } diff --git a/src/utils/xml.h b/src/utils/xml.h index 787504bb..fe896c60 100644 --- a/src/utils/xml.h +++ b/src/utils/xml.h @@ -47,7 +47,7 @@ namespace XML std::string_view textContent() const; template<typename T> - bool attribute(const char *name, T &value); + bool attribute(const char *name, T &value) const; bool hasAttribute(const char *name) const; int getProperty(const char *name, int def) const; @@ -66,10 +66,19 @@ namespace XML class Iterator { public: - explicit Iterator(xmlNodePtr node) : mNode(node) {} + explicit Iterator(xmlNodePtr node) : mNode(node) + { + while (mNode && mNode->type != XML_ELEMENT_NODE) + mNode = mNode->next; + } bool operator!=(const Iterator &other) const { return mNode != other.mNode; } - void operator++() { mNode = mNode->next; } + void operator++() + { + do { + mNode = mNode->next; + } while (mNode && mNode->type != XML_ELEMENT_NODE); + } Node operator*() const { return mNode; } private: @@ -117,7 +126,7 @@ namespace XML } template<typename T> - inline bool Node::attribute(const char *name, T &value) + inline bool Node::attribute(const char *name, T &value) const { if (const char *str = attribute(name)) { @@ -195,13 +204,13 @@ namespace XML * Returns the root node of the document (or NULL if there was a * load error). */ - Node rootNode(); + Node rootNode() const; private: xmlDocPtr mDoc; }; - inline Node Document::rootNode() + inline Node Document::rootNode() const { return mDoc ? xmlDocGetRootElement(mDoc) : nullptr; } diff --git a/src/utils/zlib.cpp b/src/utils/zlib.cpp index f78b235e..08281bcc 100644 --- a/src/utils/zlib.cpp +++ b/src/utils/zlib.cpp @@ -107,19 +107,19 @@ int inflateMemory(unsigned char *in, unsigned int inLength, { if (ret == Z_MEM_ERROR) { - logger->log("Error: Out of memory while decompressing data!"); + Log::error("Out of memory while decompressing data!"); } else if (ret == Z_VERSION_ERROR) { - logger->log("Error: Incompatible zlib version!"); + Log::error("Incompatible zlib version!"); } else if (ret == Z_DATA_ERROR) { - logger->log("Error: Incorrect zlib compressed data!"); + Log::error("Incorrect zlib compressed data!"); } else { - logger->log("Error: Unknown error while decompressing data!"); + Log::error("Unknown error while decompressing data!"); } free(out); |