diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2010-12-29 16:26:25 +0100 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2010-12-29 16:26:25 +0100 |
commit | d3148268857e01116d5d3c99ac0a43bc6a54b13c (patch) | |
tree | 6ae273025ef73942c0ac748e715a7f281a6af114 /src/misc.c | |
download | lspipat-d3148268857e01116d5d3c99ac0a43bc6a54b13c.tar.gz |
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 0000000..2bea4c8 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,89 @@ +/* + * LSPIPAT - LUA SPIPAT WRAPPER + * Copyright (C) 2010, Robin Haberkorn + * License: LGPL + * + * CORE: MISCELLANEOUS PRIMITIVES/CONSTRUCTORS + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "lspipat.h" + +LUA_SIG(l_primitive_arbno) +{ + int top = lua_gettop(L); + + VString str = VSTRING_INITIALIZER; + PATTERN_WRAPPER *new; + + luaL_argcheck(L, top == 1, top, L_NUMBER); + + if (!(new = lua_newuserdata(L, sizeof(PATTERN_WRAPPER)))) + L_ERROR(L_ALLOC); + memset(new, 0, sizeof(PATTERN_WRAPPER)); + + if (lua_isstring(L, 1)) { + str.ptr = lua_tolstring(L, 1, (size_t *)&str.len); + + new->pattern = str.len == 1 ? spipat_arbno_chr(*str.ptr) + : spipat_arbno_str(str); + } else { + PATTERN_WRAPPER *wrapper = luaL_checkudata(L, 1, PATTERN_MT); + luaL_argcheck(L, wrapper->pattern, 1, L_FREED); + + lua_insert(L, 1); /* move wrapper to bottom */ + new->type = PATTERN_ONESUBPAT; + new->u.onesubpat.pattern = luaL_ref(L, LUA_REGISTRYINDEX); + /* wrapper at top again */ + + new->pattern = spipat_arbno(wrapper->pattern); + } + + if (!new->pattern) + L_ERROR(L_ALLOC); + + luaL_getmetatable(L, PATTERN_MT); + lua_setmetatable(L, -2); + + return 1; + +} + +LUA_SIG(l_primitive_fence) +{ + int top = lua_gettop(L); + PATTERN_WRAPPER *new; + + luaL_argcheck(L, top < 2, top, L_NUMBER); + + if (!(new = lua_newuserdata(L, sizeof(PATTERN_WRAPPER)))) + L_ERROR(L_ALLOC); + memset(new, 0, sizeof(PATTERN_WRAPPER)); + + if (!top) { + new->pattern = spipat_fence_simple(); + } else { + PATTERN_WRAPPER *wrapper = luaL_checkudata(L, 1, PATTERN_MT); + luaL_argcheck(L, wrapper->pattern, 1, L_FREED); + + lua_insert(L, 1); /* move wrapper to bottom */ + new->type = PATTERN_ONESUBPAT; + new->u.onesubpat.pattern = luaL_ref(L, LUA_REGISTRYINDEX); + /* wrapper at top again */ + + new->pattern = spipat_fence_function(wrapper->pattern); + } + + if (!new->pattern) + L_ERROR(L_ALLOC); + + luaL_getmetatable(L, PATTERN_MT); + lua_setmetatable(L, -2); + + return 1; +} |