l_chk_can_parse_url: Revise error message
[libquvi.git] / src / lua / chk.c
blobfc0db948a2a3c443cae9d51c906c91934aac6dcc
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #include "config.h"
22 #include <lauxlib.h>
23 #include <glib.h>
25 #include "quvi.h"
26 /* -- */
27 #include "_quvi_s.h"
28 #include "_quvi_script_s.h"
29 /* -- */
30 #include "lua/chk.h"
31 #include "lua/def.h"
32 #include "misc/re.h"
35 * NOTE: The error messages produced in these functions are intended for
36 * developers. They would typically be seen when a new script is being
37 * developed.
39 * The messages should be clear, indicating the actual error, minimizing
40 * the time spent on locating the actual problem in the script.
43 gboolean l_chk_can_parse_url(lua_State *l, _quvi_script_t qs,
44 const gchar *k_can_parse_url,
45 const gchar *k_domains,
46 const gchar *script_func)
48 gboolean r = FALSE;
50 lua_pushnil(l);
51 while (lua_next(l, LI_KEY))
53 l_chk_assign_s(l, k_domains, qs->domains);
54 l_chk_assign_b(l, k_can_parse_url, &r);
55 lua_pop(l, 1);
57 if (qs->domains->len ==0)
59 static const gchar *_E =
60 "%s: %s: the dictionary `%s' must contain a string "
61 "value for `%s'";
63 luaL_error(l, _E, qs->fpath->str, script_func,
64 k_can_parse_url, k_domains);
66 return (r);
70 * Return the value of the named (`w') string. The value is trimmed
71 * of any extra whitespace (e.g. leading, trailing).
73 * NOTE: g_free the returned value when done using it.
75 gboolean l_chk_s(lua_State *l, const gchar *w, gchar **v)
77 if (lua_isstring(l, LI_KEY) && lua_isstring(l, LI_VALUE))
79 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
81 *v = m_trim_ws(lua_tostring(l, LI_VALUE));
82 return (TRUE);
85 return (FALSE);
88 gboolean l_chk_assign_s(lua_State *l, const gchar *k, GString *v)
90 gchar *s = NULL;
91 if (l_chk_s(l, k, &s) == TRUE)
93 g_string_assign(v, s);
94 g_free(s);
95 s = NULL;
96 return (TRUE);
98 return (FALSE);
101 gboolean l_chk_n(lua_State *l, const gchar *w, gdouble *v)
103 if (lua_isstring(l, LI_KEY) && lua_isnumber(l, LI_VALUE))
105 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
107 *v = lua_tonumber(l, LI_VALUE);
108 return (TRUE);
111 return (FALSE);
114 gboolean l_chk_assign_n(lua_State *l, const gchar *k, gdouble *v)
116 gdouble n = 0;
117 if (l_chk_n(l, k, &n) == TRUE)
119 *v = n;
120 return (TRUE);
122 return (FALSE);
125 gboolean l_chk_b(lua_State *l, const gchar *w, gboolean *v)
127 if (lua_isstring(l, LI_KEY) && lua_isboolean(l, LI_VALUE))
129 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
131 *v = lua_toboolean(l, LI_VALUE);
132 return (TRUE);
135 return (FALSE);
138 gboolean l_chk_assign_b(lua_State *l, const gchar *k, gboolean *v)
140 gboolean b = 0;
141 if (l_chk_b(l, k, &b) == TRUE)
143 *v = b;
144 return (TRUE);
146 return (FALSE);
149 /* vim: set ts=2 sw=2 tw=72 expandtab: */