Add MotD support.

Fixed config up.
Better handling of config variables
This commit is contained in:
2024-12-15 08:14:48 -06:00
parent bc88a30c0c
commit 28efbfb185
12 changed files with 261 additions and 109 deletions

View File

@@ -9,6 +9,7 @@
#include <iterator>
#include <algorithm>
#include <filesystem>
#include <chrono>
std::string replace(std::string subject, const std::string& search, const std::string& replace) {
size_t pos = 0;
@@ -19,6 +20,55 @@ std::string replace(std::string subject, const std::string& search, const std::s
return subject;
}
std::string conf_format(const std::string& fmt, const std::map<std::string, std::string>& vars) {
std::string result;
result.reserve(fmt.length());
for (size_t i = 0; i < fmt.length(); ++i) {
if (fmt[i] != '%') {
result += fmt[i];
continue;
}
// Handle %% -> %
if (i + 1 < fmt.length() && fmt[i + 1] == '%') {
result += '%';
i++;
continue;
}
// Look for variable name
if (i + 1 < fmt.length()) {
size_t start = i + 1;
size_t end = start;
// Find the end of the variable name
while (end < fmt.length() &&
(isalnum(fmt[end]) || fmt[end] == '_')) {
end++;
}
if (end > start) {
std::string var = fmt.substr(start, end - start);
auto it = vars.find(var);
if (it != vars.end()) {
result += it->second;
} else {
// Keep the original %var if not found
result += fmt.substr(i, end - i);
}
i = end - 1;
continue;
}
}
// If we get here, it's a single % with no variable
result += '%';
}
return result;
}
template <typename Out>
void split(const std::string &s, char delim, Out result, int limit) {
int it = 0;
@@ -133,6 +183,22 @@ std::string concatPath(std::string dir1, std::string dir2) {
return std::filesystem::weakly_canonical(dir1+"/"+dir2).string();
}
std::string getCurrentUTCTime() {
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::string result(9, '\0');
strftime(&result[0], result.size(), "%H:%M:%S", gmtime(&time));
return result;
}
std::string getCurrentUTCDate() {
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::string result(11, '\0');
strftime(&result[0], result.size(), "%Y-%m-%d", gmtime(&time));
return result;
}
static char* trim(char *str) {
char *end;
while(isspace(*str))