Advertisement
Redxone

[Computercraft] fPath API

May 30th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. local function path_goback(path,amm)
  2.     pathtbl={}
  3.     s=""
  4.     pathsize=0
  5.     -- Divide string and count the number of
  6.     -- divisions.
  7.      for str in string.gmatch(path, "([^/]+)") do
  8.          pathtbl[#pathtbl+1] = str  
  9.      end
  10.      for k, v in pairs(pathtbl) do
  11.          pathsize=k
  12.      end
  13.      pathsize = pathsize - amm
  14.     -- Split string into words based on seperator.
  15.     pathtbl={}
  16.     for str in string.gmatch(path, "([^/]+)") do
  17.         pathtbl[#pathtbl+1] = str  
  18.     end
  19.     -- Based on how large the user wants the string to be
  20.     -- add only the string bits that led up to the user defined
  21.     -- size.
  22.     for k, v in pairs(pathtbl) do
  23.         if(k <= pathsize)then s = s..pathtbl[k].."/" end
  24.     end
  25.     return "/" .. s
  26. end
  27. local function path_getsize(path)
  28.     pathtbl={}
  29.     pathsize=0
  30.     -- Divide string and count the number of
  31.     -- divisions.
  32.      for str in string.gmatch(path, "([^/]+)") do
  33.          pathtbl[#pathtbl+1] = str  
  34.      end
  35.      for k, v in pairs(pathtbl) do
  36.          pathsize=k
  37.      end
  38.      return pathsize
  39. end
  40.  
  41. function newpath(name,stuckindex)
  42.     if(name == nil or type(name) ~= "string")then error("PathAPI: Name must be provided for new paths. ") end
  43.     if(string.sub(name,#name,#name) ~= "/")then
  44.         name = name .. "/"
  45.     end
  46.     if(type(stuckindex) ~= "number")then stuckindex = 0 end
  47.     local pathobj = {
  48.         path = name,
  49.         stuckind = stuckindex,
  50.         getsize = function(self)
  51.            return path_getsize(self.path)
  52.         end,
  53.         goback = function(self,amm)
  54.             if(self:getsize() - amm >= self.stuckind)then
  55.                self.path = path_goback(self.path,amm)
  56.                return self.path
  57.             else
  58.                 return false
  59.             end
  60.         end,
  61.         getraw = function(self)
  62.             return self.path
  63.         end,
  64.         add = function(self,new)
  65.             self.path = self.path .. new .. "/"
  66.             return self.path
  67.         end,
  68.         set = function(self,npath)
  69.             self.path = npath
  70.             return self.path
  71.         end,
  72.         lockpath = function(self)
  73.             self.stuckind = self:getsize()
  74.         end,
  75.         unlockpath = function(self)
  76.             self.stuckind = 0
  77.         end,
  78.     }
  79.     return pathobj
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement