Make some utility functions return std::string to simplify code (#2368)

* tr_formatter_speed_KBps returns std::string

* tr_formatter_mem_B returns std::string

* tr_formatter_size_B returns std::string

* tr_strpercent/tr_strratio returns std::string
This commit is contained in:
Mike Gelfand 2021-12-31 00:30:21 +00:00 committed by GitHub
parent 26110d5c8e
commit d8d361e491
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 146 additions and 252 deletions

View File

@ -162,12 +162,7 @@ static void getStatusStr(tr_stat const* st, char* buf, size_t buflen)
}
else if (st->activity == TR_STATUS_DOWNLOAD)
{
char upStr[80];
char dnStr[80];
char ratioStr[80];
tr_formatter_speed_KBps(upStr, st->pieceUploadSpeed_KBps, sizeof(upStr));
tr_formatter_speed_KBps(dnStr, st->pieceDownloadSpeed_KBps, sizeof(dnStr));
tr_strlratio(ratioStr, st->ratio, sizeof(ratioStr));
tr_snprintf(
@ -177,17 +172,14 @@ static void getStatusStr(tr_stat const* st, char* buf, size_t buflen)
tr_truncd(100 * st->percentDone, 1),
st->peersSendingToUs,
st->peersConnected,
dnStr,
tr_formatter_speed_KBps(st->pieceDownloadSpeed_KBps).c_str(),
st->peersGettingFromUs,
upStr,
tr_formatter_speed_KBps(st->pieceUploadSpeed_KBps).c_str(),
ratioStr);
}
else if (st->activity == TR_STATUS_SEED)
{
char upStr[80];
char ratioStr[80];
tr_formatter_speed_KBps(upStr, st->pieceUploadSpeed_KBps, sizeof(upStr));
tr_strlratio(ratioStr, st->ratio, sizeof(ratioStr));
tr_snprintf(
@ -196,7 +188,7 @@ static void getStatusStr(tr_stat const* st, char* buf, size_t buflen)
"Seeding, uploading to %d of %d peer(s), %s [%s]",
st->peersGettingFromUs,
st->peersConnected,
upStr,
tr_formatter_speed_KBps(st->pieceUploadSpeed_KBps).c_str(),
ratioStr);
}
else

View File

