X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fimage_filename_sorter.cc;h=f0370bdcb5d120bde7a9b06aabb5ea4aad9b2caa;hb=742d96e86a262d0238105bc869ec8f350e36ccae;hp=c32b07115245145061bd4c87c206db9c565cb3a0;hpb=fd30397b1a1d0df6b43aedbb205913d2eec09fd0;p=dcpomatic.git diff --git a/src/lib/image_filename_sorter.cc b/src/lib/image_filename_sorter.cc index c32b07115..f0370bdcb 100644 --- a/src/lib/image_filename_sorter.cc +++ b/src/lib/image_filename_sorter.cc @@ -18,47 +18,47 @@ */ + #include "image_filename_sorter.h" #include #include -#include #include #include + using std::list; using std::string; using dcp::locale_convert; using boost::optional; + bool ImageFilenameSorter::operator() (boost::filesystem::path a, boost::filesystem::path b) { - optional na = extract_numbers (a); - optional nb = extract_numbers (b); - if (!na || !nb) { - return a.string() < b.string(); + auto an = extract_numbers (a); + auto bn = extract_numbers (b); + + int const anl = an.length (); + int const bnl = bn.length (); + + if (anl > bnl) { + bn = string(anl - bnl, '0') + bn; + } else if (bnl > anl) { + an = string(bnl - anl, '0') + an; } - return *na < *nb; + return an < bn; } -optional +string ImageFilenameSorter::extract_numbers (boost::filesystem::path p) { string numbers; - string const ps = p.leaf().string(); + auto const ps = p.filename().string(); for (size_t i = 0; i < ps.size(); ++i) { if (isdigit (ps[i])) { numbers += ps[i]; } } - - if (numbers.empty ()) { - return optional (); - } - - /* locale_convert is quicker than raw_convert and numbers can only contain - things which are isdigit() so locale_convert is fine to use. - */ - return locale_convert (numbers); + return numbers; }