mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-17 23:52:30 +00:00
Reorganized, moing stuff to src/utils subdir.
git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@290 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
noinst_LIBRARIES = libsub_utils.a
|
||||
|
||||
libsub_utils_a_SOURCES = \
|
||||
functional.h \
|
||||
parse.cc \
|
||||
parse.h \
|
||||
timer.h
|
||||
|
||||
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
|
||||
@@ -0,0 +1,109 @@
|
||||
#ifndef RTORRENT_FUNCTIONAL_H
|
||||
#define RTORRENT_FUNCTIONAL_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace func {
|
||||
|
||||
template <typename Iterator, typename Ftor>
|
||||
inline void for_each(Iterator first, Iterator last, Ftor ftor) {
|
||||
Iterator tmp;
|
||||
|
||||
while (first != last) {
|
||||
tmp = first++;
|
||||
|
||||
ftor(*tmp);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Type, typename Ftor>
|
||||
struct _accumulate {
|
||||
_accumulate(Type& t, Ftor f) : m_t(t), m_f(f) {}
|
||||
|
||||
template <typename Arg>
|
||||
void operator () (Arg& a) { m_t += m_f(a); }
|
||||
|
||||
Type& m_t;
|
||||
Ftor m_f;
|
||||
};
|
||||
|
||||
template <typename Type, typename Ftor>
|
||||
inline _accumulate<Type, Ftor>
|
||||
accumulate(Type& t, Ftor f) {
|
||||
return _accumulate<Type, Ftor>(t, f);
|
||||
}
|
||||
|
||||
template <typename Type, typename Ftor>
|
||||
struct _equal {
|
||||
_equal(Type t, Ftor f) : m_t(t), m_f(f) {}
|
||||
|
||||
template <typename Arg>
|
||||
bool operator () (Arg& a) {
|
||||
return m_t == m_f(a);
|
||||
}
|
||||
|
||||
Type m_t;
|
||||
Ftor m_f;
|
||||
};
|
||||
|
||||
template <typename Type, typename Ftor>
|
||||
inline _equal<Type, Ftor>
|
||||
equal(Type t, Ftor f) {
|
||||
return _equal<Type, Ftor>(t, f);
|
||||
}
|
||||
|
||||
template <typename Dest, typename Src>
|
||||
struct _on : public std::unary_function<typename Src::argument_type, typename Dest::result_type> {
|
||||
_on(Dest d, Src s) : m_dest(d), m_src(s) {}
|
||||
|
||||
typename Dest::result_type operator () (typename Src::argument_type arg) {
|
||||
return m_dest(m_src(arg));
|
||||
}
|
||||
|
||||
Dest m_dest;
|
||||
Src m_src;
|
||||
};
|
||||
|
||||
template <typename Dest, typename Src>
|
||||
inline _on<Dest, Src>
|
||||
on(Dest d, Src s) {
|
||||
return _on<Dest, Src>(d, s);
|
||||
}
|
||||
|
||||
template <typename Cond, typename Then>
|
||||
struct _if_then {
|
||||
_if_then(Cond c, Then t) : m_cond(c), m_then(t) {}
|
||||
|
||||
template <typename Arg>
|
||||
void operator () (Arg& a) {
|
||||
if (m_cond(a))
|
||||
m_then(a);
|
||||
}
|
||||
|
||||
Cond m_cond;
|
||||
Then m_then;
|
||||
};
|
||||
|
||||
template <typename Cond, typename Then>
|
||||
inline _if_then<Cond, Then>
|
||||
if_then(Cond c, Then t) {
|
||||
return _if_then<Cond, Then>(c, t);
|
||||
}
|
||||
|
||||
struct _call_delete
|
||||
: public std::unary_function<void, void> {
|
||||
|
||||
template <typename Type>
|
||||
void operator () (Type* t) {
|
||||
delete t;
|
||||
}
|
||||
};
|
||||
|
||||
inline _call_delete
|
||||
call_delete() {
|
||||
return _call_delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "utils/parse.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
std::string
|
||||
escape_string(const std::string& src) {
|
||||
std::stringstream stream;
|
||||
|
||||
// TODO: Correct would be to save the state.
|
||||
stream << std::hex << std::uppercase;
|
||||
|
||||
for (std::string::const_iterator itr = src.begin(); itr != src.end(); ++itr)
|
||||
if ((*itr >= 'A' && *itr <= 'Z') ||
|
||||
(*itr >= 'a' && *itr <= 'z') ||
|
||||
(*itr >= '0' && *itr <= '9') ||
|
||||
*itr == '-')
|
||||
stream << *itr;
|
||||
else
|
||||
stream << '%' << ((unsigned char)*itr >> 4) << ((unsigned char)*itr & 0xf);
|
||||
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RTORRENT_UTILS_PARSE_H
|
||||
#define RTORRENT_UTILS_PARSE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace utils {
|
||||
|
||||
// Various functinos for manipulating strings.
|
||||
|
||||
std::string escape_string(const std::string& src);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef LIBTORRENT_TIMER_H
|
||||
#define LIBTORRENT_TIMER_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace utils {
|
||||
|
||||
// Don't convert negative Timer to timeval.
|
||||
class Timer {
|
||||
public:
|
||||
Timer() : m_time(0) {}
|
||||
Timer(int64_t usec) : m_time(usec) {}
|
||||
Timer(timeval tv) : m_time((int64_t)(uint32_t)tv.tv_sec * 1000000 + (int64_t)(uint32_t)tv.tv_usec % 1000000) {}
|
||||
|
||||
int64_t usec() const { return m_time; }
|
||||
|
||||
timeval tval() const { return (timeval) { m_time / 1000000, m_time % 1000000}; }
|
||||
|
||||
Timer operator - (const Timer& t) const { return Timer(m_time - t.m_time); }
|
||||
Timer operator + (const Timer& t) const { return Timer(m_time + t.m_time); }
|
||||
|
||||
Timer operator -= (int64_t t) { m_time -= t; return *this; }
|
||||
Timer operator -= (const Timer& t) { m_time -= t.m_time; return *this; }
|
||||
|
||||
Timer operator += (int64_t t) { m_time += t; return *this; }
|
||||
Timer operator += (const Timer& t) { m_time += t.m_time; return *this; }
|
||||
|
||||
static Timer current() {
|
||||
timeval t;
|
||||
gettimeofday(&t, 0);
|
||||
|
||||
return Timer(t);
|
||||
}
|
||||
|
||||
// Cached time, updated in the beginning of torrent::work call.
|
||||
// Don't use outside socket_base read/write/except or Service::service.
|
||||
|
||||
// TODO: Find out if it's worth it. The kernel is supposed to cache the
|
||||
// time. Though system calls would be more expensive than we can afford.
|
||||
static Timer cache() { return Timer(m_cache); }
|
||||
|
||||
static void update() { m_cache = Timer::current().usec(); }
|
||||
|
||||
bool operator < (const Timer& t) const { return m_time < t.m_time; }
|
||||
bool operator > (const Timer& t) const { return m_time > t.m_time; }
|
||||
bool operator <= (const Timer& t) const { return m_time <= t.m_time; }
|
||||
bool operator >= (const Timer& t) const { return m_time >= t.m_time; }
|
||||
bool operator == (const Timer& t) const { return m_time == t.m_time; }
|
||||
|
||||
private:
|
||||
int64_t m_time;
|
||||
|
||||
// Instantiated in torrent.cc
|
||||
static int64_t m_cache;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // LIBTORRENT_TIMER_H
|
||||
Reference in New Issue
Block a user