cc35d1d627c3b24ca69ca6b6b0606f728104bf95
[libquvi.git] / src / lua / exec_subtitle_export_script_export.c
blobcc35d1d627c3b24ca69ca6b6b0606f728104bf95
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/>.
22 * NOTE: The error messages produced in these functions are intended for
23 * developers. They would typically be seen when a new script is
24 * being developed or an old one is being maintained.
26 * These messages should be clear, indicating the actual error,
27 * minimizing the time spent on locating the problem in the script.
30 #include "config.h"
32 #include <lauxlib.h>
33 #include <glib.h>
35 #include "quvi.h"
36 /* -- */
37 #include "_quvi_s.h"
38 #include "_quvi_subtitle_export_s.h"
39 #include "_quvi_subtitle_s.h"
40 #include "_quvi_script_s.h"
41 /* -- */
42 #include "misc/subtitle_export.h"
43 #include "lua/setfield.h"
44 #include "lua/chk.h"
45 #include "lua/def.h"
47 static const gchar script_func[] = "export";
49 static QuviError _chk_result(lua_State *l, _quvi_subtitle_export_t qse,
50 const gchar *script_fpath)
52 lua_pushnil(l);
53 while (lua_next(l, LI_KEY))
55 l_chk_assign_s(l, SUES_DATA, qse->data, FALSE);
56 lua_pop(l, 1);
59 if (qse->data->len ==0)
61 luaL_error(l, "%s: %s: must return `qargs.data'",
62 script_fpath, script_func);
64 lua_pop(l, 1);
65 return (QUVI_OK);
68 QuviError l_exec_subtitle_export_script_export(gpointer p, GSList *sl)
70 _quvi_subtitle_export_t qse;
71 _quvi_script_t qs;
72 lua_State *l;
74 qse = (_quvi_subtitle_export_t) p;
75 l = qse->handle.quvi->handle.lua;
77 qs = (_quvi_script_t) sl->data;
78 lua_getglobal(l, script_func);
80 if (!lua_isfunction(l, -1))
82 luaL_error(l, "%s: the function `%s' was not found",
83 qs->fpath->str, script_func);
86 lua_newtable(l);
87 l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qse->handle.quvi);
88 l_setfield_s(l, SUES_INPUT_URL, qse->url.input->str, -1);
89 l_setfield_n(l, SUES_FROM_FORMAT, qse->format.from);
91 if (lua_pcall(l, 1, 1, 0))
93 g_string_assign(qse->handle.quvi->status.errmsg, lua_tostring(l, -1));
94 return (QUVI_ERROR_SCRIPT);
97 if (!lua_istable(l, -1))
99 luaL_error(l, "%s: %s: must return a dictionary, this is "
100 "typically the `qargs'", qs->fpath->str, script_func);
102 return (_chk_result(l, qse, qs->fpath->str));
105 /* vim: set ts=2 sw=2 tw=72 expandtab: */