aboutsummaryrefslogtreecommitdiff
path: root/lua_uuid.c
blob: 60f779cbff06ba900063a520074163409f97b290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/***
Utility for generating UUIDs by wrapping libuuid's generate().

@license MIT
@module lua_uuid
*/
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <uuid/uuid.h>

#define UUID_LEN 16

/// Generate a UUID. As a shortcut, you can also call the module directly to generate a uuid.
// @return uuid_str
// @function generate()
static int generate(lua_State *L) {
    uuid_t uuid;
    char uuid_str[37];

    uuid_generate(uuid);
    uuid_unparse_lower(uuid, uuid_str);

    lua_pushstring(L, uuid_str);
    return 1;
}

/// Generate a binary UUID
// @return uuid
// @function generate_binary()
static int generate_binary(lua_State *L) {
    uuid_t uuid;
    uuid_generate(uuid);

    lua_pushlstring(L, (char *)&uuid, UUID_LEN);
    return 1;
}

/// Unparse UUID (create a hex string representation from a binary string representation)
// @tparam string binary_uuid
// @treturn string uuid_str
// @error if the binary_uuid could not be unparsed
// @function unparse
static int unparse(lua_State *L) {
    const char *binary_uuid;
    size_t binary_uuid_len;
    uuid_t uuid;
    char uuid_str[37];

    binary_uuid = luaL_checklstring(L, 1, &binary_uuid_len);

    if (binary_uuid_len != UUID_LEN) {
        lua_pushnil(L);
        lua_pushfstring(L, "could not unparse uuid: string length was != %d", UUID_LEN);
        return 2;
    }

    memcpy(&uuid[0], binary_uuid, UUID_LEN);

    uuid_unparse_lower(uuid, uuid_str);

    lua_pushstring(L, uuid_str);
    return 1;
}

/// Parse a hexadecimal UUID into a binary representation
// @tparam string uuid_str
// @treturn string uuid
// @error if the uuid_str could not be parsed
// @function parse
static int parse(lua_State *L) {
    const char *uuid_str;
    uuid_t uuid;

    uuid_str = luaL_checkstring(L, 1);

    if (uuid_parse(uuid_str, uuid) != 0) {
        lua_pushnil(L);
        lua_pushstring(L, "could not parse uuid");
        return 2;
    }

    lua_pushlstring(L, (char *)uuid, UUID_LEN);
    return 1;
}

static int mt_call(lua_State *L) {
    lua_pop(L, 1);
    return generate(L);
}

static const struct luaL_Reg mylib [] = {
    {"generate", generate},
    {"generate_binary", generate_binary},
    {"unparse", unparse},
    {"parse", parse},
    {NULL, NULL}
};

int luaopen_lua_uuid(lua_State *L) {
    // Set up library table
    lua_newtable(L);
    const luaL_Reg *fundef = mylib;
    for(fundef = mylib; fundef->name != NULL; fundef++) {
        lua_pushcfunction(L, fundef->func);
        lua_setfield(L, -2, fundef->name);
    }

    // Set up metatable
    lua_newtable(L);
    lua_pushcfunction(L, mt_call);
    lua_setfield(L, -2, "__call");

    lua_setmetatable(L, -2);
    return 1;
}