lua-apk.c 1.37 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4 5 6 7 8 9 10
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "apk_defines.h"
#include "apk_version.h"
#include "apk_blob.h"

#define LIBNAME "apk"

Natanael Copa's avatar
Natanael Copa committed
11 12 13 14 15 16 17
static apk_blob_t check_blob(lua_State *L, int index)
{
	apk_blob_t blob;
	blob.ptr = (char *)luaL_checklstring(L, index, (size_t *)&blob.len);
	return blob;
}

Natanael Copa's avatar
Natanael Copa committed
18 19 20 21
/* version_validate(verstr) */
/* returns boolean */
static int Pversion_validate(lua_State *L)
{
Natanael Copa's avatar
Natanael Copa committed
22
	apk_blob_t ver = check_blob(L, 1);
Natanael Copa's avatar
Natanael Copa committed
23 24 25 26 27 28 29 30 31 32
	lua_pushboolean(L, apk_version_validate(ver));
	return 1;
}

/* version_compare(verstr1, verstr2)
   returns either '<', '=' or '>'
*/
static int Pversion_compare(lua_State *L)
{
	apk_blob_t a, b;
Natanael Copa's avatar
Natanael Copa committed
33 34
	a = check_blob(L, 1);
	b = check_blob(L, 2);
Natanael Copa's avatar
Natanael Copa committed
35 36 37 38 39 40 41 42 43 44
	lua_pushstring(L, apk_version_op_string(apk_version_compare_blob(a, b)));
	return 1;
}

/* version_is_less(verstr1, verstr2)
   returns either '<', '=' or '>'
*/
static int Pversion_is_less(lua_State *L)
{
	apk_blob_t a, b;
Natanael Copa's avatar
Natanael Copa committed
45 46
	a = check_blob(L, 1);
	b = check_blob(L, 2);
Natanael Copa's avatar
Natanael Copa committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	lua_pushboolean(L, apk_version_compare_blob(a, b) == APK_VERSION_LESS);
	return 1;
}


static const luaL_reg R[] = {
	{"version_validate",	Pversion_validate},
	{"version_compare",	Pversion_compare},
	{"version_is_less",	Pversion_is_less},
	{NULL,		NULL}
};

LUALIB_API int luaopen_apk(lua_State *L)
{
	luaL_register(L, LIBNAME, R);
	lua_pushliteral(L, "version");
	lua_pushliteral(L, APK_VERSION);
	lua_settable(L, -3);
	return 1;
}