filepath.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /// Special predefined OS directories.
  15. ///
  16. struct Dir
  17. {
  18. /// Special OS directories:
  19. enum Enum
  20. {
  21. Current, //!< Current directory.
  22. Temp, //!< Temporary directory.
  23. Home, //!< User's home directory.
  24. Count
  25. };
  26. };
  27. /// FilePath parser and helper.
  28. ///
  29. /// /abv/gd/555/333/pod.mac
  30. /// ppppppppppppppppbbbeeee
  31. /// ^ ^ ^
  32. /// +-path base-+ +-ext
  33. /// ^^^^^^^
  34. /// +-filename
  35. ///
  36. class FilePath
  37. {
  38. public:
  39. /// Default constructor, creates empty file path.
  40. ///
  41. FilePath();
  42. /// Construct file path from special OS directory.
  43. ///
  44. FilePath(Dir::Enum _dir);
  45. /// Construct file path from C string.
  46. ///
  47. FilePath(const char* _str);
  48. /// Construct file path from string.
  49. ///
  50. FilePath(const StringView& _str);
  51. /// Assign file path from string.
  52. ///
  53. FilePath& operator=(const StringView& _rhs);
  54. /// Clear file path.
  55. ///
  56. void clear();
  57. /// Set file path from special OS directory.
  58. ///
  59. void set(Dir::Enum _dir);
  60. /// Set file path.
  61. ///
  62. void set(const StringView& _str);
  63. /// Join directory to file path.
  64. ///
  65. void join(const StringView& _str);
  66. /// Returns C string to file path.
  67. ///
  68. const char* get() const;
  69. /// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`.
  70. ///
  71. const StringView getPath() const;
  72. /// If path is `/abv/gd/555/333/pod.mac` returns `pod.mac`.
  73. ///
  74. const StringView getFileName() const;
  75. /// If path is `/abv/gd/555/333/pod.mac` returns `pod`.
  76. ///
  77. const StringView getBaseName() const;
  78. /// If path is `/abv/gd/555/333/pod.mac` returns `.mac`.
  79. ///
  80. const StringView getExt() const;
  81. /// Returns true if file path is absolute.
  82. ///
  83. bool isAbsolute() const;
  84. /// Returns true if file path is empty.
  85. ///
  86. bool isEmpty() const;
  87. private:
  88. char m_filePath[kMaxFilePath];
  89. };
  90. /// Creates a directory named `_filePath`.
  91. bool make(const FilePath& _filePath, Error* _err = NULL);
  92. /// Creates a directory named `_filePath` along with all necessary parents.
  93. bool makeAll(const FilePath& _filePath, Error* _err = NULL);
  94. /// Removes file or directory.
  95. bool remove(const FilePath& _filePath, Error* _err = NULL);
  96. /// Removes file or directory recursivelly.
  97. bool removeAll(const FilePath& _filePath, Error* _err = NULL);
  98. } // namespace bx
  99. #endif // BX_FILEPATH_H_HEADER_GUARD