18static int lvar_cmp(
const void *p1,
const void *p2 );
24static int lvar_cmp(
const void *p1,
const void *p2 )
26 const lvar *mv1, *mv2;
27 mv1 = (
const lvar*) p1;
28 mv2 = (
const lvar*) p2;
41 const lvar mv = {.name=(
char*)str};
61 lua_pushnumber(L,v->d.num);
64 lua_pushboolean(L,v->d.b);
67 lua_pushstring(L,v->d.str);
95 else if (lua_type(L,idx) == LUA_TNUMBER) {
97 var.
d.
num = (double) lua_tonumber(L,idx);
99 else if (lua_isboolean(L,idx)) {
100 var.
type = LVAR_BOOL;
101 var.
d.
b = lua_toboolean(L,idx);
103 else if (lua_type(L,idx) == LUA_TSTRING) {
105 var.
d.
str = strdup( lua_tostring(L,idx) );
109 DEBUG(
"Invalid parameter for %s.", __func__ );
110 luaL_error( L,
"Invalid parameter for %s.", __func__ );
115 var.
name = strdup(name);
204 const lvar *v = &arr[i];
205 xmlw_startElem(writer,
"var");
207 xmlw_attr(writer,
"name",
"%s",v->name);
211 xmlw_attr(writer,
"type",
"nil");
214 xmlw_attr(writer,
"type",
"num");
215 xmlw_str(writer,
"%f",v->d.num);
218 xmlw_attr(writer,
"type",
"bool");
219 xmlw_str(writer,
"%d",v->d.b);
222 xmlw_attr(writer,
"type",
"str");
223 xmlw_str(writer,
"%s",v->d.str);
226 xmlw_attr(writer,
"type",
"time");
227 xmlw_str(writer,
"%"TIME_PRI,v->d.time);
230 xmlw_endElem(writer);
245 xmlNodePtr node = parent->xmlChildrenNode;
248 if (!xml_isNode(node,
"var")) {
249 WARN(_(
"Lua Var stack has unknown node '%s'!"), xml_get(node));
254 xmlr_attr_strd(node,
"name",var.
name);
255 xmlr_attr_strd(node,
"type",str);
256 if (strcmp(str,
"nil")==0)
258 else if (strcmp(str,
"num")==0) {
260 var.
d.
num = xml_getFloat(node);
262 else if (strcmp(str,
"bool")==0) {
263 var.
type = LVAR_BOOL;
264 var.
d.
b = xml_getInt(node);
266 else if (strcmp(str,
"str")==0) {
268 var.
d.
str = xml_getStrd(node);
270 else if (strcmp(str,
"time")==0) {
271 var.
type = LVAR_TIME;
272 var.
d.
time = xml_getLong(node);
275 WARN(_(
"Unknown var type '%s'"), str);
282 }
while (xml_nextNode(node));
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
int lvar_addArray(lvar **arr, const lvar *new_var, int sort)
Adds a var to a var array.
void lvar_rmArray(lvar **arr, lvar *rm_var)
Removes a var from a var array.
static int lvar_cmp(const void *p1, const void *p2)
Compares two lua variable names. For use with qsort/bsearch.
lvar * lvar_get(const lvar *arr, const char *str)
Gets a lua var by name.
void lvar_freeArray(lvar *arr)
Frees a variable array.
lvar lvar_tovar(lua_State *L, const char *name, int idx)
Gets a lua variable from an index from a lua state.
int lvar_save(const lvar *arr, xmlTextWriterPtr writer)
Saves the mission variables.
lvar * lvar_load(xmlNodePtr parent)
Loads the vars from XML file.
int lvar_push(lua_State *L, const lvar *v)
Pushes a lua var to a lua state.
static void lvar_free(lvar *var)
Frees a lua variable.
ntime_t * lua_pushtime(lua_State *L, ntime_t time)
Pushes a time on the stack.
int lua_istime(lua_State *L, int ind)
Checks to see if ind is a time.
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
Contains a mission variable.