* ChunkSelector selected from outside the priority ranges due to

~(char)0 giving an inverted integer rather than char.

* Fixed a segfault due to too small screen size, snprintf returns the
number of bytes that could have been written rather than how much
actually was.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@626 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2006-01-24 20:05:36 +00:00
parent 825985b8ef
commit 305e3bdf2b
15 changed files with 263 additions and 127 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
AC_INIT(rtorrent, 0.4.3, jaris@ifi.uio.no)
AC_INIT(rtorrent, 0.4.4, jaris@ifi.uio.no)
AM_INIT_AUTOMAKE
AM_CONFIG_HEADER(config.h)
+1 -1
View File
@@ -126,7 +126,7 @@ Download::set_priority(uint32_t p) {
if (p >= 4)
throw torrent::input_error("Priority out of range.");
torrent::download_set_priority(m_download, p * 2);
torrent::download_set_priority(m_download, p * p);
get_bencode().get_key("rtorrent").insert_key("priority", (int64_t)p);
}
+159 -67
View File
@@ -39,6 +39,7 @@
#include <cstring>
#include <sstream>
#include <iomanip>
#include <torrent/exceptions.h>
#include <torrent/rate.h>
#include <torrent/tracker.h>
@@ -49,133 +50,224 @@
namespace display {
char*
print_string(char* buf, unsigned int length, char* str) {
// We don't have any nice simple functions for copying strings that
// return the end address.
while (length-- != 0 && *str != '\0')
*(buf++) = *(str++);
inline char*
print_buffer(char* first, char* last, const char* format) {
if (first >= last)
return first;
return buf;
int s = snprintf(first, last - first, format);
if (s < 0)
return first;
else
return std::min(first + s, last);
}
template <typename Arg1>
inline char*
print_buffer(char* first, char* last, const char* format, const Arg1& arg1) {
if (first >= last)
return first;
int s = snprintf(first, last - first, format, arg1);
if (s < 0)
return first;
else
return std::min(first + s, last);
}
template <typename Arg1, typename Arg2>
inline char*
print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2) {
if (first >= last)
return first;
int s = snprintf(first, last - first, format, arg1, arg2);
if (s < 0)
return first;
else
return std::min(first + s, last);
}
template <typename Arg1, typename Arg2, typename Arg3>
inline char*
print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) {
if (first >= last)
return first;
int s = snprintf(first, last - first, format, arg1, arg2, arg3);
if (s < 0)
return first;
else
return std::min(first + s, last);
}
char*
print_hhmmss(char* buf, unsigned int length, time_t t) {
print_string(char* first, char* last, char* str) {
// We don't have any nice simple functions for copying strings that
// return the end address.
while (first != last && *str != '\0')
*(first++) = *(str++);
return first;
}
char*
print_hhmmss(char* first, char* last, time_t t) {
std::tm *u = std::localtime(&t);
if (u == NULL)
return "inv_time";
//return "inv_time";
throw torrent::internal_error("print_hhmmss(...) failed.");
int s = snprintf(buf, length, "%2u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec);
return buf + std::max(s, 0);
return print_buffer(first, last, "%2u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec);
}
char*
print_ddhhmm(char* buf, unsigned int length, time_t t) {
int s;
print_ddhhmm(char* first, char* last, time_t t) {
if (t / (24 * 3600) < 100)
s = snprintf(buf, length, "%2i:%02i:%02i", (int)t / (24 * 3600), ((int)t / 3600) % 24, ((int)t / 60) % 60);
return print_buffer(first, last, "%2i:%02i:%02i", (int)t / (24 * 3600), ((int)t / 3600) % 24, ((int)t / 60) % 60);
else
s = snprintf(buf, length, "--:--:--");
return buf + std::max(s, 0);
return print_buffer(first, last, "--:--:--");
}
char*
print_ddmmyyyy(char* buf, unsigned int length, time_t t) {
print_ddmmyyyy(char* first, char* last, time_t t) {
std::tm *u = std::gmtime(&t);
if (u == NULL)
return "inv_time";
//return "inv_time";
throw torrent::internal_error("print_ddmmyyyy(...) failed.");
int s = snprintf(buf, length, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year));
return buf + std::max(s, 0);
return print_buffer(first, last, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year));
}
char*
print_download_title(char* buf, unsigned int length, core::Download* d) {
return buf + std::max(snprintf(buf, length, "%s", d->get_download().name().c_str()), 0);
print_download_title(char* first, char* last, core::Download* d) {
return print_buffer(first, last, "%s", d->get_download().name().c_str());
}
char*
print_download_info(char* buf, unsigned int length, core::Download* d) {
char* last = buf + length;
buf += std::max(snprintf(buf, last - buf, "Torrent: "), 0);
print_download_info(char* first, char* last, core::Download* d) {
first = print_buffer(first, last, "Torrent: ");
if (!d->get_download().is_open())
buf += std::max(snprintf(buf, last - buf, "closed "), 0);
first = print_buffer(first, last, "closed ");
else if (d->is_done())
buf += std::max(snprintf(buf, last - buf, "done %10.1f MB",
(double)d->get_download().bytes_total() / (double)(1 << 20)), 0);
first = print_buffer(first, last, "done %10.1f MB", (double)d->get_download().bytes_total() / (double)(1 << 20));
else
buf += std::max(snprintf(buf, last - buf, "%6.1f / %6.1f MB",
(double)d->get_download().bytes_done() / (double)(1 << 20),
(double)d->get_download().bytes_total() / (double)(1 << 20)), 0);
first = print_buffer(first, last, "%6.1f / %6.1f MB",
(double)d->get_download().bytes_done() / (double)(1 << 20),
(double)d->get_download().bytes_total() / (double)(1 << 20));
buf += std::max(snprintf(buf, last - buf, " Rate: %5.1f / %5.1f KB Uploaded: %7.1f MB ",
(double)d->get_download().up_rate()->rate() / (1 << 10),
(double)d->get_download().down_rate()->rate() / (1 << 10),
(double)d->get_download().up_rate()->total() / (1 << 20)), 0);
first = print_buffer(first, last, " Rate: %5.1f / %5.1f KB Uploaded: %7.1f MB ",
(double)d->get_download().up_rate()->rate() / (1 << 10),
(double)d->get_download().down_rate()->rate() / (1 << 10),
(double)d->get_download().up_rate()->total() / (1 << 20));
buf = print_download_time_left(buf, last - buf, d);
first = print_download_time_left(first, last, d);
*buf = '\0';
if (d->priority() != 2)
first = print_buffer(first, last, " [%s]", core::Download::priority_to_string(d->priority()));
return buf;
if (first > last)
throw torrent::internal_error("print_download_info(...) wrote past end of the buffer.");
return first;
}
char*
print_download_status(char* buf, unsigned int length, core::Download* d) {
char* last = buf + length;
print_download_status(char* first, char* last, core::Download* d) {
if (!d->get_download().is_active())
buf += std::max(snprintf(buf, last - buf, "Inactive: "), 0);
first = print_buffer(first, last, "Inactive: ");
if (d->get_download().is_hash_checking())
buf += std::max(snprintf(buf, last - buf, "Checking hash [%2i%%]",
(d->get_download().chunks_hashed() * 100) / d->get_download().chunks_total()), 0);
first = print_buffer(first, last, "Checking hash [%2i%%]",
(d->get_download().chunks_hashed() * 100) / d->get_download().chunks_total());
else if (d->get_download().is_tracker_busy() &&
d->get_download().tracker_focus() < d->get_download().size_trackers())
buf += std::max(snprintf(buf, last - buf, "Tracker[%i:%i]: Connecting to %s",
d->get_download().tracker(d->get_download().tracker_focus()).group(),
d->get_download().tracker_focus(),
d->get_download().tracker(d->get_download().tracker_focus()).url().c_str()), 0);
first = print_buffer(first, last, "Tracker[%i:%i]: Connecting to %s",
d->get_download().tracker(d->get_download().tracker_focus()).group(),
d->get_download().tracker_focus(),
d->get_download().tracker(d->get_download().tracker_focus()).url().c_str());
else if (!d->get_message().empty())
buf += std::max(snprintf(buf, last - buf, "%s", d->get_message().c_str()), 0);
first = print_buffer(first, last, "%s", d->get_message().c_str());
else
buf[0] = '\0';
*first = '\0';
return buf;
if (first > last)
throw torrent::internal_error("print_download_status(...) wrote past end of the buffer.");
return first;
}
char*
print_download_time_left(char* buf, unsigned int length, core::Download* d) {
print_download_time_left(char* first, char* last, core::Download* d) {
uint32_t rate;
if (!d->get_download().is_active() ||
(rate = d->get_download().down_rate()->rate()) < 512) {
buf += std::max(snprintf(buf, length, "--:--:--"), 0);
return buf;
}
(rate = d->get_download().down_rate()->rate()) < 512)
return print_buffer(first, last, "--:--:--");
time_t remaining = (d->get_download().bytes_total() - d->get_download().bytes_done()) / (rate & ~(uint32_t)(512 - 1));
return print_ddhhmm(buf, length, remaining);
return print_ddhhmm(first, last, remaining);
}
// char*
// print_entry_tags(char* buf, unsigned int length) {
char*
print_status_info(char* first, char* last) {
if (torrent::up_throttle() == 0)
first = print_buffer(first, last, "[Throttle off");
else
first = print_buffer(first, last, "[Throttle %3i", torrent::up_throttle() / 1024);
if (torrent::down_throttle() == 0)
first = print_buffer(first, last, "/off KB]");
else
first = print_buffer(first, last, "/%3i KB]", torrent::down_throttle() / 1024);
// }
first = print_buffer(first, last, " [Rate %5.1f/%5.1f KB]",
(double)torrent::up_rate()->rate() / 1024.0,
(double)torrent::down_rate()->rate() / 1024.0);
// char*
// print_entry_file(char* buf, unsigned int length, const torrent::Entry& entry);
first = print_buffer(first, last, " [Listen %s:%u]",
torrent::local_address().c_str(),
(unsigned int)torrent::listen_port());
if (first > last)
throw torrent::internal_error("print_status_info(...) wrote past end of the buffer.");
std::string bindAddress = torrent::bind_address();
if (!bindAddress.empty())
first = print_buffer(first, last, " [Bind %s]", bindAddress.c_str());
return first;
}
char*
print_status_extra(char* first, char* last, Control* c) {
first = print_buffer(first, last, " [U %i/%i]",
torrent::currently_unchoked(),
torrent::max_unchoked());
first = print_buffer(first, last, " [S %i/%i/%i]",
torrent::total_handshakes(),
torrent::open_sockets(),
torrent::max_open_sockets());
first = print_buffer(first, last, " [F %i/%i]",
torrent::open_files(),
torrent::max_open_files());
return first;
}
}
+15 -10
View File
@@ -52,21 +52,26 @@ namespace torrent {
class Entry;
}
class Control;
namespace display {
char* print_string(char* buf, unsigned int length, char* str);
char* print_string(char* first, char* last, char* str);
char* print_hhmmss(char* buf, unsigned int length, time_t t);
char* print_ddhhmm(char* buf, unsigned int length, time_t t);
char* print_ddmmyyyy(char* buf, unsigned int length, time_t t);
char* print_hhmmss(char* first, char* last, time_t t);
char* print_ddhhmm(char* first, char* last, time_t t);
char* print_ddmmyyyy(char* first, char* last, time_t t);
char* print_download_title(char* buf, unsigned int length, core::Download* d);
char* print_download_info(char* buf, unsigned int length, core::Download* d);
char* print_download_status(char* buf, unsigned int length, core::Download* d);
char* print_download_time_left(char* buf, unsigned int length, core::Download* d);
char* print_download_title(char* first, char* last, core::Download* d);
char* print_download_info(char* first, char* last, core::Download* d);
char* print_download_status(char* first, char* last, core::Download* d);
char* print_download_time_left(char* first, char* last, core::Download* d);
char* print_entry_tags(char* buf, unsigned int length);
char* print_entry_file(char* buf, unsigned int length, const torrent::Entry& entry);
char* print_entry_tags(char* first, char* last);
char* print_entry_file(char* first, char* last, const torrent::Entry& entry);
char* print_status_info(char* first, char* last);
char* print_status_extra(char* first, char* last, Control* c);
}
+3 -3
View File
@@ -87,13 +87,13 @@ WindowDownloadList::redraw() {
char* position;
char* last = buffer + m_canvas->get_width() - 2;
position = print_download_title(buffer, last - buffer, *range.first);
position = print_download_title(buffer, last, *range.first);
m_canvas->print(0, pos++, "%c %s", range.first == m_list->get_focus() ? '*' : ' ', buffer);
position = print_download_info(buffer, last - buffer, *range.first);
position = print_download_info(buffer, last, *range.first);
m_canvas->print(0, pos++, "%c %s", range.first == m_list->get_focus() ? '*' : ' ', buffer);
position = print_download_status(buffer, last - buffer, *range.first);
position = print_download_status(buffer, last, *range.first);
m_canvas->print(0, pos++, "%c %s", range.first == m_list->get_focus() ? '*' : ' ', buffer);
++range.first;
+6 -6
View File
@@ -58,11 +58,11 @@ WindowDownloadStatusbar::redraw() {
m_canvas->erase();
char buffer[m_canvas->get_width() - 2];
char buffer[m_canvas->get_width()];
char* position;
char* last = buffer + m_canvas->get_width() - 2;
position = print_download_info(buffer, last - buffer, m_download);
position = print_download_info(buffer, last, m_download);
m_canvas->print(0, 0, "%s", buffer);
position = buffer + std::max(snprintf(buffer, last - buffer, "Peers: %i(%i) Min/Max: %i/%i Uploads: %i U/I: %i/%i Failed: %i",
@@ -75,12 +75,12 @@ WindowDownloadStatusbar::redraw() {
(int)m_download->get_download().peers_currently_interested(),
(int)m_download->chunks_failed()),
0);
position = buffer + std::max(snprintf(position, last - buffer, " Priority: %s",
core::Download::priority_to_string(m_download->variables()->get("priority").as_value())),
0);
// position = buffer + std::max(snprintf(position, last - buffer, " Priority: %s",
// core::Download::priority_to_string(m_download->variables()->get("priority").as_value())),
// 0);
m_canvas->print(0, 1, "%s", buffer);
position = print_download_status(buffer, last - buffer, m_download);
position = print_download_status(buffer, last, m_download);
m_canvas->print(0, 2, "[%c:%i] %s",
m_download->get_download().is_tracker_busy() ? 'C' : ' ',
(int)(m_download->get_download().tracker_timeout() / 1000000),
+1 -1
View File
@@ -71,7 +71,7 @@ WindowLog::redraw() {
for (core::Log::iterator itr = m_log->begin(), end = find_older(); itr != end && pos < m_canvas->get_height(); ++itr) {
char buffer[16];
print_hhmmss(buffer, 16, static_cast<time_t>(itr->first.seconds()));
print_hhmmss(buffer, buffer + 16, static_cast<time_t>(itr->first.seconds()));
m_canvas->print(0, pos++, "(%s) %s", buffer, itr->second.c_str());
}
+1 -1
View File
@@ -71,7 +71,7 @@ WindowLogComplete::redraw() {
for (core::Log::iterator itr = m_log->begin(), e = m_log->end(); itr != e && pos < m_canvas->get_height(); ++itr) {
char buffer[16];
print_hhmmss(buffer, 16, static_cast<time_t>(itr->first.seconds()));
print_hhmmss(buffer, buffer + 16, static_cast<time_t>(itr->first.seconds()));
m_canvas->print(0, pos++, "(%s) %s", buffer, itr->second.c_str());
}
+3 -3
View File
@@ -71,9 +71,9 @@ WindowPeerInfo::redraw() {
d.chunks_size());
char buffer[32], *position;
position = print_ddmmyyyy(buffer, 32, static_cast<time_t>(d.creation_date()));
position = print_string(position, buffer + 32 - position, " ");
position = print_hhmmss(position, buffer + 32 - position, static_cast<time_t>(d.creation_date()));
position = print_ddmmyyyy(buffer, buffer + 32, static_cast<time_t>(d.creation_date()));
position = print_string(position, buffer + 32, " ");
position = print_hhmmss(position, buffer + 32, static_cast<time_t>(d.creation_date()));
m_canvas->print(0, y++, "Created: %s", buffer);
+42 -31
View File
@@ -41,6 +41,7 @@
#include "control.h"
#include "canvas.h"
#include "utils.h"
#include "window_statusbar.h"
namespace display {
@@ -58,42 +59,52 @@ WindowStatusbar::redraw() {
m_canvas->erase();
// TODO: Make a buffer with size = get_width?
int pos = 0;
char buf[128];
char buffer[m_canvas->get_width() + 1];
char* position;
char* last = buffer + m_canvas->get_width();
if (torrent::up_throttle() == 0)
pos = snprintf(buf, 128, "off/");
else
pos = snprintf(buf, 128, "%3i/", torrent::up_throttle() / 1024);
// if (torrent::up_throttle() == 0)
// position = std::max(snprintf(buffer, 128, "off/"), 0);
// else
// position = std::max(snprintf(buffer, 128, "%3i/", torrent::up_throttle() / 1024), 0);
if (torrent::down_throttle() == 0)
pos = snprintf(buf + pos, 128 - pos, "off");
else
pos = snprintf(buf + pos, 128 - pos, "%-3i", torrent::down_throttle() / 1024);
// if (torrent::down_throttle() == 0)
// pos = snprintf(buf + pos, 128 - pos, "off");
// else
// pos = snprintf(buf + pos, 128 - pos, "%-3i", torrent::down_throttle() / 1024);
m_canvas->print(0, 0, "Throttle U/D: %s Rate: %5.1f / %5.1f KB Listen: %s:%i%s",
buf,
(double)torrent::up_rate()->rate() / 1024.0,
(double)torrent::down_rate()->rate() / 1024.0,
!torrent::local_address().empty() ? torrent::local_address().c_str() : "<default>",
(int)torrent::listen_port(),
!torrent::bind_address().empty() ? (" Bind: " + torrent::bind_address()).c_str() : "");
// m_canvas->print(0, 0, "Throttle U/D: %s Rate: %5.1f / %5.1f KB Listen: %s:%i%s",
// buf,
// (double)torrent::up_rate()->rate() / 1024.0,
// (double)torrent::down_rate()->rate() / 1024.0,
// !torrent::local_address().empty() ? torrent::local_address().c_str() : "<default>",
// (int)torrent::listen_port(),
// !torrent::bind_address().empty() ? (" Bind: " + torrent::bind_address()).c_str() : "");
#ifndef USE_EXTRA_DEBUG
pos = snprintf(buf, 128, "[U %i/%i][S %i/%i/%i][F %i/%i]",
#else
pos = snprintf(buf, 128, "%i [U %i/%i][S %i/%i/%i][F %i/%i]",
(int)(m_control->tick() - m_lastTick),
#endif
torrent::currently_unchoked(),
torrent::max_unchoked(),
torrent::total_handshakes(),
torrent::open_sockets(),
torrent::max_open_sockets(),
torrent::open_files(),
torrent::max_open_files());
// #ifndef USE_EXTRA_DEBUG
// pos = snprintf(buf, 128, "[U %i/%i][S %i/%i/%i][F %i/%i]",
// #else
// pos = snprintf(buf, 128, "%i [U %i/%i][S %i/%i/%i][F %i/%i]",
// (int)(m_control->tick() - m_lastTick),
// #endif
// torrent::currently_unchoked(),
// torrent::max_unchoked(),
// torrent::total_handshakes(),
// torrent::open_sockets(),
// torrent::max_open_sockets(),
// torrent::open_files(),
// torrent::max_open_files());
position = print_status_info(buffer, last);
m_canvas->print(0, 0, "%s", buffer);
last = last - (position - buffer);
if (last > buffer) {
position = print_status_extra(buffer, last, m_control);
m_canvas->print(m_canvas->get_width() - (position - buffer), 0, "%s", buffer);
}
m_canvas->print(m_canvas->get_width() - pos, 0, "%s", buf);
m_lastTick = m_control->tick();
}
+1 -1
View File
@@ -158,7 +158,7 @@ main(int argc, char** argv) {
try {
// Temporary.
setlocale(LC_ALL, "");
//setlocale(LC_ALL, "");
cachedTime = rak::timer::current();
+6
View File
@@ -234,6 +234,11 @@ Download::receive_next_priority() {
m_download->set_priority((m_download->priority() + 1) % 4);
}
void
Download::receive_prev_priority() {
m_download->set_priority((m_download->priority() - 1) % 4);
}
void
Download::bind_keys() {
(*m_bindings)['1'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_uploads), -1);
@@ -243,6 +248,7 @@ Download::bind_keys() {
(*m_bindings)['5'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_peers), -5);
(*m_bindings)['6'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_peers), 5);
(*m_bindings)['+'] = sigc::mem_fun(*this, &Download::receive_next_priority);
(*m_bindings)['-'] = sigc::mem_fun(*this, &Download::receive_prev_priority);
(*m_bindings)['t'] = sigc::bind(sigc::mem_fun(m_download->get_download(), &torrent::Download::tracker_manual_request), false);
(*m_bindings)['T'] = sigc::bind(sigc::mem_fun(m_download->get_download(), &torrent::Download::tracker_manual_request), true);
+3 -2
View File
@@ -88,6 +88,9 @@ public:
void activate_display(Display d);
void disable_display();
void receive_next_priority();
void receive_prev_priority();
private:
Download(const Download&);
void operator = (const Download&);
@@ -105,8 +108,6 @@ private:
void receive_snub_peer();
void receive_next_priority();
void bind_keys();
void mark_dirty();
+18
View File
@@ -233,6 +233,22 @@ DownloadList::receive_exit_download() {
m_control->display()->adjust_layout();
}
void
DownloadList::receive_next_priority() {
if (m_downloadList.get_focus() == m_downloadList.end())
return;
(*m_downloadList.get_focus())->set_priority(((*m_downloadList.get_focus())->priority() + 1) % 4);
}
void
DownloadList::receive_prev_priority() {
if (m_downloadList.get_focus() == m_downloadList.end())
return;
(*m_downloadList.get_focus())->set_priority(((*m_downloadList.get_focus())->priority() - 1) % 4);
}
void
DownloadList::receive_check_hash() {
if (m_downloadList.get_focus() == m_downloadList.end())
@@ -304,6 +320,8 @@ DownloadList::setup_keys() {
(*m_bindings)['\x13'] = sigc::mem_fun(*this, &DownloadList::receive_start_download);
(*m_bindings)['\x04'] = sigc::mem_fun(*this, &DownloadList::receive_stop_download);
(*m_bindings)['\x12'] = sigc::mem_fun(*this, &DownloadList::receive_check_hash);
(*m_bindings)['+'] = sigc::mem_fun(*this, &DownloadList::receive_next_priority);
(*m_bindings)['-'] = sigc::mem_fun(*this, &DownloadList::receive_prev_priority);
(*m_bindings)['\x7f'] = sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_view_input), true);
(*m_bindings)[KEY_BACKSPACE] = sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_view_input), true);
+3
View File
@@ -115,6 +115,9 @@ private:
void receive_view_download();
void receive_exit_download();
void receive_next_priority();
void receive_prev_priority();
void receive_check_hash();
void receive_view_input(bool useDefault);