output/httpd/IcyMetaDataServer: use libfmt

This commit is contained in:
Max Kellermann 2023-03-06 16:20:31 +01:00
parent 18c3c2118d
commit dfc5b4972b
3 changed files with 40 additions and 42 deletions

View File

@ -120,7 +120,7 @@ bool
HttpdClient::SendResponse() noexcept HttpdClient::SendResponse() noexcept
{ {
char buffer[1024]; char buffer[1024];
AllocatedString allocated; std::string allocated;
const char *response; const char *response;
assert(state == State::RESPONSE); assert(state == State::RESPONSE);

View File

@ -3,57 +3,55 @@
#include "IcyMetaDataServer.hxx" #include "IcyMetaDataServer.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include "util/FormatString.hxx"
#include "util/AllocatedString.hxx"
#include "util/TruncateString.hxx" #include "util/TruncateString.hxx"
#include <fmt/core.h>
#include <iterator> #include <iterator>
#include <string.h> std::string
AllocatedString
icy_server_metadata_header(const char *name, icy_server_metadata_header(const char *name,
const char *genre, const char *url, const char *genre, const char *url,
const char *content_type, int metaint) noexcept const char *content_type, int metaint) noexcept
{ {
return FormatString("HTTP/1.1 200 OK\r\n" return fmt::format("HTTP/1.1 200 OK\r\n"
"icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */ "icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
"icy-notice2:MPD - The music player daemon<BR>\r\n" "icy-notice2:MPD - The music player daemon<BR>\r\n"
"icy-name: %s\r\n" /* TODO */ "icy-name: {}\r\n" /* TODO */
"icy-genre: %s\r\n" /* TODO */ "icy-genre: {}\r\n" /* TODO */
"icy-url: %s\r\n" /* TODO */ "icy-url: {}\r\n" /* TODO */
"icy-pub:1\r\n" "icy-pub:1\r\n"
"icy-metaint:%d\r\n" "icy-metaint:%d\r\n"
/* TODO "icy-br:%d\r\n" */ /* TODO "icy-br:%d\r\n" */
"Content-Type: %s\r\n" "Content-Type: {}\r\n"
"Connection: close\r\n" "Connection: close\r\n"
"Pragma: no-cache\r\n" "Pragma: no-cache\r\n"
"Cache-Control: no-cache, no-store\r\n" "Cache-Control: no-cache, no-store\r\n"
"Access-Control-Allow-Origin: *\r\n" "Access-Control-Allow-Origin: *\r\n"
"\r\n", "\r\n",
name, name,
genre, genre,
url, url,
metaint, metaint,
/* bitrate, */ /* bitrate, */
content_type); content_type);
} }
static AllocatedString static std::string
icy_server_metadata_string(const char *stream_title, icy_server_metadata_string(const char *stream_title,
const char* stream_url) noexcept const char* stream_url) noexcept
{ {
// The leading n is a placeholder for the length information // The leading n is a placeholder for the length information
auto icy_metadata = FormatString("nStreamTitle='%s';" auto icy_metadata = fmt::format("nStreamTitle='{}';"
"StreamUrl='%s';" "StreamUrl='{}';"
/* pad 15 spaces just in case /* pad 15 spaces just in case
the length needs to be the length needs to be
rounded up */ rounded up */
" ", " ",
stream_title, stream_title,
stream_url); stream_url);
size_t meta_length = strlen(icy_metadata.c_str()); size_t meta_length = icy_metadata.length();
meta_length--; // subtract placeholder meta_length--; // subtract placeholder
@ -62,7 +60,7 @@ icy_server_metadata_string(const char *stream_title,
icy_metadata[0] = meta_length; icy_metadata[0] = meta_length;
if (meta_length > 255) if (meta_length > 255)
return nullptr; return {};
return icy_metadata; 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, ""); const auto icy_string = icy_server_metadata_string(stream_title, "");
if (icy_string == nullptr) if (icy_string.empty())
return nullptr; 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});
} }

View File

@ -7,12 +7,12 @@
#include "Page.hxx" #include "Page.hxx"
#include <cstdint> #include <cstdint>
#include <string>
enum TagType : uint8_t; enum TagType : uint8_t;
struct Tag; struct Tag;
class AllocatedString;
AllocatedString std::string
icy_server_metadata_header(const char *name, icy_server_metadata_header(const char *name,
const char *genre, const char *url, const char *genre, const char *url,
const char *content_type, int metaint) noexcept; const char *content_type, int metaint) noexcept;