24#define PHYSFS_DEPRECATED
37inline bool init(
const char *argv0)
39 return PHYSFS_init(argv0) != 0;
49 return PHYSFS_getDirSeparator();
54 return PHYSFS_getBaseDir();
59 return PHYSFS_getUserDir();
62inline const char *
getPrefDir(
const char *org,
const char *app)
64 return PHYSFS_getPrefDir(org, app);
75 return PHYSFS_setWriteDir(path.c_str()) != 0;
87 return PHYSFS_mount(path.c_str(),
"/", append ? 1 : 0) != 0;
93inline bool exists(
const std::string &path)
95 return PHYSFS_exists(path.c_str()) != 0;
98inline std::optional<const char *>
getRealDir(
const std::string &path)
100 auto dir = PHYSFS_getRealDir(path.c_str());
101 return dir ? std::optional<const char *>(dir) : std::nullopt;
110 if (PHYSFS_stat(path.c_str(), &stat) != 0)
112 return stat.filetype == PHYSFS_FILETYPE_DIRECTORY;
120inline bool mkdir(
const std::string &path)
122 return PHYSFS_mkdir(path.c_str()) != 0;
134 {
return *files !=
nullptr; }
155 return Files(PHYSFS_enumerateFiles(dir.c_str()));
176 return file !=
nullptr;
179 operator bool()
const
186 if (PHYSFS_close(
file) != 0)
194 std::optional<size_t>
read(
void *data,
size_t size)
196 auto len = PHYSFS_readBytes(
file, data, size);
197 return len >= 0 ? std::optional<size_t>(len) : std::nullopt;
200 std::optional<size_t>
write(
const void *data,
size_t size)
202 auto len = PHYSFS_writeBytes(
file, data, size);
203 return len >= 0 ? std::optional<size_t>(len) : std::nullopt;
208 return PHYSFS_flush(
file) != 0;
213 return PHYSFS_seek(
file, pos) != 0;
218 auto len = PHYSFS_fileLength(
file);
219 return len >= 0 ? std::optional<size_t>(len) : std::nullopt;
222 std::optional<size_t>
tell()
const
224 auto pos = PHYSFS_tell(
file);
225 return pos >= 0 ? std::optional<size_t>(pos) : std::nullopt;
230 return PHYSFS_eof(
file) != 0;
239 return File(PHYSFS_openWrite(path.c_str()));
244 return File(PHYSFS_openAppend(path.c_str()));
249 return File(PHYSFS_openRead(path.c_str()));
254 return PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
275 PHYSFS_uint64 bufferSize = 2048)
277 if (
auto file = PHYSFS_openRead(path.c_str()))
279 PHYSFS_setBuffer(file, bufferSize);
288inline void *
loadFile(
const std::string &path,
size_t &datasize)
294 return SDL_LoadFile_RW(file, &datasize, 1);
File wrapper class to provide a more convenient API and automatic closing.
std::optional< size_t > read(void *data, size_t size)
std::optional< size_t > write(const void *data, size_t size)
std::optional< size_t > fileLength() const
std::optional< size_t > tell() const
Helper class to iterate over the files in a directory.
Files & operator=(const Files &)=delete
Files(const Files &)=delete
friend bool operator!=(const char *const *files, End)
const char *const * begin() const
These functions wrap PHYSFS functions to provide a more user-friendly interface and to limit the dire...
File openRead(const std::string &path)
bool init(const char *argv0)
const char * getPrefDir(const char *org, const char *app)
const char * getDirSeparator()
SDL_RWops * openBufferedRWops(const std::string &path, PHYSFS_uint64 bufferSize=2048)
Creates a buffered SDL_RWops.
bool setWriteDir(const std::string &path)
Sets the write directory.
bool mkdir(const std::string &path)
Creates a directory in the write path.
const char * getUserDir()
File openWrite(const std::string &path)
bool isDirectory(const std::string &path)
Checks whether the given path is a directory.
bool addToSearchPath(const std::string &path, bool append)
Adds a directory or archive to the search path.
File openAppend(const std::string &path)
const char * getLastError()
bool exists(const std::string &path)
Checks whether the given file or directory exists in the search path.
void * loadFile(const std::string &path, size_t &datasize)
SDL_RWops * openRWops(const std::string &path)
const char * getBaseDir()
std::optional< const char * > getRealDir(const std::string &path)
Files enumerateFiles(const std::string &dir)
Returns a list of files in the given directory.
SDL_RWops * PHYSFSRWOPS_openRead(const char *fname)
Open a platform-independent filename for reading, and make it accessible via an SDL_RWops structure.
SDL_RWops * PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
Make a SDL_RWops from an existing PhysicsFS file handle.