@ -841,13 +841,11 @@ void DetailsDialog::Impl::refreshInfo(std::vector<tr_torrent*> const& torrents)
}
else if (pieceSize >= 0)
{
char piecebuf[128];
tr_formatter_mem_B(piecebuf, pieceSize, sizeof(piecebuf));
str = gtr_sprintf(
ngettext("%1$s (%2$'d piece @ %3$s)", "%1$s (%2$'d pieces @ %3$s)", pieces),
sizebuf,
pieces,
piecebuf);
tr_formatter_mem_B(pieceSize));
}
else
{
@ -1251,8 +1249,8 @@ void initPeerRow(Gtk::TreeIter const& iter, std::string const& key, std::string
void refreshPeerRow(Gtk::TreeIter const& iter, tr_peer_stat const* peer)
{
char up_speed[64] = { '\0' };
char down_speed[64] = { '\0' };
std::string up_speed;
std::string down_speed;
std::string up_count;
std::string down_count;
std::string blocks_to_peer;
@ -1264,12 +1262,12 @@ void refreshPeerRow(Gtk::TreeIter const& iter, tr_peer_stat const* peer)
if (peer->rateToPeer_KBps > 0.01)
{
tr_formatter_speed_KBps(up_speed, peer->rateToPeer_KBps, sizeof(up_speed));
up_speed = tr_formatter_speed_KBps(peer->rateToPeer_KBps);
}
if (peer->rateToClient_KBps > 0)
{
tr_formatter_speed_KBps(down_speed, peer->rateToClient_KBps, sizeof(down_speed));
down_speed = tr_formatter_speed_KBps(peer->rateToClient_KBps);
}
if (peer->pendingReqsToPeer > 0)
@ -1448,14 +1446,10 @@ void DetailsDialog::Impl::refreshWebseedList(std::vector<tr_torrent*> const& tor
auto const iter = store->get_iter(hash.at(key).get_path());
auto const KBps = double(webseed.download_bytes_per_second) / speed_K;
auto buf = std::array<char, 128>{};
if (webseed.is_downloading)
{
tr_formatter_speed_KBps(std::data(buf), KBps, std::size(buf));
}
auto const buf = webseed.is_downloading ? tr_formatter_speed_KBps(KBps) : std::string();
(*iter)[webseed_cols.download_rate_double] = KBps;
(*iter)[webseed_cols.download_rate_string] = std::data(buf);
(*iter)[webseed_cols.download_rate_string] = buf;
(*iter)[webseed_cols.was_updated] = true;
}
}

View File

@ -236,10 +236,8 @@ void MainWindow::Impl::syncAltSpeedButton()
bool const b = gtr_pref_flag_get(TR_KEY_alt_speed_enabled);
char const* const stock = b ? "alt-speed-on" : "alt-speed-off";
char u[32];
tr_formatter_speed_KBps(u, gtr_pref_int_get(TR_KEY_alt_speed_up), sizeof(u));
char d[32];
tr_formatter_speed_KBps(d, gtr_pref_int_get(TR_KEY_alt_speed_down), sizeof(d));
auto const u = tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_alt_speed_up));
auto const d = tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_alt_speed_down));
auto const str = b ? gtr_sprintf(_("Click to disable Alternative Speed Limits\n (%1$s down, %2$s up)"), d, u) :
gtr_sprintf(_("Click to enable Alternative Speed Limits\n (%1$s down, %2$s up)"), d, u);
@ -303,9 +301,7 @@ Gtk::Menu* MainWindow::Impl::createSpeedMenu(tr_direction dir)
for (auto const speed : speeds_KBps)
{
char buf[128];
tr_formatter_speed_KBps(buf, speed, sizeof(buf));
auto* w = Gtk::make_managed<Gtk::MenuItem>(buf);
auto* w = Gtk::make_managed<Gtk::MenuItem>(tr_formatter_speed_KBps(speed));
w->signal_activate().connect([this, dir, speed]() { onSpeedSet(dir, speed); });
m->append(*w);
}
@ -387,16 +383,16 @@ Gtk::Menu* MainWindow::Impl::createOptionsMenu()
void MainWindow::Impl::onOptionsClicked(Gtk::Button* button)
{
char buf1[512];
tr_formatter_speed_KBps(buf1, gtr_pref_int_get(TR_KEY_speed_limit_down), sizeof(buf1));
gtr_label_set_text(*static_cast<Gtk::Label*>(speedlimit_on_item_[TR_DOWN]->get_child()), buf1);
gtr_label_set_text(
*static_cast<Gtk::Label*>(speedlimit_on_item_[TR_DOWN]->get_child()),
tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_speed_limit_down)));
(gtr_pref_flag_get(TR_KEY_speed_limit_down_enabled) ? speedlimit_on_item_[TR_DOWN] : speedlimit_off_item_[TR_DOWN])
->set_active(true);
tr_formatter_speed_KBps(buf1, gtr_pref_int_get(TR_KEY_speed_limit_up), sizeof(buf1));
gtr_label_set_text(*static_cast<Gtk::Label*>(speedlimit_on_item_[TR_UP]->get_child()), buf1);
gtr_label_set_text(
*static_cast<Gtk::Label*>(speedlimit_on_item_[TR_UP]->get_child()),
tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_speed_limit_up)));
(gtr_pref_flag_get(TR_KEY_speed_limit_up_enabled) ? speedlimit_on_item_[TR_UP] : speedlimit_off_item_[TR_UP])
->set_active(true);
@ -649,7 +645,6 @@ void MainWindow::Impl::updateSpeeds()
if (session != nullptr)
{
char speed_str[128];
double upSpeed = 0;
double downSpeed = 0;
int upCount = 0;
@ -664,12 +659,10 @@ void MainWindow::Impl::updateSpeeds()
downCount += row.get_value(torrent_cols.active_peers_down);
}
tr_formatter_speed_KBps(speed_str, downSpeed, sizeof(speed_str));
dl_lb_->set_text(gtr_sprintf("%s %s", speed_str, gtr_get_unicode_string(GTR_UNICODE_DOWN)));
dl_lb_->set_text(gtr_sprintf("%s %s", tr_formatter_speed_KBps(downSpeed), gtr_get_unicode_string(GTR_UNICODE_DOWN)));
dl_lb_->set_visible(downCount > 0);
tr_formatter_speed_KBps(speed_str, upSpeed, sizeof(speed_str));
ul_lb_->set_text(gtr_sprintf("%s %s", speed_str, gtr_get_unicode_string(GTR_UNICODE_UP)));
ul_lb_->set_text(gtr_sprintf("%s %s", tr_formatter_speed_KBps(upSpeed), gtr_get_unicode_string(GTR_UNICODE_UP)));
ul_lb_->set_visible(downCount > 0 || upCount > 0);
}
}

View File

@ -338,13 +338,10 @@ void MakeDialog::Impl::updatePiecesLabel()
tr_strlsize(builder_->totalSize),
builder_->fileCount);
gstr += "; ";
char buf[128];
tr_formatter_mem_B(buf, builder_->pieceSize, sizeof(buf));
gstr += gtr_sprintf(
ngettext("%1$'d Piece @ %2$s", "%1$'d Pieces @ %2$s", builder_->pieceCount),
builder_->pieceCount,
buf);
tr_formatter_mem_B(builder_->pieceSize));
}
gstr += "</i>";

View File

