2020-11-13 15:18:09 -05:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <fstream>
|
2021-02-02 16:29:07 -05:00
|
|
|
#include <cstdlib>
|
2020-11-13 15:18:09 -05:00
|
|
|
#include "util.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
2021-07-21 00:50:06 -04:00
|
|
|
#include <cmath>
|
2020-12-05 18:57:53 -05:00
|
|
|
#ifndef WIN32
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#define stat _stat
|
|
|
|
|
#endif
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
2021-07-21 00:50:06 -04:00
|
|
|
stringvec split(const std::string &s, char sep) {
|
|
|
|
|
stringvec result;
|
|
|
|
|
int start = 0;
|
|
|
|
|
for (int i = 0; i < int(s.size()); i++) {
|
|
|
|
|
if (s[i] == sep) {
|
|
|
|
|
result.push_back(s.substr(start, i-start));
|
|
|
|
|
start = i+1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (start < int(s.size())) {
|
|
|
|
|
result.push_back(s.substr(start));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 15:35:31 -05:00
|
|
|
std::string tolower(std::string input) {
|
|
|
|
|
for (int i = 0; i < int(input.size()); i++) {
|
|
|
|
|
input[i] = std::tolower(input[i]);
|
|
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string toupper(std::string input) {
|
|
|
|
|
for (int i = 0; i < int(input.size()); i++) {
|
|
|
|
|
input[i] = std::toupper(input[i]);
|
|
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 16:22:24 -05:00
|
|
|
bool validinteger(const std::string &value) {
|
|
|
|
|
char *endptr;
|
|
|
|
|
strtoll(value.c_str(), &endptr, 10);
|
|
|
|
|
return (endptr == value.c_str() + value.size());
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-02 16:29:07 -05:00
|
|
|
int64_t strtoint(const std::string &value, int64_t errval) {
|
|
|
|
|
char *endptr;
|
|
|
|
|
int64_t result = strtoll(value.c_str(), &endptr, 10);
|
|
|
|
|
if (endptr == value.c_str() + value.size()) {
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
return errval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 00:50:06 -04:00
|
|
|
double strtodouble(const std::string &value) {
|
|
|
|
|
char *endptr;
|
|
|
|
|
double result = strtod(value.c_str(), &endptr);
|
|
|
|
|
if (endptr == value.c_str() + value.size()) {
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
return std::nan("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-02 16:29:07 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string trim(std::string s) {
|
|
|
|
|
return ltrim(rtrim(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string get_file_contents(const std::string &fn) {
|
|
|
|
|
std::ifstream fs(fn);
|
|
|
|
|
std::stringstream buffer;
|
|
|
|
|
buffer << fs.rdbuf();
|
|
|
|
|
return buffer.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringvec get_file_lines(const std::string &path) {
|
2020-11-13 15:18:09 -05:00
|
|
|
stringvec result;
|
|
|
|
|
std::ifstream f;
|
|
|
|
|
f.open(path);
|
|
|
|
|
if (f) {
|
|
|
|
|
std::string line;
|
|
|
|
|
while (std::getline(f, line)) {
|
|
|
|
|
result.push_back(line);
|
|
|
|
|
}
|
|
|
|
|
f.close();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 18:57:53 -05:00
|
|
|
std::string get_file_fingerprint(const std::string &fn) {
|
|
|
|
|
struct stat result;
|
|
|
|
|
if(stat(fn.c_str(), &result)==0)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << result.st_mtime;
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
double distance_squared(double x1, double y1, double x2, double y2) {
|
|
|
|
|
double dx = x1 - x2;
|
|
|
|
|
double dy = y1 - y2;
|
|
|
|
|
return dx*dx + dy*dy;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 00:50:06 -04:00
|
|
|
std::string XYZ::debug_string() const {
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
oss << "(" << x << "," << y << "," << z << ")";
|
|
|
|
|
return oss.str();
|
2021-02-02 16:29:07 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
} // namespace util
|