X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fimage_filename_sorter.cc;h=ab0d298fc1bd0f9bcceb881a83744d9157681433;hb=d311043bf3c1e3e7f41b314f7ab7c91ed7e5aa7f;hp=143daed7187d2c720b306de800f6250875e1d164;hpb=d71cadd49158dde3fbb1eab5ee41943c288b45d0;p=dcpomatic.git diff --git a/src/lib/image_filename_sorter.cc b/src/lib/image_filename_sorter.cc index 143daed71..ab0d298fc 100644 --- a/src/lib/image_filename_sorter.cc +++ b/src/lib/image_filename_sorter.cc @@ -1,87 +1,64 @@ /* - Copyright (C) 2015 Carl Hetherington + Copyright (C) 2015-2017 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ -#include + +#include "image_filename_sorter.h" +#include #include #include -#include -#include "raw_convert.h" - -class ImageFilenameSorter -{ -public: - bool operator() (boost::filesystem::path a, boost::filesystem::path b) - { - std::list na = extract_numbers (a); - std::list nb = extract_numbers (b); - if (na.empty() || nb.empty()) { - return a.string() < b.string(); - } +#include - if (na.size() != nb.size()) { - /* Just use the first one */ - return na.front() < nb.front(); - } - std::list::const_iterator i = na.begin (); - std::list::const_iterator j = nb.begin (); +using std::list; +using std::string; +using dcp::locale_convert; +using boost::optional; - while (i != na.end()) { - if (*i != *j) { - return *i < *j; - } - ++i; - ++j; - } - /* All the same */ - return false; - } - -private: - std::list extract_numbers (boost::filesystem::path p) - { - p = p.leaf (); +bool +ImageFilenameSorter::operator() (boost::filesystem::path a, boost::filesystem::path b) +{ + auto an = extract_numbers (a); + auto bn = extract_numbers (b); - std::list numbers; + int const anl = an.length (); + int const bnl = bn.length (); - std::string current; - for (size_t i = 0; i < p.string().size(); ++i) { - if (isdigit (p.string()[i])) { - current += p.string()[i]; - } else { - if (!current.empty ()) { - numbers.push_back (current); - current.clear (); - } - } - } + if (anl > bnl) { + bn = string(anl - bnl, '0') + bn; + } else if (bnl > anl) { + an = string(bnl - anl, '0') + an; + } - if (!current.empty ()) { - numbers.push_back (current); - } + return an < bn; +} - std::list numbers_as_int; - BOOST_FOREACH (std::string i, numbers) { - numbers_as_int.push_back (raw_convert (i)); +string +ImageFilenameSorter::extract_numbers (boost::filesystem::path p) +{ + string numbers; + auto const ps = p.leaf().string(); + for (size_t i = 0; i < ps.size(); ++i) { + if (isdigit (ps[i])) { + numbers += ps[i]; } - - return numbers_as_int; } -}; + return numbers; +}