@ -92,51 +92,31 @@ void SystemTrayIcon::Impl::refresh()
{
double KBps;
double limit;
char up[64];
Glib::ustring upLimit;
char down[64];
Glib::ustring downLimit;
char const* idle = _("Idle");
char const* const idle = _("Idle");
auto* session = core_->get_session();
/* up */
KBps = tr_sessionGetRawSpeed_KBps(session, TR_UP);
if (KBps < 0.001)
{
g_strlcpy(up, idle, sizeof(up));
}
else
{
tr_formatter_speed_KBps(up, KBps, sizeof(up));
}
auto const up = KBps < 0.001 ? idle : tr_formatter_speed_KBps(KBps);
/* up limit */
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_UP, &limit))
{
char buf[64];
tr_formatter_speed_KBps(buf, limit, sizeof(buf));
upLimit = gtr_sprintf(_(" (Limit: %s)"), buf);
upLimit = gtr_sprintf(_(" (Limit: %s)"), tr_formatter_speed_KBps(limit));
}
/* down */
KBps = tr_sessionGetRawSpeed_KBps(session, TR_DOWN);
if (KBps < 0.001)
{
g_strlcpy(down, idle, sizeof(down));
}
else
{
tr_formatter_speed_KBps(down, KBps, sizeof(down));
}
auto const down = KBps < 0.001 ? idle : tr_formatter_speed_KBps(KBps);
/* down limit */
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_DOWN, &limit))
{
char buf[64];
tr_formatter_speed_KBps(buf, limit, sizeof(buf));
downLimit = gtr_sprintf(_(" (Limit: %s)"), buf);
downLimit = gtr_sprintf(_(" (Limit: %s)"), tr_formatter_speed_KBps(limit));
}
/* %1$s: current upload speed

View File

@ -153,26 +153,18 @@ Glib::ustring getShortTransferString(
if (haveDown)
{
char dnStr[32];
char upStr[32];
tr_formatter_speed_KBps(dnStr, downloadSpeed_KBps, sizeof(dnStr));
tr_formatter_speed_KBps(upStr, uploadSpeed_KBps, sizeof(upStr));
/* down speed, down symbol, up speed, up symbol */
buf += gtr_sprintf(
_("%1$s %2$s %3$s %4$s"),
dnStr,
tr_formatter_speed_KBps(downloadSpeed_KBps),
gtr_get_unicode_string(GTR_UNICODE_DOWN),
upStr,
tr_formatter_speed_KBps(uploadSpeed_KBps),
gtr_get_unicode_string(GTR_UNICODE_UP));
}
else if (haveUp)
{
char upStr[32];
tr_formatter_speed_KBps(upStr, uploadSpeed_KBps, sizeof(upStr));
/* up speed, up symbol */
buf += gtr_sprintf(_("%1$s %2$s"), upStr, gtr_get_unicode_string(GTR_UNICODE_UP));
buf += gtr_sprintf(_("%1$s %2$s"), tr_formatter_speed_KBps(uploadSpeed_KBps), gtr_get_unicode_string(GTR_UNICODE_UP));
}
else if (st->isStalled)
{

View File

@ -78,25 +78,17 @@ Glib::ustring gtr_get_unicode_string(int i)
Glib::ustring tr_strlratio(double ratio)
{
std::array<char, 64> buf = {};
return tr_strratio(buf.data(), buf.size(), ratio, gtr_get_unicode_string(GTR_UNICODE_INF).c_str());
return tr_strratio(ratio, gtr_get_unicode_string(GTR_UNICODE_INF).c_str());
}
Glib::ustring tr_strlpercent(double x)
{
std::array<char, 64> buf = {};
return tr_strpercent(buf.data(), x, buf.size());
return tr_strpercent(x);
}
Glib::ustring tr_strlsize(guint64 bytes)
{
if (bytes == 0)
{
return Q_("None");
}
std::array<char, 64> buf = {};
return tr_formatter_size_B(buf.data(), bytes, buf.size());
return bytes == 0 ? Q_("None") : tr_formatter_size_B(bytes);
}
Glib::ustring tr_strltime(time_t seconds)

View File

@ -248,13 +248,14 @@ static int getMaxBlocks(int64_t max_bytes)
int tr_cacheSetLimit(tr_cache* cache, int64_t max_bytes)
{
char buf[128];
cache->max_bytes = max_bytes;
cache->max_blocks = getMaxBlocks(max_bytes);
tr_formatter_mem_B(buf, cache->max_bytes, sizeof(buf));
tr_logAddNamedDbg(MY_NAME, "Maximum cache size set to %s (%d blocks)", buf, cache->max_blocks);
tr_logAddNamedDbg(
MY_NAME,
"Maximum cache size set to %s (%d blocks)",
tr_formatter_mem_B(cache->max_bytes).c_str(),
cache->max_blocks);
return cacheTrim(cache);
}

View File

@ -200,11 +200,10 @@ bool tr_metaInfoBuilderSetPieceSize(tr_metainfo_builder* b, uint32_t bytes)
{
if (!isValidPieceSize(bytes))
{
char wanted[32];
char gotten[32];
tr_formatter_mem_B(wanted, bytes, sizeof(wanted));
tr_formatter_mem_B(gotten, b->pieceSize, sizeof(gotten));
tr_logAddError(_("Failed to set piece size to %s, leaving it at %s"), wanted, gotten);
tr_logAddError(
_("Failed to set piece size to %s, leaving it at %s"),
tr_formatter_mem_B(bytes).c_str(),
tr_formatter_mem_B(b->pieceSize).c_str());
return false;
}

View File

@ -1181,36 +1181,37 @@ static char* tr_strtruncd(char* buf, double x, int precision, size_t buflen)
return buf;
}
char* tr_strpercent(char* buf, double x, size_t buflen)
std::string tr_strpercent(double x)
{
auto buf = std::array<char, 64>{};
if (x < 100.0)
{
tr_strtruncd(buf, x, 1, buflen);
tr_strtruncd(std::data(buf), x, 1, std::size(buf));
}
else
{
tr_strtruncd(buf, x, 0, buflen);
tr_strtruncd(std::data(buf), x, 0, std::size(buf));
}
return buf;
return std::data(buf);
}
char* tr_strratio(char* buf, size_t buflen, double ratio, char const* infinity)
std::string tr_strratio(double ratio, char const* infinity)
{
if ((int)ratio == TR_RATIO_NA)
{
tr_strlcpy(buf, _("None"), buflen);
}
else if ((int)ratio == TR_RATIO_INF)
{
tr_strlcpy(buf, infinity, buflen);
}
else
{
tr_strpercent(buf, ratio, buflen);
return _("None");
}
return buf;
if ((int)ratio == TR_RATIO_INF)
{
auto buf = std::array<char, 64>{};
tr_strlcpy(std::data(buf), infinity, std::size(buf));
return std::data(buf);
}
return tr_strpercent(ratio);
}
/***
@ -1420,9 +1421,10 @@ void tr_formatter_size_init(uint64_t kilo, char const* kb, char const* mb, char
formatter_init(size_units, kilo, kb, mb, gb, tb);
}
char* tr_formatter_size_B(char* buf, uint64_t bytes, size_t buflen)
std::string tr_formatter_size_B(uint64_t bytes)
{
return formatter_get_size_str(size_units, buf, bytes, buflen);
auto buf = std::array<char, 64>{};
return formatter_get_size_str(size_units, std::data(buf), bytes, std::size(buf));
}
static formatter_units speed_units;
@ -1435,11 +1437,13 @@ void tr_formatter_speed_init(size_t kilo, char const* kb, char const* mb, char c
formatter_init(speed_units, kilo, kb, mb, gb, tb);
}
char* tr_formatter_speed_KBps(char* buf, double KBps, size_t buflen)
std::string tr_formatter_speed_KBps(double KBps)
{
auto buf = std::array<char, 64>{};
if (auto speed = KBps; speed <= 999.95) /* 0.0 KB to 999.9 KB */
{
tr_snprintf(buf, buflen, "%d %s", (int)speed, speed_units[TR_FMT_KB].name);
tr_snprintf(std::data(buf), std::size(buf), "%d %s", (int)speed, speed_units[TR_FMT_KB].name);
}
else
{
@ -1449,19 +1453,19 @@ char* tr_formatter_speed_KBps(char* buf, double KBps, size_t buflen)
if (speed <= 99.995) /* 0.98 MB to 99.99 MB */
{
tr_snprintf(buf, buflen, "%.2f %s", speed, speed_units[TR_FMT_MB].name);
tr_snprintf(std::data(buf), std::size(buf), "%.2f %s", speed, speed_units[TR_FMT_MB].name);
}
else if (speed <= 999.95) /* 100.0 MB to 999.9 MB */
{
tr_snprintf(buf, buflen, "%.1f %s", speed, speed_units[TR_FMT_MB].name);
tr_snprintf(std::data(buf), std::size(buf), "%.1f %s", speed, speed_units[TR_FMT_MB].name);
}
else
{
tr_snprintf(buf, buflen, "%.1f %s", speed / K, speed_units[TR_FMT_GB].name);
tr_snprintf(std::data(buf), std::size(buf), "%.1f %s", speed / K, speed_units[TR_FMT_GB].name);
}
}
return buf;
return std::data(buf);
}
static formatter_units mem_units;
@ -1474,9 +1478,10 @@ void tr_formatter_mem_init(size_t kilo, char const* kb, char const* mb, char con
formatter_init(mem_units, kilo, kb, mb, gb, tb);
}
char* tr_formatter_mem_B(char* buf, size_t bytes_per_second, size_t buflen)
std::string tr_formatter_mem_B(size_t bytes_per_second)
{
return formatter_get_size_str(mem_units, buf, bytes_per_second, buflen);
auto buf = std::array<char, 64>{};
return formatter_get_size_str(mem_units, std::data(buf), bytes_per_second, std::size(buf));
}
void tr_formatter_get_units(void* vdict)

View File

@ -422,15 +422,13 @@ std::vector<int> tr_parseNumberRange(std::string_view str);
double tr_truncd(double x, int decimal_places);
/* return a percent formatted string of either x.xx, xx.x or xxx */
char* tr_strpercent(char* buf, double x, size_t buflen);
std::string tr_strpercent(double x);
/**
* @param buf the buffer to write the string to
* @param buflen buf's size
* @param ratio the ratio to convert to a string
* @param infinity the string represntation of "infinity"
*/
char* tr_strratio(char* buf, size_t buflen, double ratio, char const* infinity) TR_GNUC_NONNULL(1, 4);
std::string tr_strratio(double ratio, char const* infinity);
/** @brief Portability wrapper for localtime_r() that uses the system implementation if available */
struct tm* tr_localtime_r(time_t const* _clock, struct tm* _result);
@ -501,19 +499,19 @@ extern size_t tr_mem_K;
extern uint64_t tr_size_K; /* unused? */
/* format a speed from KBps into a user-readable string. */
char* tr_formatter_speed_KBps(char* buf, double KBps, size_t buflen);
std::string tr_formatter_speed_KBps(double KBps);
/* format a memory size from bytes into a user-readable string. */
char* tr_formatter_mem_B(char* buf, size_t bytes, size_t buflen);
std::string tr_formatter_mem_B(size_t bytes);
/* format a memory size from MB into a user-readable string. */
static inline char* tr_formatter_mem_MB(char* buf, double MBps, size_t buflen)
static inline std::string tr_formatter_mem_MB(double MBps)
{
return tr_formatter_mem_B(buf, (size_t)(MBps * tr_mem_K * tr_mem_K), buflen);
return tr_formatter_mem_B((size_t)(MBps * tr_mem_K * tr_mem_K));
}
/* format a file size from bytes into a user-readable string. */
char* tr_formatter_size_B(char* buf, uint64_t bytes, size_t buflen);
std::string tr_formatter_size_B(uint64_t bytes);
void tr_formatter_get_units(void* dict);

