Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --collection of function that can be used in door locks
- --they are ment to be copy pasted into lock programs
- --many of them aren't tested yet
- --some of the code is experimental
- --function to set to enable or disable ctrl-t
- --http://www.computercraft.info/forums2/index.php?/topic/2732-disable-ctrlt/
- local pullEventCopy = os.pullEvent
- function setCtrlT(value)
- if value then
- os.pullEvent = pullEventCopy
- else
- os.pullEvent = os.pullEventRaw
- end
- end
- --clears the screen and sets the cursor to 1 1
- function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- --needs the loadString function
- --loads the entire contents of the file into
- --a string and then uses unserilize
- function loadValue(path)
- return textutils.unserialize(loadString(path))
- end
- --opens the file then reads the entire contents into a string
- --which is then returned
- --if there is any error nil is returned
- function loadString(path)
- if type(path)~="string" then
- return nil
- end
- local succes=true
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local value=h.readAll()
- h.close()
- return value
- end
- --serializes the value into a string
- --and then saves the string into a
- --file with the given path
- function saveValue(path,value)
- return saveString(path,textutils.serialize(value))
- end
- --saves the given string value into
- --a file with the given path
- --overwriting any contents if there are any
- function saveString(path,value)
- if type(path)~="string" or type(value)~="string" then
- return false
- end
- local h = fs.open(path, "w")
- h.write(value)
- h.close()
- return true
- end
- --loads the lines from the given file
- --returns nil if there is an error
- --else returns an array with the lines
- --indexed from 1 upwards
- function loadLines(path)
- if type(path)~="string" or not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local res={}
- while(true) do
- local value=h.readLine()
- if value==nil then break end
- res[#res+1]=value
- end
- h.close()
- return res
- end
- function saveLines(path,lines)
- for _,line inipairs(lines) do
- end
- end
- --returns an iterator of the words from the line
- --splits on spaces
- function wordIterator(line)
- return line:gmatch("%S+")
- end
- --takes the lines from a perms file and
- --adds them to a lookup table which can
- --be used to check the perms of an user
- function toPermsTable(lines)
- if type(lines)~="table" then return nil end
- local users={}
- for _,line in lines do
- local username=nil
- for _,word in wordIterator(line) do
- if username==nil then
- username=word
- if users[username]==nil then
- users[username]={}
- end
- else
- users[username][word]=true
- end
- end
- end
- end
- function hasPerm(permsTable,username,perm)
- return permsTable[username]~=nil and permsTable[username][perm]~=nil
- end
- function loadPerms(path)
- local lines=loadLines(path)
- if lines==nil then return nil end
- return toPermsTable(lines)
- end
- function savePerms(permsTable)
- local lines={}
- for username,perms in pairs(permsTable) do
- local lineTable={username}
- for perm,_ in pairs(perms) do
- lineTable[#lineTable+1]=perm
- end
- lines[#lines+1]=table.concat(lineTable, " ")
- end
- saveLines(lines)
- end
- function appendString(path,str)
- --open code from wiki
- --Opens path in either append or write mode, depending on whether it already exists or not.
- local h = fs.open(path, fs.exists(path) and "a" or "w")
- if h==nil then
- return false
- end
- h.writeLine(str)
- h.close()
- return true
- end
- function appendUser(userName,permission)
- if(userName==nil) then
- print("UserName is nil...")
- return
- end
- local append=""
- if type(permission)=="string" and #permission>0 then
- append=" "..permission
- end
- appendString(permsPath,userName..append)
- end
- --downloads the given file and then returns 2
- function pastebin(shellObj, path,code,overwrite)
- if overwrite then
- fs.delete(path)
- end
- shellObj.run("pastebin","get", code, path)
- return 2
- end
- function loadLines(path)
- if type(path)~="string" then error("Error (loadLines): path should be a string",2) end
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local res={}
- while(true) do
- local line=h.readLine()
- if line==nil then break end
- res[#res+1]=line
- end
- h.close()
- return res
- end
- function getWords(line)
- if type(line)~="string" then error("Error (getWords): line should be a string",2) end
- local words={}
- --"%S+"
- --"%a+"
- for word in string.gmatch(line, "%S+") do
- words[#words+1]=word
- end
- return words
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement