-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadsave.lua
76 lines (70 loc) · 2.1 KB
/
loadsave.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local _ = {}
local json = require("json")
local DefaultLocation = system.DocumentsDirectory
local RealDefaultLocation = DefaultLocation
local ValidLocations = {
[system.DocumentsDirectory] = true,
[system.CachesDirectory] = true,
[system.TemporaryDirectory] = true
}
function _.saveTable(t, filename, location)
if location and (not ValidLocations[location]) then
error("Attempted to save a table to an invalid location", 2)
elseif not location then
location = DefaultLocation
end
local path = system.pathForFile( filename, location)
local file = io.open(path, "w")
if file then
local contents = json.encode(t)
file:write( contents )
io.close( file )
return true
else
return false
end
end
function _.loadTable(filename, location)
if location and (not ValidLocations[location]) then
error("Attempted to load a table from an invalid location", 2)
elseif not location then
location = DefaultLocation
end
local path = system.pathForFile( filename, location)
local contents = ""
local myTable = {}
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
myTable = json.decode(contents);
io.close( file )
return myTable
end
return nil
end
function _.changeDefault(location)
if location and (not location) then
error("Attempted to change the default location to an invalid location", 2)
elseif not location then
location = RealDefaultLocation
end
DefaultLocation = location
return true
end
function _.removeFile(filename, location)
if location and (not ValidLocations[location]) then
error("Attempted to remove settingsfile from an invalid location", 2)
elseif not location then
location = DefaultLocation
end
local path = system.pathForFile( filename, location)
local results, reason = os.remove( path )
if results then
print( "file removed" )
else
print( "file does not exist", reason )
end
return nil
end
return _