* @brief Call a lua function to get a single value. */
| 342 | * @brief Call a lua function to get a single value. |
| 343 | */ |
| 344 | gchar *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: */ |
no test coverage detected