From dfc5b4972b1483b667e8fb707625796325595493 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Mon, 6 Mar 2023 16:20:31 +0100
Subject: [PATCH] output/httpd/IcyMetaDataServer: use libfmt

---
 src/output/plugins/httpd/HttpdClient.cxx      |  2 +-
 .../plugins/httpd/IcyMetaDataServer.cxx       | 76 +++++++++----------
 .../plugins/httpd/IcyMetaDataServer.hxx       |  4 +-
 3 files changed, 40 insertions(+), 42 deletions(-)

diff --git a/src/output/plugins/httpd/HttpdClient.cxx b/src/output/plugins/httpd/HttpdClient.cxx
index 76f6b74ca..b07b286a2 100644
--- a/src/output/plugins/httpd/HttpdClient.cxx
+++ b/src/output/plugins/httpd/HttpdClient.cxx
@@ -120,7 +120,7 @@ bool
 HttpdClient::SendResponse() noexcept
 {
 	char buffer[1024];
-	AllocatedString allocated;
+	std::string allocated;
 	const char *response;
 
 	assert(state == State::RESPONSE);
diff --git a/src/output/plugins/httpd/IcyMetaDataServer.cxx b/src/output/plugins/httpd/IcyMetaDataServer.cxx
index c08d0509f..5a3d6669c 100644
--- a/src/output/plugins/httpd/IcyMetaDataServer.cxx
+++ b/src/output/plugins/httpd/IcyMetaDataServer.cxx
@@ -3,57 +3,55 @@
 
 #include "IcyMetaDataServer.hxx"
 #include "tag/Tag.hxx"
-#include "util/FormatString.hxx"
-#include "util/AllocatedString.hxx"
 #include "util/TruncateString.hxx"
 
+#include <fmt/core.h>
+
 #include <iterator>
 
-#include <string.h>
-
-AllocatedString
+std::string
 icy_server_metadata_header(const char *name,
 			   const char *genre, const char *url,
 			   const char *content_type, int metaint) noexcept
 {
-	return FormatString("HTTP/1.1 200 OK\r\n"
-			    "icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
-			    "icy-notice2:MPD - The music player daemon<BR>\r\n"
-			    "icy-name: %s\r\n"             /* TODO */
-			    "icy-genre: %s\r\n"            /* TODO */
-			    "icy-url: %s\r\n"              /* TODO */
-			    "icy-pub:1\r\n"
-			    "icy-metaint:%d\r\n"
-			    /* TODO "icy-br:%d\r\n" */
-			    "Content-Type: %s\r\n"
-			    "Connection: close\r\n"
-			    "Pragma: no-cache\r\n"
-			    "Cache-Control: no-cache, no-store\r\n"
-			    "Access-Control-Allow-Origin: *\r\n"
-			    "\r\n",
-			    name,
-			    genre,
-			    url,
-			    metaint,
-			    /* bitrate, */
-			    content_type);
+	return fmt::format("HTTP/1.1 200 OK\r\n"
+			   "icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
+			   "icy-notice2:MPD - The music player daemon<BR>\r\n"
+			   "icy-name: {}\r\n"             /* TODO */
+			   "icy-genre: {}\r\n"            /* TODO */
+			   "icy-url: {}\r\n"              /* TODO */
+			   "icy-pub:1\r\n"
+			   "icy-metaint:%d\r\n"
+			   /* TODO "icy-br:%d\r\n" */
+			   "Content-Type: {}\r\n"
+			   "Connection: close\r\n"
+			   "Pragma: no-cache\r\n"
+			   "Cache-Control: no-cache, no-store\r\n"
+			   "Access-Control-Allow-Origin: *\r\n"
+			   "\r\n",
+			   name,
+			   genre,
+			   url,
+			   metaint,
+			   /* bitrate, */
+			   content_type);
 }
 
-static AllocatedString
+static std::string
 icy_server_metadata_string(const char *stream_title,
 			   const char* stream_url) noexcept
 {
 	// The leading n is a placeholder for the length information
-	auto icy_metadata = FormatString("nStreamTitle='%s';"
-					 "StreamUrl='%s';"
-					 /* pad 15 spaces just in case
-					    the length needs to be
-					    rounded up */
-					 "               ",
-					 stream_title,
-					 stream_url);
+	auto icy_metadata = fmt::format("nStreamTitle='{}';"
+					"StreamUrl='{}';"
+					/* pad 15 spaces just in case
+					   the length needs to be
+					   rounded up */
+					"               ",
+					stream_title,
+					stream_url);
 
-	size_t meta_length = strlen(icy_metadata.c_str());
+	size_t meta_length = icy_metadata.length();
 
 	meta_length--; // subtract placeholder
 
@@ -62,7 +60,7 @@ icy_server_metadata_string(const char *stream_title,
 	icy_metadata[0] = meta_length;
 
 	if (meta_length > 255)
-		return nullptr;
+		return {};
 
 	return icy_metadata;
 }
@@ -95,8 +93,8 @@ icy_server_metadata_page(const Tag &tag, const TagType *types) noexcept
 
 	const auto icy_string = icy_server_metadata_string(stream_title, "");
 
-	if (icy_string == nullptr)
+	if (icy_string.empty())
 		return nullptr;
 
-	return std::make_shared<Page>(std::span{(const std::byte *)icy_string.c_str(), uint8_t(icy_string[0]) * 16U + 1U});
+	return std::make_shared<Page>(std::span{(const std::byte *)icy_string.data(), uint8_t(icy_string[0]) * 16U + 1U});
 }
diff --git a/src/output/plugins/httpd/IcyMetaDataServer.hxx b/src/output/plugins/httpd/IcyMetaDataServer.hxx
index 07d150eac..26f6a06d8 100644
--- a/src/output/plugins/httpd/IcyMetaDataServer.hxx
+++ b/src/output/plugins/httpd/IcyMetaDataServer.hxx
@@ -7,12 +7,12 @@
 #include "Page.hxx"
 
 #include <cstdint>
+#include <string>
 
 enum TagType : uint8_t;
 struct Tag;
-class AllocatedString;
 
-AllocatedString
+std::string
 icy_server_metadata_header(const char *name,
 			   const char *genre, const char *url,
 			   const char *content_type, int metaint) noexcept;