Load and save files outside love.filesystem.
The ffi code was mostly adapted (with my sincerest gratitude) from
attr function was adapted from
-- dir is [string], if no dir is given, start on UserDirectoryfs=lovefs(dir)-- Current Directory [string] (don't change it, use fs:cd(dir))fs.current-- drives, directories and files [tables] of current dirfs.drivesfs.dirsfs.filesfs.others (POSIXlinksanddevices)
fs.all-- [string] used by fs:loadImage, fs:loadSource, fs:loadFont and fs:saveImage if no source is givenfs.selectedFile-- user directory [string]fs.home-- [table] with extensions, like {'jpg', 'png'}. Used by fs:ls to filter files. Don't forget to NIL!fs.filter-- Show or hide hidden files and directories. Default: FALSEfs.showHidden-- update drives listfs:updDrives() These functions accept absolute and relative (to current) paths:
-- return dir (absolute path) [string], tDirs, tFiles, tOthers, tAll [tables]. Return FALSE if dir don't exist. Alias: fs:dir(dir)fs:ls(dir)
-- return TRUE if exists [boolean]fs:exists(path)
-- return TRUE if is directory. [boolean]fs:isDirectory(path)
-- return TRUE if is file. [boolean]fs:isFile(path)
-- Change directory. Populate fs.dirs and fs.files and fs.all with the new directory contents. Return TRUE if successfulfs:cd(dir)
-- move to parent directory (using cd())fs:up()
-- filter can be [nil, table or string]. sets fs.filter and calls fs:cd().-- String can be 'File type | *.ext1 *.ext2'fs:setFilter(filter) -- switch fs.showHiddenfs:switchHidden()
-- return absolute pathsfs:absPath(path)
-- return image. Use fs.selectedFile if no source is givenfs:loadImage(source)
--return sound. Use fs.selectedFile if no source is givenfs:loadSource(source) --return font. Use fs.selectedFile if no source is givenfs:loadFont(size, source)
-- Need Canvas support. Return FALSE on failure. Use fs.selectedFile if no source is givenfs:saveImage(img, dest)
-- copy file, this function only accept absolute pathsfs:copy(source, dest)
-- return a table of file attributesfs:attr(path)
-- return a file attribute valuefs:attr(path, attr)
-- (POSIX systems: follow_Symlink[boolean])fs:attr(path, [attrornil], follow_symlink)These are ready-made dialogs for various UI libraries.
Example filter:
{'All | *.*', 'Image | *.jpg *.png *.bmp', 'Sound | *.mp3 *.wav'}When the user presses OK, the selected file is available in fs.selectedFile
Use this to make a file-browser dialog with LUIGI.
-- show a load dialog, without a layoutfs:loadDialog(gui, label, filters)
-- use with a layoutfs:loadDialog(gui.Layout, label, filters)
-- show a save dialog, without a layoutfs:saveDialog(gui, label)
-- use with a layoutfs:saveDialog(gui.Layout, label)Use this to make a file-browser dialog with loveframes.
-- show a load dialogfs:loadDialog(lf, label, filters)
-- show a save dialogfs:saveDialog(lf, label)Use this to make a file-browser dialog with gspot.
-- show a load dialogfs:loadDialog(gspot, label, filters)
-- show a save dialogfs:saveDialog(gspot, label)Slab has some nice UI elements built-in, that use this library, as well.
require('lovefs')
fs=lovefs()
fs:ls()
print('Current Dir:', fs.current)
forkey, valueinpairs(fs.all) doprint(key, value)
t=fs:attr(fs:absPath(value))
for_, ainpairs(t) doprint('\t', _, a)
endprint('\t', 'Human readable timestamp')
print('\t', 'modification', os.date(_, tostring(t['modification'], 'atime')))
print('\t', 'access', os.date(_, tostring(t['access'], 'atime')))
print('\t', 'change', os.date(_, tostring(t['change'], 'atime')))
end-- POSIX Symlinksprint('Following links')
forkey, valueinpairs(fs.others) doprint(key, value)
t=fs:attr(fs:absPath(value), nil, true)
for_, ainpairs(t) doprint('\t', _, a)
endprint('\t', 'Human readable timestamp')
print('\t', 'modification', os.date(_, tostring(t['modification'], 'atime')))
print('\t', 'access', os.date(_, tostring(t['access'], 'atime')))
print('\t', 'change', os.date(_, tostring(t['change'], 'atime')))
end--[[attribs = {"access","blksize","blocks","change","dev","gid","ino","mode","modification","nlink","permissions","rdev","size","uid"}"target" for symlinks]]--You can also use lovefs-noffi, if you need support for pre-ffi love2d (before love 11), or you just don't want to use FFI. It has it's own README. It uses popen to call commands from the OS, so it's a bit slower, but maybe more cross-platform, in some situations.