Added option and flag parseing, now supports -p for port range.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@287 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-02-18 19:08:17 +00:00
parent 78d3d78ef3
commit e0f21cce78
7 changed files with 142 additions and 10 deletions
+2
View File
@@ -15,6 +15,8 @@ rtorrent_LDADD = \
rtorrent_SOURCES = \
functional.h \
main.cc \
option_parser.cc \
option_parser.h \
parse.cc \
parse.h \
signal_handler.cc \
+1 -1
View File
@@ -21,7 +21,7 @@ Manager::initialize() {
CurlStack::init();
torrent::initialize();
torrent::listen_open(10000, 20000);
torrent::listen_open(m_portFirst, m_portLast);
}
void
+11 -4
View File
@@ -13,11 +13,13 @@ public:
typedef sigc::slot1<void, DownloadList::iterator> SlotReady;
typedef sigc::slot0<void> SlotFailed;
DownloadList& get_download_list() { return m_downloadList; }
HashQueue& get_hash_queue() { return m_hashQueue; }
HttpQueue& get_http_queue() { return m_httpQueue; }
Manager() : m_portFirst(6890), m_portLast(6999) {}
Poll& get_poll() { return m_poll; }
DownloadList& get_download_list() { return m_downloadList; }
HashQueue& get_hash_queue() { return m_hashQueue; }
HttpQueue& get_http_queue() { return m_httpQueue; }
Poll& get_poll() { return m_poll; }
void initialize();
void cleanup();
@@ -28,6 +30,8 @@ public:
void start(Download* d);
void stop(Download* d);
void set_port_range(int a, int b) { m_portFirst = a; m_portLast = b; }
private:
void receive_http_done(CurlGet* http);
@@ -38,6 +42,9 @@ private:
HashQueue m_hashQueue;
HttpQueue m_httpQueue;
Poll m_poll;
int m_portFirst;
int m_portLast;
};
}
+3 -3
View File
@@ -37,7 +37,7 @@ WindowHttpQueue::redraw() {
}
m_canvas->erase();
m_canvas->print(0, 0, "Http [%2i]", m_container.size());
m_canvas->print(0, 0, "Http [%i]", m_queue->size());
int pos = 10;
Container::iterator itr = m_container.begin();
@@ -85,8 +85,8 @@ WindowHttpQueue::create_name(core::CurlGet* h) {
n.substr(n.size() - 8) == ".TORRENT"))
n = n.substr(0, n.size() - 8);
if (n.size() > 20)
n = n.substr(0, 20);
if (n.size() > 30)
n = n.substr(0, 30);
return n;
}
+9 -2
View File
@@ -20,6 +20,7 @@
#include "timer.h"
#include "signal_handler.h"
#include "option_parser.h"
int64_t Timer::m_cache;
@@ -101,6 +102,12 @@ main(int argc, char** argv) {
SignalHandler::set_handler(SIGSEGV, sigc::bind(sigc::ptr_fun(&do_panic), SIGSEGV));
SignalHandler::set_handler(SIGBUS, sigc::bind(sigc::ptr_fun(&do_panic), SIGBUS));
OptionParser optionParser;
optionParser.insert_option('p', sigc::bind(sigc::ptr_fun(OptionParser::call_int_pair),
sigc::mem_fun(uiControl.get_core(), &core::Manager::set_port_range)));
int firstArg = optionParser.process(argc, argv);
display::Canvas::init();
ui::DownloadList uiDownloadList(&uiControl);
@@ -120,8 +127,8 @@ main(int argc, char** argv) {
uiControl.get_core().initialize();
for (int i = 1; i < argc; ++i)
uiControl.get_core().insert(argv[i]);
while (firstArg < argc)
uiControl.get_core().insert(argv[firstArg++]);
uiControl.get_display().adjust_layout();
+77
View File
@@ -0,0 +1,77 @@
#include "config.h"
#include <cstdio>
#include <sstream>
#include <stdexcept>
#include <unistd.h>
#include <sigc++/hide.h>
#include "option_parser.h"
void
OptionParser::insert_flag(char c, SlotFlag s) {
m_container[c].m_slot = sigc::hide(s);
m_container[c].m_useOption = false;
}
void
OptionParser::insert_option(char c, SlotOption s) {
m_container[c].m_slot = s;
m_container[c].m_useOption = true;
}
int
OptionParser::process(int argc, char** argv) {
int c;
std::string optString = create_optstring();
opterr = 0;
while ((c = getopt(argc, argv, optString.c_str())) != -1) {
if (c == '?') {
std::stringstream s;
s << "Invalid use of option flag -" << (char)optopt;
throw std::runtime_error(s.str());
} else {
call_flag(c, optarg ? optarg : "");
}
}
return optind;
}
void
OptionParser::call_int_pair(const std::string& str, SlotIntPair slot) {
int a, b;
if (sscanf(str.c_str(), "%u-%u", &a, &b) != 2)
throw std::runtime_error("Invalid argument \"" + str + "\" should be <a-b>");
slot(a, b);
}
std::string
OptionParser::create_optstring() {
std::string s;
for (Container::iterator itr = m_container.begin(); itr != m_container.end(); ++itr) {
s += itr->first;
if (itr->second.m_useOption)
s += ':';
}
return s;
}
void
OptionParser::call_flag(char c, const std::string& arg) {
Container::iterator itr = m_container.find(c);
if (itr == m_container.end())
throw std::logic_error("OptionParser::call_flag(...) could not find the flag");
itr->second.m_slot(arg);
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef RTORRENT_OPTION_PARSER_H
#define RTORRENT_OPTION_PARSER_H
#include <map>
#include <string>
#include <sigc++/slot.h>
// Throws std::runtime_error upon receiving bad input.
class OptionParser {
public:
typedef sigc::slot0<void> SlotFlag;
typedef sigc::slot1<void, const std::string&> SlotOption;
typedef sigc::slot2<void, int, int> SlotIntPair;
struct Node {
SlotOption m_slot;
bool m_useOption;
};
typedef std::map<char, Node> Container;
void insert_flag(char c, SlotFlag s);
void insert_option(char c, SlotOption s);
// Returns the index of the first non-option argument.
int process(int argc, char** argv);
static void call_int_pair(const std::string& str, SlotIntPair slot);
private:
std::string create_optstring();
void call_flag(char c, const std::string& arg);
Container m_container;
};
#endif