Sort headers using clang-tidy
[geeqie.git] / src / lua.cc
index c3abcd9..8319147 100644 (file)
 
 #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
  * User API consists of the following namespaces:
- * 
+ *
  * @link image_methods Image:@endlink basic image information
  *
  * <b>Collection</b>: not implemented
- * 
+ *
  * @link exif_methods <exif-structure>:get_datum() @endlink get single exif parameter
  *
  */
@@ -76,16 +79,16 @@ static FileData *lua_check_image(lua_State *L, int index)
 {
        FileData **fd;
        luaL_checktype(L, index, LUA_TUSERDATA);
-       fd = (FileData **)luaL_checkudata(L, index, "Image");
-       if (fd == NULL) luaL_typerror(L, index, "Image");
+       fd = static_cast<FileData **>(luaL_checkudata(L, index, "Image"));
+       if (fd == nullptr) luaL_typerror(L, index, "Image");
        return *fd;
 }
 
 /**
  * @brief Get exif structure of selected image
- * @param L 
+ * @param L
  * @returns An #ExifData data structure containing the entire exif data
- * 
+ *
  * To be used in conjunction with @link lua_exif_get_datum <exif-structure>:get_datum() @endlink
  */
 static int lua_image_get_exif(lua_State *L)
@@ -97,7 +100,7 @@ static int lua_image_get_exif(lua_State *L)
        fd = lua_check_image(L, 1);
        exif = exif_read_fd(fd);
 
-       exif_data = (ExifData **)lua_newuserdata(L, sizeof(ExifData *));
+       exif_data = static_cast<ExifData **>(lua_newuserdata(L, sizeof(ExifData *)));
        luaL_getmetatable(L, "Exif");
        lua_setmetatable(L, -2);
 
@@ -108,10 +111,10 @@ static int lua_image_get_exif(lua_State *L)
 
 /**
  * @brief Get full path of selected image
- * @param L 
+ * @param L
  * @returns char The full path of the file, including filename and extension
- * 
- * 
+ *
+ *
  */
 static int lua_image_get_path(lua_State *L)
 {
@@ -124,10 +127,10 @@ static int lua_image_get_path(lua_State *L)
 
 /**
  * @brief Get full filename of selected image
- * @param L 
+ * @param L
  * @returns char The full filename including extension
- * 
- * 
+ *
+ *
  */
 static int lua_image_get_name(lua_State *L)
 {
@@ -140,10 +143,10 @@ static int lua_image_get_name(lua_State *L)
 
 /**
  * @brief Get file extension of selected image
- * @param L 
+ * @param L
  * @returns char The file extension including preceding dot
- * 
- * 
+ *
+ *
  */
 static int lua_image_get_extension(lua_State *L)
 {
@@ -156,9 +159,9 @@ static int lua_image_get_extension(lua_State *L)
 
 /**
  * @brief Get file date of selected image
- * @param L 
+ * @param L
  * @returns time_t The file date in Unix timestamp format.
- * 
+ *
  * time_t - signed integer which represents the number of seconds since
  * the start of the Unix epoch: midnight UTC of January 1, 1970
  */
@@ -173,10 +176,10 @@ static int lua_image_get_date(lua_State *L)
 
 /**
  * @brief Get file size of selected image
- * @param L 
+ * @param L
  * @returns integer The file size in bytes
- * 
- * 
+ *
+ *
  */
 static int lua_image_get_size(lua_State *L)
 {
@@ -189,12 +192,12 @@ static int lua_image_get_size(lua_State *L)
 
 /**
  * @brief Get marks of selected image
- * @param L 
+ * @param L
  * @returns unsigned integer Bit map of marks set
- * 
+ *
  * Bit 0 == Mark 1 etc.
- * 
- * 
+ *
+ *
  */
 static int lua_image_get_marks(lua_State *L)
 {
@@ -209,16 +212,16 @@ static ExifData *lua_check_exif(lua_State *L, int index)
 {
        ExifData **exif;
        luaL_checktype(L, index, LUA_TUSERDATA);
-       exif = (ExifData **)luaL_checkudata(L, index, "Exif");
-       if (exif == NULL) luaL_typerror(L, index, "Exif");
+       exif = static_cast<ExifData **>(luaL_checkudata(L, index, "Exif"));
+       if (exif == nullptr) luaL_typerror(L, index, "Exif");
        return *exif;
 }
 
 /**
  * @brief Interface for EXIF data
- * @param L 
+ * @param L
  * @returns <i>return</i> A single exif tag extracted from a structure output by the @link lua_image_get_exif Image:get_exif() @endlink command
- * 
+ *
  * e.g. \n
  * exif_structure = Image:get_exif(); \n
  * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
@@ -227,19 +230,19 @@ static ExifData *lua_check_exif(lua_State *L, int index)
  * Exif.Photo.DateTimeOriginal = signed integer time_t \n
  * Exif.Photo.DateTimeDigitized = signed integer time_t \n
  * otherwise char
- * 
+ *
  */
 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;
 
        exif = lua_check_exif(L, 1);
        key = luaL_checkstring(L, 2);
-       if (key == (gchar*)NULL || key[0] == '\0')
+       if (key == nullptr || key[0] == '\0')
                {
                lua_pushnil(L);
                return 1;
@@ -259,13 +262,12 @@ static int lua_exif_get_datum(lua_State *L)
                        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))
@@ -274,11 +276,9 @@ static int lua_exif_get_datum(lua_State *L)
                        lua_pushnumber(L, datetime);
                        return 1;
                        }
-               else
-                       {
-                       lua_pushnil(L);
-                       return 1;
-                       }
+
+               lua_pushnil(L);
+               return 1;
                }
        lua_pushstring(L, value);
        return 1;
@@ -298,7 +298,7 @@ static const luaL_Reg image_methods[] = {
                {"get_size", lua_image_get_size},
                {"get_exif", lua_image_get_exif},
                {"get_marks", lua_image_get_marks},
-               {NULL, NULL}
+               {nullptr, nullptr}
 };
 
 /**
@@ -306,26 +306,26 @@ static const luaL_Reg image_methods[] = {
  * Call by e.g. \n
  * @link lua_exif_get_datum <exif-structure>:get_datum() @endlink \n
  * where <exif-structure> is the output of @link lua_image_get_exif Image:get_exif() @endlink
- * 
+ *
  * exif_structure = Image:get_exif(); \n
  * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
  */
 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);
@@ -358,28 +358,11 @@ void lua_init(void)
  */
 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)
-               {
-               path = g_build_filename(get_rc_dir(), "lua", file, NULL);
-               }
-       else
+       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)
                {
-               /** @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("");
                        }
@@ -390,29 +373,31 @@ gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
        lua_setglobal(L, "Collection");
 
        /* Current Image */
-       image_data = (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);
@@ -420,9 +405,9 @@ gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
                }
        else
                {
-               g_free(data);
-               data = g_strdup(tmp);
+               std::swap(data, tmp);
                } // if (error) { ... } else
+       g_free(tmp);
        return data;
 }
 #else