summaryrefslogtreecommitdiff
path: root/scripts/functions_tools.lua
blob: 50a78831a22899352aa0bce720a38957821c485f (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
-- SLIMax Mgr Lua Script v1.4
-- PART OF SLIMAX Manager pkg
-- Copyright (c)2012-2013 by Zappadoc - All Rights Reserved.
-- CREDITS: first release 	- Zappadoc
--			updated 		- Desty 2013-04-26

-- ===============================
-- left and right functions tools
-- ===============================

-- add a function name to left and right functions list

-- Returns Index if success
-- Returns Index if FunctionName allready exists
-- Returns  -1	 if Error Reading File
-- Returns  -2	 if Error writing File

-- Usage AddFuncName(FilePath, FunctionName):

-- on left list:
-- local err = AddFuncName("cfg/sli_left_functions.ecfg", "DSII KERS")
-- or right list:
-- local err = AddFuncName("cfg/sli_right_functions.ecfg", "DSII KERS")

function AddFuncName(path, func)

	idx = 0
	idx = GetFuncIndex(path, func)
	if idx < 0 then
		print("BAD ERROR occurred!")
		return idx --returns errorcode -1 or -2
	elseif idx > 0 then
		return idx -- OK, return index
	end

	idx = GetNewFuncIndex(path)
	if idx < 1 then
		print("BAD ERROR occurred!")
		return -1
	end

	file,err = io.open(path,"r")
	if err then return -1 end

	funcStr = idx.."."..func
	isOK = false
	local t1 = {}	--IndexTable
	local t2 = {}	--functionsTable
	i= 1
	for line in file:lines() do
		if line == nil then break end
		-- Seperate Index from function name
		a =  string.find(line, '.', 1, true)
		if a then
			b = string.sub(line, a+1)
			c = string.sub(line, 1, a-1)
			d=(c + 0)
			-- check for EXACT MATCH of the function name
			if b == func then isOK = true end
			table.insert(t1, d)
			t2[d]= line
		end
	end
	table.insert(t1, idx)
	t2[idx]= funcStr

	file:close()

	if not isOK then
		-- write mode
		file, err = io.open(path,"w+")
		if err then return -2 end

		--sort IndexTable
		table.sort(t1)

		-- Write All functions in order of IndexTable
		for i = 1, #t1, 1 do
			a = t1[i]
			b = t2[a]
			--print(" a: "..a.."	b: "..b)
			file:write(b)
			file:write("\n")
		end
		file:close()
	end

  return idx

end


 -- Remove a FunctionName

-- Returns 	 1	if success
-- Returns  -1	if Error Reading File
-- Returns  -2	if Error writing File

 --Usage: RemoveFuncName(FunctionName)

-- on left list:
-- local err = RemoveFuncName("cfg/sli_left_functions.ecfg", "DSII KERS")
-- or right list:
-- local err = RemoveFuncName("cfg/sli_right_functions.ecfg", "DSII KERS")

function RemoveFuncName(path, func)

   file,err = io.open(path,"r")
   if err then return -1 end

   t = { }
   i=1
   for line in file:lines() do
		if line == nil then break end

		-- Seperate Index from function name
		a =  string.find(line, '.', 1, true)
		-- Empty Lines will be deleted
		if a then
			b = string.sub(line, a+1)
			c = string.sub(line, 1, a-1)
			d = (c + 0)
			-- check for an EXACT MATCH of the function name
			if b ~= func or d < 100 then
				t[i] = line
				i= i + 1
			end
		end
   end
   file:close()

   -- write mode, delete previous data
   file, err = io.open(path,"w+")
   if err then return -2 end

   for k, v in pairs(t) do
      file:write(v)
      file:write("\n")
   end

   file:close()
   return 1
end


-- Returns Index
-- returns  0	if Function not exists
-- returns -1	if error reading file

-- Usage: GetFuncIndex(FilePath, FunctionName)
-- on left list:
-- local idx = GetFuncIndex("cfg/sli_left_functions.ecfg", "DSII KERS")
-- or right list:
-- localidx = GetFuncIndex("cfg/sli_right_functions.ecfg", "DSII KERS")

function GetFuncIndex(path, func)

	file,err = io.open(path,"r")
	if err then return -1 end

	for line in file:lines() do
		if line == nil then break end
		-- Seperate Index from function name
		a =  string.find(line, '.', 1, true)
		if a then
			b = string.sub( line, a+1)
			-- check for an EXACT MATCH of the function name
			if b == func then
				c = string.sub( line,1, a-1)
				d = (c + 0)
				return d
			end
		end
	end

  return 0

end


-- Returns lowest function index available (above 100)
-- Returns  -1	 if Error Reading File

-- typical usage GetNewFuncIndex(FilePath):
-- on left list:
-- local num_available = GetNewFuncIndex("cfg/sli_left_functions.ecfg")
-- or right list:
-- local num_available = GetNewFuncIndex("cfg/sli_right_functions.ecfg")

function GetNewFuncIndex(path)

	local t = { 99,}
	local idx = 99

	file,err = io.open(path,"r")
	if err then return -1 end

	for line in file:lines() do
		if line == nil then break end
		-- Seperate Index from function name
		a =  string.find(line, '.', 1, true)
		if a then
			b = string.sub( line,1, a-1)
			-- string to num conversion
			c = (b + 0)
			-- store if it's greater
			if c > idx then
				table.insert(t, c)
			end
		end
	end
	file:close()

	table.sort(t)
	--s = table.concat(t, ", ")
	--print(s)

	idx = 0
	local n = #t-1
	for i = 1, n, 1 do
		if t[i] < (t[i+1]-1) then
			if idx == 0	then	--added this Iteration
				idx = t[i]+1	--to start at the lowest gap
			end
		end
	end
	if idx == 0 then
		idx = t[n+1]+1
	end

  return idx

end