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/>.
25 #include <curl/curl.h>
31 #include "lua/quvi/opts.h"
33 #include "misc/slst.h"
35 static gpointer
_opt_new(const QuviObjectOption qoo
, const gchar
*vs
,
38 l_quvi_object_opt_t o
= g_new0(struct l_quvi_object_opt_s
, 1);
39 o
->value
.str
= g_strdup(vs
);
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
;
55 if (!lua_istable(l
, index
))
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
);
70 o
= _opt_new(k
, lua_tostring(l
, LI_VALUE
), 0);
73 o
= _opt_new(k
, NULL
, lua_tonumber(l
, LI_VALUE
));
76 o
= _opt_new(k
, NULL
, (gdouble
)
77 (lua_toboolean(l
, LI_VALUE
) ? TRUE
:FALSE
) );
80 g_warning("[%s] ignored: unsupported lua type=0x%x",
86 r
= g_slist_prepend(r
, o
);
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
;
100 g_free(o
->value
.str
);
107 void l_quvi_object_opts_free(GSList
*p
)
109 m_slist_free_full(p
, _opt_free
);
113 void l_quvi_object_opts_curl(GSList
*opts
, _quvi_t q
)
115 l_quvi_object_opt_t o
;
121 if (l_quvi_object_opts_is_set(q
->handle
.lua
, opts
,
122 QUVI_OBJECT_OPTION_HTTP_USER_AGENT
, &p
,
126 o
= (l_quvi_object_opt_t
) p
->data
;
127 curl_easy_setopt(q
->handle
.curl
, CURLOPT_USERAGENT
, o
->value
.str
);
132 gboolean
l_quvi_object_opts_is_set(lua_State
*l
, GSList
*opts
,
133 const QuviObjectOption qoo
, GSList
**dst
,
135 const gboolean croak_if_error
)
141 l_quvi_object_opt_t o
= (l_quvi_object_opt_t
) (*dst
)->data
;
144 *dst
= g_slist_next(*dst
);
147 if (croak_if_error
== TRUE
&& w
!= NULL
)
148 luaL_error(l
, "%s is required", w
);
153 void l_quvi_object_opts_chk_given(lua_State
*l
, GSList
*opts
,
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
)
167 if (l_quvi_object_opts_is_set(l
, opts
, QUVI_OBJECT_OPTION_CROAK_IF_ERROR
,
171 l_quvi_object_opt_t o
= (l_quvi_object_opt_t
) p
->data
;
172 return ((o
->value
.n
) != 0 ? TRUE
:FALSE
);
177 /* vim: set ts=2 sw=2 tw=72 expandtab: */