File: exif-wrapper.cpp

package info (click to toggle)
darktable 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 62,864 kB
  • sloc: ansic: 361,898; cpp: 102,446; xml: 19,813; lisp: 14,539; sh: 3,771; javascript: 3,264; perl: 1,925; python: 1,485; ruby: 975; makefile: 543; asm: 46; sql: 38; awk: 21
file content (77 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
    This file is part of darktable,
                  2014 Edouard Gomez
    Copyright (C) 2025 darktable developers.

    darktable is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    darktable is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with darktable.  If not, see <http://www.gnu.org/licenses/>.
*/

// avoid error reported when including exiv2.hpp on macOS (XCode 15.2)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#include <exiv2/exif.hpp>
#include <exiv2/error.hpp>
#include <exiv2/image.hpp>
#pragma GCC diagnostic pop

#include <cstdio>
#include <cassert>

using namespace std;

extern "C" int
exif_get_ascii_datafield(
  const char* filename,
  const char* key,
  char* buf,
  size_t buflen)
{
  int ret = -1;

  try
  {
    auto image = Exiv2::ImageFactory::open(filename);
    assert(image.get() != 0);
    image->readMetadata();

    Exiv2::ExifData &exifData = image->exifData();

    auto val = exifData[key].getValue();

    if (val->typeId() != Exiv2::asciiString)
    {
      ret = -1;
      goto exit;
    }

    if ((size_t)val->size() >= buflen)
    {
      ret = val->size()+1;
      goto exit;
    }

    snprintf(buf, buflen, "%s", val->toString().c_str());

    ret = 0;
  }
  catch (Exiv2::Error& e)
  {
    ret = -1;
  }

exit:

  return ret;
}