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
This commit is contained in:
rakshasa
2005-06-01 13:33:44 +00:00
parent 74930f86b3
commit 0206ee066d
8 changed files with 178 additions and 33 deletions
+2
View File
@@ -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 \
+27 -1
View File
@@ -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);
+62
View File
@@ -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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
#include <functional>
#include <fstream>
#include <stdexcept>
#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.");
}
+48
View File
@@ -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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_OPTION_FILE_H
#define RTORRENT_OPTION_FILE_H
#include <iosfwd>
#include <string>
#include <sigc++/slot.h>
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<void, const std::string&, const std::string&> 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
-3
View File
@@ -28,9 +28,6 @@
#include "option_handler.h"
OptionHandlerBase::~OptionHandlerBase() {
}
void
OptionHandler::insert(const std::string& key, OptionHandlerBase* opt) {
iterator itr = find(key);
+1 -2
View File
@@ -26,9 +26,8 @@
#include <map>
#include <string>
// No members with dtor's allowed.
struct OptionHandlerBase {
virtual ~OptionHandlerBase();
virtual void process(const std::string& key, const std::string& arg) = 0;
};
+15 -14
View File
@@ -22,23 +22,24 @@
#include "config.h"
#include <cstdio>
#include <stdexcept>
#include <sigc++/bind.h>
#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);
}
+23 -13
View File
@@ -23,25 +23,35 @@
#ifndef RTORRENT_OPTION_HANDLER_RULES_H
#define RTORRENT_OPTION_HANDLER_RULES_H
#include "core/download_slot_map.h"
#include <cstdio>
#include <stdexcept>
#include <sigc++/bind.h>
#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 <void (*Apply)(core::Download*, int), bool (*Validate)(int)>
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;
};