From 8559186214143d2c11f5823dc16af8f473a61d4b Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Sat, 12 Jan 2013 17:28:48 +0200 Subject: [PATCH] lua: Add exec_subtitle_script_ident.c Signed-off-by: Toni Gundogdu --- src/lua/exec_subtitle_script_ident.c | 100 +++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/lua/exec_subtitle_script_ident.c diff --git a/src/lua/exec_subtitle_script_ident.c b/src/lua/exec_subtitle_script_ident.c new file mode 100644 index 0000000..308a856 --- /dev/null +++ b/src/lua/exec_subtitle_script_ident.c @@ -0,0 +1,100 @@ +/* libquvi + * Copyright (C) 2013 Toni Gundogdu + * + * This file is part of libquvi . + * + * This library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General + * Public License along with this library. If not, see + * . + */ + +/* + * NOTE: The error messages produced in these functions are intended for + * developers. They would typically be seen when a new script is + * being developed or an old one is being maintained. + * + * These messages should be clear, indicating the actual error, + * minimizing the time spent on locating the problem in the script. + */ + +#include "config.h" + +#include +#include + +#include "quvi.h" +/* -- */ +#include "_quvi_s.h" +#include "_quvi_subtitle_s.h" +#include "_quvi_script_s.h" +/* -- */ +#include "lua/setfield.h" +#include "lua/chk.h" +#include "lua/def.h" + +static const gchar script_func[] = "ident"; + +static QuviError _chk_results(lua_State *l, _quvi_script_t qs) +{ + QuviError r; + + r = (l_chk_can_parse_url(l, qs, SUS_CAN_PARSE_URL, + SUS_DOMAINS, script_func) == TRUE) + ? QUVI_OK + : QUVI_ERROR_NO_SUPPORT; + + lua_pop(l, 1); + return (r); +} + +QuviError l_exec_subtitle_script_ident(gpointer p, GSList *sl) +{ + _quvi_subtitle_t qsub; + _quvi_script_t qs; + lua_State *l; + + qsub = (_quvi_subtitle_t) p; + l = qsub->handle.quvi->handle.lua; + + qs = (_quvi_script_t) sl->data; + lua_pushnil(l); + + if (luaL_dofile(l, qs->fpath->str)) + luaL_error(l, "%s", lua_tostring(l, -1)); + + lua_getglobal(l, script_func); + + if (!lua_isfunction(l, -1)) + { + static const gchar *_E = "%s: the function `%s' was not found"; + luaL_error(l, _E, qs->fpath->str, script_func); + } + + lua_newtable(l); + l_setfield_s(l, SUS_INPUT_URL, qsub->url.input->str); + + if (lua_pcall(l, 1, 1, 0)) + { + g_string_assign(qsub->handle.quvi->status.errmsg, lua_tostring(l, -1)); + return (QUVI_ERROR_SCRIPT); + } + + if (!lua_istable(l, -1)) + { + luaL_error(l, "%s: %s: must return a dictionary", + qs->fpath->str, script_func); + } + return (_chk_results(l, qs)); +} + +/* vim: set ts=2 sw=2 tw=72 expandtab: */ -- 2.11.4.GIT