/* Named variable manager header code V0.31 25/8/04
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _NVAR_H
#define _NVAR_H

#include <stdarg.h>
#include "fbll.h"

typedef fbll nvars; /* nvars are a type of FBLL */

typedef struct {
	void *(*add)(nvars *n,va_list l); /* Add a new item to nvars 'n'. l is the list of arguments given to nvar_add. Must return a pointer to the variables data structure (Which will then be stored by the nvar code along in the variable definition) */
	void (*remove)(nvars *n,void *v); /* Remove an item from nvars 'n', called when the list is deleted. The data pointer 'v' is expected to be closed/freed as needed */
	void *(*read)(nvars *n,void **v); /* Return the data in some user defined way. Can be 0, to indicate a write-only variable */
	int (*set)(nvars *n,void **v,va_list l); /* Update 'v' to have value 'l', returning !0 on failure. Can be 0, to indicate a read-only variable */
} nvar_type;

extern nvars *nvar_new(); /* Create a new, empty, variable list */
extern void nvar_delete(nvars *n); /* Delete list 'n', and all its contents */
extern int nvar_add(nvars *n,char *name,nvar_type *type,...); /* Add a new item to list 'n' of name 'name' with type 'type', returning the item number or -1 on failure. The optional ... arguments are the initial value of the variable (To be passed to the type's add() function) */
extern int nvar_find(nvars *n,char *name); /* Find a variable of name 'name' in 'n', returning its index or -1 on failure. Search is case sensitive. */
extern void *nvar_read(nvars *n,int i); /* Return the value of item 'i' from 'n', or 0 for failure */
extern int nvar_set(nvars *n,int i,...); /* Set item 'i' of 'n' to value '...', returning !0 on failure */
extern int nvar_size(nvars *n); /* Returns the number of items in 'n' */
extern char *nvar_getname(nvars *n,int i); /* Return the name of item 'i' of 'n', or 0 for failure */
extern nvar_type *nvar_gettype(nvars *n,int i); /* Return the type of item 'i' of 'n', or 0 for failure */
extern void nvar_sort(nvars *n); /* Sort the list n into alphabetical order, such that item 0 is first */
extern int nvar_bfind(nvars *n,char *name); /* Do a binary search through 'n' for variable 'name'. Must have been sorted by nvar_sort first. */
extern void nvar_remove(nvars *n,int i); /* Remove item i from the variable list */

#endif
