CFileUtility.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #define VCMI_DLL
  2. #include "CFileUtility.h"
  3. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  4. #include <boost/algorithm/string/predicate.hpp>
  5. /*
  6. * CFileUtility.cpp, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. namespace fs = boost::filesystem;
  15. CFileUtility::CFileUtility(void)
  16. {
  17. }
  18. CFileUtility::~CFileUtility(void)
  19. {
  20. }
  21. void CFileUtility::getFilesWithExt(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  22. {
  23. if(!fs::exists(dirname))
  24. {
  25. tlog1 << "Cannot find " << dirname << " directory!\n";
  26. }
  27. fs::path tie(dirname);
  28. fs::directory_iterator end_iter;
  29. for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
  30. {
  31. if(fs::is_regular_file(file->status())
  32. && boost::ends_with(file->path().filename(), ext))
  33. {
  34. std::time_t date = 0;
  35. try
  36. {
  37. date = fs::last_write_time(file->path());
  38. out.resize(out.size()+1);
  39. out.back().date = date;
  40. out.back().name = file->path().string();
  41. }
  42. catch(...)
  43. {
  44. tlog2 << "\t\tWarning: very corrupted file: " << file->path().string() << std::endl;
  45. }
  46. }
  47. }
  48. }