core: remove membufferpp

This the C++ version of membuffer. Since everything is C++, it can
just be made the default.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-05 06:47:28 +02:00 committed by bstoeger
parent db4b972897
commit ead58cd039
13 changed files with 40 additions and 53 deletions

View file

@ -90,7 +90,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall
int i; int i;
bool need_pagebreak = false; bool need_pagebreak = false;
struct membufferpp buf; membuffer buf;
if (plain) { if (plain) {
ssrf = ""; ssrf = "";
@ -280,7 +280,7 @@ void export_depths(const char *filename, bool selected_only)
int i; int i;
const char *unit = NULL; const char *unit = NULL;
struct membufferpp buf; membuffer buf;
for_each_dive (i, dive) { for_each_dive (i, dive) {
if (selected_only && !dive->selected) if (selected_only && !dive->selected)

View file

@ -20,7 +20,7 @@ int verbose;
void report_info(const char *fmt, ...) void report_info(const char *fmt, ...)
{ {
struct membufferpp buf; membuffer buf;
VA_BUF(&buf, fmt); VA_BUF(&buf, fmt);
strip_mb(&buf); strip_mb(&buf);
@ -31,7 +31,7 @@ static void (*error_cb)(char *) = NULL;
int report_error(const char *fmt, ...) int report_error(const char *fmt, ...)
{ {
struct membufferpp buf; membuffer buf;
VA_BUF(&buf, fmt); VA_BUF(&buf, fmt);
strip_mb(&buf); strip_mb(&buf);

View file

@ -346,7 +346,7 @@ static int try_to_git_merge(struct git_info *info, git_reference **local_p, git_
git_commit *local_commit, *remote_commit, *base_commit; git_commit *local_commit, *remote_commit, *base_commit;
git_index *merged_index; git_index *merged_index;
git_merge_options merge_options; git_merge_options merge_options;
struct membufferpp msg; membuffer msg;
if (verbose) { if (verbose) {
char outlocal[41], outremote[41]; char outlocal[41], outremote[41];

View file

@ -12,12 +12,11 @@
#include "units.h" #include "units.h"
#include "membuffer.h" #include "membuffer.h"
membufferpp::membufferpp() membuffer::membuffer()
: membuffer{0, 0, nullptr}
{ {
} }
membufferpp::~membufferpp() membuffer::~membuffer()
{ {
free_buffer(this); free_buffer(this);
} }
@ -144,7 +143,7 @@ void put_vformat(struct membuffer *b, const char *fmt, va_list args)
/* Silly helper using membuffer */ /* Silly helper using membuffer */
char *vformat_string(const char *fmt, va_list args) char *vformat_string(const char *fmt, va_list args)
{ {
struct membuffer mb = { 0 }; struct membuffer mb;
put_vformat(&mb, fmt, args); put_vformat(&mb, fmt, args);
return detach_cstring(&mb); return detach_cstring(&mb);
} }
@ -300,7 +299,7 @@ void put_quoted(struct membuffer *b, const char *text, int is_attribute, int is_
char *add_to_string_va(char *old, const char *fmt, va_list args) char *add_to_string_va(char *old, const char *fmt, va_list args)
{ {
char *res; char *res;
struct membufferpp o, n; membuffer o, n;
put_vformat(&n, fmt, args); put_vformat(&n, fmt, args);
put_format(&o, "%s\n%s", old ?: "", mb_cstring(&n)); put_format(&o, "%s\n%s", old ?: "", mb_cstring(&n));
res = strdup(mb_cstring(&o)); res = strdup(mb_cstring(&o));

View file

@ -4,10 +4,6 @@
* 'membuffer' functions will manage memory allocation avoiding performance * 'membuffer' functions will manage memory allocation avoiding performance
* issues related to superfluous re-allocation. See 'make_room' function * issues related to superfluous re-allocation. See 'make_room' function
* *
* Before using it membuffer struct should be properly initialized
*
* struct membuffer mb = { 0 };
*
* Internal membuffer buffer will not by default contain null terminator, * Internal membuffer buffer will not by default contain null terminator,
* adding it should be done using 'mb_cstring' function * adding it should be done using 'mb_cstring' function
* *
@ -28,10 +24,6 @@
* ptr = detach_cstring(); * ptr = detach_cstring();
* *
* where the caller now has a C string and is supposed to free it. * where the caller now has a C string and is supposed to free it.
*
* Otherwise allocated memory should be freed
*
* free_buffer(&mb);
*/ */
#ifndef MEMBUFFER_H #ifndef MEMBUFFER_H
#define MEMBUFFER_H #define MEMBUFFER_H
@ -42,14 +34,10 @@
#include "units.h" #include "units.h"
struct membuffer { struct membuffer {
unsigned int len, alloc; unsigned int len = 0, alloc = 0;
char *buffer; char *buffer = nullptr;
}; membuffer();
~membuffer();
// In C++ code use this - it automatically frees the buffer, when going out of scope.
struct membufferpp : public membuffer {
membufferpp();
~membufferpp();
}; };
#ifdef __GNUC__ #ifdef __GNUC__

View file

@ -512,7 +512,7 @@ struct dir {
static int tree_insert(git_treebuilder *dir, const char *name, int mkunique, git_oid *id, git_filemode_t mode) static int tree_insert(git_treebuilder *dir, const char *name, int mkunique, git_oid *id, git_filemode_t mode)
{ {
int ret; int ret;
membufferpp uniquename; membuffer uniquename;
if (mkunique && git_treebuilder_get(dir, name)) { if (mkunique && git_treebuilder_get(dir, name)) {
char hex[8]; char hex[8];
@ -559,7 +559,7 @@ static struct dir *new_directory(git_repository *repo, struct dir *parent, struc
static struct dir *mktree(git_repository *repo, struct dir *dir, const char *fmt, ...) static struct dir *mktree(git_repository *repo, struct dir *dir, const char *fmt, ...)
{ {
membufferpp buf; membuffer buf;
VA_BUF(&buf, fmt); VA_BUF(&buf, fmt);
for (auto &subdir: dir->subdirs) { for (auto &subdir: dir->subdirs) {
@ -609,7 +609,7 @@ static int blob_insert(git_repository *repo, struct dir *tree, struct membuffer
{ {
int ret; int ret;
git_oid blob_id; git_oid blob_id;
struct membufferpp name; membuffer name;
ret = git_blob_create_frombuffer(&blob_id, repo, b->buffer, b->len); ret = git_blob_create_frombuffer(&blob_id, repo, b->buffer, b->len);
if (ret) if (ret)
@ -623,7 +623,7 @@ static int blob_insert(git_repository *repo, struct dir *tree, struct membuffer
static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct dive *dive, struct divecomputer *dc, int idx) static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct dive *dive, struct divecomputer *dc, int idx)
{ {
int ret; int ret;
struct membufferpp buf; membuffer buf;
save_dc(&buf, dive, dc); save_dc(&buf, dive, dc);
ret = blob_insert(repo, tree, &buf, "Divecomputer%c%03u", idx ? '-' : 0, idx); ret = blob_insert(repo, tree, &buf, "Divecomputer%c%03u", idx ? '-' : 0, idx);
@ -635,7 +635,7 @@ static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct
static int save_one_picture(git_repository *repo, struct dir *dir, struct picture *pic) static int save_one_picture(git_repository *repo, struct dir *dir, struct picture *pic)
{ {
int offset = pic->offset.seconds; int offset = pic->offset.seconds;
struct membufferpp buf; membuffer buf;
char sign = '+'; char sign = '+';
unsigned h; unsigned h;
@ -669,7 +669,7 @@ static int save_pictures(git_repository *repo, struct dir *dir, struct dive *div
static int save_one_dive(git_repository *repo, struct dir *tree, struct dive *dive, struct tm *tm, bool cached_ok) static int save_one_dive(git_repository *repo, struct dir *tree, struct dive *dive, struct tm *tm, bool cached_ok)
{ {
struct divecomputer *dc; struct divecomputer *dc;
struct membufferpp buf, name; membuffer buf, name;
struct dir *subdir; struct dir *subdir;
int ret, nr; int ret, nr;
@ -769,7 +769,7 @@ static int save_trip_description(git_repository *repo, struct dir *dir, dive_tri
{ {
int ret; int ret;
git_oid blob_id; git_oid blob_id;
struct membufferpp desc; membuffer desc;
put_format(&desc, "date %04u-%02u-%02u\n", put_format(&desc, "date %04u-%02u-%02u\n",
tm->tm_year, tm->tm_mon + 1, tm->tm_mday); tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
@ -809,7 +809,7 @@ static int save_one_trip(git_repository *repo, struct dir *tree, dive_trip_t *tr
int i; int i;
struct dive *dive; struct dive *dive;
struct dir *subdir; struct dir *subdir;
struct membufferpp name; membuffer name;
timestamp_t first, last; timestamp_t first, last;
/* Create trip directory */ /* Create trip directory */
@ -890,7 +890,7 @@ static void save_one_fingerprint(struct membuffer *b, int i)
static void save_settings(git_repository *repo, struct dir *tree) static void save_settings(git_repository *repo, struct dir *tree)
{ {
struct membufferpp b; membuffer b;
put_format(&b, "version %d\n", DATAFORMAT_VERSION); put_format(&b, "version %d\n", DATAFORMAT_VERSION);
for (int i = 0; i < nr_devices(divelog.devices); i++) for (int i = 0; i < nr_devices(divelog.devices); i++)
@ -918,15 +918,15 @@ static void save_settings(git_repository *repo, struct dir *tree)
static void save_divesites(git_repository *repo, struct dir *tree) static void save_divesites(git_repository *repo, struct dir *tree)
{ {
struct dir *subdir; struct dir *subdir;
struct membufferpp dirname; membuffer dirname;
put_format(&dirname, "01-Divesites"); put_format(&dirname, "01-Divesites");
subdir = new_directory(repo, tree, &dirname); subdir = new_directory(repo, tree, &dirname);
purge_empty_dive_sites(divelog.sites); purge_empty_dive_sites(divelog.sites);
for (int i = 0; i < divelog.sites->nr; i++) { for (int i = 0; i < divelog.sites->nr; i++) {
struct membufferpp b; membuffer b;
struct dive_site *ds = get_dive_site(i, divelog.sites); struct dive_site *ds = get_dive_site(i, divelog.sites);
struct membufferpp site_file_name; membuffer site_file_name;
put_format(&site_file_name, "Site-%08x", ds->uuid); put_format(&site_file_name, "Site-%08x", ds->uuid);
show_utf8(&b, "name ", ds->name.c_str(), "\n"); show_utf8(&b, "name ", ds->name.c_str(), "\n");
show_utf8(&b, "description ", ds->description.c_str(), "\n"); show_utf8(&b, "description ", ds->description.c_str(), "\n");
@ -997,15 +997,15 @@ static void format_one_filter_preset(int preset_id, struct membuffer *b)
static void save_filter_presets(git_repository *repo, struct dir *tree) static void save_filter_presets(git_repository *repo, struct dir *tree)
{ {
struct membufferpp dirname; membuffer dirname;
struct dir *filter_dir; struct dir *filter_dir;
put_format(&dirname, "02-Filterpresets"); put_format(&dirname, "02-Filterpresets");
filter_dir = new_directory(repo, tree, &dirname); filter_dir = new_directory(repo, tree, &dirname);
for (int i = 0; i < filter_presets_count(); i++) for (int i = 0; i < filter_presets_count(); i++)
{ {
membufferpp preset_name; membuffer preset_name;
membufferpp preset_buffer; membuffer preset_buffer;
put_format(&preset_name, "Preset-%03d", i); put_format(&preset_name, "Preset-%03d", i);
format_one_filter_preset(i, &preset_buffer); format_one_filter_preset(i, &preset_buffer);
@ -1221,7 +1221,7 @@ static int create_new_commit(struct git_info *info, git_oid *tree_id, bool creat
/* Else we do want to create the new branch, but with the old commit */ /* Else we do want to create the new branch, but with the old commit */
commit = (git_commit *) parent; commit = (git_commit *) parent;
} else { } else {
struct membufferpp commit_msg; membuffer commit_msg;
create_commit_message(&commit_msg, create_empty); create_commit_message(&commit_msg, create_empty);
if (git_commit_create_v(&commit_id, info->repo, NULL, author, author, NULL, mb_cstring(&commit_msg), tree, parent != NULL, parent)) { if (git_commit_create_v(&commit_id, info->repo, NULL, author, author, NULL, mb_cstring(&commit_msg), tree, parent != NULL, parent)) {

View file

@ -482,7 +482,7 @@ void export_HTML(const char *file_name, const char *photos_dir, const bool selec
{ {
FILE *f; FILE *f;
struct membufferpp buf; membuffer buf;
export_list(&buf, photos_dir, selected_only, list_only); export_list(&buf, photos_dir, selected_only, list_only);
f = subsurface_fopen(file_name, "w+"); f = subsurface_fopen(file_name, "w+");
@ -498,7 +498,7 @@ void export_translation(const char *file_name)
{ {
FILE *f; FILE *f;
struct membufferpp buf; membuffer buf;
//export translated words here //export translated words here
put_format(&buf, "translate={"); put_format(&buf, "translate={");

View file

@ -239,7 +239,7 @@ void save_subtitles_buffer(struct membuffer *b, struct dive *dive, int offset, i
int save_profiledata(const char *filename, bool select_only) int save_profiledata(const char *filename, bool select_only)
{ {
struct membufferpp buf; membuffer buf;
FILE *f; FILE *f;
int error = 0; int error = 0;

View file

@ -558,7 +558,7 @@ void save_one_dive_to_mb(struct membuffer *b, struct dive *dive, bool anonymize)
int save_dive(FILE *f, struct dive *dive, bool anonymize) int save_dive(FILE *f, struct dive *dive, bool anonymize)
{ {
struct membufferpp buf; membuffer buf;
save_one_dive_to_mb(&buf, dive, anonymize); save_one_dive_to_mb(&buf, dive, anonymize);
flush_buffer(&buf, f); flush_buffer(&buf, f);
@ -812,7 +812,7 @@ static void try_to_backup(const char *filename)
int save_dives_logic(const char *filename, const bool select_only, bool anonymize) int save_dives_logic(const char *filename, const bool select_only, bool anonymize)
{ {
struct membufferpp buf; membuffer buf;
struct git_info info; struct git_info info;
FILE *f; FILE *f;
int error = 0; int error = 0;
@ -854,7 +854,7 @@ int export_dives_xslt(const char *filename, const bool selected, const int units
static int export_dives_xslt_doit(const char *filename, struct xml_params *params, bool selected, int units, const char *export_xslt, bool anonymize) static int export_dives_xslt_doit(const char *filename, struct xml_params *params, bool selected, int units, const char *export_xslt, bool anonymize)
{ {
FILE *f; FILE *f;
struct membufferpp buf; membuffer buf;
xmlDoc *doc; xmlDoc *doc;
xsltStylesheetPtr xslt = NULL; xsltStylesheetPtr xslt = NULL;
xmlDoc *transformed; xmlDoc *transformed;
@ -933,7 +933,7 @@ static void save_dive_sites_buffer(struct membuffer *b, const struct dive_site *
int save_dive_sites_logic(const char *filename, const struct dive_site *sites[], int nr_sites, bool anonymize) int save_dive_sites_logic(const char *filename, const struct dive_site *sites[], int nr_sites, bool anonymize)
{ {
struct membufferpp buf; membuffer buf;
FILE *f; FILE *f;
int error = 0; int error = 0;

View file

@ -95,7 +95,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
char *membuf; char *membuf;
xmlDoc *transformed; xmlDoc *transformed;
struct zip_source *s; struct zip_source *s;
struct membufferpp mb; membuffer mb;
struct xml_params *params = alloc_xml_params(); struct xml_params *params = alloc_xml_params();
/* /*

View file

@ -27,7 +27,7 @@ uploadDiveShare::uploadDiveShare():
void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic) void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic)
{ {
//generate json //generate json
struct membufferpp buf; membuffer buf;
export_list(&buf, NULL, selected, false); export_list(&buf, NULL, selected, false);
QByteArray json_data(buf.buffer, buf.len); QByteArray json_data(buf.buffer, buf.len);

View file

@ -110,7 +110,7 @@ void export_worldmap_HTML(const char *file_name, const bool selected_only)
{ {
FILE *f; FILE *f;
struct membufferpp buf; membuffer buf;
export_doit(&buf, selected_only); export_doit(&buf, selected_only);
f = subsurface_fopen(file_name, "w+"); f = subsurface_fopen(file_name, "w+");

View file

@ -131,7 +131,7 @@ void TabDivePhotos::saveSubtitles()
// Only videos have non-zero duration // Only videos have non-zero duration
if (!duration) if (!duration)
continue; continue;
struct membufferpp b; membuffer b;
save_subtitles_buffer(&b, parent.currentDive, offset, duration); save_subtitles_buffer(&b, parent.currentDive, offset, duration);
const char *data = mb_cstring(&b); const char *data = mb_cstring(&b);
subtitlefile.open(QIODevice::WriteOnly); subtitlefile.open(QIODevice::WriteOnly);