filesystem.h 6.36 KB
Newer Older
RobLoach's avatar
RobLoach committed
1
2
#ifndef SRC_LOVE_FILESYSTEM_H_
#define SRC_LOVE_FILESYSTEM_H_
RobLoach's avatar
RobLoach committed
3

RobLoach's avatar
RobLoach committed
4
#include <string>
RobLoach's avatar
RobLoach committed
5
#include <vector>
6

RobLoach's avatar
RobLoach committed
7
#include "SDL.h"
8
#include "physfs.h"
9
#include "Types/FileSystem/FileInfo.h"
10
#include "Types/FileSystem/FileData.h"
11
12

using love::Types::FileSystem::FileInfo;
13
using love::Types::FileSystem::FileData;
RobLoach's avatar
RobLoach committed
14

RobLoach's avatar
RobLoach committed
15
namespace love {
RobLoach's avatar
RobLoach committed
16
/**
RobLoach's avatar
RobLoach committed
17
 * Provides an interface to the user's filesystem.
RobLoach's avatar
RobLoach committed
18
19
20
 */
class filesystem {
	public:
RobLoach's avatar
RobLoach committed
21
	/**
RobLoach's avatar
RobLoach committed
22
	 * Loads and runs a .chai file.
RobLoach's avatar
RobLoach committed
23
	 *
RobLoach's avatar
RobLoach committed
24
25
26
27
28
29
	 * ### Example
	 *
	 * @code
	 * love.filesystem.load("myscript.chai")
	 * love.filesystem.load("myotherscript")
	 * @endcode
RobLoach's avatar
RobLoach committed
30
	 *
RobLoach's avatar
RobLoach committed
31
32
	 * @param file The name (and path) of the file. Having the .chai extension at the end is optional.
	 *
RobLoach's avatar
RobLoach committed
33
	 * @return Whether or not the file was loaded and ran properly.
RobLoach's avatar
RobLoach committed
34
	 */
RobLoach's avatar
RobLoach committed
35
	bool load(const std::string& file);
RobLoach's avatar
RobLoach committed
36
	void mountlibretro();
RobLoach's avatar
RobLoach committed
37

RobLoach's avatar
RobLoach committed
38
	bool init(const std::string& file, const void* data);
RobLoach's avatar
RobLoach committed
39
40
41
42
43
	bool unload();
	SDL_RWops* openRW(const std::string& filename);
	char* readChar(const std::string& filename);

	/**
RobLoach's avatar
RobLoach committed
44
	 * Read the contents of a file.
RobLoach's avatar
RobLoach committed
45
	 *
RobLoach's avatar
0.28.0    
RobLoach committed
46
	 * @param filename The name (and path) of the file.
RobLoach's avatar
RobLoach committed
47
48
	 *
	 * @return The contents of the file.
RobLoach's avatar
RobLoach committed
49
50
51
52
53
	 *
	 * @code
	 * var contents = love.filesystem.read("test.txt")
	 * print(contents)
	 * @endcode
RobLoach's avatar
RobLoach committed
54
55
	 */
	std::string read(const std::string& filename);
56
	void* readBuffer(const std::string& filename, int& size);
RobLoach's avatar
RobLoach committed
57
58

	/**
RobLoach's avatar
RobLoach committed
59
	 * Check whether a file or directory exists.
RobLoach's avatar
RobLoach committed
60
61
62
63
64
65
66
67
68
	 *
	 * @code
	 * if (love.filesystem.exists("test.txt")) {
	 *   print("The test.txt file exists")
	 * }
	 * else {
	 *   print("The test.txt file does not exist")
	 * }
	 * @endcode
RobLoach's avatar
RobLoach committed
69
70
71
72
	 */
	bool exists(const std::string& file);

