qswiki/utils.cpp

217 行
5.2 KiB
C++

2020-04-19 22:45:51 +02:00
/* Copyright (c) 2018-2020 Albert S.
2018-11-03 17:12:20 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <sstream>
#include <string>
#include <regex>
#include <map>
#include <cstdlib>
#include <fstream>
#include "logger.h"
#include "utils.h"
2018-11-03 17:12:20 +01:00
std::string utils::html_xss(std::string_view str)
{
std::string result;
2020-04-19 22:45:51 +02:00
for(char c : str)
2018-11-03 17:12:20 +01:00
{
2020-04-19 22:45:51 +02:00
switch(c)
2018-11-03 17:12:20 +01:00
{
2020-04-19 22:45:51 +02:00
case '<':
result += "&lt;";
break;
case '>':
result += "&gt;";
break;
case '\"':
result += "&quot;";
break;
case '%':
result += "&#37;";
break;
case '\'':
result += "&#x27;";
break;
case '&':
result += "&amp;";
break;
2020-04-19 22:45:51 +02:00
default:
2018-11-03 17:12:20 +01:00
result += c;
}
}
2020-04-19 22:45:51 +02:00
2018-11-03 17:12:20 +01:00
return result;
}
std::string utils::urldecode(std::string_view str)
{
std::string result;
int size = str.length();
for(int i = 0; i < size; i++)
{
char c = str[i];
if(c == '%' && (size - i > 1))
{
char h[3];
h[0] = str[i + 1];
h[1] = str[i + 2];
h[2] = 0;
if(std::isxdigit(h[0]) && std::isxdigit(h[1]))
{
c = std::stoi(h, 0, 16);
i += 2;
}
}
result += c;
}
return result;
}
std::vector<std::string> utils::split(std::string str, char delim)
2018-11-03 17:12:20 +01:00
{
std::vector<std::string> result;
std::stringstream stream(str);
std::string item;
while(std::getline(stream, item, delim))
{
result.push_back(item);
}
return result;
}
// TODO: can easily break if we pass a regex here
std::vector<std::string> utils::split(std::string str, const std::string &delim)
2018-11-03 17:12:20 +01:00
{
std::regex regex{delim + "+"};
return split(str, regex);
2018-11-03 17:12:20 +01:00
}
2020-04-19 22:45:51 +02:00
std::vector<std::string> utils::split(const std::string &str, std::regex &regex)
2018-11-03 17:12:20 +01:00
{
std::vector<std::string> result;
std::regex reg(regex);
std::copy(std::sregex_token_iterator(str.begin(), str.end(), reg, -1), std::sregex_token_iterator(),
std::back_inserter(result));
return result;
}
std::string utils::strreplace(std::string str, const std::string &search, const std::string &replace)
2018-11-03 17:12:20 +01:00
{
std::string result = str;
auto searchlength = search.length();
auto replacelength = replace.length();
size_t pos = 0;
while((pos = result.find(search, pos)) != std::string::npos)
{
result = result.replace(pos, searchlength, replace);
pos += replacelength;
}
return result;
}
std::string utils::getenv(const std::string &key)
{
const char *result = ::getenv(key.c_str());
if(result == nullptr)
return std::string();
return std::string{result};
}
std::string utils::readCompleteFile(std::string_view filepath)
{
std::fstream stream(std::string{filepath});
if(!stream.is_open())
{
throw std::runtime_error("stream is not open");
}
2018-11-03 17:12:20 +01:00
std::stringstream ss;
ss << stream.rdbuf();
std::string content = ss.str();
return content;
}
2020-03-18 22:00:15 +01:00
std::string utils::regex_callback_replacer(std::regex regex, const std::string &input,
std::function<std::string(std::smatch &)> callback)
2018-11-03 17:12:20 +01:00
{
std::string result;
2020-03-18 22:00:15 +01:00
auto tagsbegin = std::sregex_iterator(input.begin(), input.end(), regex);
auto tagsend = std::sregex_iterator();
2018-11-03 17:12:20 +01:00
auto matchbegin = 0;
2020-03-18 22:00:15 +01:00
for(std::sregex_iterator i = tagsbegin; i != tagsend; ++i)
2018-11-03 17:12:20 +01:00
{
2020-03-18 22:00:15 +01:00
std::smatch match = *i;
2018-11-03 17:12:20 +01:00
auto matchlength = match.length(0);
auto matchpos = match.position();
result += input.substr(matchbegin, matchpos - matchbegin);
result += callback(match);
matchbegin = matchpos + matchlength;
}
result += input.substr(matchbegin);
return result;
}
2022-03-27 08:29:13 +02:00
/* TODO: Convert to C++20, but currently the state is rather poor and would
* require workarounds, so keep it this way for now, and do it properly
* once compiler support gets there */
std::string utils::formatLocalDate(time_t t, std::string format)
2018-11-03 17:12:20 +01:00
{
struct tm lt;
if(localtime_r(&t, &lt) == nullptr)
2018-11-03 17:12:20 +01:00
{
return {};
}
char result[20];
2022-03-27 08:29:13 +02:00
size_t x = strftime(result, sizeof(result), format.c_str(), &lt);
2018-11-03 17:12:20 +01:00
if(x == 0)
{
return {};
}
return std::string{result};
}
2021-10-03 16:47:35 +02:00
2022-03-27 08:29:13 +02:00
std::string utils::toISODateTime(time_t t)
{
return utils::formatLocalDate(t, "%Y-%m-%d %H:%M:%S");
}
std::string utils::toISODate(time_t t)
{
return utils::formatLocalDate(t, "%Y-%m-%d");
}
2021-10-10 12:01:16 +02:00
std::string utils::trim(std::string_view view)
2021-10-03 16:47:35 +02:00
{
std::string_view chars = " \t\n\r";
auto n = view.find_first_not_of(chars);
if(n != std::string_view::npos)
{
view.remove_prefix(n);
}
n = view.find_last_not_of(chars);
if(n != std::string_view::npos)
{
view.remove_suffix(view.size() - n - 1);
}
return std::string{view};
}