2020-11-13 15:18:09 -05:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include "util.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#ifndef WIN32
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#define stat _stat
|
|
|
|
|
#endif
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
|
|
// Read a file as lines.
|
|
|
|
|
const stringvec read_lines(const std::string &path) {
|
|
|
|
|
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
|
|
|
// Get a string encoding the modification time and size of the file.
|
|
|
|
|
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 "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string get_file_contents(const std::string &fn) {
|
|
|
|
|
std::ifstream fs(fn);
|
|
|
|
|
std::stringstream buffer;
|
|
|
|
|
buffer << fs.rdbuf();
|
|
|
|
|
return buffer.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
// Strip leading and trailing whitespace and comments.
|
|
|
|
|
const stringvec trim_and_uncomment(const stringvec &lines) {
|
|
|
|
|
stringvec result;
|
2020-12-05 18:57:53 -05:00
|
|
|
for (int i = 0; i < int(lines.size()); i++) {
|
2020-11-13 15:18:09 -05:00
|
|
|
std::string trimmed = trim(lines[i]);
|
|
|
|
|
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
|
|
|
|
|
result.push_back(trimmed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// These are Bob Jenkins' Lookup3.
|
|
|
|
|
|
|
|
|
|
#define hash_rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
|
|
|
|
|
|
|
|
|
|
#define hash_mix(a,b,c) { \
|
|
|
|
|
a -= c; a ^= hash_rot(c, 4); c += b; \
|
|
|
|
|
b -= a; b ^= hash_rot(a, 6); a += c; \
|
|
|
|
|
c -= b; c ^= hash_rot(b, 8); b += a; \
|
|
|
|
|
a -= c; a ^= hash_rot(c,16); c += b; \
|
|
|
|
|
b -= a; b ^= hash_rot(a,19); a += c; \
|
|
|
|
|
c -= b; c ^= hash_rot(b, 4); b += a; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define hash_final(a,b,c) { \
|
|
|
|
|
c ^= b; c -= hash_rot(b,14); \
|
|
|
|
|
a ^= c; a -= hash_rot(c,11); \
|
|
|
|
|
b ^= a; b -= hash_rot(a,25); \
|
|
|
|
|
c ^= b; c -= hash_rot(b,16); \
|
|
|
|
|
a ^= c; a -= hash_rot(c,4); \
|
|
|
|
|
b ^= a; b -= hash_rot(a,14); \
|
|
|
|
|
c ^= b; c -= hash_rot(b,24); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t hash3(uint32_t a, uint32_t b, uint32_t c) {
|
|
|
|
|
hash_final(a, b, c);
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double hash_to_float(double lo, double hi, uint32_t a, uint32_t b, uint32_t c) {
|
|
|
|
|
double result = hash3(a, b, c); // Lossless.
|
|
|
|
|
result *= ((hi-lo) * (1.0 / 0xFFFFFFFF));
|
|
|
|
|
result += lo;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
} // namespace util
|