	/**
RobLoach's avatar
RobLoach committed
73
	 * Get the size in bytes of a file.
RobLoach's avatar
RobLoach committed
74
	 *
RobLoach's avatar
0.28.0    
RobLoach committed
75
	 * @param file The file to get the size of.
RobLoach's avatar
RobLoach committed
76
77
	 *
	 * @return The size of the given file.
RobLoach's avatar
RobLoach committed
78
79
80
	 */
	int getSize(const std::string& file);

81
82
83
84
85
86
87
	/**
	 * Removes a file or empty directory.
	 *
	 * The directory must be empty before removal or else it will fail. Simply remove all files and folders in the directory beforehand.
	 * If the file exists in the .love but not in the save directory, it returns false as well.
	 * An opened File prevents removal of the underlying file. Simply close the File to remove it.
	 *
RobLoach's avatar
0.27.0    
RobLoach committed
88
89
	 * @param name The file or directory to remove.
	 *
90
91
92
93
	 * @return True if the file or directory was removed, false otherwise.
	 */
	bool remove(const std::string& name);

94
	/**
RobLoach's avatar
RobLoach committed
95
	 * Gets information about the specified file or directory.
96
	 *
RobLoach's avatar
RobLoach committed
97
98
	 * @param path The path of the file to get information for.
	 *
99
	 * @return A FileInfo object representing information about the path.
RobLoach's avatar
RobLoach committed
100
101
	 *
	 * @see love::Types::FileSystem::FileInfo
102
103
	 */
	FileInfo getInfo(const std::string& path);
RobLoach's avatar
RobLoach committed
104
105

	/**
106
107
	 * Creates a new FileData from a file on the storage device.
	 *
RobLoach's avatar
0.28.0    
RobLoach committed
108
	 * @param filepath Path to the file.
109
110
111
112
113
114
115
	 *
	 * @return The new FileData, or nil if an error occurred.
	 *
	 * @see love::Types::FileSystem::FileData
	 */
	FileData newFileData(const std::string& filepath);

RobLoach's avatar
RobLoach committed
116
117
118
119
120
121
122
123
124
125
126
127
	/**
	 * Creates a new FileData object.
	 *
	 * @param contents The contents fof the file.
	 * @param name The name of the file.
	 *
	 * @return Your new FileData.
	 *
	 * @see love::Types::FileSystem::FileData
	 */
	FileData newFileData(const std::string& contents, const std::string& name);

128
129
130
131
	/**
	 * Unmounts a zip file or folder previously mounted with love.filesystem.mount.
	 *
	 * @param archive The archive that was previously mounted with love.filesystem.mount.
RobLoach's avatar
RobLoach committed
132
133
	 *
	 * @return bool True, when unmounting was a success.
RobLoach's avatar
RobLoach committed
134
135
	 *
	 * @see mount
RobLoach's avatar
RobLoach committed
136
137
138
139
	 */
	bool unmount(const std::string& archive);

	/**
RobLoach's avatar
RobLoach committed
140
	 * Mounts a zip file or folder in the game's save directory for reading.
RobLoach's avatar
RobLoach committed
141
	 *
RobLoach's avatar
RobLoach committed
142
143
144
145
146
	 * @param archive The folder or zip file in the game's save directory to mount.
	 * @param mountpoint The new path the archive will be mounted to.
	 * @param appendToPath (true) Whether the archive will be searched when reading a filepath before or after already-mounted archives. This includes the game's source and save directories.
	 *
	 * @return bool True if the archive was successfully mounted, false otherwise.
RobLoach's avatar
RobLoach committed
147
148
	 *
	 * @see unmount
RobLoach's avatar
RobLoach committed
149
	 */
RobLoach's avatar
RobLoach committed
150
	bool mount(const std::string& archive, const std::string& mountpoint, bool appendToPath);
RobLoach's avatar
RobLoach committed
151
	bool mount(const std::string& archive, const std::string& mountpoint);
RobLoach's avatar
RobLoach committed
152
	bool mount(const char *archive, const std::string& mountpoint);
RobLoach's avatar
RobLoach committed
153

RobLoach's avatar
RobLoach committed
154
155
156
157
158
159
160
	/**
	 * Gets the path to the designated save directory.
	 *
	 * @return The path to the save directory.
	 */
	std::string getSaveDirectory();

RobLoach's avatar
RobLoach committed
161
162
163
164
	PHYSFS_sint64 getSize(PHYSFS_File* file);
	PHYSFS_file* openFile(const std::string& filename);

