API: Return URLs in escaped (percent-encoded) form
[libquvi.git] / src / lua / exec_subtitle_export_script_ident.c
blob2020efe6811bf41ee958b2ea91f7ed14ac8c8ab9
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_script_s.h"
40 /* -- */
41 #include "lua/setfield.h"
42 #include "lua/chk.h"
43 #include "lua/def.h"
45 static const gchar script_func[] = "ident";
47 static QuviError _chk_results(lua_State *l, _quvi_script_t qs)
49 gboolean r = FALSE;
51 lua_pushnil(l);
52 while (lua_next(l, LI_KEY))
54 l_chk_assign_s(l, SUES_EXPORT_FORMAT, qs->export.format, TRUE, FALSE);
55 l_chk_assign_b(l, SUES_CAN_EXPORT_DATA, &r);
56 lua_pop(l, 1);
59 if (qs->export.format->len ==0)
61 luaL_error(l, "%s: %s: the returned dictionary must contain "
62 "a string value `%s'",
63 qs->fpath->str, script_func, SUES_EXPORT_FORMAT);
65 lua_pop(l, 1);
67 return ((r == TRUE)
68 ? QUVI_OK
69 : QUVI_ERROR_NO_SUPPORT);
72 QuviError l_exec_subtitle_export_script_ident(gpointer p, GSList *sl)
74 _quvi_subtitle_export_t qse;
75 _quvi_script_t qs;
76 lua_State *l;
78 qse = (_quvi_subtitle_export_t) p;
79 l = qse->handle.quvi->handle.lua;
81 qs = (_quvi_script_t) sl->data;
82 lua_pushnil(l);
84 if (luaL_dofile(l, qs->fpath->str))
85 luaL_error(l, "%s", lua_tostring(l, -1));
87 lua_getglobal(l, script_func);
89 if (!lua_isfunction(l, -1))
91 static const gchar *_E = "%s: the function `%s' was not found";
92 luaL_error(l, _E, qs->fpath->str, script_func);
95 lua_newtable(l);
96 l_setfield_s(l, SUES_TO_FORMAT, qse->format.to->str, -1);
98 if (lua_pcall(l, 1, 1, 0))
100 g_string_assign(qse->handle.quvi->status.errmsg, lua_tostring(l, -1));
101 return (QUVI_ERROR_SCRIPT);
104 if (!lua_istable(l, -1))
106 luaL_error(l, "%s: %s: must return a dictionary",
107 qs->fpath->str, script_func);
109 return (_chk_results(l, qs));
112 /* vim: set ts=2 sw=2 tw=72 expandtab: */