Blame src/lua/quvi/opts.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2013  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
#include <curl/curl.h>
Packit 3ff1e7
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "lua/quvi/opts.h"
Packit 3ff1e7
#include "lua/def.h"
Packit 3ff1e7
#include "misc/slst.h"
Packit 3ff1e7
Packit 3ff1e7
static gpointer _opt_new(const QuviObjectOption qoo, const gchar *vs,
Packit 3ff1e7
                         const gdouble vn)
Packit 3ff1e7
{
Packit 3ff1e7
  l_quvi_object_opt_t o = g_new0(struct l_quvi_object_opt_s, 1);
Packit 3ff1e7
  o->value.str = g_strdup(vs);
Packit 3ff1e7
  o->value.n = vn;
Packit 3ff1e7
  o->id = qoo;
Packit 3ff1e7
  return (o);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return a single-linked list of options passed with quvi.* function. */
Packit 3ff1e7
GSList *l_quvi_object_opts_new(lua_State *l, gint index)
Packit 3ff1e7
{
Packit 3ff1e7
  l_quvi_object_opt_t o;
Packit 3ff1e7
  GSList *r;
Packit 3ff1e7
  gdouble k;
Packit 3ff1e7
  gint t;
Packit 3ff1e7
Packit 3ff1e7
  r = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (!lua_istable(l, index))
Packit 3ff1e7
    return (NULL);
Packit 3ff1e7
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
  while (lua_next(l, LI_KEY))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (lua_isnumber(l, LI_KEY))
Packit 3ff1e7
        {
Packit 3ff1e7
          k = lua_tonumber(l, LI_KEY);
Packit 3ff1e7
          t = lua_type(l, LI_VALUE);
Packit 3ff1e7
          o = NULL;
Packit 3ff1e7
Packit 3ff1e7
          switch (t)
Packit 3ff1e7
            {
Packit 3ff1e7
            case LUA_TSTRING:
Packit 3ff1e7
              o = _opt_new(k, lua_tostring(l, LI_VALUE), 0);
Packit 3ff1e7
              break;
Packit 3ff1e7
            case LUA_TNUMBER:
Packit 3ff1e7
              o = _opt_new(k, NULL, lua_tonumber(l, LI_VALUE));
Packit 3ff1e7
              break;
Packit 3ff1e7
            case LUA_TBOOLEAN:
Packit 3ff1e7
              o = _opt_new(k, NULL, (gdouble)
Packit 3ff1e7
                           (lua_toboolean(l, LI_VALUE) ? TRUE:FALSE) );
Packit 3ff1e7
              break;
Packit 3ff1e7
            default:
Packit 3ff1e7
              g_warning("[%s] ignored: unsupported lua type=0x%x",
Packit 3ff1e7
                        __func__, t);
Packit 3ff1e7
              break;
Packit 3ff1e7
            }
Packit 3ff1e7
Packit 3ff1e7
          if (o != NULL)
Packit 3ff1e7
            r = g_slist_prepend(r, o);
Packit 3ff1e7
        }
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (g_slist_reverse(r));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void _opt_free(gpointer p, gpointer userdata)
Packit 3ff1e7
{
Packit 3ff1e7
  l_quvi_object_opt_t o = (l_quvi_object_opt_t) p;
Packit 3ff1e7
Packit 3ff1e7
  if (p == NULL)
Packit 3ff1e7
    return;
Packit 3ff1e7
Packit 3ff1e7
  g_free(o->value.str);
Packit 3ff1e7
  o->value.str = NULL;
Packit 3ff1e7
Packit 3ff1e7
  g_free(o);
Packit 3ff1e7
  o = NULL;
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
void l_quvi_object_opts_free(GSList *p)
Packit 3ff1e7
{
Packit 3ff1e7
  m_slist_free_full(p, _opt_free);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
#ifdef _UNUSED
Packit 3ff1e7
void l_quvi_object_opts_curl(GSList *opts, _quvi_t q)
Packit 3ff1e7
{
Packit 3ff1e7
  l_quvi_object_opt_t o;
Packit 3ff1e7
  GSList *p;
Packit 3ff1e7
Packit 3ff1e7
  if (opts == NULL)
Packit 3ff1e7
    return;
Packit 3ff1e7
Packit 3ff1e7
  if (l_quvi_object_opts_is_set(q->handle.lua, opts,
Packit 3ff1e7
                                QUVI_OBJECT_OPTION_HTTP_USER_AGENT, &p,
Packit 3ff1e7
                                NULL, FALSE)
Packit 3ff1e7
      == TRUE)
Packit 3ff1e7
    {
Packit 3ff1e7
      o = (l_quvi_object_opt_t) p->data;
Packit 3ff1e7
      curl_easy_setopt(q->handle.curl, CURLOPT_USERAGENT, o->value.str);
Packit 3ff1e7
    }
Packit 3ff1e7
}
Packit 3ff1e7
#endif /* _UNUSED */
Packit 3ff1e7
Packit 3ff1e7
gboolean l_quvi_object_opts_is_set(lua_State *l, GSList *opts,
Packit 3ff1e7
                                   const QuviObjectOption qoo, GSList **dst,
Packit 3ff1e7
                                   const gchar *w,
Packit 3ff1e7
                                   const gboolean croak_if_error)
Packit 3ff1e7
Packit 3ff1e7
{
Packit 3ff1e7
  *dst = opts;
Packit 3ff1e7
  while (*dst != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      l_quvi_object_opt_t o = (l_quvi_object_opt_t) (*dst)->data;
Packit 3ff1e7
      if (o->id == qoo)
Packit 3ff1e7
        return (TRUE);
Packit 3ff1e7
      *dst = g_slist_next(*dst);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  if (croak_if_error == TRUE && w != NULL)
Packit 3ff1e7
    luaL_error(l, "%s is required", w);
Packit 3ff1e7
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
void l_quvi_object_opts_chk_given(lua_State *l, GSList *opts,
Packit 3ff1e7
                                  const gchar *w)
Packit 3ff1e7
{
Packit 3ff1e7
  if (opts == NULL)
Packit 3ff1e7
    luaL_error(l, "expects a table of %s options passed as an arg", w);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gboolean l_quvi_object_opts_croak_if_error(lua_State *l, GSList *opts)
Packit 3ff1e7
{
Packit 3ff1e7
  GSList *p;
Packit 3ff1e7
Packit 3ff1e7
  if (opts == NULL)
Packit 3ff1e7
    return (TRUE);
Packit 3ff1e7
Packit 3ff1e7
  if (l_quvi_object_opts_is_set(l, opts, QUVI_OBJECT_OPTION_CROAK_IF_ERROR,
Packit 3ff1e7
                                &p, NULL, FALSE)
Packit 3ff1e7
      == TRUE)
Packit 3ff1e7
    {
Packit 3ff1e7
      l_quvi_object_opt_t o = (l_quvi_object_opt_t) p->data;
Packit 3ff1e7
      return ((o->value.n) != 0 ? TRUE:FALSE);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (TRUE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */