124 lines
3.6 KiB
C++
124 lines
3.6 KiB
C++
/*
|
|
Copyright© 2021 John Sennesael
|
|
|
|
UsenetSearch 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.
|
|
|
|
UsenetSearch 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 UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <algorithm>
|
|
#include <codecvt>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
#include <locale>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include "usenetsearch/StringUtils.h"
|
|
|
|
namespace usenetsearch {
|
|
|
|
std::ostream& operator<<(std::ofstream& out, const std::string& str)
|
|
{
|
|
const std::uint64_t size = str.size();
|
|
out.write(reinterpret_cast<const char*>(&size), sizeof(size));
|
|
out.write(reinterpret_cast<const char*>(str.c_str()), size);
|
|
return out;
|
|
}
|
|
|
|
std::ifstream& operator>>(std::ifstream& in, std::string& str)
|
|
{
|
|
std::uint64_t size{0};
|
|
in.read(reinterpret_cast<char*>(&size), sizeof(size));
|
|
char buf[size + 1];
|
|
in.read(buf, size);
|
|
buf[size] = 0;
|
|
str = buf;
|
|
return in;
|
|
}
|
|
|
|
std::ostream& operator<<(std::ofstream& out, const std::wstring& str)
|
|
{
|
|
const std::uint64_t size = str.size();
|
|
out.write(reinterpret_cast<const char*>(&size), sizeof(size));
|
|
out.write(
|
|
reinterpret_cast<const char*>(str.c_str()),
|
|
size * sizeof(wchar_t)
|
|
);
|
|
return out;
|
|
}
|
|
|
|
std::ifstream& operator>>(std::ifstream& in, std::wstring& str)
|
|
{
|
|
std::uint64_t size{0};
|
|
in.read(reinterpret_cast<char*>(&size), sizeof(size));
|
|
wchar_t buf[size + 1];
|
|
in.read(reinterpret_cast<char*>(buf), size * sizeof(wchar_t));
|
|
buf[size] = 0;
|
|
str = buf;
|
|
return in;
|
|
}
|
|
|
|
std::string StringHash(const std::string& input)
|
|
{
|
|
unsigned char result[MD5_DIGEST_LENGTH];
|
|
MD5(
|
|
reinterpret_cast<unsigned char*>(const_cast<char*>(input.c_str())),
|
|
input.size(),
|
|
result
|
|
);
|
|
std::ostringstream sout;
|
|
sout << std::hex << std::setfill('0');
|
|
for(long long c: result)
|
|
{
|
|
sout << std::setw(2) << reinterpret_cast<long long>(c);
|
|
}
|
|
return sout.str();
|
|
}
|
|
|
|
bool StringStartsWith(const std::string& needle, const std::string& haystack)
|
|
{
|
|
return (std::strncmp(haystack.c_str(),needle.c_str(),needle.size()) == 0);
|
|
}
|
|
|
|
bool StringStartsWith(const std::wstring& needle, const std::wstring& haystack)
|
|
{
|
|
return (std::wcsncmp(haystack.c_str(),needle.c_str(),needle.size()) == 0);
|
|
}
|
|
|
|
bool StringToBoolean(const std::string& str)
|
|
{
|
|
const std::string lstr = StringTrim(StringToLower(str));
|
|
if ((lstr == "true") || (lstr == "yes") || (lstr == "1")) return true;
|
|
if ((lstr == "false") || (lstr == "no") || (lstr == "0")) return false;
|
|
throw StringException(EINVAL,
|
|
"The string \"" + str + "\" is not a valid boolean value."
|
|
);
|
|
}
|
|
|
|
bool StringToBoolean(const std::wstring& str)
|
|
{
|
|
const std::wstring lstr = StringTrim(StringToLower(str));
|
|
if ((lstr == L"true") || (lstr == L"yes") || (lstr == L"1")) return true;
|
|
if ((lstr == L"false") || (lstr == L"no") || (lstr == L"0")) return false;
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
|
|
throw StringException(EINVAL,
|
|
"The string \"" + conv.to_bytes(str)
|
|
+ "\" is not a valid boolean value."
|
|
);
|
|
}
|
|
|
|
} // namespace usenetsearch
|