#define _XOPEN_SOURCE
-#include <lua.h>
-#include <lauxlib.h>
-#include <lualib.h>
+#include "glua.h"
+
+#include <cstdio>
+#include <cstring>
+#include <ctime>
+#include <memory>
-#include <stdio.h>
#include <glib.h>
-#include <string.h>
-#include <time.h>
+#include <lua.hpp>
+#include "debug.h"
+#include "exif.h"
+#include "filedata.h"
+#include "main-defines.h"
#include "main.h"
-#include "glua.h"
#include "ui-fileops.h"
-#include "exif.h"
/**
* @file
FileData **fd;
luaL_checktype(L, index, LUA_TUSERDATA);
fd = static_cast<FileData **>(luaL_checkudata(L, index, "Image"));
- if (fd == NULL) luaL_typerror(L, index, "Image");
+ if (fd == nullptr) luaL_typerror(L, index, "Image");
return *fd;
}
ExifData **exif;
luaL_checktype(L, index, LUA_TUSERDATA);
exif = static_cast<ExifData **>(luaL_checkudata(L, index, "Exif"));
- if (exif == NULL) luaL_typerror(L, index, "Exif");
+ if (exif == nullptr) luaL_typerror(L, index, "Exif");
return *exif;
}
static int lua_exif_get_datum(lua_State *L)
{
const gchar *key;
- gchar *value = NULL;
+ gchar *value = nullptr;
ExifData *exif;
struct tm tm;
time_t datetime;
lua_pushnumber(L, datetime);
return 1;
}
- else
- {
- lua_pushnil(L);
- return 1;
- }
+
+ lua_pushnil(L);
+ return 1;
}
- else if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
+
+ if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
{
memset(&tm, 0, sizeof(tm));
if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
lua_pushnumber(L, datetime);
return 1;
}
- else
- {
- lua_pushnil(L);
- return 1;
- }
+
+ lua_pushnil(L);
+ return 1;
}
lua_pushstring(L, value);
return 1;
{"get_size", lua_image_get_size},
{"get_exif", lua_image_get_exif},
{"get_marks", lua_image_get_marks},
- {NULL, NULL}
+ {nullptr, nullptr}
};
/**
*/
static const luaL_Reg exif_methods[] = {
{"get_datum", lua_exif_get_datum},
- {NULL, NULL}
+ {nullptr, nullptr}
};
/**
* @brief Initialize the lua interpreter.
*/
-void lua_init(void)
+void lua_init()
{
L = luaL_newstate();
luaL_openlibs(L); /* Open all libraries for lua programs */
/* Now create custom methodes to do something */
static const luaL_Reg meta_methods[] = {
- {NULL, NULL}
+ {nullptr, nullptr}
};
LUA_register_global(L, "Image", image_methods);
*/
gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
{
- gint result;
- gchar *data = NULL;
- gchar *path;
- FileData **image_data;
- gchar *tmp;
- GError *error = NULL;
- gboolean ok;
-
- ok = access(g_build_filename(get_rc_dir(), "lua", file, NULL), R_OK);
- if (ok == 0)
+ std::unique_ptr<gchar, decltype(&g_free)> path{g_build_filename(get_rc_dir(), "lua", file, NULL), g_free};
+ if (access(path.get(), R_OK) == -1)
{
- path = g_build_filename(get_rc_dir(), "lua", file, NULL);
- }
- else
- {
- /** @FIXME what is the correct way to find the scripts folder? */
- ok = access(g_build_filename("/usr/local/lib", GQ_APPNAME_LC, file, NULL), R_OK);
- if (ok == 0)
- {
- path = g_build_filename("/usr/local/lib", GQ_APPNAME_LC, file, NULL);
- }
- else
+ path.reset(g_build_filename(gq_bindir, file, NULL));
+ if (access(path.get(), R_OK) == -1)
{
return g_strdup("");
}
lua_setglobal(L, "Collection");
/* Current Image */
- image_data = static_cast<FileData **>(lua_newuserdata(L, sizeof(FileData *)));
+ auto image_data = static_cast<FileData **>(lua_newuserdata(L, sizeof(FileData *)));
luaL_getmetatable(L, "Image");
lua_setmetatable(L, -2);
lua_setglobal(L, "Image");
*image_data = fd;
+
+ gint result;
if (file[0] == '\0')
{
result = luaL_dostring(L, function);
}
else
{
- result = luaL_dofile(L, path);
- g_free(path);
+ result = luaL_dofile(L, path.get());
}
if (result)
{
- data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
- return data;
+ return g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
}
- data = g_strdup(lua_tostring(L, -1));
- tmp = g_locale_to_utf8(data, strlen(data), NULL, NULL, &error);
+
+ gchar *data = g_strdup(lua_tostring(L, -1));
+ GError *error = nullptr;
+ gchar *tmp = g_locale_to_utf8(data, strlen(data), nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
}
else
{
- g_free(data);
- data = g_strdup(tmp);
+ std::swap(data, tmp);
} // if (error) { ... } else
+ g_free(tmp);
return data;
}
#else