API: Return URLs in escaped (percent-encoded) form
[libquvi.git] / src / lua / chk.c
blob7cc27d448952c9719c2972de776415b9a4338def
1 /* libquvi
2 * Copyright (C) 2012,2013 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/url.h"
34 #include "misc/re.h"
37 * NOTE: The error messages produced in these functions are intended for
38 * developers. They would typically be seen when a new script is being
39 * developed.
41 * The messages should be clear, indicating the actual error, minimizing
42 * the time spent on locating the actual problem in the script.
45 gboolean l_chk_can_parse_url(lua_State *l, _quvi_script_t qs,
46 const gchar *k_can_parse_url,
47 const gchar *k_domains,
48 const gchar *script_func)
50 gboolean r = FALSE;
52 lua_pushnil(l);
53 while (lua_next(l, LI_KEY))
55 l_chk_assign_s(l, k_domains, qs->domains, TRUE, FALSE);
56 l_chk_assign_b(l, k_can_parse_url, &r);
57 lua_pop(l, 1);
59 if (qs->domains->len ==0)
61 luaL_error(l, "%s: %s: the returned dictionary must contain "
62 "a string value `%s'",
63 qs->fpath->str, script_func, k_domains);
65 return (r);
69 * Return the value of the named (`w') string. The value is trimmed
70 * of any extra whitespace (e.g. leading, trailing).
72 * NOTE: g_free the returned value when done using it.
74 gboolean l_chk_s(lua_State *l, const gchar *w, gchar **v,
75 gboolean trim_flag, gboolean escape_url)
77 if (lua_isstring(l, LI_KEY) && lua_isstring(l, LI_VALUE))
79 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
81 const gchar *s = lua_tostring(l, LI_VALUE);
82 *v = (trim_flag == TRUE)
83 ? m_trim_ws(s)
84 : g_strdup(s);
85 if (escape_url == TRUE)
87 gchar *e = m_url_escaped_form(*v);
88 g_free(*v);
89 *v = e;
91 return (TRUE);
94 return (FALSE);
97 gboolean l_chk_assign_s(lua_State *l, const gchar *k, GString *v,
98 gboolean trim_flag, gboolean escape_url)
100 gchar *s = NULL;
101 if (l_chk_s(l, k, &s, trim_flag, escape_url) == TRUE)
103 g_string_assign(v, s);
104 g_free(s);
105 return (TRUE);
107 return (FALSE);
110 gboolean l_chk_n(lua_State *l, const gchar *w, gdouble *v)
112 if (lua_isstring(l, LI_KEY) && lua_isnumber(l, LI_VALUE))
114 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
116 *v = lua_tonumber(l, LI_VALUE);
117 return (TRUE);
120 return (FALSE);
123 gboolean l_chk_assign_n(lua_State *l, const gchar *k, gdouble *v)
125 gdouble n = 0;
126 if (l_chk_n(l, k, &n) == TRUE)
128 *v = n;
129 return (TRUE);
131 return (FALSE);
134 gboolean l_chk_b(lua_State *l, const gchar *w, gboolean *v)
136 if (lua_isstring(l, LI_KEY) && lua_isboolean(l, LI_VALUE))
138 if (g_strcmp0(lua_tostring(l, LI_KEY), w) == 0)
140 *v = lua_toboolean(l, LI_VALUE);
141 return (TRUE);
144 return (FALSE);
147 gboolean l_chk_assign_b(lua_State *l, const gchar *k, gboolean *v)
149 gboolean b = 0;
150 if (l_chk_b(l, k, &b) == TRUE)
152 *v = b;
153 return (TRUE);
155 return (FALSE);
158 /* vim: set ts=2 sw=2 tw=72 expandtab: */