Filesystem.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /**
  115. * Sets the path to the game source.
  116. * This can only be set once.
  117. * @param source Path to a directory or a .love-file.
  118. **/
  119. bool setSource(const char * source);
  120. /**
  121. * Creates a new file.
  122. **/
  123. File * newFile(const char* filename);
  124. /**
  125. * Creates a new FileData object. Data will be copied.
  126. * @param data Pointer to the data.
  127. * @param size The size of the data.
  128. * @param filename The full filename used to file type identification.
  129. **/
  130. FileData * newFileData(void * data, unsigned int size, const char * filename);
  131. /**
  132. * Creates a new FileData object from base64 data.
  133. * @param b64 The base64 data.
  134. **/
  135. FileData * newFileData(const char * b64, const char * filename);
  136. /**
  137. * Gets the current working directory.
  138. **/
  139. const char * getWorkingDirectory();
  140. /**
  141. * Gets the user home directory.
  142. **/
  143. const char * getUserDirectory();
  144. /**
  145. * Gets the APPDATA directory. On Windows, this is the folder
  146. * in the %APPDATA% enviroment variable. On Linux, this is the
  147. * user home folder.
  148. **/
  149. const char * getAppdataDirectory();
  150. /**
  151. * Gets the full path of the save folder.
  152. **/
  153. const char * getSaveDirectory();
  154. /**
  155. * Checks whether a file exists in the current search path
  156. * or not.
  157. * @param file The filename to check.
  158. **/
  159. bool exists(const char * file);
  160. /**
  161. * Checks if an existing file really is a directory.
  162. * @param file The filename to check.
  163. **/
  164. bool isDirectory(const char * file);
  165. /**
  166. * Checks if an existing file really is a file,
  167. * and not a directory.
  168. * @param file The filename to check.
  169. **/
  170. bool isFile(const char * file);
  171. /**
  172. * Creates a directory. Write dir must be set.
  173. * @param file The directory to create.
  174. **/
  175. bool mkdir(const char * file);
  176. /**
  177. * Removes a file (or directory).
  178. * @param file The file or directory to remove.
  179. **/
  180. bool remove(const char * file);
  181. /**
  182. * Opens a file for reading or writing. (Depends
  183. * on the mode chosen at the time of creation).
  184. * @param file The file to open.
  185. * @param mode The mode to open the file in.
  186. **/
  187. bool open(File * file, File::Mode mode);
  188. /**
  189. * Closes a file.
  190. * @param file The file to close.
  191. **/
  192. bool close(File * file);
  193. /**
  194. * Reads count bytes from an open file.
  195. * The first parameter is either a File or
  196. * a string. An optional second parameter specified the
  197. * max number of bytes to read.
  198. **/
  199. int read(lua_State * L);
  200. /**
  201. * Write the bytes in data to the file. File
  202. * must be opened for write.
  203. * The first parameter is either a File or
  204. * a string.
  205. **/
  206. int write(lua_State * L);
  207. /**
  208. * Check if end-of-file is reached.
  209. * @return True if EOF, false otherwise.
  210. **/
  211. bool eof(File * file);
  212. /**
  213. * Gets the current position in a file.
  214. * @param file An open File.
  215. **/
  216. int tell(File * file);
  217. /**
  218. * Seek to a position within a file.
  219. * @param pos The position to seek to.
  220. **/
  221. bool seek(File * file, uint64 pos);
  222. /**
  223. * This "native" method returns a table of all
  224. * files in a given directory.
  225. **/
  226. int enumerate(lua_State * L);
  227. /**
  228. * Returns an iterator which iterates over
  229. * lines in files.
  230. **/
  231. int lines(lua_State * L);
  232. /**
  233. * The line iterator function.
  234. **/
  235. static int lines_i(lua_State * L);
  236. /**
  237. * Loads a file without running it. The loaded
  238. * chunk is returned as a function.
  239. * @param filename The filename of the file to load.
  240. * @return A function.
  241. **/
  242. int load(lua_State * L);
  243. int getLastModified(lua_State * L);
  244. }; // Filesystem
  245. } // physfs
  246. } // filesystem
  247. } // love
  248. #endif // LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H