Filesystem.h 6.9 KB

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