dir_access.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************/
  2. /* dir_access.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef DIR_ACCESS_H
  30. #define DIR_ACCESS_H
  31. #include "typedefs.h"
  32. #include "ustring.h"
  33. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. //@ TOOD, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies
  37. class DirAccess {
  38. public:
  39. enum AccessType {
  40. ACCESS_RESOURCES,
  41. ACCESS_USERDATA,
  42. ACCESS_FILESYSTEM,
  43. ACCESS_MAX
  44. };
  45. typedef DirAccess*(*CreateFunc)();
  46. private:
  47. AccessType _access_type;
  48. static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object
  49. protected:
  50. String _get_root_path() const;
  51. String _get_root_string() const;
  52. String fix_path(String p_path) const;
  53. bool next_is_dir;
  54. template<class T>
  55. static DirAccess* _create_builtin() {
  56. return memnew( T );
  57. }
  58. public:
  59. static String normalize_path(const String& p_path);
  60. virtual bool list_dir_begin()=0; ///< This starts dir listing
  61. virtual String get_next(bool* p_is_dir); // compatibility
  62. virtual String get_next()=0;
  63. virtual bool current_is_dir() const=0;
  64. virtual bool current_is_hidden() const=0;
  65. virtual void list_dir_end()=0; ///<
  66. virtual int get_drive_count()=0;
  67. virtual String get_drive(int p_drive)=0;
  68. virtual int get_current_drive();
  69. virtual Error change_dir(String p_dir)=0; ///< can be relative or absolute, return false on success
  70. virtual String get_current_dir()=0; ///< return current dir location
  71. virtual Error make_dir(String p_dir)=0;
  72. virtual Error make_dir_recursive(String p_dir);
  73. virtual Error erase_contents_recursive(); //super dangerous, use with care!
  74. virtual bool file_exists(String p_file)=0;
  75. virtual bool dir_exists(String p_dir)=0;
  76. static bool exists(String p_dir);
  77. virtual size_t get_space_left()=0;
  78. virtual Error copy(String p_from,String p_to);
  79. virtual Error rename(String p_from, String p_to)=0;
  80. virtual Error remove(String p_name)=0;
  81. static String get_full_path(const String& p_path,AccessType p_access);
  82. static DirAccess *create_for_path(const String& p_path);
  83. /*
  84. enum DirType {
  85. FILE_TYPE_INVALID,
  86. FILE_TYPE_FILE,
  87. FILE_TYPE_DIR,
  88. };
  89. //virtual DirType get_file_type() const=0;
  90. */
  91. static DirAccess *create(AccessType p_access);
  92. template<class T>
  93. static void make_default(AccessType p_access) {
  94. create_func[p_access]=_create_builtin<T>;
  95. }
  96. static DirAccess *open(const String& p_path,Error *r_error=NULL);
  97. DirAccess();
  98. virtual ~DirAccess();
  99. };
  100. struct DirAccessRef {
  101. _FORCE_INLINE_ DirAccess* operator->() {
  102. return f;
  103. }
  104. operator bool() const { return f!=NULL; }
  105. DirAccess *f;
  106. DirAccessRef(DirAccess *fa) { f = fa; }
  107. ~DirAccessRef() { if (f) memdelete(f); }
  108. };
  109. #endif