FileSystem.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef FILESYSTEM_H_
  2. #define FILESYSTEM_H_
  3. namespace gameplay
  4. {
  5. class Properties;
  6. /**
  7. * Defines a set of functions for interacting with the device filesystem.
  8. */
  9. class FileSystem
  10. {
  11. public:
  12. /**
  13. * Destructor.
  14. */
  15. ~FileSystem();
  16. /**
  17. * Sets the path to the root of the resources folder for the game.
  18. *
  19. * Once set, all resource/file loading will load from the given path.
  20. * The default resource path is "./".
  21. *
  22. * @param path The path to the root of the resources folder.
  23. */
  24. static void setResourcePath(const char* path);
  25. /**
  26. * Returns the currently set resource path.
  27. *
  28. * @return The currently set resource path.
  29. */
  30. static const char* getResourcePath();
  31. /**
  32. * Loads a properties file containing a list of filesystem aliases.
  33. *
  34. * The specified aliases file is a valid properties file that contains one
  35. * or more namespaces with a list of fielsystem aliases that will be used
  36. * to establish soft links to files when reading files through this class.
  37. *
  38. * This can be helpful for managing loading of resources that may change
  39. * from one platform to another (such as texture formats). An aliases
  40. * file per-platform can be maintained and asset loading code can refer
  41. * to the alias name instead of the actual hard file name.
  42. *
  43. * @param aliasFilePath Path to a properties file containing filesystem aliases.
  44. * @see Properties
  45. */
  46. static void loadResourceAliases(const char* aliasFilePath);
  47. /**
  48. * Loads a set of filesystem aliases from the given Properties object.
  49. *
  50. * The specified properties object contains a single namespace with a list
  51. * of filesystem aliases that will be used to establish soft links to files
  52. * when reading files through this class.
  53. *
  54. * This can be helpful for managing loading of resources that may change
  55. * from one platform to another (such as texture formats). An aliases
  56. * file per-platform can be maintained and asset loading code can refer
  57. * to the alias name instead of the actual hard file name.
  58. *
  59. * @param properties Properties object containing filesystem aliases.
  60. * @see Properties
  61. */
  62. static void loadResourceAliases(Properties* properties);
  63. /**
  64. * Resolves a filesystem path.
  65. *
  66. * If the specified path is a filesystem alias, the alias will be
  67. * resolved and the physical file will be returned.
  68. *
  69. * Note that this method does not convert a relative path to an
  70. * absolute filesystem path.
  71. *
  72. * @param path Path to resolve.
  73. */
  74. static const char* resolvePath(const char* path);
  75. /**
  76. * Lists the files in the specified directory and adds the files to the vector. Excludes directories.
  77. *
  78. * @param dirPath Directory path relative to the path set in <code>setResourcePath(const char*)</code>.
  79. * @param files The vector to append the files to.
  80. *
  81. * @return True if successful, false if error.
  82. * @script{ignore}
  83. */
  84. static bool listFiles(const char* dirPath, std::vector<std::string>& files);
  85. /**
  86. * Checks if the file at the given path exists.
  87. *
  88. * @param filePath The path to the file.
  89. * @return <code>true</code> if the file exists; <code>false</code> otherwise.
  90. */
  91. static bool fileExists(const char* filePath);
  92. /**
  93. * Opens the specified file.
  94. *
  95. * The file at the specified location is opened, relative to the currently set
  96. * resource path.
  97. *
  98. * @param filePath The path to the file to be opened, relative to the currently set resource path.
  99. * @param mode The mode used to open the file, passed directly to fopen.
  100. *
  101. * @see setResourcePath(const char*)
  102. * @script{ignore}
  103. */
  104. static FILE* openFile(const char* filePath, const char* mode);
  105. /**
  106. * Reads the entire contents of the specified file and returns its contents.
  107. *
  108. * The returned character array is allocated with new[] and must therefore
  109. * deleted by the caller using delete[].
  110. *
  111. * @param filePath The path to the file to be read.
  112. * @param fileSize The size of the file in bytes (optional).
  113. *
  114. * @return A newly allocated (NULL-terminated) character array containing the
  115. * contents of the file, or NULL if the file could not be read.
  116. */
  117. static char* readAll(const char* filePath, int* fileSize = NULL);
  118. /**
  119. * Determines if the file path is an absolute path for the current platform.
  120. *
  121. * @param filePath The file path to test.
  122. *
  123. * @return True if the path is an absolute path or false otherwise.
  124. */
  125. static bool isAbsolutePath(const char* filePath);
  126. private:
  127. /**
  128. * Constructor.
  129. */
  130. FileSystem();
  131. };
  132. }
  133. #endif