More tweaks to image filename ordering.
authorCarl Hetherington <cth@carlh.net>
Wed, 28 Oct 2015 11:29:55 +0000 (11:29 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 28 Oct 2015 11:29:55 +0000 (11:29 +0000)
ChangeLog
src/lib/image_filename_sorter.cc
test/image_filename_sorter_test.cc

index d532d99a7e1ae739fa626446a55d22ee69e05903..4aaf68af93dafa3af0653c44066edb65567f29fe 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2015-10-28  Carl Hetherington  <cth@carlh.net>
 
+       * Fix ordering of filename in image sets in some cases
+       (e.g. foo_01042015_00000, foo_01042015_00001...)
+
        * Updated da_DK translation from Anders Uhl Pedersen.
 
        * Fix erroneous addition of referenced assets to PKLs (#723).
index 69114b2bd46a569a2844b975baa4feed489fc99d..143daed7187d2c720b306de800f6250875e1d164 100644 (file)
@@ -20,6 +20,7 @@
 #include <iostream>
 #include <boost/filesystem.hpp>
 #include <boost/optional.hpp>
+#include <boost/foreach.hpp>
 #include "raw_convert.h"
 
 class ImageFilenameSorter
@@ -27,20 +28,37 @@ 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::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::list<std::string> numbers;
 
                std::string current;
@@ -59,17 +77,11 @@ private:
                        numbers.push_back (current);
                }
 
-               std::string longest;
-               for (std::list<std::string>::const_iterator i = numbers.begin(); i != numbers.end(); ++i) {
-                       if (i->length() > longest.length()) {
-                               longest = *i;
-                       }
-               }
-
-               if (longest.empty ()) {
-                       return boost::optional<int> ();
+               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> (longest);
+               return numbers_as_int;
        }
 };
index 4bdd16f5547d663f99fcceda0465af5dff6c7acf..a4762a8c6943713f85c4e6c908beaa07b2066e84 100644 (file)
@@ -32,6 +32,7 @@ BOOST_AUTO_TEST_CASE (image_filename_sorter_test)
        BOOST_CHECK (x ("00057.tif", "00166.tif"));
        BOOST_CHECK (x ("/my/numeric999/path/00057.tif", "/my/numeric999/path/00166.tif"));
        BOOST_CHECK (x ("1_01.tif", "1_02.tif"));
+       BOOST_CHECK (x ("EWS_DCP_092815_000000.j2c", "EWS_DCP_092815_000001.j2c"));
 
        BOOST_CHECK (!x ("abc0000000002", "abc0000000001"));
        BOOST_CHECK (!x ("2", "1"));
@@ -40,4 +41,6 @@ BOOST_AUTO_TEST_CASE (image_filename_sorter_test)
        BOOST_CHECK (!x ("999", "1"));
        BOOST_CHECK (!x ("/my/numeric999/path/00166.tif", "/my/numeric999/path/00057.tif"));
        BOOST_CHECK (!x ("1_02.tif", "1_01.tif"));
+       BOOST_CHECK (!x ("EWS_DCP_092815_000000.j2c", "EWS_DCP_092815_000000.j2c"));
+       BOOST_CHECK (!x ("EWS_DCP_092815_000100.j2c", "EWS_DCP_092815_000000.j2c"));
 }