FileSystem.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef FILESYSTEM_H_
  2. #define FILESYSTEM_H_
  3. #include "Stream.h"
  4. namespace gameplay
  5. {
  6. class Properties;
  7. /**
  8. * Defines a set of functions for interacting with the device filesystem.
  9. */
  10. class FileSystem
  11. {
  12. public:
  13. /**
  14. * Mode flags for opening a stream.
  15. * @script{ignore}
  16. */
  17. enum StreamMode
  18. {
  19. READ = 1,
  20. WRITE = 2
  21. };
  22. /**
  23. * Destructor.
  24. */
  25. ~FileSystem();
  26. /**
  27. * Sets the path to the root of the resources folder for the game.
  28. *
  29. * Once set, all resource/file loading will load from the given path.
  30. * The default resource path is "./".
  31. *
  32. * @param path The path to the root of the resources folder.
  33. */
  34. static void setResourcePath(const char* path);
  35. /**
  36. * Returns the currently set resource path.
  37. *
  38. * @return The currently set resource path.
  39. */
  40. static const char* getResourcePath();
  41. /**
  42. * Loads a properties file containing a list of filesystem aliases.
  43. *
  44. * The specified aliases file is a valid properties file that contains one
  45. * or more namespaces with a list of filesystem aliases that will be used
  46. * to establish soft links to files when reading files through this class.
  47. *
  48. * This can be helpful for managing loading of resources that may change
  49. * from one platform to another (such as texture formats). An aliases
  50. * file per-platform can be maintained and asset loading code can refer
  51. * to the alias name instead of the actual hard file name.
  52. *
  53. * @param aliasFilePath Path to a properties file containing filesystem aliases.
  54. *
  55. * @see Properties
  56. */
  57. static void loadResourceAliases(const char* aliasFilePath);
  58. /**
  59. * Loads a set of filesystem aliases from the given Properties object.
  60. *
  61. * The specified properties object contains a single namespace with a list
  62. * of filesystem aliases that will be used to establish soft links to files
  63. * when reading files through this class.
  64. *
  65. * This can be helpful for managing loading of resources that may change
  66. * from one platform to another (such as texture formats). An aliases
  67. * file per-platform can be maintained and asset loading code can refer
  68. * to the alias name instead of the actual hard file name.
  69. *
  70. * @param properties Properties object containing filesystem aliases.
  71. *
  72. * @see Properties
  73. */
  74. static void loadResourceAliases(Properties* properties);
  75. /**
  76. * Resolves a filesystem path.
  77. *
  78. * If the specified path is a filesystem alias, the alias will be
  79. * resolved and the physical file will be returned.
  80. *
  81. * Note that this method does not convert a relative path to an
  82. * absolute filesystem path.
  83. *
  84. * @param path Path to resolve.
  85. *
  86. * @return The resolved file path.
  87. */
  88. static const char* resolvePath(const char* path);
  89. /**
  90. * Lists the files in the specified directory and adds the files to the vector. Excludes directories.
  91. *
  92. * @param dirPath Directory path relative to the path set in <code>setResourcePath(const char*)</code>.
  93. * @param files The vector to append the files to.
  94. *
  95. * @return True if successful, false if error.
  96. *
  97. * @script{ignore}
  98. */
  99. static bool listFiles(const char* dirPath, std::vector<std::string>& files);
  100. /**
  101. * Checks if the file at the given path exists.
  102. *
  103. * @param filePath The path to the file.
  104. *
  105. * @return <code>true</code> if the file exists; <code>false</code> otherwise.
  106. */
  107. static bool fileExists(const char* filePath);
  108. /**
  109. * Opens a byte stream for the given resource path.
  110. *
  111. * If <code>path</code> is a file path, the file at the specified location is opened relative to the currently set
  112. * resource path.
  113. *
  114. * @param path The path to the resource to be opened, relative to the currently set resource path.
  115. * @param mode The mode used to open the file.
  116. *
  117. * @return A stream that can be used to read or write to the file depending on the mode.
  118. * Returns NULL if there was an error. (Request mode not supported).
  119. *
  120. * @script{ignore}
  121. */
  122. static Stream* open(const char* path, size_t mode = READ);
  123. /**
  124. * Opens the specified file.
  125. *
  126. * The file at the specified location is opened, relative to the currently set
  127. * resource path.
  128. *
  129. * @param filePath The path to the file to be opened, relative to the currently set resource path.
  130. * @param mode The mode used to open the file, passed directly to fopen.
  131. *
  132. * @return A pointer to a FILE object that can be used to identify the stream or NULL on error.
  133. *
  134. * @see setResourcePath(const char*)
  135. * @script{ignore}
  136. */
  137. static FILE* openFile(const char* filePath, const char* mode);
  138. /**
  139. * Reads the entire contents of the specified file and returns its contents.
  140. *
  141. * The returned character array is allocated with new[] and must therefore
  142. * deleted by the caller using delete[].
  143. *
  144. * @param filePath The path to the file to be read.
  145. * @param fileSize The size of the file in bytes (optional).
  146. *
  147. * @return A newly allocated (NULL-terminated) character array containing the
  148. * contents of the file, or NULL if the file could not be read.
  149. */
  150. static char* readAll(const char* filePath, int* fileSize = NULL);
  151. /**
  152. * Determines if the file path is an absolute path for the current platform.
  153. *
  154. * @param filePath The file path to test.
  155. *
  156. * @return True if the path is an absolute path or false otherwise.
  157. */
  158. static bool isAbsolutePath(const char* filePath);
  159. /**
  160. * Creates a file on the file system from the specified asset (Android-specific).
  161. *
  162. * @param path The path to the file.
  163. */
  164. static void createFileFromAsset(const char* path);
  165. /**
  166. * Returns the extension of the given file path.
  167. *
  168. * The extension returned includes all character after and including the last '.'
  169. * in the file path. The extension is returned as all uppercase.
  170. *
  171. * If the path does not contain an extension, an empty string is returned.
  172. *
  173. * @param path File path.
  174. *
  175. * @return The file extension, all uppercase, including the '.'.
  176. */
  177. static std::string getExtension(const char* path);
  178. private:
  179. /**
  180. * Constructor.
  181. */
  182. FileSystem();
  183. };
  184. }
  185. #endif