2022-03-21 21:52:44 -04:00
|
|
|
#include "Utils.h"
|
2022-05-11 13:18:24 -04:00
|
|
|
#include <cstring>
|
2022-08-09 23:15:07 -04:00
|
|
|
#include <algorithm>
|
2022-05-11 13:18:24 -04:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define strdup _strdup
|
|
|
|
#endif
|
2022-03-21 21:52:44 -04:00
|
|
|
|
2022-07-31 23:22:02 -04:00
|
|
|
namespace Ship {
|
|
|
|
namespace Math {
|
|
|
|
float clamp(float d, float min, float max) {
|
|
|
|
const float t = d < min ? min : d;
|
|
|
|
return t > max ? max : t;
|
|
|
|
}
|
2022-07-05 23:53:42 -04:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:52:44 -04:00
|
|
|
std::vector<std::string> SplitText(const std::string text, char separator = ' ', bool keep_quotes = false) {
|
|
|
|
std::vector<std::string> args;
|
2022-05-11 13:18:24 -04:00
|
|
|
char* input = strdup(text.c_str());
|
2022-03-21 21:52:44 -04:00
|
|
|
const size_t length = strlen(input);
|
|
|
|
|
|
|
|
bool inQuotes = false;
|
|
|
|
size_t count = 0, from = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
if (input[i] == '"') {
|
|
|
|
inQuotes = !inQuotes;
|
|
|
|
}
|
|
|
|
else if (input[i] == separator && !inQuotes) {
|
|
|
|
size_t strlen = i - from;
|
|
|
|
|
|
|
|
if (strlen > 0) {
|
|
|
|
if (!keep_quotes && input[from] == '"' && input[i - 1] == '"') {
|
|
|
|
from++; strlen -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* tmp = new char[strlen + 1]();
|
|
|
|
strncpy(tmp, &input[from], strlen);
|
|
|
|
count++;
|
|
|
|
args.emplace_back(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
from = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from < length) {
|
|
|
|
size_t strlen = length - from;
|
|
|
|
|
|
|
|
if (!keep_quotes && input[from] == L'"' && input[length - 1] == L'"') {
|
|
|
|
from++; strlen -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* tmp = new char[strlen + 1]();
|
|
|
|
strncpy(tmp, &input[from], strlen);
|
|
|
|
count++;
|
|
|
|
args.emplace_back(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
2022-08-09 23:15:07 -04:00
|
|
|
|
|
|
|
std::string toLowerCase(std::string in) {
|
|
|
|
std::string cpy(in);
|
|
|
|
std::transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower);
|
|
|
|
return cpy;
|
|
|
|
}
|
2022-03-21 21:52:44 -04:00
|
|
|
}
|