From 5b050b594b625e49716449f60e6fd2f70bfa0630 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sat, 19 Feb 2005 21:45:36 +0000 Subject: [PATCH] Working on sessions. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@291 e378c898-3ddf-0310-93e7-cc216c733640 --- src/core/session_manager.cc | 35 +++++++++++++++++++++++++++++++++ src/core/session_manager.h | 27 +++++++++++++++++++++++++ src/main.cc | 18 +++++++++++++++++ src/utils/Makefile.am | 2 ++ src/utils/directory.cc | 34 ++++++++++++++++++++++++++++++++ src/utils/directory.h | 39 +++++++++++++++++++++++++++++++++++++ src/utils/parse.cc | 13 ++++++++++++- src/utils/parse.h | 1 + 8 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/core/session_manager.cc create mode 100644 src/core/session_manager.h create mode 100644 src/utils/directory.cc create mode 100644 src/utils/directory.h diff --git a/src/core/session_manager.cc b/src/core/session_manager.cc new file mode 100644 index 00000000..992d89a9 --- /dev/null +++ b/src/core/session_manager.cc @@ -0,0 +1,35 @@ +#include "config.h" + +#include "download.h" +#include "session_manager.h" + +namespace core { + +void +SessionManager::activate(const std::string& path) { + m_path = path; + + if (m_path.empty()) + throw std::logic_error("core::SessionManager::activate(...) received an empty path"); + + if (m_path.rbegin() != '/') + m_path += '/'; +} + +void +SessionManager::disable() { + m_path = ""; +} + +void +SessionManager::save(Download* d) { + std::fstream f( + + +} + +void +SessionManager::remove(Download* d) { +} + +} diff --git a/src/core/session_manager.h b/src/core/session_manager.h new file mode 100644 index 00000000..8ef9482c --- /dev/null +++ b/src/core/session_manager.h @@ -0,0 +1,27 @@ +#ifndef RTORRENT_CORE_SESSION_MANAGER_H +#define RTORRENT_CORE_SESSION_MANAGER_H + +#include + +namespace core { + +class Download; + +class SessionManager { +public: + + void activate(const std::string& path); + void disable(); + + bool is_active() { return !m_path.empty(); } + + void save(Download* d); + void remove(Download* d); + +private: + std::string m_path; +}; + +} + +#endif diff --git a/src/main.cc b/src/main.cc index 6b114ee3..dac8d23e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -18,6 +18,7 @@ #include "ui/download_list.h" #include "input/bindings.h" #include "utils/timer.h" +#include "utils/directory.h" #include "signal_handler.h" #include "option_parser.h" @@ -69,6 +70,20 @@ do_shutdown(ui::Control* c) { start_shutdown = false; } +void +test_dir(const std::string& s) { + utils::Directory d(s); + + d.update(); + + std::cout << "Listing dir \"" << s << '"' << std::endl; + + for (utils::Directory::iterator itr = d.begin(); itr != d.end(); ++itr) + std::cout << '"' << *itr << '"' << std::endl; + + exit(0); +} + int main(int argc, char** argv) { ui::Control uiControl; @@ -83,6 +98,9 @@ main(int argc, char** argv) { optionParser.insert_flag('h', sigc::ptr_fun(&print_help)); optionParser.insert_option('p', sigc::bind(sigc::ptr_fun(OptionParser::call_int_pair), sigc::mem_fun(uiControl.get_core(), &core::Manager::set_port_range))); + + // Test function. + optionParser.insert_option('s', sigc::ptr_fun(&test_dir)); int firstArg = optionParser.process(argc, argv); diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index 677ad837..fc68ba42 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -1,6 +1,8 @@ noinst_LIBRARIES = libsub_utils.a libsub_utils_a_SOURCES = \ + directory.cc \ + directory.h \ functional.h \ parse.cc \ parse.h \ diff --git a/src/utils/directory.cc b/src/utils/directory.cc new file mode 100644 index 00000000..58720677 --- /dev/null +++ b/src/utils/directory.cc @@ -0,0 +1,34 @@ +#include "config.h" + +#include +#include +#include +#include + +#include "directory.h" + +namespace utils { + +void +Directory::update() { + if (m_path.empty()) + throw std::logic_error("Directory::update() tried to open an empty path"); + + DIR* d = opendir(m_path.c_str()); + + if (d == NULL) + throw std::runtime_error("Directory::update() could not open directory"); + + struct dirent* ent; + + while ((ent = readdir(d)) != NULL) { + std::string de(ent->d_name); + + if (de != "." && de != "..") + Base::push_back(ent->d_name); + } + + std::sort(begin(), end(), std::less()); +} + +} diff --git a/src/utils/directory.h b/src/utils/directory.h new file mode 100644 index 00000000..c5ea2f22 --- /dev/null +++ b/src/utils/directory.h @@ -0,0 +1,39 @@ +#ifndef RTORRENT_UTILS_DIRECTORY_H +#define RTORRENT_UTILS_DIRECTORY_H + +#include +#include + +namespace utils { + +class Directory : private std::vector { +public: + typedef std::vector Base; + + using Base::iterator; + using Base::const_iterator; + using Base::reverse_iterator; + using Base::const_reverse_iterator; + + using Base::begin; + using Base::end; + using Base::rbegin; + using Base::rend; + + using Base::empty; + using Base::size; + + Directory() {} + Directory(const std::string& path) : m_path(path) {} + + void update(); + + const std::string& get_path() { return m_path; } + +private: + std::string m_path; +}; + +} + +#endif diff --git a/src/utils/parse.cc b/src/utils/parse.cc index 3fa1d82f..b3884581 100644 --- a/src/utils/parse.cc +++ b/src/utils/parse.cc @@ -10,7 +10,6 @@ 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) @@ -25,4 +24,16 @@ escape_string(const std::string& src) { return stream.str(); } +std::string +string_to_hex(const std::string& src) { + std::stringstream stream; + + stream << std::hex << std::uppercase; + + for (std::string::const_iterator itr = src.begin(); itr != src.end(); ++itr) + stream << ((unsigned char)*itr >> 4) << ((unsigned char)*itr & 0xf); + + return stream.str(); +} + } diff --git a/src/utils/parse.h b/src/utils/parse.h index 9af051b7..eaaa1512 100644 --- a/src/utils/parse.h +++ b/src/utils/parse.h @@ -8,6 +8,7 @@ namespace utils { // Various functinos for manipulating strings. std::string escape_string(const std::string& src); +std::string string_to_hex(const std::string& src); }