Next: , Previous: Argument parsing, Up: Argument parsing


A.1.1 slua_args

read in the arguments passed to a C function from Lua

Synopsis

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

Parameters

SLua_ErrFunc errfunc
the error function
const char *func
the name of the Lua called function. Used for error messages
...
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.

As slua_args 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. If the number of arguments passed by Lua is different than in the passed list, an error message is generated.

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( errfunc, "mono flux generator",
                     "name",         SLua_STRING,     &name,
                     "energy",       SLua_NUMBER,     &energy,
                     "photon flux",  SLua_NUMBER,     &base_flux,
                     NULL
                     );
     
       free( name );
     
     }