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>
|
2021-07-21 16:10:29 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <iomanip>
|
2021-10-04 17:45:18 -04:00
|
|
|
#include <cassert>
|
2021-07-21 16:10:29 -04:00
|
|
|
|
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-08-03 11:25:12 -04:00
|
|
|
|
2021-09-07 17:37:23 -04:00
|
|
|
static bool ascii_isalpha(char c) {
|
|
|
|
|
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool ascii_isdigit(char c) {
|
|
|
|
|
return ((c >= '0') && (c <= '9'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_identifier(const std::string &str) {
|
|
|
|
|
if (str.size() == 0) return false;
|
|
|
|
|
char c=str[0];
|
|
|
|
|
if ((!ascii_isalpha(c)) && (c!='_')) return false;
|
|
|
|
|
for (int i = 1; i < int(str.size()); i++) {
|
|
|
|
|
char c = str[i];
|
|
|
|
|
if ((!ascii_isalpha(c)) && (!ascii_isdigit(c)) && (c!='_')) return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 01:32:08 -04:00
|
|
|
void quote_string(const std::string &s, std::ostream *os) {
|
2021-09-09 18:23:17 -04:00
|
|
|
bool anysq = false;
|
|
|
|
|
bool anydq = false;
|
2021-09-08 01:32:08 -04:00
|
|
|
for (char c : s) {
|
2021-09-09 18:23:17 -04:00
|
|
|
if (c == '\'') anysq = true;
|
|
|
|
|
if (c == '"') anydq = true;
|
2021-09-08 01:32:08 -04:00
|
|
|
}
|
2021-09-09 18:23:17 -04:00
|
|
|
bool usesinglequote = (!anysq)||(anydq);
|
2021-09-08 01:32:08 -04:00
|
|
|
(*os) << (usesinglequote ? '\'' : '"');
|
|
|
|
|
for (char c : s) {
|
|
|
|
|
if (c >= 32) {
|
|
|
|
|
if (c == '"') {
|
|
|
|
|
(*os) << (usesinglequote ? "\"" : "\\\"");
|
|
|
|
|
} else if (c == '\'') {
|
|
|
|
|
(*os) << (usesinglequote ? "\\'" : "'");
|
|
|
|
|
} else {
|
|
|
|
|
(*os) << c;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
switch (c) {
|
|
|
|
|
case '\n': (*os) << "\\n"; break;
|
|
|
|
|
case '\t': (*os) << "\\t"; break;
|
|
|
|
|
case '\r': (*os) << "\\r"; break;
|
|
|
|
|
default:
|
|
|
|
|
(*os) << "\\" << std::setw(3) << int(c);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(*os) << (usesinglequote ? '\'' : '"');
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
IdVector id_vector_create(int64_t id1, int64_t id2, int64_t id3, int64_t id4) {
|
|
|
|
|
IdVector result;
|
|
|
|
|
if (id1 >= 0) result.push_back(id1);
|
|
|
|
|
if (id2 >= 0) result.push_back(id2);
|
|
|
|
|
if (id3 >= 0) result.push_back(id3);
|
|
|
|
|
if (id4 >= 0) result.push_back(id4);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-08-03 11:25:12 -04:00
|
|
|
|
|
|
|
|
std::string id_vector_debug_string(const IdVector &idv) {
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
bool first = true;
|
|
|
|
|
for (int64_t id : idv) {
|
|
|
|
|
if (!first) oss << ",";
|
|
|
|
|
oss << id;
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2) {
|
|
|
|
|
IdVector result(v1.size() + v2.size());
|
|
|
|
|
int next = 0;
|
|
|
|
|
for (int64_t id : v1) result[next++] = id;
|
|
|
|
|
for (int64_t id : v2) result[next++] = id;
|
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
|
int64_t prev = -1;
|
|
|
|
|
int64_t count = 0;
|
|
|
|
|
for (int64_t id : result) {
|
|
|
|
|
if (id != prev) {
|
|
|
|
|
prev = id;
|
|
|
|
|
result[count++] = id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.resize(count);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 12:40:21 -04:00
|
|
|
HashValue hash_string(const std::string &s) {
|
|
|
|
|
uint64_t hash1 = 0;
|
|
|
|
|
uint64_t hash2 = 0;
|
|
|
|
|
SpookyHash::Hash128(s.c_str(), s.size(), &hash1, &hash2);
|
|
|
|
|
return util::HashValue(hash1, hash2);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 12:54:32 -04:00
|
|
|
HashValue hash_id_vector(const IdVector &idv) {
|
|
|
|
|
uint64_t hash1 = 0;
|
|
|
|
|
uint64_t hash2 = 0;
|
|
|
|
|
SpookyHash::Hash128(&idv[0], idv.size() * sizeof(int64_t), &hash1, &hash2);
|
2021-09-23 12:40:21 -04:00
|
|
|
return util::HashValue(hash1, hash2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string hash_to_hex(const HashValue &hv) {
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
oss << std::hex << std::setw(16) << std::setfill('0') << hv.first;
|
|
|
|
|
oss << std::hex << std::setw(16) << std::setfill('0') << hv.second;
|
|
|
|
|
return oss.str();
|
2021-08-09 12:54:32 -04:00
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
StringVec split(const std::string &s, char sep) {
|
|
|
|
|
StringVec result;
|
2021-07-21 00:50:06 -04:00
|
|
|
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;
|
2021-07-30 13:22:23 -04:00
|
|
|
if (value.size() == 0) return false;
|
2021-02-10 16:22:24 -05:00
|
|
|
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;
|
2021-07-30 13:22:23 -04:00
|
|
|
if (value.size() == 0) return errval;
|
2021-02-02 16:29:07 -05:00
|
|
|
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;
|
2021-07-30 13:22:23 -04:00
|
|
|
if (value.size() == 0) return std::nan("");
|
2021-07-21 00:50:06 -04:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:22:23 -04: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-02-02 16:29:07 -05:00
|
|
|
|
2021-10-04 17:45:18 -04:00
|
|
|
static std::string get_file_contents(const std::string &fn) {
|
2021-02-02 16:29:07 -05:00
|
|
|
std::ifstream fs(fn);
|
|
|
|
|
std::stringstream buffer;
|
|
|
|
|
buffer << fs.rdbuf();
|
|
|
|
|
return buffer.str();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 17:45:18 -04:00
|
|
|
static StringVec read_control_lst(const std::string &path) {
|
|
|
|
|
StringVec lines = split(get_file_contents(path), '\n');
|
|
|
|
|
util::StringVec result;
|
|
|
|
|
for (int i = 0; i < int(lines.size()); i++) {
|
|
|
|
|
std::string trimmed = trim(lines[i]);
|
|
|
|
|
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
|
|
|
|
|
result.push_back(trimmed);
|
2020-11-13 15:18:09 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 12:54:37 -04:00
|
|
|
LuaSourcePtr read_lua_source(const std::string &dir) {
|
2021-10-04 17:45:18 -04:00
|
|
|
StringVec files = read_control_lst(dir + "/control.lst");
|
|
|
|
|
assert (!files.empty());
|
2021-10-05 12:54:37 -04:00
|
|
|
LuaSourcePtr result(new LuaSourceVec);
|
2021-10-04 17:45:18 -04:00
|
|
|
for (const std::string &file : files) {
|
|
|
|
|
std::string data = get_file_contents(dir + "/" + file);
|
2021-10-05 12:54:37 -04:00
|
|
|
result->emplace_back(file, data);
|
2020-12-05 18:57:53 -05:00
|
|
|
}
|
2021-10-05 12:54:37 -04:00
|
|
|
return std::move(result);
|
2020-12-05 18:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
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
|
2021-07-21 16:10:29 -04:00
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex64 &v) {
|
|
|
|
|
oss << "0x" << std::setw(16) << std::setfill('0') << std::hex;
|
|
|
|
|
return oss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex32 &v) {
|
|
|
|
|
oss << "0x" << std::setw(8) << std::setfill('0') << std::hex;
|
|
|
|
|
return oss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex16 &v) {
|
|
|
|
|
oss << "0x" << std::setw(4) << std::setfill('0') << std::hex;
|
|
|
|
|
return oss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex8 &v) {
|
|
|
|
|
oss << "0x" << std::setw(2) << std::setfill('0') << std::hex;
|
|
|
|
|
return oss;
|
|
|
|
|
}
|
2021-07-30 13:22:23 -04:00
|
|
|
|
|
|
|
|
LuaDefine(unittests_util, "c") {
|
|
|
|
|
// Test the unioning of ID vectors.
|
|
|
|
|
util::IdVector idv1,idv2;
|
|
|
|
|
idv1.push_back(1);
|
|
|
|
|
idv1.push_back(6);
|
|
|
|
|
idv1.push_back(4);
|
|
|
|
|
idv2.push_back(5);
|
|
|
|
|
idv2.push_back(1);
|
|
|
|
|
idv2.push_back(6);
|
|
|
|
|
util::IdVector joined = util::sort_union_id_vectors(idv1, idv2);
|
|
|
|
|
LuaAssert(L, joined.size() == 4);
|
|
|
|
|
LuaAssert(L, joined[0] == 1);
|
|
|
|
|
LuaAssert(L, joined[1] == 4);
|
|
|
|
|
LuaAssert(L, joined[2] == 5);
|
|
|
|
|
LuaAssert(L, joined[3] == 6);
|
|
|
|
|
|
|
|
|
|
// Test the string split routine.
|
|
|
|
|
util::StringVec sv1 = util::split("foo,bar,baz", ',');
|
|
|
|
|
LuaAssert(L, sv1.size() == 3);
|
|
|
|
|
LuaAssert(L, sv1[0] == "foo");
|
|
|
|
|
LuaAssert(L, sv1[1] == "bar");
|
|
|
|
|
LuaAssert(L, sv1[2] == "baz");
|
|
|
|
|
util::StringVec sv2 = util::split(",foo,,bar", ',');
|
|
|
|
|
LuaAssert(L, sv2.size() == 4);
|
|
|
|
|
LuaAssert(L, sv2[0]=="");
|
|
|
|
|
LuaAssert(L, sv2[1]=="foo");
|
|
|
|
|
LuaAssert(L, sv2[2]=="");
|
|
|
|
|
LuaAssert(L, sv2[3]=="bar");
|
|
|
|
|
|
|
|
|
|
// test toupper and tolower
|
|
|
|
|
LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
|
|
|
|
|
LuaAssert(L, util::tolower("fooBar") == "foobar");
|
|
|
|
|
|
|
|
|
|
// test validinteger, strtoint, strtodouble
|
|
|
|
|
LuaAssert(L, util::validinteger("123") == true);
|
|
|
|
|
LuaAssert(L, util::validinteger("123.4") == false);
|
|
|
|
|
LuaAssert(L, util::validinteger("12ab") == false);
|
|
|
|
|
LuaAssert(L, util::validinteger("") == false);
|
|
|
|
|
LuaAssert(L, util::strtoint("123", -5) == 123);
|
|
|
|
|
LuaAssert(L, util::strtoint("123.4", -5) == -5);
|
|
|
|
|
LuaAssert(L, util::strtoint("12ab", -5) == -5);
|
|
|
|
|
LuaAssert(L, util::strtoint("", -5) == -5);
|
|
|
|
|
LuaAssert(L, util::strtodouble("123.5") == 123.5);
|
|
|
|
|
LuaAssert(L, std::isnan(util::strtodouble("12ab")));
|
|
|
|
|
LuaAssert(L, std::isnan(util::strtodouble("")));
|
|
|
|
|
|
|
|
|
|
// Test trim, ltrim, rtrim
|
|
|
|
|
LuaAssert(L, util::ltrim(" foo ") == "foo ");
|
|
|
|
|
LuaAssert(L, util::rtrim(" foo ") == " foo");
|
|
|
|
|
LuaAssert(L, util::trim(" foo ") == "foo");
|
|
|
|
|
LuaAssert(L, util::trim("foo") == "foo");
|
|
|
|
|
LuaAssert(L, util::trim("") == "");
|
|
|
|
|
|
|
|
|
|
// Test distance_squared
|
|
|
|
|
LuaAssert(L, util::distance_squared(1, 1, 5, 4) == 25.0);
|
|
|
|
|
LuaAssert(L, util::distance_squared(5, 4, 1, 1) == 25.0);
|
|
|
|
|
|
|
|
|
|
// Test XYZ.
|
|
|
|
|
util::XYZ xyza(3,4,5), xyzb(3,4,5), xyzc(3,4,6);
|
|
|
|
|
LuaAssert(L, xyza.x == 3);
|
|
|
|
|
LuaAssert(L, xyza.y == 4);
|
|
|
|
|
LuaAssert(L, xyza.z == 5);
|
|
|
|
|
LuaAssert(L, xyza == xyzb);
|
|
|
|
|
LuaAssert(L, xyza != xyzc);
|
|
|
|
|
LuaAssert(L, xyza.debug_string() == "(3,4,5)");
|
|
|
|
|
|
2021-09-23 12:40:21 -04:00
|
|
|
// Test hash_to_string
|
|
|
|
|
LuaAssertStrEq(L, util::hash_to_hex(util::HashValue(0x1234,0x789a)),
|
|
|
|
|
"0000000000001234000000000000789a");
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|