From e7a6de3894ac647c706b94875a819d9f3cf43536 Mon Sep 17 00:00:00 2001
From: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Date: Fri, 29 Mar 2024 08:47:10 +0100
Subject: [PATCH] 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>
---
 Subsurface-mobile.pro |  1 -
 core/CMakeLists.txt   |  1 -
 core/git-access.cpp   |  1 -
 core/liquivision.cpp  | 27 +++++++++++----------------
 core/save-xml.cpp     |  1 -
 core/strndup.h        | 22 ----------------------
 6 files changed, 11 insertions(+), 42 deletions(-)
 delete mode 100644 core/strndup.h

diff --git a/Subsurface-mobile.pro b/Subsurface-mobile.pro
index c64dacf4f..88e1d3ae2 100644
--- a/Subsurface-mobile.pro
+++ b/Subsurface-mobile.pro
@@ -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 \
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 7a38d82d8..b4cc55723 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -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
diff --git a/core/git-access.cpp b/core/git-access.cpp
index de50bc987..1c9678c98 100644
--- a/core/git-access.cpp
+++ b/core/git-access.cpp
@@ -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"
diff --git a/core/liquivision.cpp b/core/liquivision.cpp
index 5d4b5e6a9..f748a2de6 100644
--- a/core/liquivision.cpp
+++ b/core/liquivision.cpp
@@ -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);
diff --git a/core/save-xml.cpp b/core/save-xml.cpp
index d0d095f94..e053db15c 100644
--- a/core/save-xml.cpp
+++ b/core/save-xml.cpp
@@ -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"
diff --git a/core/strndup.h b/core/strndup.h
deleted file mode 100644
index 2f6891757..000000000
--- a/core/strndup.h
+++ /dev/null
@@ -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 */