Next: , Previous: slua_args_n, Up: Argument parsing


A.1.3 slua_args_va

read in the arguments passed to a C function from Lua

Synopsis

     #include <luasup/luasup.h>
     
     
     
int slua_args_va( SLua_ErrFunc errfunc, const char *func, int n, va_list ap );

Parameters

SLua_ErrFunc errfunc
the error function
const char *func
the name of the Lua called function. Used for error messages
int n
the Lua argument position at which to start. Unary based.
va_list ap
the list of arguments

Description

This routine will read the arguments passed by Lua to a C function when the function is called from Lua. It expects a va_list of triples, each of which contains the argument's name (for error reporting purposes), the argument's type (one of SLua_STRING, SLua_NUMBER, or SLua_TABLE) and a pointer to storage for the argument. The list should be terminated by a NULL value. Support is provided for optional arguements. These must be at the end of the arguement list, and should have their type or'd with SLua_OPTIONAL (e.g. SLua_TABLE | SLua_OPTIONAL). To determine if an optional argument was present, use slua_args_va's return value.

Numeric values are stored as double. String values are returned as a pointer to a duplicate string. The calling routine is responsible for deallocating this memory.

Arguments are returned beginning with the Lua argument at the position specified by n.

Returns

It returns the number of arguments found, or `-1' if an error ocurred.

Errors

Errors are returned via the passed SLua_ErrFunc function.

Author

Diab Jerius

Example

     /* --8<--8<--8<--8<--
      *
      * Copyright (C) 2006 Smithsonian Astrophysical Observatory
      *
      * This file is part of luasup
      *
      * luasup 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 2
      * of the License, or (at your option) any later version.
      *
      * luasup 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 this program; if not, write to the
      *       Free Software Foundation, Inc.
      *       51 Franklin Street, Fifth Floor
      *       Boston, MA  02110-1301, USA
      *
      * -->8-->8-->8-->8-- */
     
     #include <stdarg.h>
     
     #include <luasup.h>
     
     int foo ( SLua_ErrFunc errfunc, char *func, ... )
     {
       va_list	ap;
       int		n;
     
       va_start( ap, func );
     
       n = slua_args_va( errfunc, func, 1, ap );
     
       va_end( ap );
     
       return n;
     }