util.h
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 27 Mar 2009 09:43:25 +0200
changeset 15 1a5dd51722f5
parent 6 90073cbb535d
child 17 ab4470465a0c
permissions -rw-r--r--
Fix to lua_pushconststring


#ifndef MISC_LUA_UTIL_H
#define MISC_LUA_UTIL_H

#include <lua.h>

// You can set this if anther type is required
#ifndef enum_value_t
#define enum_value_t int
#endif

#define lua_pushconststring(L, STRING) { lua_pushlstring (L, STRING, sizeof(STRING)-1); }

// Array of string2eunm_t's must be ended with pair { NULL, fallback_value }
typedef struct {
	const char   *string;
	enum_value_t  value;
} string2enum_t;

// If not found, fallback_value is returned
enum_value_t  string2enum (const char *string, const string2enum_t *set);
// If not found, NULL is returned
const char   *enum2string (enum_value_t value, const string2enum_t *set);

// returns result of string2enum on specified stack index, can handle plain numbers (no s2e called in this case)
enum_value_t luaL_checkenum (lua_State *L, int index, const string2enum_t *set);
// pushes onto a stack result of enum2string on a given value, if not recognized, pushes value as number
void         luaL_pushenum (lua_State *L, enum_value_t value, const string2enum_t *set);

// as luaL_checknum, but additionally handles tables as a multiple flags set.
// table can contain both array and hash key-value pairs
// hash entries are ORed with string2enum(key) if value resolves to lua true value
// array entries are always ORed with string2enum(value)
// eg { flag1, flag2, 16, flag3 = true, flag4 = { }, flag5 = nil } will set all flags except flag5 (and 16 too will be ORed)
enum_value_t luaL_checkenum_multi (lua_State *L, int index, const string2enum_t *set);
// pushes to stack table with all matched values from a set in a hash format
// eg { flag1 = true, flag2 = true, flag6 = true, 16 } - that's it, if some bits will not match, they will be passed as a first array element.
void         luaL_pushenum_multi (lua_State *L, enum_value_t value, const string2enum_t *set);

void *luaL_malloc (lua_State *L, size_t size);
void *luaL_realloc (lua_State *L, void *ptr, size_t osize, size_t nsize);
void  luaL_free (lua_State *L, void *ptr);

#endif