#include #include #include #include #include "util.hpp" #include #include #ifndef WIN32 #include #define stat _stat #endif namespace util { 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; } bool validinteger(const std::string &value) { char *endptr; strtoll(value.c_str(), &endptr, 10); return (endptr == value.c_str() + value.size()); } 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; } } std::string ltrim(std::string s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); return s; } std::string rtrim(std::string s) { s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(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) { 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; } 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 ""; } 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 hash functions are part of 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); \ } std::ostream & operator << (std::ostream &out, const XYZ &xyz) { out << "(" << xyz.x << "," << xyz.y << "," << xyz.z << ")"; return out; } } // namespace util