From 0206ee066d9aa2a0fef7217d620d5ef6a6f3b767 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Wed, 1 Jun 2005 13:33:44 +0000 Subject: [PATCH] Working option file, doesn't yet load from a sane place. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@449 e378c898-3ddf-0310-93e7-cc216c733640 --- src/Makefile.am | 2 ++ src/main.cc | 28 ++++++++++++++++- src/option_file.cc | 62 +++++++++++++++++++++++++++++++++++++ src/option_file.h | 48 ++++++++++++++++++++++++++++ src/option_handler.cc | 3 -- src/option_handler.h | 3 +- src/option_handler_rules.cc | 29 ++++++++--------- src/option_handler_rules.h | 36 +++++++++++++-------- 8 files changed, 178 insertions(+), 33 deletions(-) create mode 100644 src/option_file.cc create mode 100644 src/option_file.h diff --git a/src/Makefile.am b/src/Makefile.am index 1db80528..a20407f3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,6 +16,8 @@ rtorrent_LDADD = \ rtorrent_SOURCES = \ main.cc \ + option_file.cc \ + option_file.h \ option_handler.cc \ option_handler.h \ option_handler_rules.cc \ diff --git a/src/main.cc b/src/main.cc index c2a58de4..3d993b7a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -48,6 +48,7 @@ #include "utils/task_schedule.h" #include "signal_handler.h" +#include "option_file.h" #include "option_handler.h" #include "option_handler_rules.h" #include "option_parser.h" @@ -108,6 +109,30 @@ parse_options(ui::Control* c, OptionHandler* optionHandler, int argc, char** arg return optionParser.process(argc, argv); } +void +initialize_option_handler(ui::Control* c, OptionHandler* optionHandler) { + core::DownloadSlotMap* dsm = &c->get_core().get_default_settings(); + + optionHandler->insert("min_peers", new OptionHandlerDownloadInt<&apply_download_min_peers, &validate_download_peers>(dsm)); + optionHandler->insert("max_peers", new OptionHandlerDownloadInt<&apply_download_max_peers, &validate_download_peers>(dsm)); + optionHandler->insert("max_uploads", new OptionHandlerDownloadInt<&apply_download_max_uploads, &validate_download_peers>(dsm)); +} + +void +load_option_file(const std::string& filename, OptionHandler* optionHandler, bool require = false) { + std::fstream f(filename.c_str(), std::ios::in); + + if (!f.is_open()) + return; + + std::cout << "Loaded option file \"" << filename << "\"" << std::endl; + + OptionFile optionFile; + + optionFile.slot_option(sigc::mem_fun(*optionHandler, &OptionHandler::process)); + optionFile.process(&f); +} + void load_session_torrents(ui::Control* c) { // Load session torrents. @@ -146,7 +171,7 @@ main(int argc, char** argv) { srandom(utils::Timer::cache().usec()); srand48(utils::Timer::cache().usec()); - optionHandler.insert("min_peers", new OptionHandlerDownloadMinPeers(&uiControl.get_core().get_default_settings())); + initialize_option_handler(&uiControl, &optionHandler); try { @@ -155,6 +180,7 @@ 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)); + load_option_file("./test_options", &optionHandler); int firstArg = parse_options(&uiControl, &optionHandler, argc, argv); initialize_display(&uiControl); diff --git a/src/option_file.cc b/src/option_file.cc new file mode 100644 index 00000000..5eb956b5 --- /dev/null +++ b/src/option_file.cc @@ -0,0 +1,62 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005, Jari Sundell +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#include +#include +#include +#include + +#include "option_file.h" + +void +OptionFile::process(std::istream* stream) { + char buf[max_size_line]; + + while (stream->good()) { + stream->getline(buf, max_size_line); + + parse_line(buf); + } +} + +void +OptionFile::parse_line(const char* line) { + //const char* last = std::find(line, line + max_size_line, '\0'); + + if (line[0] == '#') + return; + + char key[64]; + char opt[512]; + + int result; + + // Check for empty lines, and options within "abc". + if ((result = std::sscanf(line, "%64s = %512s", key, opt)) == 2) + m_slotOption(key, opt); + + // Don't throw on empty lines or lines with key and opt. + if (result == 1) + throw std::runtime_error("Error parseing option file."); +} diff --git a/src/option_file.h b/src/option_file.h new file mode 100644 index 00000000..7c8e8da5 --- /dev/null +++ b/src/option_file.h @@ -0,0 +1,48 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005, Jari Sundell +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#ifndef RTORRENT_OPTION_FILE_H +#define RTORRENT_OPTION_FILE_H + +#include +#include +#include + +class OptionFile { +public: + static const int max_size_key = 64; + static const int max_size_opt = 512; + static const int max_size_line = max_size_key + max_size_opt + 64; + + typedef sigc::slot2 SlotStringPair; + + void slot_option(const SlotStringPair& s) { m_slotOption = s; } + + void process(std::istream* stream); + +private: + void parse_line(const char* line); + + SlotStringPair m_slotOption; +}; + +#endif diff --git a/src/option_handler.cc b/src/option_handler.cc index 48642533..0a3086c4 100644 --- a/src/option_handler.cc +++ b/src/option_handler.cc @@ -28,9 +28,6 @@ #include "option_handler.h" -OptionHandlerBase::~OptionHandlerBase() { -} - void OptionHandler::insert(const std::string& key, OptionHandlerBase* opt) { iterator itr = find(key); diff --git a/src/option_handler.h b/src/option_handler.h index d5853a37..d5eea8bd 100644 --- a/src/option_handler.h +++ b/src/option_handler.h @@ -26,9 +26,8 @@ #include #include +// No members with dtor's allowed. struct OptionHandlerBase { - virtual ~OptionHandlerBase(); - virtual void process(const std::string& key, const std::string& arg) = 0; }; diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index 38196c25..9c0df41f 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -22,23 +22,24 @@ #include "config.h" -#include -#include -#include - #include "option_handler_rules.h" -OptionHandlerDownloadMinPeers::~OptionHandlerDownloadMinPeers() { +bool +validate_download_peers(int arg) { + return arg > 0 && arg < (1 << 16); } void -OptionHandlerDownloadMinPeers::process(const std::string& key, const std::string& arg) { - int a; - - if (std::sscanf(arg.c_str(), "%i", &a) != 1 || - a <= 0 || - a >= (1 << 16)) - throw std::runtime_error("Invalid argument for min peers \"" + arg + "\""); - - (*m_map)[key] = sigc::bind(sigc::ptr_fun(&OptionHandlerDownloadMinPeers::apply), a); +apply_download_min_peers(core::Download* d, int arg) { + d->get_download().set_peers_min(arg); +} + +void +apply_download_max_peers(core::Download* d, int arg) { + d->get_download().set_peers_max(arg); +} + +void +apply_download_max_uploads(core::Download* d, int arg) { + d->get_download().set_uploads_max(arg); } diff --git a/src/option_handler_rules.h b/src/option_handler_rules.h index 772e7cc7..0123748c 100644 --- a/src/option_handler_rules.h +++ b/src/option_handler_rules.h @@ -23,25 +23,35 @@ #ifndef RTORRENT_OPTION_HANDLER_RULES_H #define RTORRENT_OPTION_HANDLER_RULES_H -#include "core/download_slot_map.h" +#include +#include +#include #include "option_handler.h" +#include "core/download_slot_map.h" -class OptionHandlerDownloadMinPeers : public OptionHandlerBase { +bool validate_download_peers(int arg); + +void apply_download_min_peers(core::Download* d, int arg); +void apply_download_max_peers(core::Download* d, int arg); +void apply_download_max_uploads(core::Download* d, int arg); + +template +class OptionHandlerDownloadInt : public OptionHandlerBase { public: - OptionHandlerDownloadMinPeers(core::DownloadSlotMap* m) : m_map(m) {} - ~OptionHandlerDownloadMinPeers(); - - virtual void process(const std::string& key, const std::string& arg); - -private: - // Move these somewhere else? A seperate file with lots of different - // setting appliers. Those would perform the nessesary checks - // themselves. - static void apply(core::Download* d, int arg) { - d->get_download().set_peers_min(arg); + OptionHandlerDownloadInt(core::DownloadSlotMap* m) : m_map(m) {} + + virtual void process(const std::string& key, const std::string& arg) { + int a; + + if (std::sscanf(arg.c_str(), "%i", &a) != 1 || + !Validate(a)) + throw std::runtime_error("Invalid argument for \"" + key + "\": \"" + arg + "\""); + + (*m_map)[key] = sigc::bind(sigc::ptr_fun(Apply), a); } +private: core::DownloadSlotMap* m_map; };