2018-05-15 10:22:46 +00:00
|
|
|
/*
|
2019-05-06 20:51:09 +00:00
|
|
|
* This file is part of Checkpoint
|
|
|
|
* Copyright (C) 2017-2019 Bernardo Giordano, FlagBrew
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
|
|
|
|
* * Requiring preservation of specified reasonable legal notices or
|
|
|
|
* author attributions in that material or in the Appropriate Legal
|
|
|
|
* Notices displayed by works containing it.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
|
|
|
*/
|
2018-05-15 10:22:46 +00:00
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
std::string DateTime::timeStr(void)
|
|
|
|
{
|
2019-07-09 21:13:23 +00:00
|
|
|
time_t unixTime;
|
|
|
|
struct tm timeStruct;
|
|
|
|
time(&unixTime);
|
|
|
|
localtime_r(&unixTime, &timeStruct);
|
|
|
|
return StringUtils::format("%02i:%02i:%02i", timeStruct.tm_hour, timeStruct.tm_min, timeStruct.tm_sec);
|
2018-05-15 10:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DateTime::dateTimeStr(void)
|
2019-05-27 19:28:33 +00:00
|
|
|
{
|
|
|
|
time_t unixTime;
|
2019-07-09 21:13:23 +00:00
|
|
|
struct tm timeStruct;
|
2019-05-27 19:28:33 +00:00
|
|
|
time(&unixTime);
|
2019-07-09 21:13:23 +00:00
|
|
|
localtime_r(&unixTime, &timeStruct);
|
|
|
|
return StringUtils::format("%04i%02i%02i-%02i%02i%02i", timeStruct.tm_year + 1900, timeStruct.tm_mon + 1, timeStruct.tm_mday, timeStruct.tm_hour,
|
|
|
|
timeStruct.tm_min, timeStruct.tm_sec);
|
2019-05-27 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 22:27:04 +00:00
|
|
|
std::string DateTime::logDateTime(void)
|
|
|
|
{
|
|
|
|
time_t unixTime;
|
|
|
|
struct tm timeStruct;
|
|
|
|
time(&unixTime);
|
|
|
|
localtime_r(&unixTime, &timeStruct);
|
|
|
|
return StringUtils::format("%04i-%02i-%02i %02i:%02i:%02i", timeStruct.tm_year + 1900, timeStruct.tm_mon + 1, timeStruct.tm_mday,
|
|
|
|
timeStruct.tm_hour, timeStruct.tm_min, timeStruct.tm_sec);
|
|
|
|
}
|
|
|
|
|
2018-05-15 10:22:46 +00:00
|
|
|
std::string StringUtils::UTF16toUTF8(const std::u16string& src)
|
|
|
|
{
|
2019-05-06 20:51:09 +00:00
|
|
|
static std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
2018-05-15 10:22:46 +00:00
|
|
|
std::string dst = convert.to_bytes(src);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StringUtils::removeForbiddenCharacters(std::string src)
|
|
|
|
{
|
|
|
|
static const std::string illegalChars = ".,!\\/:?*\"<>|";
|
2019-05-06 20:51:09 +00:00
|
|
|
for (size_t i = 0, sz = src.length(); i < sz; i++) {
|
|
|
|
if (illegalChars.find(src[i]) != std::string::npos) {
|
2018-05-15 10:22:46 +00:00
|
|
|
src[i] = ' ';
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 20:51:09 +00:00
|
|
|
|
2018-05-15 10:22:46 +00:00
|
|
|
size_t i;
|
2019-05-06 20:51:09 +00:00
|
|
|
for (i = src.length() - 1; i > 0 && src[i] == ' '; i--)
|
|
|
|
;
|
2018-05-15 10:22:46 +00:00
|
|
|
src.erase(i + 1, src.length() - i);
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StringUtils::format(const std::string fmt_str, ...)
|
|
|
|
{
|
2018-08-05 15:26:18 +00:00
|
|
|
va_list ap;
|
2019-05-06 20:51:09 +00:00
|
|
|
char* fp = NULL;
|
2018-08-05 15:26:18 +00:00
|
|
|
va_start(ap, fmt_str);
|
|
|
|
vasprintf(&fp, fmt_str.c_str(), ap);
|
|
|
|
va_end(ap);
|
|
|
|
std::unique_ptr<char[]> formatted(fp);
|
2018-08-05 15:36:50 +00:00
|
|
|
return std::string(formatted.get());
|
2018-05-15 10:22:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 05:24:28 +00:00
|
|
|
bool StringUtils::containsInvalidChar(const std::string& str)
|
|
|
|
{
|
2019-05-06 20:51:09 +00:00
|
|
|
for (size_t i = 0, sz = str.length(); i < sz; i++) {
|
|
|
|
if (!isascii(str[i])) {
|
2018-07-23 05:24:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-04-12 21:39:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 20:51:09 +00:00
|
|
|
void StringUtils::ltrim(std::string& s)
|
2019-04-12 21:39:55 +00:00
|
|
|
{
|
2019-05-06 20:51:09 +00:00
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
|
2019-04-12 21:39:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 20:51:09 +00:00
|
|
|
void StringUtils::rtrim(std::string& s)
|
2019-04-12 21:39:55 +00:00
|
|
|
{
|
2019-05-06 20:51:09 +00:00
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
|
2019-04-12 21:39:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 20:51:09 +00:00
|
|
|
void StringUtils::trim(std::string& s)
|
2019-04-12 21:39:55 +00:00
|
|
|
{
|
|
|
|
ltrim(s);
|
|
|
|
rtrim(s);
|
2019-05-04 10:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* getConsoleIP(void)
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
in.s_addr = gethostid();
|
2019-05-06 20:51:09 +00:00
|
|
|
return inet_ntoa(in);
|
2018-05-15 10:22:46 +00:00
|
|
|
}
|