Filesystem.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/Filesystem.h>
  6. #include <AnKi/Util/HighRezTimer.h>
  7. namespace anki {
  8. void getFilepathExtension(const CString& filename, String& out)
  9. {
  10. const Char* pc = std::strrchr(filename.cstr(), '.');
  11. if(pc == nullptr)
  12. {
  13. // Do nothing
  14. }
  15. else
  16. {
  17. ++pc;
  18. if(*pc != '\0')
  19. {
  20. out = pc;
  21. }
  22. }
  23. }
  24. void getFilepathFilename(const CString& filename, String& out)
  25. {
  26. const Char* pc1 = std::strrchr(filename.cstr(), '/');
  27. #if ANKI_OS_WINDOWS
  28. const Char* pc2 = std::strrchr(filename.cstr(), '\\');
  29. #else
  30. const Char* pc2 = pc1;
  31. #endif
  32. const Char* pc = (pc1 > pc2) ? pc1 : pc2;
  33. if(pc == nullptr)
  34. {
  35. out = filename;
  36. }
  37. else
  38. {
  39. ++pc;
  40. if(*pc != '\0')
  41. {
  42. out = pc;
  43. }
  44. }
  45. }
  46. void getParentFilepath(const CString& filename, String& out)
  47. {
  48. const Char* pc1 = std::strrchr(filename.cstr(), '/');
  49. #if ANKI_OS_WINDOWS
  50. const Char* pc2 = std::strrchr(filename.cstr(), '\\');
  51. #else
  52. const Char* pc2 = pc1;
  53. #endif
  54. const Char* pc = (pc1 > pc2) ? pc1 : pc2;
  55. if(pc == nullptr)
  56. {
  57. out = "";
  58. }
  59. else
  60. {
  61. out = String(filename.cstr(), pc);
  62. }
  63. }
  64. Error removeFile(const CString& filename)
  65. {
  66. const int err = std::remove(filename.cstr());
  67. if(err)
  68. {
  69. ANKI_UTIL_LOGE("Couldn't delete file (%s): %s", strerror(errno), filename.cstr());
  70. return Error::kFunctionFailed;
  71. }
  72. return Error::kNone;
  73. }
  74. CleanupFile::~CleanupFile()
  75. {
  76. if(!m_fileToDelete.isEmpty() && fileExists(m_fileToDelete))
  77. {
  78. // Loop until success because Windows antivirus may be keeping the file in use
  79. while(std::remove(m_fileToDelete.cstr()) && m_tries > 0)
  80. {
  81. HighRezTimer::sleep(m_seepTimeBeforeNextTry);
  82. --m_tries;
  83. }
  84. }
  85. }
  86. } // end namespace anki