-- 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