filepath.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #ifndef BX_FILEPATH_H_HEADER_GUARD
  6. #define BX_FILEPATH_H_HEADER_GUARD
  7. #include "error.h"
  8. #include "string.h"
  9. BX_ERROR_RESULT(BX_ERROR_ACCESS, BX_MAKEFOURCC('b', 'x', 0, 0) );
  10. BX_ERROR_RESULT(BX_ERROR_NOT_DIRECTORY, BX_MAKEFOURCC('b', 'x', 0, 1) );
  11. namespace bx
  12. {
  13. const int32_t kMaxFilePath = 1024;
  14. ///
  15. struct Dir
  16. {
  17. enum Enum ///
  18. {
  19. Current,
  20. Temp,
  21. Home,
  22. Count
  23. };
  24. };
  25. /// FilePath parser and helper.
  26. ///
  27. /// /abv/gd/555/333/pod.mac
  28. /// ppppppppppppppppbbbeeee
  29. /// ^ ^ ^
  30. /// +-path base-+ +-ext
  31. /// ^^^^^^^
  32. /// +-filename
  33. ///
  34. class FilePath
  35. {
  36. public:
  37. ///
  38. FilePath();
  39. ///
  40. FilePath(Dir::Enum _dir);
  41. ///
  42. FilePath(const char* _str);
  43. ///
  44. FilePath(const StringView& _str);
  45. ///
  46. FilePath& operator=(const StringView& _rhs);
  47. ///
  48. void clear();
  49. ///
  50. void set(Dir::Enum _dir);
  51. ///
  52. void set(const StringView& _str);
  53. ///
  54. void join(const StringView& _str);
  55. ///
  56. const char* get() const;
  57. /// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`.
  58. ///
  59. const StringView getPath() const;
  60. /// If path is `/abv/gd/555/333/pod.mac` returns `pod.mac`.
  61. ///
  62. const StringView getFileName() const;
  63. /// If path is `/abv/gd/555/333/pod.mac` returns `pod`.
  64. ///
  65. const StringView getBaseName() const;
  66. /// If path is `/abv/gd/555/333/pod.mac` returns `.mac`.
  67. ///
  68. const StringView getExt() const;
  69. ///
  70. bool isAbsolute() const;
  71. ///
  72. bool isEmpty() const;
  73. private:
  74. char m_filePath[kMaxFilePath];
  75. };
  76. /// Creates a directory named `_filePath`.
  77. bool make(const FilePath& _filePath, Error* _err = NULL);
  78. /// Creates a directory named `_filePath` along with all necessary parents.
  79. bool makeAll(const FilePath& _filePath, Error* _err = NULL);
  80. /// Removes file or directory.
  81. bool remove(const FilePath& _filePath, Error* _err = NULL);
  82. /// Removes file or directory recursivelly.
  83. bool removeAll(const FilePath& _filePath, Error* _err = NULL);
  84. } // namespace bx
  85. #endif // BX_FILEPATH_H_HEADER_GUARD