MCPcopy Create free account
hub / github.com/BestImageViewer/geeqie / lua_callvalue

Function lua_callvalue

src/lua.cc:344–396  ·  view source on GitHub ↗

* @brief Call a lua function to get a single value. */

Source from the content-addressed store, hash-verified

342 * @brief Call a lua function to get a single value.
343 */
344gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
345{
346 g_autofree gchar *path = g_build_filename(get_rc_dir(), "lua", file, NULL);
347 if (access(path, R_OK) == -1)
348 {
349 g_free(path);
350 path = g_build_filename(gq_bindir, file, NULL);
351 if (access(path, R_OK) == -1)
352 {
353 return g_strdup("");
354 }
355 }
356
357 /* Collection Table (Dummy at the moment) */
358 lua_newtable(L);
359 lua_setglobal(L, "Collection");
360
361 /* Current Image */
362 auto image_data = static_cast<FileData **>(lua_newuserdata(L, sizeof(FileData *)));
363 luaL_getmetatable(L, "Image");
364 lua_setmetatable(L, -2);
365 lua_setglobal(L, "Image");
366
367 *image_data = fd;
368
369 gint result;
370 if (file[0] == '\0')
371 {
372 result = luaL_dostring(L, function);
373 }
374 else
375 {
376 result = luaL_dofile(L, path);
377 }
378
379 if (result)
380 {
381 return g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
382 }
383
384 gchar *data = g_strdup(lua_tostring(L, -1));
385 g_autoptr(GError) error = nullptr;
386 g_autofree gchar *tmp = g_locale_to_utf8(data, strlen(data), nullptr, nullptr, &error);
387 if (error)
388 {
389 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
390 }
391 else
392 {
393 std::swap(data, tmp);
394 }
395 return data;
396}
397
398/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

Callers 3

gq_luaFunction · 0.85
metadata_lua_infoFunction · 0.85
get_osd_dataFunction · 0.85

Calls 1

get_rc_dirFunction · 0.85

Tested by

no test coverage detected