CFileUtility.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. namespace fs = boost::filesystem;
  6. CFileUtility::CFileUtility(void)
  7. {
  8. }
  9. CFileUtility::~CFileUtility(void)
  10. {
  11. }
  12. void CFileUtility::getFilesWithExt(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  13. {
  14. if(!fs::exists(dirname))
  15. {
  16. tlog1 << "Cannot find " << dirname << " directory!\n";
  17. }
  18. fs::path tie(dirname);
  19. fs::directory_iterator end_iter;
  20. for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
  21. {
  22. if(fs::is_regular_file(file->status())
  23. && boost::ends_with(file->path().filename(), ext))
  24. {
  25. std::time_t date = 0;
  26. try
  27. {
  28. date = fs::last_write_time(file->path());
  29. out.resize(out.size()+1);
  30. out.back().date = date;
  31. out.back().name = file->path().string();
  32. }
  33. catch(...)
  34. {
  35. tlog2 << "\t\tWarning: very corrupted file: " << file->path().string() << std::endl;
  36. }
  37. }
  38. }
  39. }