Blame src/lua/chk.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2012,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
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
#include "_quvi_script_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "lua/chk.h"
Packit 3ff1e7
#include "lua/def.h"
Packit 3ff1e7
#include "misc/url.h"
Packit 3ff1e7
#include "misc/re.h"
Packit 3ff1e7
Packit 3ff1e7
/*
Packit 3ff1e7
 * NOTE: The error messages produced in these functions are intended for
Packit 3ff1e7
 * developers. They would typically be seen when a new script is being
Packit 3ff1e7
 * developed.
Packit 3ff1e7
 *
Packit 3ff1e7
 * The messages should be clear, indicating the actual error, minimizing
Packit 3ff1e7
 * the time spent on locating the actual problem in the script.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
gboolean l_chk_can_parse_url(lua_State *l, _quvi_script_t qs,
Packit 3ff1e7
                             const gchar *k_can_parse_url,
Packit 3ff1e7
                             const gchar *k_domains,
Packit 3ff1e7
                             const gchar *script_func)
Packit 3ff1e7
{
Packit 3ff1e7
  gboolean r = FALSE;
Packit 3ff1e7
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
  while (lua_next(l, LI_KEY))
Packit 3ff1e7
    {
Packit 3ff1e7
      l_chk_assign_s(l, k_domains, qs->domains, TRUE, FALSE);
Packit 3ff1e7
      l_chk_assign_b(l, k_can_parse_url, &r);
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
  if (qs->domains->len ==0)
Packit 3ff1e7
    {
Packit 3ff1e7
      luaL_error(l, "%s: %s: the returned dictionary must contain "
Packit 3ff1e7
                 "a string value `%s'",
Packit 3ff1e7
                 qs->fpath->str, script_func, k_domains);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/*
Packit 3ff1e7
 * Return the value of the named (`w') string. The value is trimmed
Packit 3ff1e7
 * of any extra whitespace (e.g. leading, trailing).
Packit 3ff1e7
 *
Packit 3ff1e7
 * NOTE: g_free the returned value when done using it.
Packit 3ff1e7
 */
Packit 3ff1e7
gboolean l_chk_s(lua_State *l, const gchar *w, gchar **v,
Packit 3ff1e7
                 gboolean trim_flag, gboolean escape_url)
Packit 3ff1e7
{
Packit 3ff1e7
  if (lua_isstring(l, LI_KEY) && lua_isstring(l, LI_VALUE))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
Packit 3ff1e7
        {
Packit 3ff1e7
          const gchar *s = lua_tostring(l, LI_VALUE);
Packit 3ff1e7
          *v = (trim_flag == TRUE)
Packit 3ff1e7
               ? m_trim_ws(s)
Packit 3ff1e7
               : g_strdup(s);
Packit 3ff1e7
          if (escape_url == TRUE)
Packit 3ff1e7
            {
Packit 3ff1e7
              gchar *e = m_url_escaped_form(*v);
Packit 3ff1e7
              g_free(*v);
Packit 3ff1e7
              *v = e;
Packit 3ff1e7
            }
Packit 3ff1e7
          return (TRUE);
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gboolean l_chk_assign_s(lua_State *l, const gchar *k, GString *v,
Packit 3ff1e7
                        gboolean trim_flag, gboolean escape_url)
Packit 3ff1e7
{
Packit 3ff1e7
  gchar *s = NULL;
Packit 3ff1e7
  if (l_chk_s(l, k, &s, trim_flag, escape_url) == TRUE)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_string_assign(v, s);
Packit 3ff1e7
      g_free(s);
Packit 3ff1e7
      return (TRUE);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gboolean l_chk_n(lua_State *l, const gchar *w, gdouble *v)
Packit 3ff1e7
{
Packit 3ff1e7
  if (lua_isstring(l, LI_KEY) && lua_isnumber(l, LI_VALUE))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
Packit 3ff1e7
        {
Packit 3ff1e7
          *v = lua_tonumber(l, LI_VALUE);
Packit 3ff1e7
          return (TRUE);
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gboolean l_chk_assign_n(lua_State *l, const gchar *k, gdouble *v)
Packit 3ff1e7
{
Packit 3ff1e7
  gdouble n = 0;
Packit 3ff1e7
  if (l_chk_n(l, k, &n) == TRUE)
Packit 3ff1e7
    {
Packit 3ff1e7
      *v = n;
Packit 3ff1e7
      return (TRUE);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gboolean l_chk_b(lua_State *l, const gchar *w, gboolean *v)
Packit 3ff1e7
{
Packit 3ff1e7
  if (lua_isstring(l, LI_KEY) && lua_isboolean(l, LI_VALUE))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
Packit 3ff1e7
        {
Packit 3ff1e7
          *v = lua_toboolean(l, LI_VALUE);
Packit 3ff1e7
          return (TRUE);
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gboolean l_chk_assign_b(lua_State *l, const gchar *k, gboolean *v)
Packit 3ff1e7
{
Packit 3ff1e7
  gboolean b = 0;
Packit 3ff1e7
  if (l_chk_b(l, k, &b) == TRUE)
Packit 3ff1e7
    {
Packit 3ff1e7
      *v = b;
Packit 3ff1e7
      return (TRUE);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */