Do not use printf-style formatting in C++ code

This commit is contained in:
Mike Gelfand 2014-12-14 15:34:31 +00:00
parent 737fdab775
commit 6c02008280
4 changed files with 45 additions and 26 deletions

View File

@ -23,6 +23,7 @@
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QHeaderView> #include <QHeaderView>
#include <QHostAddress>
#include <QInputDialog> #include <QInputDialog>
#include <QItemSelectionModel> #include <QItemSelectionModel>
#include <QLabel> #include <QLabel>
@ -91,26 +92,20 @@ namespace
class PeerItem: public QTreeWidgetItem class PeerItem: public QTreeWidgetItem
{ {
Peer peer; Peer peer;
QString collatedAddress; mutable QString collatedAddress;
QString status; QString status;
public: public:
PeerItem (const Peer& p): peer(p) {}
virtual ~PeerItem () {} virtual ~PeerItem () {}
PeerItem (const Peer& p):
peer(p)
{
int q[4];
if (sscanf (p.address.toUtf8 ().constData (), "%d.%d.%d.%d", q+0, q+1, q+2, q+3) == 4)
collatedAddress.sprintf ("%03d.%03d.%03d.%03d", q[0], q[1], q[2], q[3]);
else
collatedAddress = p.address;
}
public: public:
void refresh (const Peer& p)
void refresh (const Peer& p) { peer = p; } {
if (p.address != peer.address)
collatedAddress.clear ();
peer = p;
}
void setStatus (const QString& s) { status = s; } void setStatus (const QString& s) { status = s; }
@ -127,9 +122,40 @@ class PeerItem: public QTreeWidgetItem
case COL_STATUS: return status < i->status; case COL_STATUS: return status < i->status;
case COL_CLIENT: return peer.clientName < i->peer.clientName; case COL_CLIENT: return peer.clientName < i->peer.clientName;
case COL_LOCK: return peer.isEncrypted && !i->peer.isEncrypted; case COL_LOCK: return peer.isEncrypted && !i->peer.isEncrypted;
default: return collatedAddress < i->collatedAddress; default: return address () < i->address ();
} }
} }
private:
const QString& address () const
{
if (collatedAddress.isEmpty ())
{
QHostAddress ipAddress;
if (ipAddress.setAddress (peer.address))
{
if (ipAddress.protocol () == QAbstractSocket::IPv4Protocol)
{
const quint32 ipv4Address = ipAddress.toIPv4Address ();
collatedAddress = QLatin1String ("1-") +
QString::fromLatin1 (QByteArray::number (ipv4Address, 16).rightJustified (8, '0'));
}
else if (ipAddress.protocol () == QAbstractSocket::IPv6Protocol)
{
const Q_IPV6ADDR ipv6Address = ipAddress.toIPv6Address ();
QByteArray tmp (16, '\0');
for (int i = 0; i < 16; ++i)
tmp[i] = ipv6Address[i];
collatedAddress = QLatin1String ("2-") + QString::fromLatin1 (tmp.toHex ());
}
}
if (collatedAddress.isEmpty ())
collatedAddress = QLatin1String ("3-") + peer.address.toLower ();
}
return collatedAddress;
}
}; };
/*** /***

View File

@ -834,7 +834,7 @@ FileTreeDelegate::paint (QPainter * painter,
p.textAlignment = Qt::AlignCenter; p.textAlignment = Qt::AlignCenter;
p.textVisible = true; p.textVisible = true;
p.progress = (int)(100.0*index.data().toDouble()); p.progress = (int)(100.0*index.data().toDouble());
p.text = QString().sprintf("%d%%", p.progress); p.text = QString::fromLatin1 ("%1%").arg (p.progress);
style->drawControl(QStyle::CE_ProgressBar, &p, painter); style->drawControl(QStyle::CE_ProgressBar, &p, painter);
} }
else if(column == COL_WANTED) else if(column == COL_WANTED)

View File

@ -19,7 +19,6 @@
#include <QMessageBox> #include <QMessageBox>
#include <libtransmission/transmission.h> #include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include <libtransmission/version.h> #include <libtransmission/version.h>
#include "about.h" #include "about.h"
@ -672,12 +671,8 @@ TrMainWindow::openDonate ()
void void
TrMainWindow::openHelp () TrMainWindow::openHelp ()
{ {
const char * fmt = "http://www.transmissionbt.com/help/gtk/%d.%dx"; QDesktopServices::openUrl (QUrl (QString::fromLatin1 ("http://www.transmissionbt.com/help/gtk/%1.%2x").
int major, minor; arg (MAJOR_VERSION).arg (MINOR_VERSION / 10)));
sscanf (SHORT_VERSION_STRING, "%d.%d", &major, &minor);
char url[128];
tr_snprintf (url, sizeof (url), fmt, major, minor/10);
QDesktopServices::openUrl (QUrl (url));
} }
void void

View File

@ -184,9 +184,7 @@ TorrentDelegateMin::drawTorrent (QPainter * painter,
myProgressBarStyle->palette.setColor (QPalette::Window, silverBack); myProgressBarStyle->palette.setColor (QPalette::Window, silverBack);
} }
myProgressBarStyle->state = progressBarState; myProgressBarStyle->state = progressBarState;
char buf[32]; myProgressBarStyle->text = QString::fromLatin1 ("%1%").arg (static_cast<int> (tr_truncd (100.0 * tor.percentDone (), 0)));
tr_snprintf (buf, sizeof (buf), "%d%%", (int)tr_truncd (100.0 * tor.percentDone(), 0));
myProgressBarStyle->text = buf;
myProgressBarStyle->textVisible = true; myProgressBarStyle->textVisible = true;
myProgressBarStyle->textAlignment = Qt::AlignCenter; myProgressBarStyle->textAlignment = Qt::AlignCenter;
setProgressBarPercentDone (option, tor); setProgressBarPercentDone (option, tor);