FileSystem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 filesystem 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. *
  45. * @see Properties
  46. */
  47. static void loadResourceAliases(const char* aliasFilePath);
  48. /**
  49. * Loads a set of filesystem aliases from the given Properties object.
  50. *
  51. * The specified properties object contains a single namespace with a list
  52. * of filesystem aliases that will be used to establish soft links to files
  53. * when reading files through this class.
  54. *
  55. * This can be helpful for managing loading of resources that may change
  56. * from one platform to another (such as texture formats). An aliases
  57. * file per-platform can be maintained and asset loading code can refer
  58. * to the alias name instead of the actual hard file name.
  59. *
  60. * @param properties Properties object containing filesystem aliases.
  61. *
  62. * @see Properties
  63. */
  64. static void loadResourceAliases(Properties* properties);
  65. /**
  66. * Resolves a filesystem path.
  67. *
  68. * If the specified path is a filesystem alias, the alias will be
  69. * resolved and the physical file will be returned.
  70. *
  71. * Note that this method does not convert a relative path to an
  72. * absolute filesystem path.
  73. *
  74. * @param path Path to resolve.
  75. *
  76. * @return The resolved file path.
  77. */
  78. static const char* resolvePath(const char* path);
  79. /**
  80. * Lists the files in the specified directory and adds the files to the vector. Excludes directories.
  81. *
  82. * @param dirPath Directory path relative to the path set in <code>setResourcePath(const char*)</code>.
  83. * @param files The vector to append the files to.
  84. *
  85. * @return True if successful, false if error.
  86. *
  87. * @script{ignore}
  88. */
  89. static bool listFiles(const char* dirPath, std::vector<std::string>& files);
  90. /**
  91. * Checks if the file at the given path exists.
  92. *
  93. * @param filePath The path to the file.
  94. *
  95. * @return <code>true</code> if the file exists; <code>false</code> otherwise.
  96. */
  97. static bool fileExists(const char* filePath);
  98. /**
  99. * Opens the specified file.
  100. *
  101. * The file at the specified location is opened, relative to the currently set
  102. * resource path.
  103. *
  104. * @param filePath The path to the file to be opened, relative to the currently set resource path.
  105. * @param mode The mode used to open the file, passed directly to fopen.
  106. *
  107. * @return A pointer to a FILE object that can be used to identify the stream or NULL on error.
  108. *
  109. * @see setResourcePath(const char*)
  110. * @script{ignore}
  111. */
  112. static FILE* openFile(const char* filePath, const char* mode);
  113. /**
  114. * Reads the entire contents of the specified file and returns its contents.
  115. *
  116. * The returned character array is allocated with new[] and must therefore
  117. * deleted by the caller using delete[].
  118. *
  119. * @param filePath The path to the file to be read.
  120. * @param fileSize The size of the file in bytes (optional).
  121. *
  122. * @return A newly allocated (NULL-terminated) character array containing the
  123. * contents of the file, or NULL if the file could not be read.
  124. */
  125. static char* readAll(const char* filePath, int* fileSize = NULL);
  126. /**
  127. * Determines if the file path is an absolute path for the current platform.
  128. *
  129. * @param filePath The file path to test.
  130. *
  131. * @return True if the path is an absolute path or false otherwise.
  132. */
  133. static bool isAbsolutePath(const char* filePath);
  134. /**
  135. * Creates a file on the file system from the specified asset (Android-specific).
  136. *
  137. * @param path The path to the file.
  138. */
  139. static void createFileFromAsset(const char* path);
  140. private:
  141. /**
  142. * Constructor.
  143. */
  144. FileSystem();
  145. };
  146. }
  147. #endif