next_filename.cpp 974 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  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. using namespace std;
  20. // O(n), for huge lists could at least find bounds with exponential search
  21. // and then narrow with binary search O(log(n))
  22. int i = 0;
  23. while(true)
  24. {
  25. next = STR(prefix << setfill('0') << setw(zeros)<<i<<suffix);
  26. if(!file_exists(next))
  27. {
  28. return true;
  29. }
  30. i++;
  31. if(zeros > 0 && i >= pow(10,zeros))
  32. {
  33. return false;
  34. }
  35. }
  36. }