View File

@ -71,9 +71,7 @@ QString Formatter::memToString(int64_t bytes) const
return tr("None");
}
auto buf = std::array<char, 128>{};
tr_formatter_mem_B(buf.data(), bytes, buf.size());
return QString::fromUtf8(buf.data());
return QString::fromStdString(tr_formatter_mem_B(bytes));
}
QString Formatter::sizeToString(uint64_t bytes) const
@ -83,9 +81,7 @@ QString Formatter::sizeToString(uint64_t bytes) const
return tr("None");
}
auto buf = std::array<char, 128>{};
tr_formatter_size_B(buf.data(), bytes, buf.size());
return QString::fromUtf8(buf.data());
return QString::fromStdString(tr_formatter_size_B(bytes));
}
QString Formatter::sizeToString(int64_t bytes) const
@ -100,9 +96,7 @@ QString Formatter::sizeToString(int64_t bytes) const
QString Formatter::speedToString(Speed const& speed) const
{
auto buf = std::array<char, 128>{};
tr_formatter_speed_KBps(buf.data(), speed.getKBps(), buf.size());
return QString::fromUtf8(buf.data());
return QString::fromStdString(tr_formatter_speed_KBps(speed.getKBps()));
}
QString Formatter::uploadSpeedToString(Speed const& upload_speed) const
@ -121,14 +115,12 @@ QString Formatter::downloadSpeedToString(Speed const& download_speed) const
QString Formatter::percentToString(double x) const
{
auto buf = std::array<char, 128>{};
return QString::fromUtf8(tr_strpercent(buf.data(), x, buf.size()));
return QString::fromStdString(tr_strpercent(x));
}
QString Formatter::ratioToString(double ratio) const
{
auto buf = std::array<char, 128>{};
return QString::fromUtf8(tr_strratio(buf.data(), buf.size(), ratio, "\xE2\x88\x9E"));
return QString::fromStdString(tr_strratio(ratio, "\xE2\x88\x9E"));
}
QString Formatter::timeToString(int seconds) const

View File

