next_filename.cpp 961 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "next_filename.h"
  9. #include "STR.h"
  10. #include "file_exists.h"
  11. #include <cmath>
  12. #include <iomanip>
  13. bool igl::next_filename(
  14. const std::string & prefix,
  15. const int zeros,
  16. const std::string & suffix,
  17. std::string & next)
  18. {
  19. // O(n), for huge lists could at least find bounds with exponential search
  20. // and then narrow with binary search O(log(n))
  21. int i = 0;
  22. while(true)
  23. {
  24. next = STR(prefix << std::setfill('0') << std::setw(zeros)<<i<<suffix);
  25. if(!file_exists(next))
  26. {
  27. return true;
  28. }
  29. i++;
  30. if(zeros > 0 && i >= pow(10,zeros))
  31. {
  32. return false;
  33. }
  34. }
  35. }