Filesystem.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2009-2022, 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. namespace anki {
  7. void getFilepathExtension(const CString& filename, StringAuto& out)
  8. {
  9. const Char* pc = std::strrchr(filename.cstr(), '.');
  10. if(pc == nullptr)
  11. {
  12. // Do nothing
  13. }
  14. else
  15. {
  16. ++pc;
  17. if(*pc != '\0')
  18. {
  19. out.create(CString(pc));
  20. }
  21. }
  22. }
  23. void getFilepathFilename(const CString& filename, StringAuto& out)
  24. {
  25. const Char* pc1 = std::strrchr(filename.cstr(), '/');
  26. #if ANKI_OS_WINDOWS
  27. const Char* pc2 = std::strrchr(filename.cstr(), '\\');
  28. #else
  29. const Char* pc2 = pc1;
  30. #endif
  31. const Char* pc = (pc1 > pc2) ? pc1 : pc2;
  32. if(pc == nullptr)
  33. {
  34. out.create(filename);
  35. }
  36. else
  37. {
  38. ++pc;
  39. if(*pc != '\0')
  40. {
  41. out.create(CString(pc));
  42. }
  43. }
  44. }
  45. void getParentFilepath(const CString& filename, StringAuto& out)
  46. {
  47. const Char* pc1 = std::strrchr(filename.cstr(), '/');
  48. #if ANKI_OS_WINDOWS
  49. const Char* pc2 = std::strrchr(filename.cstr(), '\\');
  50. #else
  51. const Char* pc2 = pc1;
  52. #endif
  53. const Char* pc = (pc1 > pc2) ? pc1 : pc2;
  54. if(pc == nullptr)
  55. {
  56. out.create("");
  57. }
  58. else
  59. {
  60. out.create(filename.cstr(), pc);
  61. }
  62. }
  63. Error removeFile(const CString& filename)
  64. {
  65. const int err = std::remove(filename.cstr());
  66. if(err)
  67. {
  68. ANKI_UTIL_LOGE("Couldn't delete file: %s", filename.cstr());
  69. }
  70. return Error::NONE;
  71. }
  72. } // end namespace anki