Files
fluxengine/lib/utils.cc
2022-03-04 23:42:29 +01:00

70 lines
1.6 KiB
C++

#include "globals.h"
#include "utils.h"
bool emergencyStop = false;
static const char* WHITESPACE = " \t\n\r\f\v";
static const char* SEPARATORS = "/\\";
void ErrorException::print() const
{
std::cerr << message << '\n';
}
bool beginsWith(const std::string& value, const std::string& ending)
{
if (ending.size() > value.size())
return false;
return std::equal(ending.begin(), ending.end(), value.begin());
}
// Case-insensitive for endings within ASCII.
bool endsWith(const std::string& value, const std::string& ending)
{
if (ending.size() > value.size())
return false;
std::string lowercase(ending.size(), 0);
std::transform(value.rbegin(), value.rbegin() + ending.size(), lowercase.begin(), [](unsigned char c){ return std::tolower(c); });
return std::equal(ending.rbegin(), ending.rend(), value.rbegin()) ||
std::equal(ending.rbegin(), ending.rend(), lowercase.begin());
}
std::string leftTrimWhitespace(std::string value)
{
value.erase(0, value.find_first_not_of(WHITESPACE));
return value;
}
std::string rightTrimWhitespace(std::string value)
{
value.erase(value.find_last_not_of(WHITESPACE) + 1);
return value;
}
std::string trimWhitespace(const std::string& value)
{
return leftTrimWhitespace(rightTrimWhitespace(value));
}
std::string getLeafname(const std::string& value)
{
constexpr char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = value.find_last_of(SEPARATORS);
if (i != std::string::npos) {
return value.substr(i+1, value.length() - i);
}
return value;
}
void testForEmergencyStop()
{
if (emergencyStop)
throw EmergencyStopException();
}