iw4x-client/src/Utils/Utils.cpp

288 lines
5.8 KiB
C++
Raw Normal View History

#include "STDInclude.hpp"
2015-12-23 08:45:53 -05:00
#define VA_BUFFER_COUNT 4
2016-01-01 20:28:09 -05:00
#define VA_BUFFER_SIZE 65536
2015-12-23 08:45:53 -05:00
namespace Utils
{
std::string GetMimeType(std::string url)
{
wchar_t* mimeType = nullptr;
FindMimeFromData(NULL, std::wstring(url.begin(), url.end()).data(), NULL, 0, NULL, 0, &mimeType, 0);
if (mimeType)
{
std::wstring wMimeType(mimeType);
return std::string(wMimeType.begin(), wMimeType.end());
}
return "application/octet-stream";
}
2015-12-23 08:45:53 -05:00
const char *VA(const char *fmt, ...)
{
static char g_vaBuffer[VA_BUFFER_COUNT][VA_BUFFER_SIZE];
static int g_vaNextBufferIndex = 0;
va_list ap;
va_start(ap, fmt);
char* dest = g_vaBuffer[g_vaNextBufferIndex];
2016-01-01 20:28:09 -05:00
vsnprintf(g_vaBuffer[g_vaNextBufferIndex], VA_BUFFER_SIZE, fmt, ap);
2015-12-23 08:45:53 -05:00
g_vaNextBufferIndex = (g_vaNextBufferIndex + 1) % VA_BUFFER_COUNT;
va_end(ap);
return dest;
}
std::string StrToLower(std::string input)
{
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
return input;
}
2015-12-24 08:28:08 -05:00
2016-06-28 13:38:14 -04:00
std::string StrToUpper(std::string input)
{
std::transform(input.begin(), input.end(), input.begin(), ::toupper);
return input;
}
bool EndsWith(std::string haystack, std::string needle)
2015-12-24 08:28:08 -05:00
{
return (strstr(haystack.data(), needle.data()) == (haystack.data() + haystack.size() - needle.size()));
2015-12-24 08:28:08 -05:00
}
2015-12-24 11:30:36 -05:00
2016-02-22 17:35:53 -05:00
std::string DumpHex(std::string data, std::string separator)
{
std::string result;
for (unsigned int i = 0; i < data.size(); ++i)
{
if (i > 0)
{
2016-02-22 17:35:53 -05:00
result.append(separator);
}
result.append(Utils::VA("%02X", data[i] & 0xFF));
}
return result;
}
2016-06-28 05:30:49 -04:00
std::string XORString(std::string str, char value)
{
for (unsigned int i = 0; i < str.size(); ++i)
{
str[i] ^= value;
}
return str;
}
2016-02-08 12:43:31 -05:00
// Complementary function for memset, which checks if a memory is set
bool MemIsSet(void* mem, char chr, size_t length)
{
char* memArr = reinterpret_cast<char*>(mem);
for (size_t i = 0; i < length; ++i)
2016-02-08 12:43:31 -05:00
{
if (memArr[i] != chr)
{
return false;
}
}
return true;
}
2015-12-25 15:42:35 -05:00
std::vector<std::string> Explode(const std::string& str, char delim)
2015-12-24 11:30:36 -05:00
{
2015-12-25 15:42:35 -05:00
std::vector<std::string> result;
std::istringstream iss(str);
2015-12-24 11:30:36 -05:00
2015-12-25 15:42:35 -05:00
for (std::string token; std::getline(iss, token, delim);)
2015-12-24 11:30:36 -05:00
{
2015-12-27 22:02:30 -05:00
std::string _entry = std::move(token);
// Remove trailing 0x0 bytes
while (_entry.size() && !_entry[_entry.size() - 1])
{
_entry = _entry.substr(0, _entry.size() - 1);
}
result.push_back(_entry);
2015-12-24 11:30:36 -05:00
}
2015-12-25 15:42:35 -05:00
return result;
2015-12-24 11:30:36 -05:00
}
void Replace(std::string &string, std::string find, std::string replace)
{
size_t nPos = 0;
while ((nPos = string.find(find, nPos)) != std::string::npos)
{
string = string.replace(nPos, find.length(), replace);
nPos += replace.length();
}
}
2016-01-10 09:48:49 -05:00
bool StartsWith(std::string haystack, std::string needle)
{
return (haystack.size() >= needle.size() && !strncmp(needle.data(), haystack.data(), needle.size()));
}
2016-01-01 14:01:19 -05:00
// trim from start
std::string &LTrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
std::string &RTrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
std::string &Trim(std::string &s)
{
return LTrim(RTrim(s));
}
2016-01-10 09:48:49 -05:00
std::string FormatTimeSpan(int milliseconds)
{
int secondsTotal = milliseconds / 1000;
int seconds = secondsTotal % 60;
int minutesTotal = secondsTotal / 60;
int minutes = minutesTotal % 60;
int hoursTotal = minutesTotal / 60;
return Utils::VA("%02d:%02d:%02d", hoursTotal, minutes, seconds);
}
2016-01-04 05:32:05 -05:00
std::string ParseChallenge(std::string data)
{
2016-02-10 11:18:45 -05:00
auto pos = data.find_first_of("\n ");
if (pos == std::string::npos) return data;
return data.substr(0, pos).data();
2016-01-04 05:32:05 -05:00
}
2016-01-05 19:23:43 -05:00
// TODO: Use modern file reading methods
bool FileExists(std::string file)
{
FILE* fp;
fopen_s(&fp, file.data(), "r");
if (fp)
{
fclose(fp);
return true;
}
return false;
}
void WriteFile(std::string file, std::string data)
{
std::ofstream stream(file, std::ios::binary);
2016-01-28 15:37:48 -05:00
if (stream.is_open())
{
stream.write(data.data(), data.size());
stream.close();
}
2016-01-05 19:23:43 -05:00
}
std::string ReadFile(std::string file)
{
std::string buffer;
if (FileExists(file))
{
std::streamsize size = 0;
std::ifstream stream(file, std::ios::binary);
if (!stream.is_open()) return buffer;
2016-01-05 19:23:43 -05:00
stream.seekg(0, std::ios::end);
size = stream.tellg();
stream.seekg(0, std::ios::beg);
if (size > -1)
{
buffer.clear();
buffer.resize((uint32_t)size);
stream.read(const_cast<char*>(buffer.data()), size);
2016-01-05 19:23:43 -05:00
}
stream.close();
}
return buffer;
}
2015-12-24 11:30:36 -05:00
// Infostring class
void InfoString::Set(std::string key, std::string value)
{
this->KeyValuePairs[key] = value;
}
std::string InfoString::Get(std::string key)
{
if (this->KeyValuePairs.find(key) != this->KeyValuePairs.end())
{
return this->KeyValuePairs[key];
}
return "";
}
std::string InfoString::Build()
{
std::string infoString;
bool first = true;
2016-01-24 13:58:13 -05:00
for (auto i = this->KeyValuePairs.begin(); i != this->KeyValuePairs.end(); ++i)
2015-12-24 11:30:36 -05:00
{
if (first) first = false;
else infoString.append("\\");
infoString.append(i->first); // Key
infoString.append("\\");
infoString.append(i->second); // Value
}
return infoString;
}
2015-12-25 15:42:35 -05:00
void InfoString::Dump()
{
2016-01-24 13:58:13 -05:00
for (auto i = this->KeyValuePairs.begin(); i != this->KeyValuePairs.end(); ++i)
2015-12-25 15:42:35 -05:00
{
OutputDebugStringA(Utils::VA("%s: %s", i->first.data(), i->second.data()));
}
}
2016-06-05 11:34:55 -04:00
json11::Json InfoString::to_json()
{
return this->KeyValuePairs;
}
2015-12-24 11:30:36 -05:00
void InfoString::Parse(std::string buffer)
{
2015-12-27 22:02:30 -05:00
if (buffer[0] == '\\')
{
buffer = buffer.substr(1);
}
2015-12-25 15:42:35 -05:00
std::vector<std::string> KeyValues = Utils::Explode(buffer, '\\');
2015-12-24 11:30:36 -05:00
for (unsigned int i = 0; i < (KeyValues.size() - 1); i+=2)
{
this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1];
}
}
2016-06-14 13:49:27 -04:00
}