Skip to content

Commit

Permalink
use more stringFormat
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Feb 3, 2025
1 parent 152fefa commit 3a3d02a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 35 deletions.
23 changes: 7 additions & 16 deletions src/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "error.hpp"
#include "exif.hpp"
#include "futils.hpp"
#include "image_int.hpp"
#include "iptc.hpp"
#include "types.hpp"
#include "xmp_exiv2.hpp"
Expand Down Expand Up @@ -621,7 +622,6 @@ void Converter::cnvExifDate(const char* from, const char* to) {
int min = 0;
int sec = 0;
std::string subsec;
char buf[30];

if (std::string(from) != "Exif.GPSInfo.GPSTimeStamp") {
std::string value = pos->toString();
Expand Down Expand Up @@ -678,10 +678,7 @@ void Converter::cnvExifDate(const char* from, const char* to) {
sec = static_cast<int>(dsec);
dsec -= sec;

snprintf(buf, sizeof(buf), "%.9f", dsec);
buf[sizeof(buf) - 1] = 0;
buf[1] = '.'; // some locales use ','
subsec = buf + 1;
subsec = std::format(".{:09.0f}", dsec * 1'000'000'000);

auto datePos = exifData_->findKey(ExifKey("Exif.GPSInfo.GPSDateStamp"));
if (datePos == exifData_->end()) {
Expand Down Expand Up @@ -733,10 +730,8 @@ void Converter::cnvExifDate(const char* from, const char* to) {

if (subsec.size() > 10)
subsec.resize(10);
snprintf(buf, sizeof(buf), "%4d-%02d-%02dT%02d:%02d:%02d%s", year, month, day, hour, min, sec, subsec.c_str());
buf[sizeof(buf) - 1] = 0;

(*xmpData_)[to] = buf;
(*xmpData_)[to] = stringFormat("{:4}-{:02}-{:02}T{:02}:{:02}:{:02}{}", year, month, day, hour, min, sec, subsec);
if (erase_)
exifData_->erase(pos);
}
Expand Down Expand Up @@ -923,14 +918,11 @@ void Converter::cnvXmpDate(const char* from, const char* to) {
XMP_DateTime datetime;
try {
SXMPUtils::ConvertToDate(value, &datetime);
char buf[30];
if (std::string(to) != "Exif.GPSInfo.GPSTimeStamp") {
SXMPUtils::ConvertToLocalTime(&datetime);

snprintf(buf, sizeof(buf), "%4d:%02d:%02d %02d:%02d:%02d", datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second);
buf[sizeof(buf) - 1] = 0;
(*exifData_)[to] = buf;
(*exifData_)[to] = stringFormat("{:4}:{:02}:{:02} {:02}:{:02}:{:02}", datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second);

if (datetime.nanoSecond) {
const char* subsecTag = nullptr;
Expand Down Expand Up @@ -968,9 +960,8 @@ void Converter::cnvXmpDate(const char* from, const char* to) {
(*exifData_)[to] = array.str();

prepareExifTarget("Exif.GPSInfo.GPSDateStamp", true);
snprintf(buf, sizeof(buf), "%4d:%02d:%02d", datetime.year, datetime.month, datetime.day);
buf[sizeof(buf) - 1] = 0;
(*exifData_)["Exif.GPSInfo.GPSDateStamp"] = buf;
(*exifData_)["Exif.GPSInfo.GPSDateStamp"] =
stringFormat("{:4}:{:02}:{:02}", datetime.year, datetime.month, datetime.day);
}
}
#ifndef SUPPRESS_WARNINGS
Expand Down
4 changes: 3 additions & 1 deletion src/image_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#include <format>
#endif
#ifndef EXV_HAVE_STD_FORMAT
#include <fmt/core.h>
#include <fmt/format.h>
#define stringFormat fmt::format
#define stringFormatTo fmt::format_to
#else
#define stringFormat std::format
#define stringFormatTo std::format_to
#endif

// *****************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions src/iptc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ void IptcData::printStructure(std::ostream& out, const Slice<byte*>& bytes, size
uint16_t dataset = bytes.at(i + 2);
Internal::enforce(bytes.size() - i >= 5, ErrorCode::kerCorruptedMetadata);
uint16_t len = getUShort(bytes.subSlice(i + 3, bytes.size()), bigEndian);
snprintf(buff, sizeof(buff), " %6hu | %7hu | %-24s | %6hu | ", record, dataset,
Exiv2::IptcDataSets::dataSetName(dataset, record).c_str(), len);

Internal::enforce(bytes.size() - i >= 5 + static_cast<size_t>(len), ErrorCode::kerCorruptedMetadata);
out << std::format(" {:6} | {:7} | {:<24} | {:6} | ", record, dataset,
Exiv2::IptcDataSets::dataSetName(dataset, record), len);
out << buff << Internal::binaryToString(makeSlice(bytes, i + 5, i + 5 + (len > 40 ? 40 : len)))
<< (len > 40 ? "..." : "") << '\n';
i += 5 + len;
Expand Down
7 changes: 2 additions & 5 deletions src/jpgimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,9 @@ void JpegBase::printStructure(std::ostream& out, PrintStructureOption option, si
// 0xc0 .. 0xcf are SOFn (except 4)
nm[0xc4] = "DHT";
for (int i = 0; i <= 15; i++) {
char MN[16];
snprintf(MN, sizeof(MN), "APP%d", i);
nm[0xe0 + i] = MN;
nm[0xe0 + i] = stringFormat("APP{}", i);
if (i != 4) {
snprintf(MN, sizeof(MN), "SOF%d", i);
nm[0xc0 + i] = MN;
nm[0xc0 + i] = stringFormat("SOF{}", i);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ convertlib = static_library(
'convert.cpp',
cpp_args: cargs,
gnu_symbol_visibility: 'hidden',
dependencies: iconv_dep,
dependencies: [fmt_dep, iconv_dep],
include_directories: libinc,
build_by_default: false,
)
Expand Down
19 changes: 9 additions & 10 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,11 +817,10 @@ size_t DateValue::copy(byte* buf, ByteOrder /*byteOrder*/) const {
// \note Here the date is copied in the Basic format YYYYMMDD, as the IPTC key Iptc.Application2.DateCreated
// wants it. Check https://exiv2.org/iptc.html

// sprintf wants to add the null terminator, so use oversized buffer
char temp[9];
auto wrote = static_cast<size_t>(snprintf(temp, sizeof(temp), "%04d%02d%02d", date_.year, date_.month, date_.day));
std::copy_n(temp, wrote, buf);
return wrote;
auto out = reinterpret_cast<char*>(buf);
auto it = std::format_to(out, "{:04}{:02}{:02}", date_.year, date_.month, date_.day);

return it - out;
}

const DateValue::Date& DateValue::getDate() const {
Expand Down Expand Up @@ -987,17 +986,17 @@ void TimeValue::setTime(const Time& src) {
size_t TimeValue::copy(byte* buf, ByteOrder /*byteOrder*/) const {
// NOTE: Here the time is copied in the Basic format HHMMSS:HHMM, as the IPTC key
// Iptc.Application2.TimeCreated wants it. Check https://exiv2.org/iptc.html
char temp[12];
char plusMinus = '+';
if (time_.tzHour < 0 || time_.tzMinute < 0)
plusMinus = '-';

const auto wrote = static_cast<size_t>(snprintf(temp, sizeof(temp), // 11 bytes are written + \0
"%02d%02d%02d%1c%02d%02d", time_.hour, time_.minute, time_.second,
plusMinus, abs(time_.tzHour), abs(time_.tzMinute)));
auto out = reinterpret_cast<char*>(buf);
auto it = std::format_to(out, "{:02}{:02}{:02}{}{:02}{:02}",
time_.hour, time_.minute, time_.second,
plusMinus, std::abs(time_.tzHour), std::abs(time_.tzMinute));

auto wrote = static_cast<size_t>(it - out);
Internal::enforce(wrote == 11, Exiv2::ErrorCode::kerUnsupportedTimeFormat);
std::copy_n(temp, wrote, buf);
return wrote;
}

Expand Down

0 comments on commit 3a3d02a

Please sign in to comment.