diff options
author | Max Kellermann <max@duempel.org> | 2016-04-12 22:27:15 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2016-04-12 22:59:23 +0200 |
commit | 6a1f5667f95f99c4f4457f370abb10172fa7751e (patch) | |
tree | 4a35bbd04a4db80773ee81fe9cf0bb6989bd6439 | |
parent | fab5f58ee058ad7de633c26bb3fb90762bdeb6a9 (diff) |
util/FormatString: return AllocatedString
-rw-r--r-- | src/client/ClientWrite.cxx | 5 | ||||
-rw-r--r-- | src/client/Response.cxx | 6 | ||||
-rw-r--r-- | src/decoder/plugins/GmeDecoderPlugin.cxx | 5 | ||||
-rw-r--r-- | src/decoder/plugins/SidplayDecoderPlugin.cxx | 3 | ||||
-rw-r--r-- | src/output/plugins/httpd/HttpdClient.cxx | 8 | ||||
-rw-r--r-- | src/output/plugins/httpd/IcyMetaDataServer.cxx | 69 | ||||
-rw-r--r-- | src/output/plugins/httpd/IcyMetaDataServer.hxx | 6 | ||||
-rw-r--r-- | src/util/FormatString.cxx | 15 | ||||
-rw-r--r-- | src/util/FormatString.hxx | 14 |
9 files changed, 64 insertions, 67 deletions
diff --git a/src/client/ClientWrite.cxx b/src/client/ClientWrite.cxx index 6ceb7ab5b..f7a4b0363 100644 --- a/src/client/ClientWrite.cxx +++ b/src/client/ClientWrite.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "Client.hxx" #include "util/FormatString.hxx" +#include "util/AllocatedString.hxx" #include <string.h> @@ -45,9 +46,7 @@ client_puts(Client &client, const char *s) void client_vprintf(Client &client, const char *fmt, va_list args) { - char *p = FormatNewV(fmt, args); - client.Write(p); - delete[] p; + client.Write(FormatStringV(fmt, args).c_str()); } void diff --git a/src/client/Response.cxx b/src/client/Response.cxx index b741d7eb5..62aca5ebb 100644 --- a/src/client/Response.cxx +++ b/src/client/Response.cxx @@ -21,6 +21,7 @@ #include "Response.hxx" #include "Client.hxx" #include "util/FormatString.hxx" +#include "util/AllocatedString.hxx" bool Response::Write(const void *data, size_t length) @@ -37,10 +38,7 @@ Response::Write(const char *data) bool Response::FormatV(const char *fmt, va_list args) { - char *p = FormatNewV(fmt, args); - bool success = Write(p); - delete[] p; - return success; + return Write(FormatStringV(fmt, args).c_str()); } bool diff --git a/src/decoder/plugins/GmeDecoderPlugin.cxx b/src/decoder/plugins/GmeDecoderPlugin.cxx index 97e406581..0791f39e0 100644 --- a/src/decoder/plugins/GmeDecoderPlugin.cxx +++ b/src/decoder/plugins/GmeDecoderPlugin.cxx @@ -26,6 +26,7 @@ #include "fs/Path.hxx" #include "fs/AllocatedPath.hxx" #include "util/FormatString.hxx" +#include "util/AllocatedString.hxx" #include "util/UriUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -121,8 +122,8 @@ gme_container_scan(Path path_fs, const unsigned int tnum) const char *subtune_suffix = uri_get_suffix(path_fs.c_str()); if (tnum <= num_songs){ - return FormatNew(SUBTUNE_PREFIX "%03u.%s", - tnum, subtune_suffix); + return FormatString(SUBTUNE_PREFIX "%03u.%s", + tnum, subtune_suffix).Steal(); } else return nullptr; } diff --git a/src/decoder/plugins/SidplayDecoderPlugin.cxx b/src/decoder/plugins/SidplayDecoderPlugin.cxx index 7d6ccff89..50c6ec4c6 100644 --- a/src/decoder/plugins/SidplayDecoderPlugin.cxx +++ b/src/decoder/plugins/SidplayDecoderPlugin.cxx @@ -24,6 +24,7 @@ #include "fs/Path.hxx" #include "fs/AllocatedPath.hxx" #include "util/FormatString.hxx" +#include "util/AllocatedString.hxx" #include "util/Domain.hxx" #include "util/Error.hxx" #include "system/ByteOrder.hxx" @@ -352,7 +353,7 @@ sidplay_container_scan(Path path_fs, const unsigned int tnum) /* Construct container/tune path names, eg. Delta.sid/tune_001.sid */ if(tnum<=info.songs) { - return FormatNew(SUBTUNE_PREFIX "%03u.sid", tnum); + return FormatString(SUBTUNE_PREFIX "%03u.sid", tnum).Steal(); } else return nullptr; } diff --git a/src/output/plugins/httpd/HttpdClient.cxx b/src/output/plugins/httpd/HttpdClient.cxx index e21f2a768..f12f833e2 100644 --- a/src/output/plugins/httpd/HttpdClient.cxx +++ b/src/output/plugins/httpd/HttpdClient.cxx @@ -21,6 +21,7 @@ #include "HttpdClient.hxx" #include "HttpdInternal.hxx" #include "util/ASCII.hxx" +#include "util/AllocatedString.hxx" #include "Page.hxx" #include "IcyMetaDataServer.hxx" #include "net/SocketError.hxx" @@ -141,7 +142,8 @@ HttpdClient::HandleLine(const char *line) bool HttpdClient::SendResponse() { - char buffer[1024], *allocated = nullptr; + char buffer[1024]; + AllocatedString<> allocated = nullptr; const char *response; assert(state == RESPONSE); @@ -162,11 +164,12 @@ HttpdClient::SendResponse() response = buffer; } else if (metadata_requested) { - response = allocated = + allocated = icy_server_metadata_header(httpd.name, httpd.genre, httpd.website, httpd.content_type, metaint); + response = allocated.c_str(); } else { /* revert to a normal HTTP request */ snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\n" @@ -180,7 +183,6 @@ HttpdClient::SendResponse() } ssize_t nbytes = SocketMonitor::Write(response, strlen(response)); - delete[] allocated; if (gcc_unlikely(nbytes < 0)) { const SocketErrorMessage msg; FormatWarning(httpd_output_domain, diff --git a/src/output/plugins/httpd/IcyMetaDataServer.cxx b/src/output/plugins/httpd/IcyMetaDataServer.cxx index 55ba88c96..dccf6c8ee 100644 --- a/src/output/plugins/httpd/IcyMetaDataServer.cxx +++ b/src/output/plugins/httpd/IcyMetaDataServer.cxx @@ -22,48 +22,49 @@ #include "Page.hxx" #include "tag/Tag.hxx" #include "util/FormatString.hxx" +#include "util/AllocatedString.hxx" #include "util/StringUtil.hxx" #include "util/Macros.hxx" #include <string.h> -char* +AllocatedString<> icy_server_metadata_header(const char *name, const char *genre, const char *url, const char *content_type, int metaint) { - return FormatNew("ICY 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" - "\r\n", - name, - genre, - url, - metaint, - /* bitrate, */ - content_type); + return FormatString("ICY 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" + "\r\n", + name, + genre, + url, + metaint, + /* bitrate, */ + content_type); } -static char * +static AllocatedString<> icy_server_metadata_string(const char *stream_title, const char* stream_url) { // The leading n is a placeholder for the length information - char *icy_metadata = FormatNew("nStreamTitle='%s';" - "StreamUrl='%s';", - stream_title, - stream_url); + auto icy_metadata = FormatString("nStreamTitle='%s';" + "StreamUrl='%s';", + stream_title, + stream_url); - size_t meta_length = strlen(icy_metadata); + size_t meta_length = strlen(icy_metadata.c_str()); meta_length--; // subtract placeholder @@ -71,10 +72,8 @@ icy_server_metadata_string(const char *stream_title, const char* stream_url) icy_metadata[0] = meta_length; - if (meta_length > 255) { - delete[] icy_metadata; + if (meta_length > 255) return nullptr; - } return icy_metadata; } @@ -105,14 +104,10 @@ icy_server_metadata_page(const Tag &tag, const TagType *types) p = CopyString(p, " - ", end - p); } - char *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.IsNull()) return nullptr; - Page *icy_metadata = Page::Copy(icy_string, (icy_string[0] * 16) + 1); - - delete[] icy_string; - - return icy_metadata; + return Page::Copy(icy_string.c_str(), (icy_string[0] * 16) + 1); } diff --git a/src/output/plugins/httpd/IcyMetaDataServer.hxx b/src/output/plugins/httpd/IcyMetaDataServer.hxx index c0d2e091a..2cb51a906 100644 --- a/src/output/plugins/httpd/IcyMetaDataServer.hxx +++ b/src/output/plugins/httpd/IcyMetaDataServer.hxx @@ -24,11 +24,9 @@ struct Tag; class Page; +template<typename T> class AllocatedString; -/** - * Free the return value with delete[]. - */ -char* +AllocatedString<char> icy_server_metadata_header(const char *name, const char *genre, const char *url, const char *content_type, int metaint); diff --git a/src/util/FormatString.cxx b/src/util/FormatString.cxx index 819037c74..2619bbf84 100644 --- a/src/util/FormatString.cxx +++ b/src/util/FormatString.cxx @@ -18,6 +18,7 @@ */ #include "FormatString.hxx" +#include "AllocatedString.hxx" #include <stdio.h> #include <stdlib.h> @@ -26,8 +27,8 @@ #include <string.h> #endif -char * -FormatNewV(const char *fmt, va_list args) +AllocatedString<> +FormatStringV(const char *fmt, va_list args) { #ifndef WIN32 va_list tmp; @@ -41,7 +42,7 @@ FormatNewV(const char *fmt, va_list args) char *buffer = new char[length + 1]; vsnprintf(buffer, length + 1, fmt, args); - return buffer; + return AllocatedString<>::Donate(buffer); #else /* On mingw32, snprintf() expects a 64 bit integer instead of a "long int" for "%li". This is not consistent with our @@ -56,16 +57,16 @@ FormatNewV(const char *fmt, va_list args) const size_t length = strlen(buffer); char *p = new char[length + 1]; memcpy(p, buffer, length + 1); - return p; + return AllocatedString<>::Donate(buffer); #endif } -char * -FormatNew(const char *fmt, ...) +AllocatedString<> +FormatString(const char *fmt, ...) { va_list args; va_start(args, fmt); - char *p = FormatNewV(fmt, args); + auto p = FormatStringV(fmt, args); va_end(args); return p; } diff --git a/src/util/FormatString.hxx b/src/util/FormatString.hxx index 8315ca209..a14c4b167 100644 --- a/src/util/FormatString.hxx +++ b/src/util/FormatString.hxx @@ -24,20 +24,22 @@ #include <stdarg.h> +template<typename T> class AllocatedString; + /** * Format into a newly allocated string. The caller frees the return * value with delete[]. */ -gcc_malloc gcc_nonnull_all -char * -FormatNewV(const char *fmt, va_list args); +gcc_nonnull_all +AllocatedString<char> +FormatStringV(const char *fmt, va_list args); /** * Format into a newly allocated string. The caller frees the return * value with delete[]. */ -gcc_malloc gcc_nonnull(1) gcc_printf(1,2) -char * -FormatNew(const char *fmt, ...); +gcc_nonnull(1) gcc_printf(1,2) +AllocatedString<char> +FormatString(const char *fmt, ...); #endif |