From b1696f33dd7ae2b621ad8d04f1547df57731d6d0 Mon Sep 17 00:00:00 2001 From: thefosk Date: Fri, 6 Nov 2015 21:22:07 -0800 Subject: 0.1-1 version --- .gitignore | 3 +++ LICENSE | 21 +++++++++++++++++++++ Makefile | 17 +++++++++++++++++ README.md | 14 ++++++++++++++ lua_uuid-0.1-1.rockspec | 25 +++++++++++++++++++++++++ lua_uuid.c | 42 ++++++++++++++++++++++++++++++++++++++++++ test/lua_uuid_test.lua | 8 ++++++++ 7 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 lua_uuid-0.1-1.rockspec create mode 100644 lua_uuid.c create mode 100644 test/lua_uuid_test.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8472ef4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +*.o +*.so diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b1ffa4b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Mashape, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b872e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +LUA ?= lua5.1 +LUA_PC ?= lua5.1 +LUA_CFLAGS = $(shell pkg-config $(LUA_PC) --cflags) + +CFLAGS ?= -O3 -Wall -Werror + +all: lua_uuid.so + +%.o: %.c + $(CC) -c $(CFLAGS) -fPIC $(LUA_CFLAGS) -o $@ $< + +lua_uuid.so: lua_uuid.o test/lua_uuid_test.lua + $(CC) -shared lua_uuid.o -o $@ + $(LUA) test/lua_uuid_test.lua + +clean: + rm -f lua_uuid.so lua_uuid.o *.rock diff --git a/README.md b/README.md new file mode 100644 index 0000000..cfa7c92 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# lua_uuid + +Lua library that generate UUIDs leveraging libuuid. + +## Usage + +To generate an unique UUID string: + +```lua +local lua_uuid = require "lua_uuid" +local uuid_str = lua_uuid.generate() + +print("The UUID is "..uuid_str) +``` \ No newline at end of file diff --git a/lua_uuid-0.1-1.rockspec b/lua_uuid-0.1-1.rockspec new file mode 100644 index 0000000..3d6eccb --- /dev/null +++ b/lua_uuid-0.1-1.rockspec @@ -0,0 +1,25 @@ +package = "lua_uuid" +version = "0.1-1" +source = { + url = "https://github.com/Mashape/lua-uuid/archive/0.1-1.tar.gz", + dir = "lua_uuid-0.1-1" +} +description = { + summary = "UUID generation", + detailed = [[ + This is an utility that leverages libuuid to generate UUID strings + ]], + homepage = "https://github.com/Mashape/lua-uuid", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + lua_uuid = { + sources = {"lua_uuid.c"}, + } + } +} \ No newline at end of file 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 +#include +#include + +#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 diff --git a/test/lua_uuid_test.lua b/test/lua_uuid_test.lua new file mode 100644 index 0000000..3cc44c2 --- /dev/null +++ b/test/lua_uuid_test.lua @@ -0,0 +1,8 @@ +local lua_uuid = require "lua_uuid" + +assert(lua_uuid.generate()) + +local first = lua_uuid.generate() +local second = lua_uuid.generate() + +assert(first ~= second) \ No newline at end of file -- cgit v1.2.3-70-g09d2