-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_value.lua
125 lines (94 loc) · 1.8 KB
/
key_value.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
-- functions related to key-value messages sent to us from other programs
function KvReloadCounter(name)
local S, str
local count=0
str=process.getenv("HOME").."/.barmaid/"..name..".lst"
S=stream.STREAM(str, "r")
if S ~= nil
then
str=S:readln()
while str ~= nil
do
count=count+1
str=S:readln()
end
S:close()
end
display_values[name]=count
end
function KvUpdateCounter(name, value)
local val
if strutil.strlen(value)==0
then
val=0
elseif display_values[name] ~= nil
then
val=tonumber(display_values[name]) +1
else
val=1
end
display_values[name]=val
end
function KvUpdateListFile(name, value)
local S, path, mode
path=process.getenv("HOME").."/.barmaid/"..name..".lst"
filesys.mkdirPath(path)
if strutil.strlen(value) == 0 then mode="w"
else mode="a"
end
S=stream.STREAM(path, mode)
if S ~= nil
then
if strutil.strlen(value) > 0 then S:writeln(value.."\n") end
S:close()
end
end
function KvLineRead(S)
local line, str, toks, prefix, name, value
line=S:readln()
if line ~= nil
then
line=strutil.trim(line)
if string.len(line) > 0
then
toks=strutil.TOKENIZER(line, "=")
str=toks:next()
prefix=string.sub(str, 1, 1)
name=string.sub(str, 2)
value=toks:remaining()
if prefix=="@"
then
KvUpdateCounter(name, value)
elseif prefix==">"
then
KvUpdateCounter(name, value)
KvUpdateListFile(name, value)
else -- if prefix isn't a prefix char, then name is the whole of 'str'
display_values[str]=toks:remaining()
end
end
return true
else
return false
end
end
function KvFileRead(feed)
local S
S=stream.STREAM(feed.path)
if S ~= nil
then
while KvLineRead(S)
do
--nothing
end
S:close()
end
end
function KvFileAdd(path)
local S
local feed={}
feed.type="kvfile"
feed.path=path
feed.read=KvFileRead
table.insert(settings.datafeeds, feed)
end