From b6bb91acb97f48a1aa38e4b00d756c31de617bb1 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 1 Mar 2024 23:27:55 +0100 Subject: [PATCH] core: use std::string to format battery extra data Create a format_string_std function that works like format_string, but does return a std::string instead of a strdup()ed C string. Make it a global function to be used in other parts of the code. Signed-off-by: Berthold Stoeger --- core/parse-xml.cpp | 38 ++++++++------------------------------ core/subsurface-string.h | 13 +++++++++++++ 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index dd3c3aa75..0383a92a6 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -2256,42 +2256,20 @@ extern "C" int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divel /* Recording the starting battery status to extra data */ if (battery_start.volt1) { - size_t stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_start.volt1, battery_start.percent1) + 1; - char *ptr = (char *)malloc(stringsize); + std::string str = format_string_std("%dmV (%d%%)", battery_start.volt1, battery_start.percent1); + add_extra_data(state.cur_dc, "Battery 1 (start)", str.c_str()); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_start.volt1, battery_start.percent1); - add_extra_data(state.cur_dc, "Battery 1 (start)", ptr); - free(ptr); - } - - stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_start.volt2, battery_start.percent2) + 1; - ptr = (char *)malloc(stringsize); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_start.volt2, battery_start.percent2); - add_extra_data(state.cur_dc, "Battery 2 (start)", ptr); - free(ptr); - } + str = format_string_std("%dmV (%d%%)", battery_start.volt2, battery_start.percent2); + add_extra_data(state.cur_dc, "Battery 2 (start)", str.c_str()); } /* Recording the ending battery status to extra data */ if (battery_end.volt1) { - size_t stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_end.volt1, battery_end.percent1) + 1; - char *ptr = (char *)malloc(stringsize); + std::string str = format_string_std("%dmV (%d%%)", battery_end.volt1, battery_end.percent1); + add_extra_data(state.cur_dc, "Battery 1 (end)", str.c_str()); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_end.volt1, battery_end.percent1); - add_extra_data(state.cur_dc, "Battery 1 (end)", ptr); - free(ptr); - } - - stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_end.volt2, battery_end.percent2) + 1; - ptr = (char *)malloc(stringsize); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_end.volt2, battery_end.percent2); - add_extra_data(state.cur_dc, "Battery 2 (end)", ptr); - free(ptr); - } + str = format_string_std("%dmV (%d%%)", battery_end.volt2, battery_end.percent2); + add_extra_data(state.cur_dc, "Battery 2 (end)", str.c_str()); } divecomputer_end(&state); diff --git a/core/subsurface-string.h b/core/subsurface-string.h index e2127d02c..cf136c260 100644 --- a/core/subsurface-string.h +++ b/core/subsurface-string.h @@ -60,5 +60,18 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags #ifdef __cplusplus } + +#include +template +std::string format_string_std(const char *fmt, Args&&... args) +{ + // Note: std::string automatically null terminates, so we actually want snprintf to truncate. + size_t stringsize = snprintf(NULL, 0, fmt, std::forward(args)...); + std::string res; + res.resize(stringsize); // Pointless clearing, oh my. + snprintf(res.data(), stringsize, fmt, std::forward(args)...); + return res; +} + #endif #endif // SUBSURFACE_STRING_H