diff --git a/include/modules/murmur.h b/include/modules/murmur.h new file mode 100644 index 0000000..ccdc91f --- /dev/null +++ b/include/modules/murmur.h @@ -0,0 +1,127 @@ +/** + * Copyright 2017-2018, Pavel Kraynyukhov + * + * This file is a part of LAppS (Lua Application Server). + * + * LAppS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LAppS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LAppS. If not, see . + * + * $Id: murmur.h July 9, 2018 1:38 AM $ + * + **/ + + +#ifndef __MURMUR_H__ +# define __MURMUR_H__ + +#include +#include + +extern "C" { + #include + #include + #include + #include + #include + + LUA_API int hashstr(lua_State *L) + { + int argc=lua_gettop(L); + if(argc != 1) + { + lua_pushfstring(L,"Usage: murmur.hashstr(number|string); returns string. args now: ",argc); + lua_error(L); + } + int type=lua_type(L,argc); + switch(type) + { + case LUA_TSTRING: + { + std::hash hash; + std::string result; + std::string tmp(lua_tostring(L,argc)); + + long long unsigned int digest=hash(std::move(tmp)); + result.resize(16); + snprintf((char*)(result.c_str()),16,"%016llx",digest); + lua_pushlstring(L,result.c_str(),result.length()); + } + break; + case LUA_TNUMBER: + { + std::hash hash; + std::string result; + size_t tmp=static_cast(lua_tonumber(L,argc)); + long long unsigned int digest=hash(tmp); + result.resize(16); + snprintf((char*)(result.c_str()),16,"%016llx",digest); + lua_pushlstring(L,result.c_str(),result.length()); + } + break; + default: + lua_pushstring(L,"Usage: murmur.hashstr(number|string); returns string"); + lua_error(L); + } + return 1; + } + + LUA_API int hashnum(lua_State *L) + { + int argc=lua_gettop(L); + if(argc != 1) + { + lua_pushfstring(L,"Usage: murmur.hashnum(number|string); returns size_t. args now: ",argc); + lua_error(L); + } + int type=lua_type(L,argc); + switch(type) + { + case LUA_TSTRING: + { + std::hash hash; + std::string tmp(lua_tostring(L,argc)); + + long long unsigned int digest=hash(std::move(tmp)); + lua_pushinteger(L,digest); + } + break; + case LUA_TNUMBER: + { + std::hash hash; + size_t tmp=static_cast(lua_tonumber(L,argc)); + long long unsigned int digest=hash(tmp); + lua_pushinteger(L,digest); + } + break; + default: + lua_pushstring(L,"Usage: murmur.hashnum(number|string); returns size_t"); + lua_error(L); + } + return 1; + } + + LUA_API int luaopen_murmur(lua_State *L) + { + luaL_Reg functions[]= { + {"hashstr", hashstr }, + {"hashnum", hashnum }, + {nullptr,nullptr} + }; + luaL_register(L,"murmur",functions); + return 1; + } +} + + +#endif /* __MURMUR_H__ */ + diff --git a/include/modules/time_now.h b/include/modules/time_now.h new file mode 100644 index 0000000..c6d9530 --- /dev/null +++ b/include/modules/time_now.h @@ -0,0 +1,59 @@ +/** + * Copyright 2017-2018, Pavel Kraynyukhov + * + * This file is a part of LAppS (Lua Application Server). + * + * LAppS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LAppS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LAppS. If not, see . + * + * $Id: time_now.h July 9, 2018 1:51 AM $ + * + **/ + + +#ifndef __TIME_NOW_H__ +# define __TIME_NOW_H__ + + +#include + +extern "C" { + #include + #include + #include + + LUA_API int now(lua_State *L) + { + + uint64_t now=std::chrono::duration_cast( + std::chrono::time_point_cast(std::chrono::system_clock::now()).time_since_epoch() + ).count(); + + lua_pushinteger(L,now); + return 1; + } + + LUA_API int luaopen_time(lua_State *L) + { + luaL_Reg functions[]= { + {"now", now }, + {nullptr,nullptr} + }; + luaL_register(L,"time",functions); + return 1; + } +} + + +#endif /* __TIME_NOW_H__ */ +