diff --git a/libtransmission/announcer-udp.cc b/libtransmission/announcer-udp.cc index 4caddca15..702e4f5fb 100644 --- a/libtransmission/announcer-udp.cc +++ b/libtransmission/announcer-udp.cc @@ -32,6 +32,8 @@ #define dbgmsg(key, ...) tr_logAddDeepNamed(tr_quark_get_string(key), __VA_ARGS__) +using namespace std::literals; + /**** ***** ****/ @@ -221,11 +223,15 @@ static void tau_scrape_request_finished(struct tau_scrape_request const* request } } -static void tau_scrape_request_fail(struct tau_scrape_request* request, bool did_connect, bool did_timeout, char const* errmsg) +static void tau_scrape_request_fail( + struct tau_scrape_request* request, + bool did_connect, + bool did_timeout, + std::string_view errmsg) { request->response.did_connect = did_connect; request->response.did_timeout = did_timeout; - request->response.errmsg = errmsg == nullptr ? "" : errmsg; + request->response.errmsg = errmsg; tau_scrape_request_finished(request); } @@ -369,11 +375,11 @@ static void tau_announce_request_fail( struct tau_announce_request* request, bool did_connect, bool did_timeout, - char const* errmsg) + std::string_view errmsg) { request->response.did_connect = did_connect; request->response.did_timeout = did_timeout; - request->response.errmsg = tr_strdup(errmsg); + request->response.errmsg = tr_strvDup(errmsg); tau_announce_request_finished(request); } @@ -400,10 +406,10 @@ static void on_announce_response(struct tau_announce_request* request, tau_actio } else { - char* const errmsg = action == TAU_ACTION_ERROR && buflen > 0 ? tr_strndup(evbuffer_pullup(buf, -1), buflen) : - tr_strdup(_("Unknown error")); + auto const errmsg = action == TAU_ACTION_ERROR && buflen > 0 ? + std::string_view{ reinterpret_cast(evbuffer_pullup(buf, -1)), buflen } : + _("Unknown error"); tau_announce_request_fail(request, true, false, errmsg); - tr_free(errmsg); } } @@ -460,7 +466,7 @@ static void tau_tracker_free(struct tau_tracker* t) delete t; } -static void tau_tracker_fail_all(struct tau_tracker* tracker, bool did_connect, bool did_timeout, char const* errmsg) +static void tau_tracker_fail_all(struct tau_tracker* tracker, bool did_connect, bool did_timeout, std::string_view errmsg) { /* fail all the scrapes */ tr_ptrArray* reqs = &tracker->scrapes; @@ -495,10 +501,9 @@ static void tau_tracker_on_dns(int errcode, struct evutil_addrinfo* addr, void* if (errcode != 0) { - char* errmsg = tr_strdup_printf(_("DNS Lookup failed: %s"), evutil_gai_strerror(errcode)); - dbgmsg(tracker->key, "%s", errmsg); - tau_tracker_fail_all(tracker, false, false, errmsg); - tr_free(errmsg); + auto const errmsg = tr_strvJoin("DNS Lookup failed: "sv, evutil_gai_strerror(errcode)); + dbgmsg(tracker->key, "%s", errmsg.c_str()); + tau_tracker_fail_all(tracker, false, false, errmsg.c_str()); } else { @@ -621,7 +626,7 @@ static void tau_tracker_timeout_reqs(struct tau_tracker* tracker) if (cancel_all || req->created_at + TauRequestTtl < now) { dbgmsg(tracker->key, "timeout announce req %p", (void*)req); - tau_announce_request_fail(req, false, true, nullptr); + tau_announce_request_fail(req, false, true, ""); tau_announce_request_free(req); tr_ptrArrayRemove(reqs, i); --i; @@ -638,7 +643,7 @@ static void tau_tracker_timeout_reqs(struct tau_tracker* tracker) if (cancel_all || req->created_at + TauRequestTtl < now) { dbgmsg(tracker->key, "timeout scrape req %p", (void*)req); - tau_scrape_request_fail(req, false, true, nullptr); + tau_scrape_request_fail(req, false, true, ""); tau_scrape_request_free(req); tr_ptrArrayRemove(reqs, i); --i; diff --git a/libtransmission/error.cc b/libtransmission/error.cc index a00817666..dc1bcba47 100644 --- a/libtransmission/error.cc +++ b/libtransmission/error.cc @@ -109,53 +109,17 @@ void tr_error_clear(tr_error** error) *error = nullptr; } -static void error_prefix_valist(tr_error** error, char const* prefix_format, va_list args) TR_GNUC_PRINTF(2, 0); - -static void error_prefix_valist(tr_error** error, char const* prefix_format, va_list args) +void tr_error_prefix(tr_error** error, char const* prefix) { - TR_ASSERT(error != nullptr); - TR_ASSERT(*error != nullptr); - TR_ASSERT(prefix_format != nullptr); - - char* prefix = tr_strdup_vprintf(prefix_format, args); - - char* new_message = tr_strdup_printf("%s%s", prefix, (*error)->message); - tr_free((*error)->message); - (*error)->message = new_message; - - tr_free(prefix); -} - -void tr_error_prefix(tr_error** error, char const* prefix_format, ...) -{ - TR_ASSERT(prefix_format != nullptr); + TR_ASSERT(prefix != nullptr); if (error == nullptr || *error == nullptr) { return; } - va_list args; - - va_start(args, prefix_format); - error_prefix_valist(error, prefix_format, args); - va_end(args); -} - -void tr_error_propagate_prefixed(tr_error** new_error, tr_error** old_error, char const* prefix_format, ...) -{ - TR_ASSERT(prefix_format != nullptr); - - tr_error_propagate(new_error, old_error); - - if (new_error == nullptr) - { - return; - } - - va_list args; - - va_start(args, prefix_format); - error_prefix_valist(new_error, prefix_format, args); - va_end(args); + auto* err = *error; + auto* const new_message = tr_strvDup(tr_strvJoin(prefix, err->message)); + tr_free(err->message); + err->message = new_message; } diff --git a/libtransmission/error.h b/libtransmission/error.h index 05ed86d73..93ecbe2e6 100644 --- a/libtransmission/error.h +++ b/libtransmission/error.h @@ -96,21 +96,6 @@ void tr_error_clear(tr_error** error); * @param[in] prefix_format Prefix format string. * @param[in] ... Format arguments. */ -void tr_error_prefix(tr_error** error, char const* prefix_format, ...) TR_GNUC_PRINTF(2, 3); - -/** - * @brief Prefix message and propagate existing error object upwards. - * - * If passed pointer to new error object is not `nullptr`, copy old error object - * to new error object, prefix its message with `printf`-style formatted text, - * and free old error object. Otherwise, just free old error object. - * - * @param[in,out] new_error Pointer to error object to be set. - * @param[in,out] old_error Error object to be propagated. Cleared on return. - * @param[in] prefix_format Prefix format string. - * @param[in] ... Format arguments. - */ -void tr_error_propagate_prefixed(tr_error** new_error, tr_error** old_error, char const* prefix_format, ...) - TR_GNUC_PRINTF(3, 4); +void tr_error_prefix(tr_error** error, char const* prefix); /** @} */ diff --git a/libtransmission/magnet-metainfo.cc b/libtransmission/magnet-metainfo.cc index 40203ffe3..36102aa0f 100644 --- a/libtransmission/magnet-metainfo.cc +++ b/libtransmission/magnet-metainfo.cc @@ -144,7 +144,7 @@ bool tr_magnet_metainfo::parseMagnet(std::string_view magnet_link, tr_error** er auto const parsed = tr_urlParse(magnet_link); if (!parsed || parsed->scheme != "magnet"sv) { - tr_error_set(error, TR_ERROR_EINVAL, "Error parsing URL"); + tr_error_set_literal(error, TR_ERROR_EINVAL, "Error parsing URL"); return false; } diff --git a/libtransmission/makemeta.cc b/libtransmission/makemeta.cc index 119c0a388..5c2b6c150 100644 --- a/libtransmission/makemeta.cc +++ b/libtransmission/makemeta.cc @@ -30,6 +30,8 @@ #include "version.h" #include "web-utils.h" +using namespace std::literals; + /**** ***** ****/ @@ -590,7 +592,7 @@ void tr_makeMetaInfo( } else { - builder->outputFile = tr_strdup_printf("%s.torrent", builder->top); + builder->outputFile = tr_strvDup(tr_strvJoin(builder->top, ".torrent"sv)); } /* enqueue the builder */ diff --git a/libtransmission/metainfo.cc b/libtransmission/metainfo.cc index 2d04f57dd..9c412c8bf 100644 --- a/libtransmission/metainfo.cc +++ b/libtransmission/metainfo.cc @@ -300,7 +300,7 @@ static char* fix_webseed_url(tr_info const* inf, std::string_view url) if (inf->fileCount > 1 && !std::empty(url) && url.back() != '/') { - return tr_strdup_printf("%" TR_PRIsv "/", TR_PRIsv_ARG(url)); + return tr_strvDup(tr_strvJoin(url, "/"sv)); } return tr_strvDup(url); diff --git a/libtransmission/rpc-server.cc b/libtransmission/rpc-server.cc index 09809d9b6..46882e695 100644 --- a/libtransmission/rpc-server.cc +++ b/libtransmission/rpc-server.cc @@ -364,9 +364,8 @@ static void serve_file(struct evhttp_request* req, tr_rpc_server* server, char c if (file == nullptr) { - char* tmp = tr_strdup_printf("%s (%s)", filename, error->message); - send_simple_response(req, HTTP_NOTFOUND, tmp); - tr_free(tmp); + auto const tmp = tr_strvJoin(filename, " ("sv, error->message, ")"sv); + send_simple_response(req, HTTP_NOTFOUND, tmp.c_str()); tr_error_free(error); } else @@ -423,13 +422,11 @@ static void handle_web_client(struct evhttp_request* req, tr_rpc_server* server) } else { - char* filename = tr_strdup_printf( - "%s%s%s", + auto const filename = tr_strvJoin( webClientDir, TR_PATH_DELIMITER_STR, tr_str_is_empty(subpath) ? "index.html" : subpath); - serve_file(req, server, filename); - tr_free(filename); + serve_file(req, server, filename.c_str()); } tr_free(subpath); @@ -493,7 +490,7 @@ static void handle_rpc(struct evhttp_request* req, tr_rpc_server* server) struct rpc_response_data* data = tr_new0(struct rpc_response_data, 1); data->req = req; data->server = server; - tr_rpc_request_exec_uri(server->session, q + 1, TR_BAD_SIZE, rpc_response_func, data); + tr_rpc_request_exec_uri(server->session, q + 1, rpc_response_func, data); return; } } @@ -644,11 +641,11 @@ static void handle_request(struct evhttp_request* req, void* arg) ++server->loginattempts; } - char* unauthuser = tr_strdup_printf( - "

Unauthorized User. %d unsuccessful login attempts.

", - server->loginattempts); - send_simple_response(req, 401, unauthuser); - tr_free(unauthuser); + auto const unauthuser = tr_strvJoin( + "

Unauthorized User. "sv, + std::to_string(server->loginattempts), + " unsuccessful login attempts.

"sv); + send_simple_response(req, 401, unauthuser.c_str()); return; } @@ -673,7 +670,7 @@ static void handle_request(struct evhttp_request* req, void* arg) } else if (!isHostnameAllowed(server, req)) { - char* const tmp = tr_strdup_printf( + char const* const tmp = "

Transmission received your request, but the hostname was unrecognized.

" "

To fix this, choose one of the following options:" "