Advertisement
theinsekt

collection of functions

Sep 19th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. --collection of function that can be used in door locks
  2. --they are ment to be copy pasted into lock programs
  3. --many of them aren't tested yet
  4. --some of the code is experimental
  5.  
  6. --function to set to enable or disable ctrl-t
  7. --http://www.computercraft.info/forums2/index.php?/topic/2732-disable-ctrlt/
  8. local pullEventCopy = os.pullEvent
  9. function setCtrlT(value)
  10.  if value then
  11.   os.pullEvent = pullEventCopy
  12.  else
  13.   os.pullEvent = os.pullEventRaw
  14.  end
  15. end
  16.  
  17.  
  18.  
  19. --clears the screen and sets the cursor to 1 1
  20. function clear()
  21.  term.clear()
  22.  term.setCursorPos(1, 1)
  23. end
  24.  
  25.  
  26.  
  27. --needs the loadString function
  28. --loads the entire contents of the file into
  29. --a string and then uses unserilize
  30. function loadValue(path)
  31.  return textutils.unserialize(loadString(path))
  32. end
  33.  
  34. --opens the file then reads the entire contents into a string
  35. --which is then returned
  36. --if there is any error nil is returned
  37. function loadString(path)
  38.  if type(path)~="string" then
  39.   return nil
  40.  end
  41.  
  42.  local succes=true
  43.  if  not fs.exists(path) then
  44.   return nil
  45.  end
  46.  local h=fs.open(path, "r")
  47.  if h==nil then
  48.   return nil
  49.  end
  50.  local value=h.readAll()
  51.  h.close()
  52.  return value
  53. end
  54.  
  55.  
  56.  
  57.  
  58. --serializes the value into a string
  59. --and then saves the string into a
  60. --file with the given path
  61. function saveValue(path,value)
  62.  return saveString(path,textutils.serialize(value))
  63. end
  64.  
  65.  
  66.  
  67.  
  68.  
  69. --saves the given string value into
  70. --a file with the given path
  71. --overwriting any contents if there are any
  72. function saveString(path,value)
  73.  if type(path)~="string" or type(value)~="string" then
  74.   return false
  75.  end
  76.  local h = fs.open(path, "w")
  77.  h.write(value)
  78.  h.close()
  79.  return true
  80. end
  81.  
  82.  
  83.  
  84.  
  85. --loads the lines from the given file
  86. --returns nil if there is an error
  87. --else returns an array with the lines
  88. --indexed from 1 upwards
  89. function loadLines(path)
  90.  if type(path)~="string" or not fs.exists(path) then
  91.   return nil
  92.  end
  93.  local h=fs.open(path, "r")
  94.  if h==nil then
  95.   return nil
  96.  end
  97.  local res={}
  98.  while(true) do
  99.   local value=h.readLine()
  100.   if value==nil then break end
  101.   res[#res+1]=value
  102.  end
  103.  h.close()
  104.  return res
  105. end
  106.  
  107.  
  108. function saveLines(path,lines)
  109.  for _,line inipairs(lines) do
  110.  
  111.  end
  112. end
  113.  
  114.  
  115.  
  116.  
  117. --returns an iterator of the words from the line
  118. --splits on spaces
  119. function wordIterator(line)
  120.  return line:gmatch("%S+")
  121. end
  122.  
  123.  
  124.  
  125.  
  126. --takes the lines from a perms file and
  127. --adds them to a lookup table which can
  128. --be used to check the perms of an user
  129. function toPermsTable(lines)
  130.  if type(lines)~="table" then return nil end
  131.  local users={}
  132.  for _,line in lines do
  133.   local username=nil
  134.   for _,word in wordIterator(line) do
  135.    if username==nil then
  136.     username=word
  137.     if users[username]==nil then
  138.      users[username]={}
  139.     end
  140.    else
  141.     users[username][word]=true
  142.    end
  143.   end
  144.  end
  145. end
  146.  
  147.  
  148.  
  149.  
  150. function hasPerm(permsTable,username,perm)
  151.  return permsTable[username]~=nil and permsTable[username][perm]~=nil
  152. end
  153.  
  154.  
  155.  
  156. function loadPerms(path)
  157.  local lines=loadLines(path)
  158.  if lines==nil then return nil end
  159.  return toPermsTable(lines)
  160. end
  161.  
  162.  
  163.  
  164.  
  165.  
  166. function savePerms(permsTable)
  167.  local lines={}
  168.  for username,perms in pairs(permsTable) do
  169.   local lineTable={username}
  170.   for perm,_ in pairs(perms) do
  171.    lineTable[#lineTable+1]=perm
  172.   end
  173.   lines[#lines+1]=table.concat(lineTable, " ")
  174.  end
  175.  saveLines(lines)
  176. end
  177.  
  178.  
  179.  
  180. function appendString(path,str)
  181.  --open code from wiki
  182.  --Opens path in either append or write mode, depending on whether it already exists or not.
  183.  local h = fs.open(path, fs.exists(path) and "a" or "w")
  184.  if h==nil then
  185.   return false
  186.  end
  187.  h.writeLine(str)
  188.  h.close()
  189.  return true
  190. end
  191.  
  192.  
  193.  
  194.  
  195. function appendUser(userName,permission)
  196.   if(userName==nil) then
  197.    print("UserName is nil...")
  198.    return
  199.   end
  200.   local append=""
  201.   if type(permission)=="string" and #permission>0 then
  202.    append=" "..permission
  203.   end
  204.   appendString(permsPath,userName..append)
  205. end
  206.  
  207.  
  208.  
  209. --downloads the given file and then returns 2
  210. function pastebin(shellObj, path,code,overwrite)
  211.   if overwrite then
  212.    fs.delete(path)
  213.   end
  214.   shellObj.run("pastebin","get", code, path)
  215.   return 2
  216. end
  217.  
  218. function loadLines(path)
  219.  if type(path)~="string" then error("Error (loadLines): path should be a string",2) end
  220.  if  not fs.exists(path) then
  221.   return nil
  222.  end
  223.  local h=fs.open(path, "r")
  224.  if h==nil then
  225.   return nil
  226.  end
  227.  local res={}
  228.  while(true) do
  229.   local line=h.readLine()
  230.   if line==nil then break end
  231.   res[#res+1]=line
  232.  end
  233.  h.close()
  234.  return res
  235. end
  236.  
  237.  
  238.  
  239. function getWords(line)
  240.  if type(line)~="string" then error("Error (getWords): line should be a string",2) end
  241.  local words={}
  242.  --"%S+"
  243.  --"%a+"
  244.  for word in string.gmatch(line, "%S+") do
  245.   words[#words+1]=word
  246.  end
  247.  return words
  248. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement