core: use std::string instead of strndup()

Allows us to remove the strndup.h header. This code will be
even more simple, once core is fully converted away from C-strings.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-03-29 08:47:10 +01:00 committed by bstoeger
parent 177a1a6706
commit e7a6de3894
6 changed files with 11 additions and 42 deletions

View file

@ -245,7 +245,6 @@ HEADERS += \
core/sample.h \
core/selection.h \
core/sha1.h \
core/strndup.h \
core/string-format.h \
core/subsurfacestartup.h \
core/subsurfacesysinfo.h \

View file

@ -167,7 +167,6 @@ set(SUBSURFACE_CORE_LIB_SRCS
ssrf.h
statistics.c
statistics.h
strndup.h
string-format.h
string-format.cpp
strtod.c

View file

@ -24,7 +24,6 @@
#include "subsurface-string.h"
#include "format.h"
#include "membuffer.h"
#include "strndup.h"
#include "qthelper.h"
#include "file.h"
#include "errorhelper.h"

View file

@ -7,9 +7,9 @@
#include "dive.h"
#include "divelog.h"
#include "errorhelper.h"
#include "subsurface-string.h"
#include "file.h"
#include "sample.h"
#include "strndup.h"
// Convert bytes into an INT
#define array_uint16_le(p) ((unsigned int) (p)[0] \
@ -175,28 +175,23 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int
// Dive location, assemble Location and Place
unsigned int len, place_len;
char *location;
std::string location;
len = array_uint32_le(buf + ptr);
ptr += 4;
place_len = array_uint32_le(buf + ptr + len);
if (len && place_len) {
location = (char *)malloc(len + place_len + 4);
memset(location, 0, len + place_len + 4);
memcpy(location, buf + ptr, len);
memcpy(location + len, ", ", 2);
memcpy(location + len + 2, buf + ptr + len + 4, place_len);
location = std::string((char *)buf + ptr, len) + ", " +
std::string((char *)buf + ptr + len + 4, place_len);
} else if (len) {
location = strndup((char *)buf + ptr, len);
location = std::string((char *)buf + ptr, len);
} else if (place_len) {
location = strndup((char *)buf + ptr + len + 4, place_len);
location = std::string((char *)buf + ptr + len + 4, place_len);
}
/* Store the location only if we have one */
if (len || place_len) {
add_dive_to_dive_site(dive, find_or_create_dive_site_with_name(location, sites));
free(location);
}
if (!location.empty())
add_dive_to_dive_site(dive, find_or_create_dive_site_with_name(location.c_str(), sites));
ptr += len + 4 + place_len;
@ -205,9 +200,9 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int
ptr += 4;
// Blank notes are better than the default text
if (len && strncmp((char *)buf + ptr, "Comment ...", 11)) {
dive->notes = strndup((char *)buf + ptr, len);
}
std::string notes((char *)buf + ptr, len);
if (!starts_with(notes, "Comment ..."))
dive->notes = strdup(notes.c_str());
ptr += len;
dive->id = array_uint32_le(buf + ptr);

View file

@ -28,7 +28,6 @@
#include "file.h"
#include "membuffer.h"
#include "picture.h"
#include "strndup.h"
#include "git-access.h"
#include "qthelper.h"
#include "gettext.h"

View file

@ -1,22 +0,0 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef STRNDUP_H
#define STRNDUP_H
#if __WIN32__
static char *strndup (const char *s, size_t n)
{
char *cpy;
size_t len = strlen(s);
if (n < len)
len = n;
if ((cpy = (char *)malloc(len + 1)) !=
NULL) {
cpy[len] =
'\0';
memcpy(cpy,
s,
len);
}
return cpy;
}
#endif
#endif /* STRNDUP_H */