Fix slightly odd #includes of image_filename_sorter.cc
[dcpomatic.git] / src / lib / image_filename_sorter.cc
index 805c469b03f14642218190511bada56aa4bc6500..112077f55fa3d0b43f908e60d7a21452dc2cb4e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
 
     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
@@ -19,7 +19,7 @@
 
 #include "raw_convert.h"
 #include <boost/filesystem.hpp>
-#include <boost/optional.hpp>
+#include <boost/foreach.hpp>
 #include <iostream>
 
 class ImageFilenameSorter
@@ -27,36 +27,60 @@ class ImageFilenameSorter
 public:
        bool operator() (boost::filesystem::path a, boost::filesystem::path b)
        {
-               boost::optional<int> na = extract_number (a);
-               boost::optional<int> nb = extract_number (b);
-               if (!na || !nb) {
-                       std::cout << a << " " << b << " " << (a.string() < b.string()) << "\n";
+               std::list<int> na = extract_numbers (a);
+               std::list<int> nb = extract_numbers (b);
+               if (na.empty() || nb.empty()) {
                        return a.string() < b.string();
                }
 
-               return na.get() < nb.get();
+               if (na.size() != nb.size()) {
+                       /* Just use the first one */
+                       return na.front() < nb.front();
+               }
+
+               std::list<int>::const_iterator i = na.begin ();
+               std::list<int>::const_iterator j = nb.begin ();
+
+               while (i != na.end()) {
+                       if (*i != *j) {
+                               return *i < *j;
+                       }
+                       ++i;
+                       ++j;
+               }
+
+               /* All the same */
+               return false;
        }
 
 private:
-       boost::optional<int> extract_number (boost::filesystem::path p)
+       std::list<int> extract_numbers (boost::filesystem::path p)
        {
                p = p.leaf ();
-               
-               std::string number;
+
+               std::list<std::string> numbers;
+
+               std::string current;
                for (size_t i = 0; i < p.string().size(); ++i) {
                        if (isdigit (p.string()[i])) {
-                               number += p.string()[i];
+                               current += p.string()[i];
                        } else {
-                               if (!number.empty ()) {
-                                       break;
+                               if (!current.empty ()) {
+                                       numbers.push_back (current);
+                                       current.clear ();
                                }
                        }
                }
 
-               if (number.empty ()) {
-                       return boost::optional<int> ();
+               if (!current.empty ()) {
+                       numbers.push_back (current);
+               }
+
+               std::list<int> numbers_as_int;
+               BOOST_FOREACH (std::string i, numbers) {
+                       numbers_as_int.push_back (raw_convert<int> (i));
                }
 
-               return raw_convert<int> (number);
+               return numbers_as_int;
        }
 };