Filesystem.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Copyright (c) 2006-2012 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
  21. #define LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
  22. // STD
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <iostream>
  26. #include <string>
  27. // LOVE
  28. #include "common/Module.h"
  29. #include "common/config.h"
  30. #include "common/int.h"
  31. #include "filesystem/FileData.h"
  32. #include "File.h"
  33. // For great CWD. (Current Working Directory)
  34. // Using this instead of boost::filesystem which totally
  35. // cramped our style.
  36. #ifdef LOVE_WINDOWS
  37. # include <windows.h>
  38. # include <direct.h>
  39. #else
  40. # include <sys/param.h>
  41. # include <unistd.h>
  42. #endif
  43. // In Windows, we would like to use "LOVE" as the
  44. // application folder, but in Linux, we like .love.
  45. #define LOVE_APPDATA_PREFIX ""
  46. #ifdef LOVE_WINDOWS
  47. # define LOVE_APPDATA_FOLDER "LOVE"
  48. # define LOVE_PATH_SEPARATOR "/"
  49. # define LOVE_MAX_PATH _MAX_PATH
  50. #else
  51. # ifdef LOVE_MACOSX
  52. # define LOVE_APPDATA_FOLDER "LOVE"
  53. # elif defined(LOVE_LINUX)
  54. # define LOVE_APPDATA_FOLDER "love"
  55. # else
  56. # define LOVE_APPDATA_PREFIX "."
  57. # define LOVE_APPDATA_FOLDER "love"
  58. # endif
  59. # define LOVE_PATH_SEPARATOR "/"
  60. # define LOVE_MAX_PATH MAXPATHLEN
  61. #endif
  62. namespace love
  63. {
  64. namespace filesystem
  65. {
  66. namespace physfs
  67. {
  68. class Filesystem : public Module
  69. {
  70. private:
  71. // Counts open files.
  72. int open_count;
  73. // Pointer used for file reads.
  74. char *buffer;
  75. // Contains the current working directory (UTF8).
  76. std::string cwd;
  77. // %APPDATA% on Windows.
  78. std::string appdata;
  79. // This name will be used to create the folder
  80. // in the appdata/userdata folder.
  81. std::string save_identity;
  82. // Full and relative paths of the game save folder.
  83. // (Relative to the %APPDATA% folder, meaning that the
  84. // relative string will look something like: ./LOVE/game)
  85. std::string save_path_relative, save_path_full;
  86. // The full path to the source of the game.
  87. std::string game_source;
  88. // Workaround for machines without PhysFS 2.0
  89. bool isInited;
  90. // Allow saving outside of the LOVE_APPDATA_FOLDER
  91. // for release 'builds'
  92. bool release;
  93. bool releaseSet;
  94. protected:
  95. public:
  96. Filesystem();
  97. ~Filesystem();
  98. const char *getName() const;
  99. void init(const char *arg0);
  100. void setRelease(bool release);
  101. bool isRelease() const;
  102. /**
  103. * This sets up the save directory. If the
  104. * it is already set up, nothing happens.
  105. * @return True on success, false otherwise.
  106. **/
  107. bool setupWriteDirectory();
  108. /**
  109. * Sets the name of the save folder.
  110. * @param ident The name of the game. Will be used to
  111. * to create the folder in the LOVE data folder.
  112. **/
  113. bool setIdentity(const char *ident);
  114. const char *getIdentity() const;
  115. /**
  116. * Sets the path to the game source.
  117. * This can only be set once.
  118. * @param source Path to a directory or a .love-file.
  119. **/
  120. bool setSource(const char *source);
  121. /**
  122. * Creates a new file.
  123. **/
  124. File *newFile(const char *filename);
  125. /**
  126. * Creates a new FileData object. Data will be copied.
  127. * @param data Pointer to the data.
  128. * @param size The size of the data.
  129. * @param filename The full filename used to file type identification.
  130. **/
  131. FileData *newFileData(void *data, unsigned int size, const char *filename);
  132. /**
  133. * Creates a new FileData object from base64 data.
  134. * @param b64 The base64 data.
  135. **/
  136. FileData *newFileData(const char *b64, const char *filename);
  137. /**
  138. * Gets the current working directory.
  139. **/
  140. const char *getWorkingDirectory();
  141. /**
  142. * Gets the user home directory.
  143. **/
  144. const char *getUserDirectory();
  145. /**
  146. * Gets the APPDATA directory. On Windows, this is the folder
  147. * in the %APPDATA% enviroment variable. On Linux, this is the
  148. * user home folder.
  149. **/
  150. const char *getAppdataDirectory();
  151. /**
  152. * Gets the full path of the save folder.
  153. **/
  154. const char *getSaveDirectory();
  155. /**
  156. * Checks whether a file exists in the current search path
  157. * or not.
  158. * @param file The filename to check.
  159. **/
  160. bool exists(const char *file);
  161. /**
  162. * Checks if an existing file really is a directory.
  163. * @param file The filename to check.
  164. **/
  165. bool isDirectory(const char *file);
  166. /**
  167. * Checks if an existing file really is a file,
  168. * and not a directory.
  169. * @param file The filename to check.
  170. **/
  171. bool isFile(const char *file);
  172. /**
  173. * Creates a directory. Write dir must be set.
  174. * @param file The directory to create.
  175. **/
  176. bool mkdir(const char *file);
  177. /**
  178. * Removes a file (or directory).
  179. * @param file The file or directory to remove.
  180. **/
  181. bool remove(const char *file);
  182. /**
  183. * Opens a file for reading or writing. (Depends
  184. * on the mode chosen at the time of creation).
  185. * @param file The file to open.
  186. * @param mode The mode to open the file in.
  187. **/
  188. bool open(File *file, File::Mode mode);
  189. /**
  190. * Closes a file.
  191. * @param file The file to close.
  192. **/
  193. bool close(File *file);
  194. /**
  195. * Reads count bytes from an open file.
  196. * The first parameter is either a File or
  197. * a string. An optional second parameter specified the
  198. * max number of bytes to read.
  199. **/
  200. int read(lua_State *L);
  201. /**
  202. * Write the bytes in data to the file. File
  203. * must be opened for write.
  204. * The first parameter is either a File or
  205. * a string.
  206. **/
  207. int write(lua_State *L);
  208. /**
  209. * Check if end-of-file is reached.
  210. * @return True if EOF, false otherwise.
  211. **/
  212. bool eof(File *file);
  213. /**
  214. * Gets the current position in a file.
  215. * @param file An open File.
  216. **/
  217. int tell(File *file);
  218. /**
  219. * Seek to a position within a file.
  220. * @param pos The position to seek to.
  221. **/
  222. bool seek(File *file, uint64 pos);
  223. /**
  224. * This "native" method returns a table of all
  225. * files in a given directory.
  226. **/
  227. int enumerate(lua_State *L);
  228. /**
  229. * Loads a file without running it. The loaded
  230. * chunk is returned as a function.
  231. * @param filename The filename of the file to load.
  232. * @return A function.
  233. **/
  234. int load(lua_State *L);
  235. int getLastModified(lua_State *L);
  236. /**
  237. * Text file line-reading iterator function used and
  238. * pushed on the Lua stack by love.filesystem.lines
  239. * and File:lines.
  240. **/
  241. static int lines_i(lua_State *L);
  242. }; // Filesystem
  243. } // physfs
  244. } // filesystem
  245. } // love
  246. #endif // LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H