ChaiLove API  0.28.0
Public Member Functions
love::filesystem Class Reference

Provides an interface to the user's filesystem. More...

Public Member Functions

bool load (const std::string &file)
 Loads and runs a .chai file. More...
 
std::string read (const std::string &filename)
 Read the contents of a file. More...
 
bool exists (const std::string &file)
 Check whether a file or directory exists. More...
 
int getSize (const std::string &file)
 Get the size in bytes of a file. More...
 
bool remove (const std::string &name)
 Removes a file or empty directory. More...
 
FileInfo getInfo (const std::string &path)
 Gets information about the specified file or directory. More...
 
FileData newFileData (const std::string &filepath)
 Creates a new FileData from a file on the storage device. More...
 
FileData newFileData (const std::string &contents, const std::string &name)
 Creates a new FileData object. More...
 
bool unmount (const std::string &archive)
 Unmounts a zip file or folder previously mounted with love.filesystem.mount. More...
 
bool mount (const std::string &archive, const std::string &mountpoint, bool appendToPath)
 Mounts a zip file or folder in the game's save directory for reading. More...
 
std::string getSaveDirectory ()
 Gets the path to the designated save directory. More...
 
std::vector< std::string > getDirectoryItems (const std::string &dir)
 Returns all files and subdirectories in the directory.
 
bool isDirectory (const std::string &filename)
 Check whether something is a directory. More...
 
bool isFile (const std::string &filename)
 Checks whether something is a file. More...
 
bool isSymlink (const std::string &filename)
 Checks whether something is a symlink. More...
 
bool createDirectory (const std::string &name)
 Recursively creates a directory in the save directory. More...
 
bool write (const std::string &name, const std::string &data)
 Write data to a file in the save directory. More...
 
std::vector< std::string > lines (const std::string &filename, const std::string &delimiter)
 Iterate over the lines in a file, with the given delimiter. More...
 
std::string getExecutablePath ()
 Get the path to the executable that was used to run this application. More...
 

Detailed Description

Provides an interface to the user's filesystem.

Member Function Documentation

◆ createDirectory()

bool love::filesystem::createDirectory ( const std::string &  name)

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.

Parameters
nameThe directory to create.
Returns
True if the directory was created, false otherwise.

◆ exists()

bool love::filesystem::exists ( const std::string &  file)

Check whether a file or directory exists.

if (love.filesystem.exists("test.txt")) {
print("The test.txt file exists")
}
else {
print("The test.txt file does not exist")
}

◆ getExecutablePath()

std::string love::filesystem::getExecutablePath ( )

Get the path to the executable that was used to run this application.

Returns
The base path of the application.

◆ getInfo()

FileInfo love::filesystem::getInfo ( const std::string &  path)

Gets information about the specified file or directory.

Parameters
pathThe path of the file to get information for.
Returns
A FileInfo object representing information about the path.
See also
love::Types::FileSystem::FileInfo

◆ getSaveDirectory()

std::string love::filesystem::getSaveDirectory ( )

Gets the path to the designated save directory.

Returns
The path to the save directory.

◆ getSize()

int love::filesystem::getSize ( const std::string &  file)

Get the size in bytes of a file.

Parameters
fileThe file to get the size of.
Returns
The size of the given file.

◆ isDirectory()

bool love::filesystem::isDirectory ( const std::string &  filename)

Check whether something is a directory.

See also
isFile
isSymlink

◆ isFile()

bool love::filesystem::isFile ( const std::string &  filename)

Checks whether something is a file.

See also
isDirectory
isSymlink

◆ isSymlink()

bool love::filesystem::isSymlink ( const std::string &  filename)

Checks whether something is a symlink.

See also
isDirectory
isFile

◆ lines()

std::vector<std::string> love::filesystem::lines ( const std::string &  filename,
const std::string &  delimiter 
)

Iterate over the lines in a file, with the given delimiter.

Parameters
filenameThe file to load the lines from.
delimiter("\n") A string of characters representing what would be considered a new line.
Returns
A vector array of strings representing all the lines in the given file.
See also
read

◆ load()

bool love::filesystem::load ( const std::string &  file)

Loads and runs a .chai file.

Example

love.filesystem.load("myscript.chai")
love.filesystem.load("myotherscript")
Parameters
fileThe name (and path) of the file. Having the .chai extension at the end is optional.
Returns
Whether or not the file was loaded and ran properly.

◆ mount()

bool love::filesystem::mount ( const std::string &  archive,
const std::string &  mountpoint,
bool  appendToPath 
)

Mounts a zip file or folder in the game's save directory for reading.

Parameters
archiveThe folder or zip file in the game's save directory to mount.
mountpointThe new path the archive will be mounted to.
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.
Returns
bool True if the archive was successfully mounted, false otherwise.
See also
unmount

◆ newFileData() [1/2]

FileData love::filesystem::newFileData ( const std::string &  filepath)

Creates a new FileData from a file on the storage device.

Parameters
filepathPath to the file.
Returns
The new FileData, or nil if an error occurred.
See also
love::Types::FileSystem::FileData

◆ newFileData() [2/2]

FileData love::filesystem::newFileData ( const std::string &  contents,
const std::string &  name 
)

Creates a new FileData object.

Parameters
contentsThe contents fof the file.
nameThe name of the file.
Returns
Your new FileData.
See also
love::Types::FileSystem::FileData

◆ read()

std::string love::filesystem::read ( const std::string &  filename)

Read the contents of a file.

Parameters
filenameThe name (and path) of the file.
Returns
The contents of the file.
var contents = love.filesystem.read("test.txt")
print(contents)

◆ remove()

bool love::filesystem::remove ( const std::string &  name)

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.

Parameters
nameThe file or directory to remove.
Returns
True if the file or directory was removed, false otherwise.

◆ unmount()

bool love::filesystem::unmount ( const std::string &  archive)

Unmounts a zip file or folder previously mounted with love.filesystem.mount.

Parameters
archiveThe archive that was previously mounted with love.filesystem.mount.
Returns
bool True, when unmounting was a success.
See also
mount

◆ write()

bool love::filesystem::write ( const std::string &  name,
const std::string &  data 
)

Write data to a file in the save directory.

If the file existed already, it will be completely replaced by the new contents.

Parameters
nameThe name (and path) of the file.
dataThe string data to write to the file.
Returns
True if the file was written, false otherwise.