Use word "library" in the source file headers
[libquvi.git] / src / lua / quvi / opts.c
blob2daf9e8bc041724d17de04d811cc4bede16f57ee
1 /* libquvi
2 * Copyright (C) 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>
25 #include <curl/curl.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 /* -- */
31 #include "lua/quvi/opts.h"
32 #include "lua/def.h"
33 #include "misc/slst.h"
35 static gpointer _opt_new(const QuviObjectOption qoo, const gchar *vs,
36 const gdouble vn)
38 l_quvi_object_opt_t o = g_new0(struct l_quvi_object_opt_s, 1);
39 o->value.str = g_strdup(vs);
40 o->value.n = vn;
41 o->id = qoo;
42 return (o);
45 /* Return a single-linked list of options passed with quvi.* function. */
46 GSList *l_quvi_object_opts_new(lua_State *l, gint index)
48 l_quvi_object_opt_t o;
49 GSList *r;
50 gdouble k;
51 gint t;
53 r = NULL;
55 if (!lua_istable(l, index))
56 return (NULL);
58 lua_pushnil(l);
59 while (lua_next(l, LI_KEY))
61 if (lua_isnumber(l, LI_KEY))
63 k = lua_tonumber(l, LI_KEY);
64 t = lua_type(l, LI_VALUE);
65 o = NULL;
67 switch (t)
69 case LUA_TSTRING:
70 o = _opt_new(k, lua_tostring(l, LI_VALUE), 0);
71 break;
72 case LUA_TNUMBER:
73 o = _opt_new(k, NULL, lua_tonumber(l, LI_VALUE));
74 break;
75 case LUA_TBOOLEAN:
76 o = _opt_new(k, NULL, (gdouble)
77 (lua_toboolean(l, LI_VALUE) ? TRUE:FALSE) );
78 break;
79 default:
80 g_warning("[%s] ignored: unsupported lua type=0x%x",
81 __func__, t);
82 break;
85 if (o != NULL)
86 r = g_slist_prepend(r, o);
88 lua_pop(l, 1);
90 return (g_slist_reverse(r));
93 static void _opt_free(gpointer p, gpointer userdata)
95 l_quvi_object_opt_t o = (l_quvi_object_opt_t) p;
97 if (p == NULL)
98 return;
100 g_free(o->value.str);
101 o->value.str = NULL;
103 g_free(o);
104 o = NULL;
107 void l_quvi_object_opts_free(GSList *p)
109 m_slist_free_full(p, _opt_free);
112 #ifdef _UNUSED
113 void l_quvi_object_opts_curl(GSList *opts, _quvi_t q)
115 l_quvi_object_opt_t o;
116 GSList *p;
118 if (opts == NULL)
119 return;
121 if (l_quvi_object_opts_is_set(q->handle.lua, opts,
122 QUVI_OBJECT_OPTION_HTTP_USER_AGENT, &p,
123 NULL, FALSE)
124 == TRUE)
126 o = (l_quvi_object_opt_t) p->data;
127 curl_easy_setopt(q->handle.curl, CURLOPT_USERAGENT, o->value.str);
130 #endif /* _UNUSED */
132 gboolean l_quvi_object_opts_is_set(lua_State *l, GSList *opts,
133 const QuviObjectOption qoo, GSList **dst,
134 const gchar *w,
135 const gboolean croak_if_error)
138 *dst = opts;
139 while (*dst != NULL)
141 l_quvi_object_opt_t o = (l_quvi_object_opt_t) (*dst)->data;
142 if (o->id == qoo)
143 return (TRUE);
144 *dst = g_slist_next(*dst);
147 if (croak_if_error == TRUE && w != NULL)
148 luaL_error(l, "%s is required", w);
150 return (FALSE);
153 void l_quvi_object_opts_chk_given(lua_State *l, GSList *opts,
154 const gchar *w)
156 if (opts == NULL)
157 luaL_error(l, "expects a table of %s options passed as an arg", w);
160 gboolean l_quvi_object_opts_croak_if_error(lua_State *l, GSList *opts)
162 GSList *p;
164 if (opts == NULL)
165 return (TRUE);
167 if (l_quvi_object_opts_is_set(l, opts, QUVI_OBJECT_OPTION_CROAK_IF_ERROR,
168 &p, NULL, FALSE)
169 == TRUE)
171 l_quvi_object_opt_t o = (l_quvi_object_opt_t) p->data;
172 return ((o->value.n) != 0 ? TRUE:FALSE);
174 return (TRUE);
177 /* vim: set ts=2 sw=2 tw=72 expandtab: */