Blame src/lua/getfield.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2012  Toni Gundogdu <legatvs@gmail.com>
Packit 3ff1e7
 *
Packit 3ff1e7
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is free software: you can redistribute it and/or
Packit 3ff1e7
 * modify it under the terms of the GNU Affero General Public
Packit 3ff1e7
 * License as published by the Free Software Foundation, either
Packit 3ff1e7
 * version 3 of the License, or (at your option) any later version.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is distributed in the hope that it will be useful,
Packit 3ff1e7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff1e7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3ff1e7
 * GNU Affero General Public License for more details.
Packit 3ff1e7
 *
Packit 3ff1e7
 * You should have received a copy of the GNU Affero General
Packit 3ff1e7
 * Public License along with this library.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <lauxlib.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "lua/getfield.h"
Packit 3ff1e7
Packit 3ff1e7
gpointer l_get_reg_userdata(lua_State *l, const gchar *k)
Packit 3ff1e7
{
Packit 3ff1e7
  lua_pushstring(l, k);
Packit 3ff1e7
  lua_gettable(l, LUA_REGISTRYINDEX);
Packit 3ff1e7
Packit 3ff1e7
  if (!lua_isuserdata(l, -1))
Packit 3ff1e7
    luaL_error(l, "expected to find the key `%s' in LUA_REGISTRYINDEX", k);
Packit 3ff1e7
Packit 3ff1e7
  return ((gpointer) lua_touserdata(l, -1));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
#ifdef _1 /* Unused */
Packit 3ff1e7
Packit 3ff1e7
static const gchar *_E =
Packit 3ff1e7
  "%s: %s: expected to find the key `%s' in the returned table";
Packit 3ff1e7
Packit 3ff1e7
#define _pop(ltype, ctype, dval) \
Packit 3ff1e7
  do { \
Packit 3ff1e7
    ctype r = dval; \
Packit 3ff1e7
    lua_pushstring(l ,k); \
Packit 3ff1e7
    lua_gettable(l, -2); \
Packit 3ff1e7
    if (!lua_is##ltype(l, -1)) \
Packit 3ff1e7
      luaL_error(l, _E, script_path, script_func, k); \
Packit 3ff1e7
    r = lua_to##ltype(l, -1); \
Packit 3ff1e7
    lua_pop(l, 1); \
Packit 3ff1e7
    return (r); \
Packit 3ff1e7
  } while (0)
Packit 3ff1e7
Packit 3ff1e7
const gchar *l_getfield_s(lua_State *l, const gchar *k,
Packit 3ff1e7
                          const gchar *script_path,
Packit 3ff1e7
                          const gchar *script_func)
Packit 3ff1e7
{
Packit 3ff1e7
  _pop(string, const gchar*, NULL);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
const gdouble l_getfield_n(lua_State *l, const gchar *k,
Packit 3ff1e7
                           const gchar *script_path,
Packit 3ff1e7
                           const gchar *script_func)
Packit 3ff1e7
{
Packit 3ff1e7
  _pop(number, gdouble, 0);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
const gboolean l_getfield_b(lua_State *l, const gchar *k,
Packit 3ff1e7
                            const gchar *script_path,
Packit 3ff1e7
                            const gchar *script_func)
Packit 3ff1e7
{
Packit 3ff1e7
  _pop(boolean, gboolean, FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
QuviError l_getfield_table_iter_s(lua_State *l,
Packit 3ff1e7
                                  const gpointer dst,
Packit 3ff1e7
                                  const gchar *k,
Packit 3ff1e7
                                  const gchar *script_path,
Packit 3ff1e7
                                  const gchar *script_func,
Packit 3ff1e7
                                  l_callback_getfield_table_iter_s cb)
Packit 3ff1e7
{
Packit 3ff1e7
  QuviError rc = QUVI_OK;
Packit 3ff1e7
Packit 3ff1e7
  lua_pushstring(l, k);
Packit 3ff1e7
  lua_gettable(l, -2);
Packit 3ff1e7
Packit 3ff1e7
  if (!lua_istable(l, -1))
Packit 3ff1e7
    {
Packit 3ff1e7
      luaL_error(l, "%s: %s: expected to find the table `%s'",
Packit 3ff1e7
                 script_path, script_func, k);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
Packit 3ff1e7
  while (lua_next(l, -2) && rc == QUVI_OK)
Packit 3ff1e7
    {
Packit 3ff1e7
      rc = (cb)(dst, lua_tostring(l, -1));
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  lua_pop(l, 1);
Packit 3ff1e7
Packit 3ff1e7
  return (rc);
Packit 3ff1e7
}
Packit 3ff1e7
#endif /* _1, unused */
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */