From 5ec26979d2547b7444c4e1f00aa76d86d22f4c84 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sat, 21 Jul 2012 11:56:23 +0100 Subject: Return title number from dvd_titles(). --- src/lib/copy_from_dvd_job.cc | 10 +++++----- src/lib/dvd.cc | 31 ++++++++++++++++++++++++------- src/lib/dvd.h | 14 +++++++++++++- 3 files changed, 42 insertions(+), 13 deletions(-) (limited to 'src/lib') diff --git a/src/lib/copy_from_dvd_job.cc b/src/lib/copy_from_dvd_job.cc index b82169ad1..55259eb1e 100644 --- a/src/lib/copy_from_dvd_job.cc +++ b/src/lib/copy_from_dvd_job.cc @@ -58,7 +58,7 @@ CopyFromDVDJob::run () set_state (FINISHED_ERROR); } - vector const t = dvd_titles (dvd); + list const t = dvd_titles (dvd); if (t.empty ()) { set_error ("no titles found on DVD"); set_state (FINISHED_ERROR); @@ -66,10 +66,10 @@ CopyFromDVDJob::run () int longest_title = 0; uint64_t longest_size = 0; - for (vector::size_type i = 0; i < t.size(); ++i) { - if (longest_size < t[i]) { - longest_size = t[i]; - longest_title = i; + for (list::const_iterator i = t.begin(); i != t.end(); ++i) { + if (longest_size < i->size) { + longest_size = i->size; + longest_title = i->number; } } diff --git a/src/lib/dvd.cc b/src/lib/dvd.cc index 629ba1ac8..19b59b588 100644 --- a/src/lib/dvd.cc +++ b/src/lib/dvd.cc @@ -20,6 +20,7 @@ #include #include #include +#include "dvd.h" using namespace std; using namespace boost; @@ -42,13 +43,13 @@ find_dvd () return ""; } -vector +list dvd_titles (string dvd) { filesystem::path video (dvd); video /= "VIDEO_TS"; - vector sizes; + list titles; for (filesystem::directory_iterator i = filesystem::directory_iterator (video); i != filesystem::directory_iterator(); ++i) { #if BOOST_FILESYSTEM_VERSION == 3 @@ -63,16 +64,32 @@ dvd_titles (string dvd) if (p.size() == 4) { int const a = atoi (p[1].c_str ()); int const b = atoi (p[2].c_str ()); - while (a >= int (sizes.size())) { - sizes.push_back (0); + if (b == 0) { + continue; } - if (b > 0) { - sizes[a] += size; + list::iterator j = titles.begin (); + while (j != titles.end() && j->number != a) { + ++j; + } + + if (j == titles.end ()) { + titles.push_back (DVDTitle (a, size)); + } else { + j->size += size; } } } } + + titles.sort (); - return sizes; + return titles; +} + + +bool +operator< (DVDTitle const & a, DVDTitle const & b) +{ + return a.number < b.number; } diff --git a/src/lib/dvd.h b/src/lib/dvd.h index 170472121..d3f6043ce 100644 --- a/src/lib/dvd.h +++ b/src/lib/dvd.h @@ -17,5 +17,17 @@ */ -extern std::vector dvd_titles (std::string); +class DVDTitle +{ +public: + DVDTitle () : number (-1), size (0) {} + DVDTitle (int n, int s) : number (n), size (s) {} + + int number; + uint64_t size; +}; + +extern bool operator< (DVDTitle const &, DVDTitle const &); + +extern std::list dvd_titles (std::string); extern std::string find_dvd (); -- cgit v1.2.3