core: convert filter_constraint_data_to_string to C++

Return an std::string to avoid memory management headaches.

While doing that, convert time.c to C++ so that
format_datetime directly returns an std::string.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-02-29 11:31:46 +01:00
parent a49e84082c
commit 5d36ba4593
8 changed files with 29 additions and 32 deletions

View file

@ -95,7 +95,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/strtod.c \ core/strtod.c \
core/tag.c \ core/tag.c \
core/taxonomy.c \ core/taxonomy.c \
core/time.c \ core/time.cpp \
core/trip.c \ core/trip.c \
core/units.c \ core/units.c \
core/uemis.c \ core/uemis.c \

View file

@ -181,7 +181,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
tag.h tag.h
taxonomy.c taxonomy.c
taxonomy.h taxonomy.h
time.c time.cpp
timer.c timer.c
timer.h timer.h
trip.c trip.c

View file

@ -622,27 +622,23 @@ filter_constraint::~filter_constraint()
delete data.string_list; delete data.string_list;
} }
extern "C" char *filter_constraint_data_to_string(const filter_constraint *c) std::string filter_constraint_data_to_string(const filter_constraint *c)
{ {
QString s;
if (filter_constraint_is_timestamp(c->type)) { if (filter_constraint_is_timestamp(c->type)) {
char *from_s = format_datetime(c->data.timestamp_range.from); std::string from_s = format_datetime(c->data.timestamp_range.from);
char *to_s = format_datetime(c->data.timestamp_range.to); std::string to_s = format_datetime(c->data.timestamp_range.to);
s = QString(from_s) + ',' + QString(to_s); return from_s + ',' + to_s;
free(from_s);
free(to_s);
} else if (filter_constraint_is_string(c->type)) { } else if (filter_constraint_is_string(c->type)) {
// TODO: this obviously breaks if the strings contain ",". // TODO: this obviously breaks if the strings contain ",".
// That is currently not supported by the UI, but one day we might // That is currently not supported by the UI, but one day we might
// have to escape the strings. // have to escape the strings.
s = c->data.string_list->join(","); return c->data.string_list->join(",").toStdString();
} else if (filter_constraint_is_multiple_choice(c->type)) { } else if (filter_constraint_is_multiple_choice(c->type)) {
s = QString::number(c->data.multiple_choice); return std::to_string(c->data.multiple_choice);
} else { } else {
s = QString::number(c->data.numerical_range.from) + ',' + return std::to_string(c->data.numerical_range.from) + ',' +
QString::number(c->data.numerical_range.to); std::to_string(c->data.numerical_range.to);
} }
return copy_qstring(s);
} }
void filter_constraint_set_stringlist(filter_constraint &c, const QString &s) void filter_constraint_set_stringlist(filter_constraint &c, const QString &s)

View file

