diff options
author | thefosk <marco@mashape.com> | 2015-11-06 21:22:07 -0800 |
---|---|---|
committer | thefosk <marco@mashape.com> | 2015-11-06 21:22:07 -0800 |
commit | b1696f33dd7ae2b621ad8d04f1547df57731d6d0 (patch) | |
tree | f77bd2bb6215647d2681ae62f091aa813ec44fcb /lua_uuid.c | |
download | lua_uuid-b1696f33dd7ae2b621ad8d04f1547df57731d6d0.tar.gz lua_uuid-b1696f33dd7ae2b621ad8d04f1547df57731d6d0.tar.bz2 lua_uuid-b1696f33dd7ae2b621ad8d04f1547df57731d6d0.zip |
0.1-1 version
Diffstat (limited to 'lua_uuid.c')
-rw-r--r-- | lua_uuid.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lua_uuid.c b/lua_uuid.c new file mode 100644 index 0000000..e0eca64 --- /dev/null +++ b/lua_uuid.c @@ -0,0 +1,42 @@ +/*** +Utility for generating UUIDs + +@license MIT +@module lua_uuid +*/ +#define LUA_LIB +#include <lua.h> +#include <lauxlib.h> +#include <uuid/uuid.h> + +#if LUA_VERSION_NUM < 502 +# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) +# define lua_rawlen lua_objlen +#endif + +/// Generate an UUID +// @return uuid_str +// @function generate() +static int generate(lua_State *L) { + + // Generate UUID + uuid_t uuid; + uuid_generate(uuid); + + // Unparse to a string + char uuid_str[37]; // For example: "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0" + uuid_unparse_lower(uuid, uuid_str); + + lua_pushstring(L, uuid_str); + return 1; +} + +static const luaL_Reg lua_uuid[] = { + {"generate", generate}, + {NULL, NULL} +}; + +int luaopen_lua_uuid(lua_State *L){ + luaL_newlib(L, lua_uuid); + return 1; +}
\ No newline at end of file |