mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-14 14:12:31 +00:00
* New non-template VariableStringSlot and VariableValueSlot.
* Support B, K, M and G in options. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@670 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -6,6 +6,7 @@ libsub_utils_a_SOURCES = \
|
||||
list_focus.h \
|
||||
lockfile.cc \
|
||||
lockfile.h \
|
||||
variable.cc \
|
||||
variable.h \
|
||||
variable_generic.cc \
|
||||
variable_generic.h \
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, 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
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <torrent/exceptions.h>
|
||||
|
||||
#include "variable.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
const torrent::Object Variable::m_emptyObject;
|
||||
|
||||
const char*
|
||||
Variable::string_to_value_unit(const char* pos, value_type* value, int base, int unit) {
|
||||
char* last;
|
||||
|
||||
*value = strtoll(pos, &last, base);
|
||||
|
||||
if (last == pos)
|
||||
throw torrent::input_error("Could not convert string to value.");
|
||||
|
||||
switch (*last) {
|
||||
case 'b':
|
||||
case 'B':
|
||||
++last;
|
||||
break;
|
||||
|
||||
case 'k':
|
||||
case 'K':
|
||||
*value = *value << 10;
|
||||
++last;
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
case 'M':
|
||||
*value = *value << 20;
|
||||
++last;
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
case 'G':
|
||||
*value = *value << 30;
|
||||
++last;
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
case '\0':
|
||||
*value = *value * unit;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("Could not parse value.");
|
||||
}
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
}
|
||||
+14
-6
@@ -37,25 +37,33 @@
|
||||
#ifndef RTORRENT_UTILS_VARIABLE_H
|
||||
#define RTORRENT_UTILS_VARIABLE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace torrent {
|
||||
class Object;
|
||||
}
|
||||
#include <torrent/object.h>
|
||||
|
||||
namespace utils {
|
||||
|
||||
class Variable {
|
||||
public:
|
||||
typedef torrent::Object::value_type value_type;
|
||||
typedef torrent::Object::string_type string_type;
|
||||
typedef torrent::Object::list_type list_type;
|
||||
typedef torrent::Object::map_type map_type;
|
||||
typedef torrent::Object::key_type key_type;
|
||||
|
||||
Variable() {}
|
||||
virtual ~Variable() {}
|
||||
|
||||
virtual const torrent::Object& get() = 0;
|
||||
virtual void set(const torrent::Object& arg) = 0;
|
||||
virtual void set(const torrent::Object& arg) = 0;
|
||||
|
||||
protected:
|
||||
Variable(const Variable&);
|
||||
void operator = (const Variable&);
|
||||
|
||||
static const char* string_to_value_unit(const char* pos, value_type* value, int base, int unit);
|
||||
|
||||
// Temporary hack, until torrent::Object is extended to allow
|
||||
// references so we can return a copy, not a const reference.
|
||||
static const torrent::Object m_emptyObject;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -163,4 +163,58 @@ VariableObject::set(const torrent::Object& arg) {
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// New and prettified.
|
||||
//
|
||||
|
||||
const torrent::Object&
|
||||
VariableValueSlot::get() {
|
||||
m_cache = m_slotGet() / m_unit;
|
||||
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
void
|
||||
VariableValueSlot::set(const torrent::Object& arg) {
|
||||
value_type value;
|
||||
|
||||
switch (arg.type()) {
|
||||
case torrent::Object::TYPE_STRING:
|
||||
string_to_value_unit(arg.as_string().c_str(), &value, m_base, m_unit);
|
||||
|
||||
// Check if we hit the end of the input.
|
||||
|
||||
m_slotSet(value);
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_VALUE:
|
||||
m_slotSet(arg.as_value());
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("Not a value");
|
||||
}
|
||||
}
|
||||
|
||||
const torrent::Object&
|
||||
VariableStringSlot::get() {
|
||||
m_cache = m_slotGet();
|
||||
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
void
|
||||
VariableStringSlot::set(const torrent::Object& arg) {
|
||||
switch (arg.type()) {
|
||||
case torrent::Object::TYPE_STRING:
|
||||
m_slotSet(arg.as_string());
|
||||
break;
|
||||
case torrent::Object::TYPE_NONE:
|
||||
m_slotSet(std::string());
|
||||
break;
|
||||
default:
|
||||
throw torrent::input_error("Not a string.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -111,111 +111,69 @@ private:
|
||||
Type m_type;
|
||||
};
|
||||
|
||||
template <typename Get = std::string, typename Set = const std::string&>
|
||||
class VariableSlotString : public Variable {
|
||||
//
|
||||
// New and pretty.
|
||||
//
|
||||
|
||||
class VariableValueSlot : public Variable {
|
||||
public:
|
||||
typedef rak::function0<Get> SlotGet;
|
||||
typedef rak::function1<void, Set> SlotSet;
|
||||
typedef rak::function0<value_type> slot_get_type;
|
||||
typedef rak::function1<void, value_type> slot_set_type;
|
||||
typedef std::pair<value_type, value_type> range_type;
|
||||
|
||||
VariableSlotString(typename SlotGet::base_type* slotGet, typename SlotSet::base_type* slotSet) {
|
||||
m_slotGet.set(slotGet);
|
||||
m_slotSet.set(slotSet);
|
||||
template <typename SlotGet, typename SlotSet>
|
||||
VariableValueSlot(SlotGet* slotGet, SlotSet* slotSet, unsigned int base = 0, unsigned int unit = 1,
|
||||
range_type range = range_type(std::numeric_limits<value_type>::min(), std::numeric_limits<value_type>::max())) :
|
||||
m_base(base),
|
||||
m_unit(unit),
|
||||
m_range(range) {
|
||||
|
||||
m_slotGet.set(rak::convert_fn<value_type>(slotGet));
|
||||
m_slotSet.set(rak::convert_fn<void, value_type>(slotSet));
|
||||
}
|
||||
|
||||
virtual ~VariableSlotString() {}
|
||||
|
||||
virtual const torrent::Object& get() {
|
||||
m_cache = m_slotGet();
|
||||
|
||||
if (!m_cache.is_string())
|
||||
throw torrent::internal_error("VariableSlotString::get() got wrong type.");
|
||||
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
virtual void set(const torrent::Object& arg) {
|
||||
switch (arg.type()) {
|
||||
case torrent::Object::TYPE_STRING:
|
||||
m_slotSet(arg.as_string());
|
||||
break;
|
||||
case torrent::Object::TYPE_NONE:
|
||||
m_slotSet("");
|
||||
break;
|
||||
default:
|
||||
throw torrent::internal_error("VariableSlotString::set(...) got wrong type.");
|
||||
}
|
||||
}
|
||||
virtual const torrent::Object& get();
|
||||
virtual void set(const torrent::Object& arg);
|
||||
|
||||
private:
|
||||
SlotGet m_slotGet;
|
||||
SlotSet m_slotSet;
|
||||
slot_get_type m_slotGet;
|
||||
slot_set_type m_slotSet;
|
||||
|
||||
unsigned int m_base;
|
||||
unsigned int m_unit;
|
||||
range_type m_range;
|
||||
|
||||
// Store the cache here to avoid unnessesary copying and such. This
|
||||
// should not result in any unresonable memory usage since few
|
||||
// strings will be very large.
|
||||
torrent::Object m_cache;
|
||||
torrent::Object m_cache;
|
||||
};
|
||||
|
||||
template <typename Get, typename Set>
|
||||
class VariableSlotValue : public Variable {
|
||||
class VariableStringSlot : public Variable {
|
||||
public:
|
||||
typedef rak::function0<Get> SlotGet;
|
||||
typedef rak::function1<void, Set> SlotSet;
|
||||
typedef std::pair<int64_t, int64_t> Range;
|
||||
typedef rak::function0<string_type> slot_get_type;
|
||||
typedef rak::function1<void, const string_type&> slot_set_type;
|
||||
|
||||
VariableSlotValue(typename SlotGet::base_type* slotGet,
|
||||
typename SlotSet::base_type* slotSet,
|
||||
const char* pattern,
|
||||
Range range = Range(std::numeric_limits<int64_t>::min(),
|
||||
std::numeric_limits<int64_t>::max())) {
|
||||
m_slotGet.set(slotGet);
|
||||
m_slotSet.set(slotSet);
|
||||
m_pattern = pattern;
|
||||
m_range = range;
|
||||
template <typename SlotGet, typename SlotSet>
|
||||
VariableStringSlot(SlotGet* slotGet, SlotSet* slotSet) {
|
||||
m_slotGet.set(rak::convert_fn<string_type>(slotGet));
|
||||
m_slotSet.set(rak::convert_fn<void, const string_type&>(slotSet));
|
||||
}
|
||||
|
||||
virtual ~VariableSlotValue() {}
|
||||
|
||||
virtual const torrent::Object& get() {
|
||||
m_cache = m_slotGet();
|
||||
|
||||
// Need this?
|
||||
if (!m_cache.is_value())
|
||||
throw torrent::internal_error("VariableSlotValue::get() got wrong type.");
|
||||
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
virtual void set(const torrent::Object& arg) {
|
||||
if (arg.is_string()) {
|
||||
Set v;
|
||||
|
||||
if (std::sscanf(arg.as_string().c_str(), m_pattern, &v) != 1)
|
||||
throw torrent::input_error("Not a value.");
|
||||
|
||||
m_slotSet(v);
|
||||
|
||||
} else if (arg.is_value()) {
|
||||
m_slotSet(arg.as_value());
|
||||
|
||||
} else {
|
||||
throw torrent::input_error("Not a value");
|
||||
}
|
||||
}
|
||||
virtual const torrent::Object& get();
|
||||
virtual void set(const torrent::Object& arg);
|
||||
|
||||
private:
|
||||
SlotGet m_slotGet;
|
||||
SlotSet m_slotSet;
|
||||
|
||||
const char* m_pattern;
|
||||
Range m_range;
|
||||
slot_get_type m_slotGet;
|
||||
slot_set_type m_slotSet;
|
||||
|
||||
// Store the cache here to avoid unnessesary copying and such. This
|
||||
// should not result in any unresonable memory usage since few
|
||||
// strings will be very large.
|
||||
torrent::Object m_cache;
|
||||
torrent::Object m_cache;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user