#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
-#include <dcp/raw_convert.h>
+#include <boost/foreach.hpp>
+#include "raw_convert.h"
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 libdcp::raw_convert<int> (number);
+ return numbers_as_int;
}
};