Return title number from dvd_titles().
authorCarl Hetherington <cth@carlh.net>
Sat, 21 Jul 2012 10:56:23 +0000 (11:56 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 21 Jul 2012 10:56:23 +0000 (11:56 +0100)
src/lib/copy_from_dvd_job.cc
src/lib/dvd.cc
src/lib/dvd.h
test/test.cc

index b82169ad14fd3a81fe79ed8d247cbec935435661..55259eb1eac883f8dab1caa4b8a638897ab98e59 100644 (file)
@@ -58,7 +58,7 @@ CopyFromDVDJob::run ()
                set_state (FINISHED_ERROR);
        }
 
-       vector<uint64_t> const t = dvd_titles (dvd);
+       list<DVDTitle> 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<int>::size_type i = 0; i < t.size(); ++i) {
-               if (longest_size < t[i]) {
-                       longest_size = t[i];
-                       longest_title = i;
+       for (list<DVDTitle>::const_iterator i = t.begin(); i != t.end(); ++i) {
+               if (longest_size < i->size) {
+                       longest_size = i->size;
+                       longest_title = i->number;
                }
        }
 
index 629ba1ac8c21bb2ebb0736248c0a6c13e58050a8..19b59b5881daa7949b82a92047a60e02164c764b 100644 (file)
@@ -20,6 +20,7 @@
 #include <fstream>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
+#include "dvd.h"
 
 using namespace std;
 using namespace boost;
@@ -42,13 +43,13 @@ find_dvd ()
        return "";
 }
 
-vector<uint64_t>
+list<DVDTitle>
 dvd_titles (string dvd)
 {
        filesystem::path video (dvd);
        video /= "VIDEO_TS";
 
-       vector<uint64_t> sizes;
+       list<DVDTitle> 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<DVDTitle>::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;
 }
index 1704721217531afe0b8d822ae888616fc5be4f6f..d3f6043ce7c0006c45e2d3fd19fabdf0a5dea151 100644 (file)
 
 */
 
-extern std::vector<uint64_t> 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<DVDTitle> dvd_titles (std::string);
 extern std::string find_dvd ();
index 3f010379c74429977acdb93f47582b9b425647fb..3128b46290974390b106ef1c42924c4e1fddc0a8 100644 (file)
@@ -125,11 +125,20 @@ BOOST_AUTO_TEST_CASE (util_test)
 
 BOOST_AUTO_TEST_CASE (dvd_test)
 {
-       vector<uint64_t> const t = dvd_titles ("test/dvd");
-       BOOST_CHECK_EQUAL (t.size(), 4);
-       BOOST_CHECK_EQUAL (t[1], 0);
-       BOOST_CHECK_EQUAL (t[2], 14);
-       BOOST_CHECK_EQUAL (t[3], 7);
+       list<DVDTitle> const t = dvd_titles ("test/dvd");
+       BOOST_CHECK_EQUAL (t.size(), 3);
+       list<DVDTitle>::const_iterator i = t.begin ();
+       
+       BOOST_CHECK_EQUAL (i->number, 1);
+       BOOST_CHECK_EQUAL (i->size, 0);
+       ++i;
+       
+       BOOST_CHECK_EQUAL (i->number, 2);
+       BOOST_CHECK_EQUAL (i->size, 14);
+       ++i;
+       
+       BOOST_CHECK_EQUAL (i->number, 3);
+       BOOST_CHECK_EQUAL (i->size, 7);
 }
 
 void