#ifndef UTIL_HPP #define UTIL_HPP #include #include #include #include #include #include #include #include "luastack.hpp" #include "spookyv2.hpp" namespace util { enum WorldType { WORLD_TYPE_STANDALONE, WORLD_TYPE_C_SYNC, WORLD_TYPE_S_SYNC, WORLD_TYPE_MASTER, }; using StringVec = std::vector; using HashValue = std::pair; using IdVector = std::vector; // Return true if the string is a valid lua identifier. bool is_identifier(const std::string &str); // Output a string to a stream using Lua string escaping and quoting. void quote_string(const std::string &str, std::ostream *os); // ID vector quick create. IdVector id_vector_create(int64_t id1=-1, int64_t id2=-1, int64_t id3=-1, int64_t id4=-1); // ID vector debug string. std::string id_vector_debug_string(const IdVector &idv); // Unions and sorts two ID vectors. IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2); // Get a 64-bit hashvalue for a string. HashValue hash_string(const std::string &str); // Get a 64-bit hashvalue for an ID vector. HashValue hash_id_vector(const IdVector &idv); // Convert a hash to a hexadecimal string. std::string hash_to_hex(const HashValue &hash); // Split a string into multiple strings StringVec split(const std::string &s, char sep); // String to lowercase/uppercase. Ascii only, no unicode. std::string tolower(std::string input); std::string toupper(std::string input); // Return true if the string can be parsed as an integer. bool validinteger(const std::string &value); // String to integer. Returns errval if the number is not parseable. int64_t strtoint(const std::string &value, int64_t errval); // String to double. Returns NAN if the number is not parseable. double strtodouble(const std::string &value); // Trim strings: left end, right end, both ends. std::string ltrim(std::string s); std::string rtrim(std::string s); std::string trim(std::string s); // Calculate distance between two points double distance_squared(double x1, double y1, double x2, double y2); // Read a file as one big string. std::string get_file_contents(const std::string &path); // Read a file as a vector of lines. StringVec get_file_lines(const std::string &path); // Get a file's fingerprint - ie, size and modification time. std::string get_file_fingerprint(const std::string &path); // An XYZ coordinate, general purpose. struct XYZ { float x, y, z; XYZ() { x=0; y=0; z=0; } XYZ(float ix, float iy, float iz) { x=ix; y=iy; z=iz; } bool operator ==(const XYZ &o) const { return x==o.x && y == o.y && z==o.z; } bool operator !=(const XYZ &o) const { return x!=o.x || y != o.y || z!=o.z; } std::string debug_string() const; }; // These are formatting directives that can be sent to a std::ostream. class hex64 {}; class hex32 {}; class hex16 {}; class hex8 {}; } // namespace util std::ostream &operator<<(std::ostream &oss, const util::hex64 &v); std::ostream &operator<<(std::ostream &oss, const util::hex32 &v); std::ostream &operator<<(std::ostream &oss, const util::hex16 &v); std::ostream &operator<<(std::ostream &oss, const util::hex8 &v); #endif // UTIL_HPP