*/
-#include "raw_convert.h"
+#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
-#include <iostream>
+#include <boost/foreach.hpp>
+#include "raw_convert.h"
class ImageFilenameSorter
{
public:
bool operator() (boost::filesystem::path a, boost::filesystem::path b)
{
- std::vector<int> na = extract_numbers (a);
- std::vector<int> nb = extract_numbers (b);
+ std::list<int> na = extract_numbers (a);
+ std::list<int> nb = extract_numbers (b);
+ if (na.empty() || nb.empty()) {
+ return a.string() < b.string();
+ }
- std::vector<int>::const_iterator i = na.begin ();
- std::vector<int>::const_iterator j = nb.begin ();
+ if (na.size() != nb.size()) {
+ /* Just use the first one */
+ return na.front() < nb.front();
+ }
- while (true) {
- if (i == na.end () || j == nb.end ()) {
- return false;
- }
+ 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;
}
- /* NOT REACHED */
+ /* All the same */
return false;
}
private:
- std::vector<int> extract_numbers (boost::filesystem::path p)
+ std::list<int> extract_numbers (boost::filesystem::path p)
{
p = p.leaf ();
- std::vector<int> numbers;
- 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];
- } else if (!number.empty ()) {
- numbers.push_back (raw_convert<int> (number));
- number.clear ();
+ current += p.string()[i];
+ } else {
+ if (!current.empty ()) {
+ numbers.push_back (current);
+ current.clear ();
+ }
}
}
- if (!number.empty ()) {
- numbers.push_back (raw_convert<int> (number));
+ 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 numbers;
+ return numbers_as_int;
}
};