-
-
Save DoubleSlashDesign2/58c0559e3e1c47d9b318 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- io operations | |
local iolib = {} | |
-- returns true if the file exists | |
local function fileExists( filename, base ) | |
local path = system.pathForFile( filename, base ) | |
local file = io.open( path, "r" ) | |
if file then | |
io.close(file) | |
return true | |
end | |
return false | |
end | |
iolib.fileExists = fileExists | |
-- deletes a file | |
function removeFile( filename, base ) | |
local path = system.pathForFile( filename, base ) | |
return os.remove( path ) | |
end | |
iolib.removeFile = removeFile | |
-- will write data to the specified application directory | |
function write( filename, base, data ) | |
local path = system.pathForFile( filename, base ) | |
print("Write Path:",path) | |
local file = io.open( path, "w" ) | |
file:write( data ) | |
io.close( file ) | |
file = nil | |
return nil | |
end | |
iolib.write = write | |
-- will read data from the specified application directory | |
function read( filename, base, decrypt ) | |
local path = system.pathForFile( filename, base ) | |
print("Read Path:",path) | |
local file, errStr = io.open( path, "rb" ) | |
local data = nil | |
if (file) then | |
data = file:read( "*a" ) | |
io.close( file ) | |
end | |
file = nil | |
return data, success | |
end | |
iolib.read = read | |
-- reads from the read-only resource directory | |
function rResource( filename ) | |
if (data) then | |
return nil | |
else | |
return read( filename, system.ResourceDirectory ) | |
end | |
end | |
iolib.rResource = rResource | |
-- writes and reads to/from the permanent documents directory | |
function wrDocs( filename, data ) | |
if (data) then | |
return write( filename, system.DocumentsDirectory, data ) | |
else | |
return read( filename, system.DocumentsDirectory ) | |
end | |
end | |
iolib.wrDocs = wrDocs | |
-- writes and reads to/from the short-term temporary directory | |
function wrTemp( filename, data ) | |
if (data) then | |
return write( filename, system.TemporaryDirectory, data ) | |
else | |
return read( filename, system.TemporaryDirectory ) | |
end | |
end | |
iolib.wrTemp = wrTemp | |
-- writes and reads to/from the cache directory (longer life than temp) | |
function wrCache( filename, data ) | |
if (data) then | |
return write( filename, system.CachesDirectory, data ) | |
else | |
return read( filename, system.CachesDirectory ) | |
end | |
end | |
iolib.wrCache = wrCache | |
-- save table to json file | |
function saveTable(fp, tbl) | |
local path = system.pathForFile(fp, system.DocumentsDirectory) | |
file = io.open(path, "w") | |
file:write(json.encode(tbl)) | |
io.close(file) | |
end | |
iolib.saveTable = saveTable | |
-- load table from json file | |
function readTable(fp, baseDirectory) | |
local path = system.pathForFile( fp, baseDirectory or system.DocumentsDirectory) | |
file = io.open(path, "r") | |
local myTable = {} | |
if file then | |
myTable = json.decode( file:read("*a")) | |
io.close(file) | |
return myTable | |
else | |
return nil | |
end | |
end | |
iolib.readTable = readTable | |
return iolib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment