Files
integration/luprex/core/cpp/util.hpp

117 lines
3.6 KiB
C++

#ifndef UTIL_HPP
#define UTIL_HPP
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <ostream>
#include <memory>
#include <tuple>
#include <utility>
#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<std::string>;
using StringPair = std::pair<std::string, std::string>;
using LuaSourceVec = std::vector<StringPair>;
using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
using HashValue = std::pair<uint64_t, uint64_t>;
using IdVector = std::vector<int64_t>;
// 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);
// Return N repetitions of string A
std::string repeat_string(const std::string &a, int n);
// Return the length of the common prefix of A and B.
int common_prefix_length(const std::string &a, const std::string &b);
// 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);
// Split a string of the form "hostname:port" into a hostname and a port.
// On failure, returns empty strings.
void split_host_port(const std::string &target, std::string &host, std::string &port);
// 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 the lua source code from the specified directory.
LuaSourcePtr read_lua_source(const std::string &directory);
// 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