MantisBase v0.4.9
Loading...
Searching...
No Matches
utils.h File Reference

Collection of utility functions that are re-used across different files. More...

#include <string>
#include <filesystem>
#include <random>
#include <chrono>
#include <string_view>
#include <algorithm>
#include <soci/row.h>
#include <nlohmann/json.hpp>
#include "../core/logger/logger.h"
#include "../core/types.h"
#include <spdlog/fmt/bundled/format.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  std
namespace  mb

Functions

MANTISBASE_API fs::path mb::joinPaths (const std::string &path1, const std::string &path2)
 Joins absolute path and a relative path, relative to the first path.
MANTISBASE_API fs::path mb::resolvePath (const std::string &input_path)
MANTISBASE_API bool mb::createDirs (const fs::path &path)
 Create directory, given a path.
MANTISBASE_API std::string mb::dirFromPath (const std::string &path)
 Returns a created/existing directory from a path.
MANTISBASE_API void mb::toLowerCase (std::string &str)
 Converts a string to its lowercase variant.
MANTISBASE_API void mb::toUpperCase (std::string &str)
 Converts a string to its uppercase variant.
MANTISBASE_API std::string mb::trim (const std::string &s)
 Trims leading and trailing whitespaces from a string.
MANTISBASE_API std::string mb::sqlIdentifier (const std::string &ident)
 Validate a string as a safe SQL identifier and return it unchanged.
MANTISBASE_API std::pair< json, std::string > mb::tryParseJsonStr (const std::string &json_str)
 Try parsing a string to a JSON object and return the object and any error if parsing failed.
MANTISBASE_API bool mb::strToBool (const std::string &value)
 Convert given string value to boolean type.
MANTISBASE_API std::string mb::generateTimeBasedId ()
 Generate a time base UUID.
MANTISBASE_API std::string mb::generateReadableTimeId ()
 Generates a readable time-based UUID.
MANTISBASE_API std::string mb::generateShortId (size_t length=16)
 Generates a short UUID.
MANTISBASE_API std::vector< std::string > mb::splitString (const std::string &input, const std::string &delimiter)
 Split given string based on given delimiter.
MANTISBASE_API std::string mb::getEnvOrDefault (const std::string &key, const std::string &defaultValue)
 Retrieves a value from an environment variable or a default value if the env variable was not set.
MANTISBASE_API bool mb::invalidChar (unsigned char c)
 Check if a character is invalid in a filename.
MANTISBASE_API void mb::sanitizeInPlace (std::string &s)
 Sanitize a string in-place by removing or replacing invalid characters.
MANTISBASE_API std::string mb::sanitizeFilename (std::string_view original, std::size_t maxLen=50, std::size_t idLen=12, std::string_view idSep="_")
 Sanitize a filename and ensure uniqueness.
std::string mb::sanitizeFilename_JSWrapper (const std::string &original)
MANTISBASE_API bool mb::isAllowedFileExtension (const std::string &extension)
MANTISBASE_API std::string mb::safeContentType (const std::string &extension)
MANTISBASE_API std::string mb::hashPassword (const std::string &password)
 Digests user password + a generated salt to yield a hashed password.
MANTISBASE_API bool mb::verifyPassword (const std::string &password, const std::string &stored_hash)
 Verifies user password if it matches the given hashed password.
MANTISBASE_API std::tm mb::toUtcTime (std::time_t t)
 Thread-safe conversion of an epoch time to a UTC std::tm.
MANTISBASE_API std::tm mb::toLocalTime (std::time_t t)
 Thread-safe conversion of an epoch time to a local-time std::tm.
MANTISBASE_API std::string mb::getCurrentTimestampUTC ()
MANTISBASE_API std::string mb::tmToStr (const std::tm &t)
 Convert c++ std::tm date/time value to ISO formatted string.
MANTISBASE_API std::tm mb::strToTM (const std::string &value)
 Convert ISO formatted datetime string to std::tm structure.
MANTISBASE_API std::string mb::dbDateToString (const std::string &db_type, const soci::row &row, int index)
 Convert database date value from SOCI row to string.
int mb::safe_stoi (const std::string &s, const int default_val)
 Safely convert string to integer with default fallback.
MANTISBASE_API bool mb::isValidIPv4 (const std::string &ip)
 Validates if a string is a valid IPv4 address.
MANTISBASE_API bool mb::isValidIPv6 (const std::string &ip)
 Validates if a string is a valid IPv6 address.
MANTISBASE_API bool mb::isValidIP (const std::string &ip)
 Validates if a string is a valid IP address (IPv4 or IPv6).

Detailed Description

Collection of utility functions that are re-used across different files.

Created by allan on 11/05/2025.