-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_io.lua
More file actions
74 lines (60 loc) · 1.27 KB
/
state_io.lua
File metadata and controls
74 lines (60 loc) · 1.27 KB
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
function scriptPath()
local info = debug.getinfo(1,'S');
return info.source:match[[^@?(.*[\/])[^\/]-$]]
end
local stateDir = scriptPath() .. "/state"
local queryFile = stateDir .. "/query"
local timeFile = stateDir .. "/last_time"
local lastActionFile = stateDir .. "/last_action"
local lastContextFile = stateDir .. "/last_context"
function withFile(path, mode, fun)
local f = io.open(path, mode)
local ret
if f then
ret = fun(f)
f:close()
end
return ret
end
function setState(file, data, mode)
mode = mode or "w"
withFile(file, mode, function(f)
f:write(data)
end)
end
function readFile(file)
return withFile(file, 'r', function(f)
if f then return
f:read('*a')
else
return nil
end
end)
end
function clearQuery()
setState(queryFile, '')
end
function updateQuery(ch)
setState(queryFile, ch, 'a')
end
function getQuery()
return readFile(queryFile)
end
function updateLastTime(t)
setState(timeFile, t)
end
function getLastTime(t)
return readFile(timeFile)
end
function setLastAction(a)
setState(lastActionFile, a)
end
function getLastAction()
return readFile(lastActionFile)
end
function getLastContext()
return readFile(lastContextFile)
end
function setLastContext(c)
setState(lastContextFile, c)
end