CFileInfo.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "StdInc.h"
  2. #include "CFileInfo.h"
  3. CFileInfo::CFileInfo() : name("")
  4. {
  5. }
  6. CFileInfo::CFileInfo(const std::string & name)
  7. : name(name)
  8. {
  9. }
  10. bool CFileInfo::exists() const
  11. {
  12. return boost::filesystem::exists(name);
  13. }
  14. bool CFileInfo::isDirectory() const
  15. {
  16. return boost::filesystem::is_directory(name);
  17. }
  18. void CFileInfo::setName(const std::string & name)
  19. {
  20. this->name = name;
  21. }
  22. std::string CFileInfo::getName() const
  23. {
  24. return name;
  25. }
  26. std::string CFileInfo::getPath() const
  27. {
  28. size_t found = name.find_last_of("/\\");
  29. return name.substr(0, found);
  30. }
  31. std::string CFileInfo::getExtension() const
  32. {
  33. // Get position of file extension dot
  34. size_t dotPos = name.find_last_of("/.");
  35. if(dotPos != std::string::npos && name[dotPos] == '.')
  36. return name.substr(dotPos);
  37. else
  38. return "";
  39. }
  40. std::string CFileInfo::getFilename() const
  41. {
  42. size_t found = name.find_last_of("/\\");
  43. return name.substr(found + 1);
  44. }
  45. std::string CFileInfo::getStem() const
  46. {
  47. std::string rslt = name;
  48. // Remove file extension
  49. size_t dotPos = name.find_last_of("/.");
  50. if(dotPos != std::string::npos && name[dotPos] == '.')
  51. rslt.erase(dotPos);
  52. // Remove path
  53. size_t found = rslt.find_last_of("/\\");
  54. return rslt.substr(found + 1);
  55. }
  56. EResType CFileInfo::getType() const
  57. {
  58. return EResTypeHelper::getTypeFromExtension(getExtension());
  59. }
  60. std::time_t CFileInfo::getDate() const
  61. {
  62. return boost::filesystem::last_write_time(name);
  63. }
  64. std::unique_ptr<std::list<CFileInfo> > CFileInfo::listFiles(const std::string & extensionFilter /*= ""*/) const
  65. {
  66. std::unique_ptr<std::list<CFileInfo> > fileListPtr;
  67. if(exists() && isDirectory())
  68. {
  69. std::list<CFileInfo> * fileList = new std::list<CFileInfo>;
  70. boost::filesystem::directory_iterator enddir;
  71. for(boost::filesystem::directory_iterator it(name); it != enddir; ++it)
  72. {
  73. if(extensionFilter == "" || it->path().extension() == extensionFilter)
  74. {
  75. CFileInfo file(it->path().string());
  76. fileList->push_back(file);
  77. }
  78. }
  79. fileListPtr.reset(fileList);
  80. }
  81. return fileListPtr;
  82. }