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 | 26 | ||||
-rw-r--r-- | src/utils/stringutils.h | 15 | ||||
-rw-r--r-- | src/utils/xml.cpp | 16 | ||||
-rw-r--r-- | src/utils/zlib.cpp | 8 |
6 files changed, 54 insertions, 17 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 05b6982c..7b05b365 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -152,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 53c36bc6..885ddcb8 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -117,9 +117,18 @@ 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/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); |