@ -116,7 +116,6 @@ extern bool filter_constraint_has_date_widget(enum filter_constraint_type);
extern bool filter_constraint_has_time_widget(enum filter_constraint_type); extern bool filter_constraint_has_time_widget(enum filter_constraint_type);
extern int filter_constraint_num_decimals(enum filter_constraint_type); extern int filter_constraint_num_decimals(enum filter_constraint_type);
extern bool filter_constraint_is_valid(const struct filter_constraint *constraint); extern bool filter_constraint_is_valid(const struct filter_constraint *constraint);
extern char *filter_constraint_data_to_string(const struct filter_constraint *constraint); // caller takes ownership of returned string
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -152,6 +151,8 @@ void filter_constraint_set_timestamp_from(filter_constraint &c, timestamp_t from
void filter_constraint_set_timestamp_to(filter_constraint &c, timestamp_t to); // convert according to current units (metric or imperial) void filter_constraint_set_timestamp_to(filter_constraint &c, timestamp_t to); // convert according to current units (metric or imperial)
void filter_constraint_set_multiple_choice(filter_constraint &c, uint64_t); void filter_constraint_set_multiple_choice(filter_constraint &c, uint64_t);
bool filter_constraint_match_dive(const filter_constraint &c, const struct dive *d); bool filter_constraint_match_dive(const filter_constraint &c, const struct dive *d);
std::string filter_constraint_data_to_string(const struct filter_constraint *constraint); // caller takes ownership of returned string
#endif #endif
#endif #endif

View file

@ -957,7 +957,6 @@ static void format_one_filter_constraint(int preset_id, int constraint_id, struc
{ {
const struct filter_constraint *constraint = filter_preset_constraint(preset_id, constraint_id); const struct filter_constraint *constraint = filter_preset_constraint(preset_id, constraint_id);
const char *type = filter_constraint_type_to_string(constraint->type); const char *type = filter_constraint_type_to_string(constraint->type);
char *data;
show_utf8(b, "constraint type=", type, ""); show_utf8(b, "constraint type=", type, "");
if (filter_constraint_has_string_mode(constraint->type)) { if (filter_constraint_has_string_mode(constraint->type)) {
@ -970,9 +969,8 @@ static void format_one_filter_constraint(int preset_id, int constraint_id, struc
} }
if (constraint->negate) if (constraint->negate)
put_format(b, " negate"); put_format(b, " negate");
data = filter_constraint_data_to_string(constraint); std::string data = filter_constraint_data_to_string(constraint);
show_utf8(b, " data=", data, "\n"); show_utf8(b, " data=", data.c_str(), "\n");
free(data);
} }
/* /*

View file

@ -657,7 +657,6 @@ static void save_filter_presets(struct membuffer *b)
free(fulltext); free(fulltext);
for (int j = 0; j < filter_preset_constraint_count(i); j++) { for (int j = 0; j < filter_preset_constraint_count(i); j++) {
char *data;
const struct filter_constraint *constraint = filter_preset_constraint(i, j); const struct filter_constraint *constraint = filter_preset_constraint(i, j);
const char *type = filter_constraint_type_to_string(constraint->type); const char *type = filter_constraint_type_to_string(constraint->type);
put_format(b, " <constraint"); put_format(b, " <constraint");
@ -673,9 +672,8 @@ static void save_filter_presets(struct membuffer *b)
if (constraint->negate) if (constraint->negate)
put_format(b, " negate='1'"); put_format(b, " negate='1'");
put_format(b, ">"); put_format(b, ">");
data = filter_constraint_data_to_string(constraint); std::string data = filter_constraint_data_to_string(constraint);
show_utf8(b, data, "", "", 0); show_utf8(b, data.c_str(), "", "", 0);
free(data);
put_format(b, "</constraint>\n"); put_format(b, "</constraint>\n");
} }
put_format(b, " </filterpreset>\n"); put_format(b, " </filterpreset>\n");

View file

@ -16,12 +16,15 @@ extern int utc_weekday(timestamp_t timestamp);
/* parse and format date times of the form YYYY-MM-DD hh:mm:ss */ /* parse and format date times of the form YYYY-MM-DD hh:mm:ss */
extern timestamp_t parse_datetime(const char *s); /* returns 0 on error */ extern timestamp_t parse_datetime(const char *s); /* returns 0 on error */
extern char *format_datetime(timestamp_t timestamp); /* ownership of string passed to caller */
extern const char *monthname(int mon); extern const char *monthname(int mon);
#ifdef __cplusplus #ifdef __cplusplus
} }
#include <string>
std::string format_datetime(timestamp_t timestamp); /* ownership of string passed to caller */
#endif #endif
#endif #endif

View file

@ -2,6 +2,7 @@
#include "subsurface-time.h" #include "subsurface-time.h"
#include "subsurface-string.h" #include "subsurface-string.h"
#include "gettext.h" #include "gettext.h"
#include <QtCore> // for QT_TRANSLATE_NOOP
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -32,7 +33,7 @@
* are unnecessary once you're counting minutes (32-bit minutes: * are unnecessary once you're counting minutes (32-bit minutes:
* 8000+ years). * 8000+ years).
*/ */
void utc_mkdate(timestamp_t timestamp, struct tm *tm) extern "C" void utc_mkdate(timestamp_t timestamp, struct tm *tm)
{ {
static const unsigned int mdays[] = { static const unsigned int mdays[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
@ -99,7 +100,7 @@ void utc_mkdate(timestamp_t timestamp, struct tm *tm)
tm->tm_mon = m; tm->tm_mon = m;
} }
timestamp_t utc_mktime(const struct tm *tm) extern "C" timestamp_t utc_mktime(const struct tm *tm)
{ {
static const int mdays[] = { static const int mdays[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
@ -146,7 +147,7 @@ timestamp_t utc_mktime(const struct tm *tm)
* out unused calculations. If it turns out to be a bottle neck * out unused calculations. If it turns out to be a bottle neck
* we will have to cache a struct tm per dive. * we will have to cache a struct tm per dive.
*/ */
int utc_year(timestamp_t timestamp) extern "C" int utc_year(timestamp_t timestamp)
{ {
struct tm tm; struct tm tm;
utc_mkdate(timestamp, &tm); utc_mkdate(timestamp, &tm);
@ -161,7 +162,7 @@ int utc_year(timestamp_t timestamp)
* at throwing out unused calculations, so this is more efficient * at throwing out unused calculations, so this is more efficient
* than it looks. * than it looks.
*/ */
int utc_weekday(timestamp_t timestamp) extern "C" int utc_weekday(timestamp_t timestamp)
{ {
struct tm tm; struct tm tm;
utc_mkdate(timestamp, &tm); utc_mkdate(timestamp, &tm);
@ -173,7 +174,7 @@ int utc_weekday(timestamp_t timestamp)
* an 64-bit decimal and return 64-bit timestamp. On failure or * an 64-bit decimal and return 64-bit timestamp. On failure or
* if passed an empty string, return 0. * if passed an empty string, return 0.
*/ */
extern timestamp_t parse_datetime(const char *s) extern "C" timestamp_t parse_datetime(const char *s)
{ {
int y, m, d; int y, m, d;
int hr, min, sec; int hr, min, sec;
@ -200,19 +201,19 @@ extern timestamp_t parse_datetime(const char *s)
* Format 64-bit timestamp in the form "YYYY-MM-DD hh:mm:ss". * Format 64-bit timestamp in the form "YYYY-MM-DD hh:mm:ss".
* Returns the empty string for timestamp = 0 * Returns the empty string for timestamp = 0
*/ */
extern char *format_datetime(timestamp_t timestamp) std::string format_datetime(timestamp_t timestamp)
{ {
char buf[32]; char buf[32];
struct tm tm; struct tm tm;
if (!timestamp) if (!timestamp)
return strdup(""); return std::string();
utc_mkdate(timestamp, &tm); utc_mkdate(timestamp, &tm);
snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u:%02u", snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u:%02u",
tm.tm_year, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); tm.tm_year, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
return strdup(buf); return std::string(buf);
} }
/* Turn month (0-12) into three-character short name */ /* Turn month (0-12) into three-character short name */