MantisBase v0.3.4
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
9#ifndef MANTIS_UTILS_H
10#define MANTIS_UTILS_H
11
12#include <string>
13#include <filesystem>
14#include <random>
15#include <chrono>
16#include <string_view>
17#include <algorithm>
18#include <soci/row.h>
19#include <nlohmann/json.hpp>
20
21#include "../core/logger/logger.h"
22
23#ifdef MANTIS_ENABLE_SCRIPTING
24#include "dukglue/dukvalue.h"
25#endif
26
27
28// Wrap around std::format for compilers that do no support
29// std::format yet, especially on Windows
30#if __has_include(<format>)
31#include <format>
32#else
33#include <spdlog/fmt/bundled/format.h>
34namespace std {
35 using fmt::format;
36}
37#endif
38
39namespace mb {
40 namespace fs = std::filesystem;
41 using json = nlohmann::json;
42
43 // ----------------------------------------------------------------- //
44 // PATH UTILS
45 // ----------------------------------------------------------------- //
46
53 fs::path joinPaths(const std::string &path1, const std::string &path2);
54
65 fs::path resolvePath(const std::string &input_path);
66
76 bool createDirs(const fs::path &path);
77
89 std::string dirFromPath(const std::string &path);
90
91 // ----------------------------------------------------------------- //
92 // STRING UTILS
93 // ----------------------------------------------------------------- //
102 void toLowerCase(std::string &str);
103
112 void toUpperCase(std::string &str);
113
120 std::string trim(const std::string &s);
121
135 std::optional<json> tryParseJsonStr(const std::string &json_str, std::optional<json> default_value = std::nullopt);
136
146 bool strToBool(const std::string &value);
147
161 std::string generateTimeBasedId();
162
176 std::string generateReadableTimeId();
177
191 std::string generateShortId(size_t length = 16);
192
206 std::vector<std::string> splitString(const std::string &input, const std::string &delimiter);
207
214 std::string getEnvOrDefault(const std::string &key, const std::string &defaultValue);
215
227 bool invalidChar(unsigned char c);
228
237 void sanitizeInPlace(std::string &s);
238
256 std::string sanitizeFilename(std::string_view original,
257 std::size_t maxLen = 50,
258 std::size_t idLen = 12,
259 std::string_view idSep = "_");
260
261 std::string sanitizeFilename_JSWrapper(const std::string &original);
262
263 // ----------------------------------------------------------------- //
264 // AUTH UTILS
265 // ----------------------------------------------------------------- //
271 std::string hashPassword(const std::string &password);
272
283 bool verifyPassword(const std::string &password, const std::string &stored_hash);
284
285 // ----------------------------------------------------------------- //
286 // AUTH UTILS
287 // ----------------------------------------------------------------- //
288
289 inline std::string getCurrentTimestampUTC() {
290 const std::time_t now = std::time(nullptr);
291 const std::tm* utc = std::gmtime(&now); // Use UTC time
292
293 char buffer[20];
294 std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", utc);
295 return std::string{ buffer };
296 }
297
303 std::string tmToStr(const std::tm &t);
304
310 std::tm strToTM(const std::string &value);
311
318 std::string dbDateToString(const soci::row &row, int index);
319
336 int safe_stoi(const std::string &s, const int default_val);
337
338 // ----------------------------------------------------------------- //
339 // NETWORK UTILS
340 // ----------------------------------------------------------------- //
341
357 bool isValidIPv4(const std::string &ip);
358
368 bool isValidIPv6(const std::string &ip);
369
376 bool isValidIP(const std::string &ip);
377
378#ifdef MANTIS_ENABLE_SCRIPTING
380#endif
381}
382
383#endif // MANTIS_UTILS_H
router.h
Definition auth.h:15
bool isValidIPv6(const std::string &ip)
Validates if a string is a valid IPv6 address.
Definition string_utils.cpp:212
std::vector< std::string > splitString(const std::string &input, const std::string &delimiter)
Split given string based on given delimiter.
Definition string_utils.cpp:93
int safe_stoi(const std::string &s, const int default_val)
Safely convert string to integer with default fallback.
Definition string_utils.cpp:191
fs::path resolvePath(const std::string &input_path)
Definition path_utils.cpp:11
bool createDirs(const fs::path &path)
Create directory, given a path.
Definition path_utils.cpp:24
std::string trim(const std::string &s)
Trims leading and trailing whitespaces from a string.
Definition string_utils.cpp:36
void registerUtilsToDuktapeEngine()
Definition dukglue_utils_bindings.cpp:5
std::string dbDateToString(const soci::row &row, int index)
Convert database date value from SOCI row to string.
Definition date_utils.cpp:28
std::string generateTimeBasedId()
Generate a time base UUID.
Definition string_utils.cpp:42
void toLowerCase(std::string &str)
Converts a string to its lowercase variant.
Definition string_utils.cpp:26
bool isValidIP(const std::string &ip)
Validates if a string is a valid IP address (IPv4 or IPv6).
Definition string_utils.cpp:226
std::tm strToTM(const std::string &value)
Convert ISO formatted datetime string to std::tm structure.
Definition date_utils.cpp:16
std::string tmToStr(const std::tm &t)
Convert c++ std::tm date/time value to ISO formatted string.
Definition date_utils.cpp:8
void sanitizeInPlace(std::string &s)
Sanitize a string in-place by removing or replacing invalid characters.
Definition string_utils.cpp:120
std::string 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.
Definition string_utils.cpp:107
bool verifyPassword(const std::string &password, const std::string &stored_hash)
Verifies user password if it matches the given hashed password.
Definition auth_utils.cpp:14
std::optional< json > tryParseJsonStr(const std::string &json_str, std::optional< json > default_value=std::nullopt)
Attempt to parse a JSON string.
Definition string_utils.cpp:7
bool invalidChar(unsigned char c)
Check if a character is invalid in a filename.
Definition string_utils.cpp:112
std::string getCurrentTimestampUTC()
Definition utils.h:289
std::string 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.
Definition string_utils.cpp:154
std::string hashPassword(const std::string &password)
Digests user password + a generated salt to yield a hashed password.
Definition auth_utils.cpp:8
fs::path joinPaths(const std::string &path1, const std::string &path2)
‍JSON convenience for the nlomann::json namespace
Definition path_utils.cpp:5
std::string generateShortId(size_t length=16)
Generates a short UUID.
Definition string_utils.cpp:79
void toUpperCase(std::string &str)
Converts a string to its uppercase variant.
Definition string_utils.cpp:31
std::string sanitizeFilename_JSWrapper(const std::string &original)
Definition string_utils.cpp:187
std::string dirFromPath(const std::string &path)
Returns a created/existing directory from a path.
Definition path_utils.cpp:44
bool strToBool(const std::string &value)
Convert given string value to boolean type.
Definition string_utils.cpp:20
bool isValidIPv4(const std::string &ip)
Validates if a string is a valid IPv4 address.
Definition string_utils.cpp:199
std::string generateReadableTimeId()
Generates a readable time-based UUID.
Definition string_utils.cpp:58
nlohmann::json json
Shorten JSON namespace.
Definition context_store.h:18
Definition utils.h:34