CFileUtility.cpp 1.2 KB

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