@ -203,15 +203,14 @@ int tr_main(int argc, char* argv[])
tr_metaInfoBuilderSetPieceSize(b, options.piecesize_kib * KiB);
}
char buf[128];
printf(
b->fileCount > 1 ? " %" PRIu32 " files, %s\n" : " %" PRIu32 " file, %s\n",
b->fileCount,
tr_formatter_size_B(buf, b->totalSize, sizeof(buf)));
tr_formatter_size_B(b->totalSize).c_str());
printf(
b->pieceCount > 1 ? " %" PRIu32 " pieces, %s each\n" : " %" PRIu32 " piece, %s\n",
b->pieceCount,
tr_formatter_size_B(buf, b->pieceSize, sizeof(buf)));
tr_formatter_size_B(b->pieceSize).c_str());
tr_makeMetaInfo(
b,

View File

@ -173,17 +173,17 @@ static char* tr_strltime(char* buf, int seconds, size_t buflen)
return buf;
}
static char* strlpercent(char* buf, double x, size_t buflen)
static std::string strlpercent(double x)
{
return tr_strpercent(buf, x, buflen);
return tr_strpercent(x);
}
static char* strlratio2(char* buf, double ratio, size_t buflen)
static std::string strlratio2(double ratio)
{
return tr_strratio(buf, buflen, ratio, "Inf");
return tr_strratio(ratio, "Inf");
}
static char* strlratio(char* buf, int64_t numerator, int64_t denominator, size_t buflen)
static std::string strlratio(int64_t numerator, int64_t denominator)
{
double ratio;
@ -200,39 +200,27 @@ static char* strlratio(char* buf, int64_t numerator, int64_t denominator, size_t
ratio = TR_RATIO_NA;
}
return strlratio2(buf, ratio, buflen);
return strlratio2(ratio);
}
static char* strlmem(char* buf, int64_t bytes, size_t buflen)
static std::string strlmem(int64_t bytes)
{
if (bytes == 0)
{
tr_strlcpy(buf, "None", buflen);
}
else
{
tr_formatter_mem_B(buf, bytes, buflen);
}
return buf;
return bytes == 0 ? "None"s : tr_formatter_mem_B(bytes);
}
static char* strlsize(char* buf, int64_t bytes, size_t buflen)
static std::string strlsize(int64_t bytes)
{
if (bytes < 0)
{
tr_strlcpy(buf, "Unknown", buflen);
}
else if (bytes == 0)
{
tr_strlcpy(buf, "None", buflen);
}
else
{
tr_formatter_size_B(buf, bytes, buflen);
return "Unknown"s;
}
return buf;
if (bytes == 0)
{
return "None"s;
}
return tr_formatter_size_B(bytes);
}
enum
@ -945,7 +933,6 @@ static void printDetails(tr_variant* top)
tr_variant* t = tr_variantListChild(torrents, ti);
tr_variant* l;
char buf[512];
char buf2[512];
int64_t i;
int64_t j;
int64_t k;
@ -1003,8 +990,7 @@ static void printDetails(tr_variant* top)
if (tr_variantDictFindInt(t, TR_KEY_sizeWhenDone, &i) && tr_variantDictFindInt(t, TR_KEY_leftUntilDone, &j))
{
strlpercent(buf, 100.0 * (i - j) / i, sizeof(buf));
printf(" Percent Done: %s%%\n", buf);
printf(" Percent Done: %s%%\n", strlpercent(100.0 * (i - j) / i).c_str());
}
if (tr_variantDictFindInt(t, TR_KEY_eta, &i))
@ -1014,19 +1000,17 @@ static void printDetails(tr_variant* top)
if (tr_variantDictFindInt(t, TR_KEY_rateDownload, &i))
{
printf(" Download Speed: %s\n", tr_formatter_speed_KBps(buf, i / (double)tr_speed_K, sizeof(buf)));
printf(" Download Speed: %s\n", tr_formatter_speed_KBps(i / (double)tr_speed_K).c_str());
}
if (tr_variantDictFindInt(t, TR_KEY_rateUpload, &i))
{
printf(" Upload Speed: %s\n", tr_formatter_speed_KBps(buf, i / (double)tr_speed_K, sizeof(buf)));
printf(" Upload Speed: %s\n", tr_formatter_speed_KBps(i / (double)tr_speed_K).c_str());
}
if (tr_variantDictFindInt(t, TR_KEY_haveUnchecked, &i) && tr_variantDictFindInt(t, TR_KEY_haveValid, &j))
{
strlsize(buf, i + j, sizeof(buf));
strlsize(buf2, j, sizeof(buf2));
printf(" Have: %s (%s verified)\n", buf, buf2);
printf(" Have: %s (%s verified)\n", strlsize(i + j).c_str(), strlsize(j).c_str());
}
if (tr_variantDictFindInt(t, TR_KEY_sizeWhenDone, &i))
@ -1039,32 +1023,25 @@ static void printDetails(tr_variant* top)
if (tr_variantDictFindInt(t, TR_KEY_desiredAvailable, &j) && tr_variantDictFindInt(t, TR_KEY_leftUntilDone, &k))
{
j += i - k;
strlpercent(buf, 100.0 * j / i, sizeof(buf));
printf(" Availability: %s%%\n", buf);
printf(" Availability: %s%%\n", strlpercent(100.0 * j / i).c_str());
}
if (tr_variantDictFindInt(t, TR_KEY_totalSize, &j))
{
strlsize(buf2, i, sizeof(buf2));
strlsize(buf, j, sizeof(buf));
printf(" Total size: %s (%s wanted)\n", buf, buf2);
printf(" Total size: %s (%s wanted)\n", strlsize(j).c_str(), strlsize(i).c_str());
}
}
if (tr_variantDictFindInt(t, TR_KEY_downloadedEver, &i) && tr_variantDictFindInt(t, TR_KEY_uploadedEver, &j))
{
strlsize(buf, i, sizeof(buf));
printf(" Downloaded: %s\n", buf);
strlsize(buf, j, sizeof(buf));
printf(" Uploaded: %s\n", buf);
strlratio(buf, j, i, sizeof(buf));
printf(" Ratio: %s\n", buf);
printf(" Downloaded: %s\n", strlsize(i).c_str());
printf(" Uploaded: %s\n", strlsize(j).c_str());
printf(" Ratio: %s\n", strlratio(j, i).c_str());
}
if (tr_variantDictFindInt(t, TR_KEY_corruptEver, &i))
{
strlsize(buf, i, sizeof(buf));
printf(" Corrupt DL: %s\n", buf);
printf(" Corrupt DL: %s\n", strlsize(i).c_str());
}
if (tr_variantDictFindStrView(t, TR_KEY_errorString, &sv) && !std::empty(sv) &&
@ -1176,7 +1153,7 @@ static void printDetails(tr_variant* top)
if (tr_variantDictFindInt(t, TR_KEY_pieceSize, &i))
{
printf(" Piece Size: %s\n", strlmem(buf, i, sizeof(buf)));
printf(" Piece Size: %s\n", strlmem(i).c_str());
}
printf("\n");
@ -1190,7 +1167,7 @@ static void printDetails(tr_variant* top)
if (boolVal)
{
printf("%s\n", tr_formatter_speed_KBps(buf, i, sizeof(buf)));
printf("%s\n", tr_formatter_speed_KBps(i).c_str());
}
else
{
@ -1204,7 +1181,7 @@ static void printDetails(tr_variant* top)
if (boolVal)
{
printf("%s\n", tr_formatter_speed_KBps(buf, i, sizeof(buf)));
printf("%s\n", tr_formatter_speed_KBps(i).c_str());
}
else
{
@ -1223,7 +1200,7 @@ static void printDetails(tr_variant* top)
case TR_RATIOLIMIT_SINGLE:
if (tr_variantDictFindReal(t, TR_KEY_seedRatioLimit, &d))
{
printf(" Ratio Limit: %s\n", strlratio2(buf, d, sizeof(buf)));
printf(" Ratio Limit: %s\n", strlratio2(d).c_str());
}
break;
@ -1294,10 +1271,8 @@ static void printFileList(tr_variant* top)
tr_variantGetInt(tr_variantListChild(priorities, j), &priority) &&
tr_variantGetBool(tr_variantListChild(wanteds, j), &wanted))
{
char sizestr[64];
double percent = (double)have / length;
char const* pristr;
strlsize(sizestr, length, sizeof(sizestr));
switch (priority)
{
@ -1320,7 +1295,7 @@ static void printFileList(tr_variant* top)
floor(100.0 * percent),
pristr,
wanted ? "Yes" : "No",
sizestr,
strlsize(length).c_str(),
TR_PRIsv_ARG(filename));
}
}
@ -1468,7 +1443,6 @@ static void printTorrentList(tr_variant* top)
int64_t total_size = 0;
double total_up = 0;
double total_down = 0;
char haveStr[32];
printf(
"%6s %-4s %9s %-8s %6s %6s %-5s %-11s %s\n",
@ -1504,7 +1478,6 @@ static void printTorrentList(tr_variant* top)
{
char etaStr[16];
char statusStr[64];
char ratioStr[32];
char doneStr[8];
int64_t error;
char errorMark;
@ -1518,8 +1491,6 @@ static void printTorrentList(tr_variant* top)
tr_strlcpy(doneStr, "n/a", sizeof(doneStr));
}
strlsize(haveStr, sizeWhenDone - leftUntilDone, sizeof(haveStr));
if (leftUntilDone != 0 || eta != -1)
{
etaToString(etaStr, sizeof(etaStr), eta);
@ -1543,11 +1514,11 @@ static void printTorrentList(tr_variant* top)
(int)torId,
errorMark,
doneStr,
haveStr,
strlsize(sizeWhenDone - leftUntilDone).c_str(),
etaStr,
up / (double)tr_speed_K,
down / (double)tr_speed_K,
strlratio2(ratioStr, ratio, sizeof(ratioStr)),
strlratio2(ratio).c_str(),
getStatusString(d, statusStr, sizeof(statusStr)),
TR_PRIsv_ARG(name));
@ -1559,7 +1530,7 @@ static void printTorrentList(tr_variant* top)
printf(
"Sum: %9s %6.1f %6.1f\n",
strlsize(haveStr, total_size, sizeof(haveStr)),
strlsize(total_size).c_str(),
total_up / (double)tr_speed_K,
total_down / (double)tr_speed_K);
}
@ -1751,7 +1722,6 @@ static void printSession(tr_variant* top)
int64_t i;
bool boolVal;
auto sv = std::string_view{};
char buf[128];
printf("VERSION\n");
@ -1821,7 +1791,7 @@ static void printSession(tr_variant* top)
if (tr_variantDictFindInt(args, TR_KEY_cache_size_mb, &i))
{
printf(" Maximum memory cache size: %s\n", tr_formatter_mem_MB(buf, i, sizeof(buf)));
printf(" Maximum memory cache size: %s\n", tr_formatter_mem_MB(i).c_str());
}
printf("\n");
@ -1857,64 +1827,56 @@ static void printSession(tr_variant* top)
tr_variantDictFindReal(args, TR_KEY_seedRatioLimit, &seedRatioLimit) &&
tr_variantDictFindBool(args, TR_KEY_seedRatioLimited, &seedRatioLimited))
{
char buf2[128];
char buf3[128];
printf("LIMITS\n");
printf(" Peer limit: %" PRId64 "\n", peerLimit);
if (seedRatioLimited)
{
strlratio2(buf, seedRatioLimit, sizeof(buf));
}
else
{
tr_strlcpy(buf, "Unlimited", sizeof(buf));
}
printf(" Default seed ratio limit: %s\n", seedRatioLimited ? strlratio2(seedRatioLimit).c_str() : "Unlimited");
printf(" Default seed ratio limit: %s\n", buf);
std::string effective_up_limit;
if (altEnabled)
{
tr_formatter_speed_KBps(buf, altUp, sizeof(buf));
effective_up_limit = tr_formatter_speed_KBps(altUp);
}
else if (upEnabled)
{
tr_formatter_speed_KBps(buf, upLimit, sizeof(buf));
effective_up_limit = tr_formatter_speed_KBps(upLimit);
}
else
{
tr_strlcpy(buf, "Unlimited", sizeof(buf));
effective_up_limit = "Unlimited"s;
}
printf(
" Upload speed limit: %s (%s limit: %s; %s turtle limit: %s)\n",
buf,
effective_up_limit.c_str(),
upEnabled ? "Enabled" : "Disabled",
tr_formatter_speed_KBps(buf2, upLimit, sizeof(buf2)),
tr_formatter_speed_KBps(upLimit).c_str(),
altEnabled ? "Enabled" : "Disabled",
tr_formatter_speed_KBps(buf3, altUp, sizeof(buf3)));
tr_formatter_speed_KBps(altUp).c_str());
std::string effective_down_limit;
if (altEnabled)
{
tr_formatter_speed_KBps(buf, altDown, sizeof(buf));
effective_down_limit = tr_formatter_speed_KBps(altDown);
}
else if (downEnabled)
{
tr_formatter_speed_KBps(buf, downLimit, sizeof(buf));
effective_down_limit = tr_formatter_speed_KBps(downLimit);
}
else
{
tr_strlcpy(buf, "Unlimited", sizeof(buf));
effective_down_limit = "Unlimited"s;
}
printf(
" Download speed limit: %s (%s limit: %s; %s turtle limit: %s)\n",
buf,
effective_down_limit.c_str(),
downEnabled ? "Enabled" : "Disabled",
tr_formatter_speed_KBps(buf2, downLimit, sizeof(buf2)),
tr_formatter_speed_KBps(downLimit).c_str(),
altEnabled ? "Enabled" : "Disabled",
tr_formatter_speed_KBps(buf3, altDown, sizeof(buf3)));
tr_formatter_speed_KBps(altDown).c_str());
if (altTimeEnabled)
{
@ -1998,9 +1960,9 @@ static void printSessionStats(tr_variant* top)
tr_variantDictFindInt(d, TR_KEY_downloadedBytes, &down) && tr_variantDictFindInt(d, TR_KEY_secondsActive, &secs))
{
printf("\nCURRENT SESSION\n");
printf(" Uploaded: %s\n", strlsize(buf, up, sizeof(buf)));
printf(" Downloaded: %s\n", strlsize(buf, down, sizeof(buf)));
printf(" Ratio: %s\n", strlratio(buf, up, down, sizeof(buf)));
printf(" Uploaded: %s\n", strlsize(up).c_str());
printf(" Downloaded: %s\n", strlsize(down).c_str());
printf(" Ratio: %s\n", strlratio(up, down).c_str());
printf(" Duration: %s\n", tr_strltime(buf, secs, sizeof(buf)));
}
@ -2010,9 +1972,9 @@ static void printSessionStats(tr_variant* top)
{
printf("\nTOTAL\n");
printf(" Started %lu times\n", (unsigned long)sessions);
printf(" Uploaded: %s\n", strlsize(buf, up, sizeof(buf)));
printf(" Downloaded: %s\n", strlsize(buf, down, sizeof(buf)));
printf(" Ratio: %s\n", strlratio(buf, up, down, sizeof(buf)));
printf(" Uploaded: %s\n", strlsize(up).c_str());
printf(" Downloaded: %s\n", strlsize(down).c_str());
printf(" Ratio: %s\n", strlratio(up, down).c_str());
printf(" Duration: %s\n", tr_strltime(buf, secs, sizeof(buf)));
}
}

View File

@ -112,8 +112,6 @@ auto toString(time_t timestamp)
void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
{
auto buf = std::array<char, 128>{};
/**
*** General Info
**/
@ -135,8 +133,8 @@ void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
}
printf(" Piece Count: %" PRIu64 "\n", metainfo.pieceCount());
printf(" Piece Size: %s\n", tr_formatter_mem_B(std::data(buf), metainfo.pieceSize(), std::size(buf)));
printf(" Total Size: %s\n", tr_formatter_size_B(std::data(buf), metainfo.totalSize(), std::size(buf)));
printf(" Piece Size: %s\n", tr_formatter_mem_B(metainfo.pieceSize()).c_str());
printf(" Total Size: %s\n", tr_formatter_size_B(metainfo.totalSize()).c_str());
printf(" Privacy: %s\n", metainfo.isPrivate() ? "Private torrent" : "Public torrent");
/**
@ -183,7 +181,7 @@ void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
{
std::string filename = file.path();
filename += " (";
filename += tr_formatter_size_B(std::data(buf), file.length(), std::size(buf));
filename += tr_formatter_size_B(file.length());
filename += ')';
filenames.emplace_back(filename);
}