ab2214fb90981f69c8e579ba1b6e2edd4e5d494b
[libquvi.git] / src / lua / chk.c
blobab2214fb90981f69c8e579ba1b6e2edd4e5d494b
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <lauxlib.h>
24 #include <glib.h>
26 #include "quvi.h"
27 /* -- */
28 #include "_quvi_s.h"
29 #include "_quvi_script_s.h"
30 /* -- */
31 #include "lua/chk.h"
32 #include "lua/def.h"
33 #include "misc/re.h"
36 * NOTE: The error messages produced in these functions are intended for
37 * developers. They would typically be seen when a new script is being
38 * developed.
40 * The messages should be clear, indicating the actual error, minimizing
41 * the time spent on locating the actual problem in the script.
44 gboolean l_chk_can_parse_url(lua_State *l, _quvi_script_t qs,
45 const gchar *k_can_parse_url,
46 const gchar *k_domains,
47 const gchar *script_func)
49 gboolean r = FALSE;
51 lua_pushnil(l);
52 while (lua_next(l, LI_KEY))
54 l_chk_assign_s(l, k_domains, qs->domains, TRUE);
55 l_chk_assign_b(l, k_can_parse_url, &r);
56 lua_pop(l, 1);
58 if (qs->domains->len ==0)
60 luaL_error(l, "%s: %s: the returned dictionary must contain "
61 "a string value `%s'",
62 qs->fpath->str, script_func, k_domains);
64 return (r);
68 * Return the value of the named (`w') string. The value is trimmed
69 * of any extra whitespace (e.g. leading, trailing).
71 * NOTE: g_free the returned value when done using it.
73 gboolean l_chk_s(lua_State *l, const gchar *w, gchar **v, gboolean trim_flag)
75 if (lua_isstring(l, LI_KEY) && lua_isstring(l, LI_VALUE))
77 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
79 const gchar *s = lua_tostring(l, LI_VALUE);
80 *v = (trim_flag == TRUE)
81 ? m_trim_ws(s)
82 : g_strdup(s);
83 return (TRUE);
86 return (FALSE);
89 gboolean l_chk_assign_s(lua_State *l, const gchar *k, GString *v,
90 gboolean trim_flag)
92 gchar *s = NULL;
93 if (l_chk_s(l, k, &s, trim_flag) == TRUE)
95 g_string_assign(v, s);
96 g_free(s);
97 return (TRUE);
99 return (FALSE);
102 gboolean l_chk_n(lua_State *l, const gchar *w, gdouble *v)
104 if (lua_isstring(l, LI_KEY) && lua_isnumber(l, LI_VALUE))
106 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
108 *v = lua_tonumber(l, LI_VALUE);
109 return (TRUE);
112 return (FALSE);
115 gboolean l_chk_assign_n(lua_State *l, const gchar *k, gdouble *v)
117 gdouble n = 0;
118 if (l_chk_n(l, k, &n) == TRUE)
120 *v = n;
121 return (TRUE);
123 return (FALSE);
126 gboolean l_chk_b(lua_State *l, const gchar *w, gboolean *v)
128 if (lua_isstring(l, LI_KEY) && lua_isboolean(l, LI_VALUE))
130 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
132 *v = lua_toboolean(l, LI_VALUE);
133 return (TRUE);
136 return (FALSE);
139 gboolean l_chk_assign_b(lua_State *l, const gchar *k, gboolean *v)
141 gboolean b = 0;
142 if (l_chk_b(l, k, &b) == TRUE)
144 *v = b;
145 return (TRUE);
147 return (FALSE);
150 /* vim: set ts=2 sw=2 tw=72 expandtab: */