	/**
RobLoach's avatar
RobLoach committed
165
	 * Returns all files and subdirectories in the directory.
RobLoach's avatar
RobLoach committed
166
167
168
169
	 */
	std::vector<std::string> getDirectoryItems(const std::string& dir);

	/**
RobLoach's avatar
RobLoach committed
170
	 * Check whether something is a directory.
RobLoach's avatar
RobLoach committed
171
172
173
	 *
	 * @see isFile
	 * @see isSymlink
RobLoach's avatar
RobLoach committed
174
175
176
177
	 */
	bool isDirectory(const std::string& filename);

	/**
RobLoach's avatar
RobLoach committed
178
	 * Checks whether something is a file.
RobLoach's avatar
RobLoach committed
179
180
181
	 *
	 * @see isDirectory
	 * @see isSymlink
RobLoach's avatar
RobLoach committed
182
183
184
	 */
	bool isFile(const std::string& filename);

185
	/**
RobLoach's avatar
RobLoach committed
186
	 * Checks whether something is a symlink.
RobLoach's avatar
RobLoach committed
187
188
189
	 *
	 * @see isDirectory
	 * @see isFile
190
191
192
	 */
	bool isSymlink(const std::string& filename);

RobLoach's avatar
RobLoach committed
193
194
195
196
197
198
199
	/**
	 * Recursively creates a directory in the save directory.
	 *
	 * When called with "a/b" it creates both "a" and "a/b", if they don't exist already.
	 *
	 * @param name The directory to create.
	 *
RobLoach's avatar
0.22.2    
RobLoach committed
200
	 * @return True if the directory was created, false otherwise.
RobLoach's avatar
RobLoach committed
201
202
203
204
205
206
207
208
209
	 */
	bool createDirectory(const std::string& name);

	/**
	 * Write data to a file in the save directory. If the file existed already, it will be completely replaced by the new contents.
	 *
	 * @param name The name (and path) of the file.
	 * @param data The string data to write to the file.
	 *
RobLoach's avatar
0.22.2    
RobLoach committed
210
	 * @return True if the file was written, false otherwise.
RobLoach's avatar
RobLoach committed
211
212
213
	 */
	bool write(const std::string& name, const std::string& data);

RobLoach's avatar
RobLoach committed
214
	/**
RobLoach's avatar
RobLoach committed
215
	 * Iterate over the lines in a file, with the given delimiter.
RobLoach's avatar
RobLoach committed
216
217
218
219
220
	 *
	 * @param filename The file to load the lines from.
	 * @param delimiter ("\n") A string of characters representing what would be considered a new line.
	 *
	 * @return A vector array of strings representing all the lines in the given file.
RobLoach's avatar
RobLoach committed
221
222
	 *
	 * @see read
RobLoach's avatar
RobLoach committed
223
224
	 */
	std::vector<std::string> lines(const std::string& filename, const std::string& delimiter);
RobLoach's avatar
RobLoach committed
225
	std::vector<std::string> lines(const std::string& filename);
RobLoach's avatar
RobLoach committed
226

227
	/**
RobLoach's avatar
RobLoach committed
228
	 * Get the path to the executable that was used to run this application.
229
	 *
RobLoach's avatar
RobLoach committed
230
	 * @return The base path of the application.
231
	 */
RobLoach's avatar
RobLoach committed
232
	std::string getExecutablePath();
233

RobLoach's avatar
RobLoach committed
234
	std::string getLastError();
RobLoach's avatar
RobLoach committed
235
236
};

RobLoach's avatar
RobLoach committed
237
}  // namespace love
RobLoach's avatar
RobLoach committed
238

RobLoach's avatar
RobLoach committed
239
#endif  // SRC_LOVE_FILESYSTEM_H_