Sort headers using clang-tidy
[geeqie.git] / src / exiv2.cc
index ca70401..6b9194d 100644 (file)
 #define HAVE_EXIV2_ERROR_CODE
 #endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #if EXIV2_TEST_VERSION(0,27,0)
 #define EXV_PACKAGE "exiv2"
 
 #include <glib.h>
 
-#include "main.h"
 #include "exif.h"
 
+#include "debug.h"
+#include "filedata.h"
 #include "filefilter.h"
-#include "ui-fileops.h"
-
 #include "misc.h"
+#include "options.h"
+#include "ui-fileops.h"
 
 #if EXIV2_TEST_VERSION(0,28,0)
 #define AnyError Error
@@ -75,7 +76,7 @@ struct AltKey
 };
 
 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
-static const AltKey alt_keys[] = {
+static constexpr AltKey alt_keys[] = {
        {"Xmp.tiff.Orientation",                "Exif.Image.Orientation",       nullptr},
        {"Xmp.dc.title",                        nullptr,                                "Iptc.Application2.ObjectName"          },
        {"Xmp.photoshop.Urgency",               nullptr,                                "Iptc.Application2.Urgency"             },
@@ -98,7 +99,6 @@ static const AltKey alt_keys[] = {
        {"Xmp.dc.rights",                       nullptr,                                "Iptc.Application2.Copyright"           },
        {"Xmp.dc.description",                  nullptr,                                "Iptc.Application2.Caption"             },
        {"Xmp.photoshop.CaptionWriter",         nullptr,                                "Iptc.Application2.Writer"              },
-       {nullptr, nullptr, nullptr}
        };
 
 static void _debug_exception(const char* file,
@@ -123,7 +123,7 @@ struct ExifData
 
        virtual ~ExifData() = default;
 
-       virtual void writeMetadata(gchar *UNUSED(path) = nullptr)
+       virtual void writeMetadata(gchar * = nullptr)
        {
                g_critical("Unsupported method of writing metadata");
        }
@@ -189,7 +189,6 @@ public:
                try
                        {
                        image_ = Exiv2::ImageFactory::open(pathl_);
-//                     g_assert (image.get() != 0);
                        image_->readMetadata();
 
 #if EXIV2_TEST_VERSION(0,16,0)
@@ -713,7 +712,6 @@ char *exif_item_get_description(ExifItem *item)
                return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
        }
        catch (std::exception& e) {
-//             debug_exception(e);
                return nullptr;
        }
 }
@@ -765,7 +763,7 @@ guint exif_item_get_format_id(ExifItem *item)
        }
 }
 
-const char *exif_item_get_format_name(ExifItem *item, gboolean UNUSED(brief))
+const char *exif_item_get_format_name(ExifItem *item, gboolean)
 {
        try {
                if (!item) return nullptr;
@@ -899,13 +897,8 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
 
 static const AltKey *find_alt_key(const gchar *xmp_key)
 {
-       gint i = 0;
-
-       while (alt_keys[i].xmp_key)
-               {
-               if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
-               i++;
-               }
+       for (const auto& k : alt_keys)
+               if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
        return nullptr;
 }
 
@@ -1227,19 +1220,13 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
 
                        Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
 
-                       Exiv2::DataBuf buf = image.copy();
-
-#if EXIV2_TEST_VERSION(0,28,0)
-                       *data_len = buf.size();
-                       auto b = buf.data();
-                       buf.reset();
-                       return b;
-#else
-                       std::pair<Exiv2::byte*, long> p = buf.release();
-
-                       *data_len = p.second;
-                       return p.first;
-#endif
+                       // Let's not touch data_len until we finish copy.
+                       // Just in case we run into OOM.
+                       size_t img_sz = image.size();
+                       auto* b = new Exiv2::byte[img_sz];
+                       std::copy_n(image.pData(), img_sz, b);
+                       *data_len = img_sz;
+                       return b;
                        }
                return nullptr;
        }
@@ -1249,9 +1236,9 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
        }
 }
 
-void exif_free_preview(guchar *buf)
+void exif_free_preview(const guchar *buf)
 {
-       delete[] static_cast<Exiv2::byte*>(buf);
+delete[] static_cast<const Exiv2::byte*>(buf);
 }
 #endif
 
@@ -1346,7 +1333,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
 
 }
 
-void exif_free_preview(guchar *buf)
+void exif_free_preview(const guchar *buf)
 {
        GList *work = exif_unmap_list;