Next: , Previous: slua_args, Up: Argument parsing


A.1.2 slua_args_n

read in the arguments passed to a C function from Lua

Synopsis

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

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.
...
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 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.

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.

It differs from slua_args in that it will start with the Lua argument at the position specified by n, and will not complain about extra passed arguments.

As slua_args_n uses slua_args_va, see its write-up for more information.

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 <stddef.h>
     #include <stdarg.h>
     
     #include <luasup.h>
     
     extern SLua_ErrFunc errfunc;
     
     void foo ( void )
     {
       char *name;
       double energy, base_flux;
     
       slua_args_n( errfunc, "mono flux generator", 1,
                     "name",         SLua_STRING,     &name,
                     "energy",       SLua_NUMBER,     &energy,
                     "photon flux",  SLua_NUMBER,     &base_flux,
                     NULL
                     );
     
       free( name );
     
       slua_args_n( errfunc, "mono flux generator", 4,
                     "name",         SLua_STRING,     &name,
                     "energy",       SLua_NUMBER,     &energy,
                     "photon flux",  SLua_NUMBER,     &base_flux,
                     NULL
                     );
     
       free( name );
     }