diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-10-28 11:29:55 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-10-28 11:29:55 +0000 |
| commit | d71cadd49158dde3fbb1eab5ee41943c288b45d0 (patch) | |
| tree | 07d4e3c45704195434ea3c62c55424930dc8ca69 /src | |
| parent | 2bbe490b330e858d9f3ab6822cc6bd8e9ac7da4c (diff) | |
More tweaks to image filename ordering.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/image_filename_sorter.cc | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/src/lib/image_filename_sorter.cc b/src/lib/image_filename_sorter.cc index 69114b2bd..143daed71 100644 --- a/src/lib/image_filename_sorter.cc +++ b/src/lib/image_filename_sorter.cc @@ -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; } }; |
