bspwm/luautils.c

68 lines
1.5 KiB
C
Raw Normal View History

2012-08-01 23:16:07 +02:00
#define _BSD_SOURCE
2012-08-01 12:25:18 +02:00
#include <string.h>
2012-07-30 14:54:20 +02:00
#include "utils.h"
#include "luautils.h"
2012-08-02 09:57:57 +02:00
int eval_expr(lua_State *L, char *expr)
2012-07-30 14:54:20 +02:00
{
2012-08-01 12:25:18 +02:00
char buf[BUFSIZ];
sprintf(buf, "return %s", expr);
return luaL_dostring(L, buf);
2012-07-30 17:23:25 +02:00
}
2012-08-02 20:41:25 +02:00
int has_table(lua_State *L, char *name)
2012-07-30 17:23:25 +02:00
{
2012-08-01 12:25:18 +02:00
int result = 0;
2012-08-02 09:57:57 +02:00
eval_expr(L, name);
2012-08-01 12:25:18 +02:00
result = lua_istable(L, -1);
2012-07-30 14:54:20 +02:00
lua_pop(L, 1);
2012-08-01 12:25:18 +02:00
return result;
2012-07-30 14:54:20 +02:00
}
2012-08-02 09:57:57 +02:00
char *string_expr(lua_State *L, char *expr, char* fallback)
2012-07-30 14:54:20 +02:00
{
2012-08-01 12:25:18 +02:00
char *result;
2012-08-02 09:57:57 +02:00
if (eval_expr(L, expr) == 0) {
2012-08-01 12:25:18 +02:00
if (lua_isstring(L, -1))
result = strdup(lua_tostring(L, -1));
else
result = strdup(fallback);
lua_pop(L, 1);
}
return result;
2012-07-30 14:54:20 +02:00
}
2012-08-02 09:57:57 +02:00
double double_expr(lua_State *L, char *expr, double fallback)
2012-07-30 14:54:20 +02:00
{
2012-08-01 12:25:18 +02:00
double result = fallback;
2012-08-02 09:57:57 +02:00
if (eval_expr(L, expr) == 0) {
2012-08-01 12:25:18 +02:00
if (lua_isnumber(L, -1))
result = (double) lua_tonumber(L, -1);
lua_pop(L, 1);
}
return result;
2012-07-30 14:54:20 +02:00
}
2012-08-02 09:57:57 +02:00
int int_expr(lua_State *L, char *expr, int fallback)
2012-07-30 14:54:20 +02:00
{
2012-08-01 12:25:18 +02:00
int result = fallback;
2012-08-02 09:57:57 +02:00
if (eval_expr(L, expr) == 0) {
2012-08-01 12:25:18 +02:00
if (lua_isnumber(L, -1))
result = (int) lua_tonumber(L, -1);
lua_pop(L, 1);
}
return result;
}
2012-08-02 20:41:25 +02:00
bool bool_expr(lua_State *L, char *expr, bool fallback)
2012-08-01 12:25:18 +02:00
{
bool result = fallback;
2012-08-02 09:57:57 +02:00
if (eval_expr(L, expr) == 0) {
2012-08-01 12:25:18 +02:00
if (lua_isboolean(L, -1))
result = (bool) lua_toboolean(L, -1);
lua_pop(L, 1);
}
return result;
2012-07-30 14:54:20 +02:00
}