Guest User

FileSnipe: Fix 1

a guest
Jul 22nd, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local snipeComment = {"--[[","{{FILESNIPE VAR HANDLE}}"," ","please do not edit anything in this comment block"," ","{ ,}","]]",}
  2. local mv = {
  3.   __index = function(t,k)
  4.     local file = fs.open(k,"r")
  5.     if not file then return nil end
  6.     local total = {}
  7.     line = file.readLine()
  8.     repeat
  9.       table.insert(total,line)
  10.       line = file.readLine()
  11.     until not line
  12.     file.close()
  13.     return total
  14.   end;
  15.   __newindex = function(t,k,v)
  16.     if type(v)==nil then
  17.       fs.delete(k)
  18.     else
  19.       local file = fs.open(k,"w")
  20.       for i=1,#v do
  21.         file.writeLine(v[i])
  22.       end
  23.       file.close()
  24.     end
  25.   end;
  26. }
  27. local function init()
  28.   fileSnipeNew = nil
  29.   fileSnipeNew = setmetatable({
  30.     fileData = {},
  31.     fileName = "nil",
  32.     fileMode = false,
  33.     bypass = false,
  34.     fileIsVar = false,
  35.     handle = function(self,bool)
  36.       if not bool and not self.bypass and not self.fileIsVar and self.fileData[2]=="{{FILESNIPE VAR HANDLE}}" then
  37.         self.fileIsVar = {}
  38.         for i=1,7 do
  39.          table.insert(self.fileIsVar,self.fileData[1])
  40.          table.remove(self.fileData,1)
  41.         end
  42.       elseif not bypass and self.fileIsVar then
  43.         for i=#self.fileIsVar,1,-1 do
  44.           table.insert(self.fileData,1,self.fileIsVar[i])
  45.         end
  46.         self.fileIsVar = false
  47.       end
  48.     end,
  49.     },{
  50.     __index = function(t,k)  
  51.       if t.fileMode then t.fileData=snipeAPIFiles[t.fileName] end      
  52.       t:handle(true)
  53.       if t.fileData[2]~="{{FILESNIPE VAR HANDLE}}" then
  54.         for i=#snipeComment,1,-1 do
  55.           table.insert(t.fileData,1,snipeComment[i])
  56.         end
  57.       end
  58.       local var = textutils.unserialize(t.fileData[6]) or {}
  59.       if tostring(var[k]):sub(1,8)=="function" then return loadstring("return "..var[k])() end
  60.       return var[k]
  61.     end;
  62.     __newindex = function(t,k,v)
  63.       if type(v)=="function" then
  64.         func = setmetatable({},{
  65.           __call = function(_,...)
  66.             if rawequal(t,({...})[1]) then
  67.               return v(...)
  68.             end
  69.             return v(t,...)
  70.           end;
  71.         })
  72.         rawset(t,k,func)
  73.       else
  74.         if t.fileMode then t.fileData=snipeAPIFiles[t.fileName] end
  75.         t:handle(true)
  76.         if t.fileData[2]~="{{FILESNIPE VAR HANDLE}}" then
  77.           for i=#snipeComment,1,-1 do
  78.             table.insert(t.fileData,1,snipeComment[i])
  79.           end
  80.           snipeAPIFiles[t.fileName] = t.fileData
  81.         end
  82.         local var=textutils.unserialize(t.fileData[6]) or {}
  83.         var[k] = v
  84.         t.fileData[6] = textutils.serialize(var):gsub("\n","")
  85.         snipeAPIFiles[t.fileName] = t.fileData
  86.       end
  87.     end;
  88.   })
  89.   fileSnipeNew.getLine = function(self,...)
  90.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  91.     self:handle(false)
  92.     for _,line in ipairs({...}) do
  93.       if line<0 then line=#self.fileData+(line+1) end
  94.       if line>#self.fileData or line<0 then return nil
  95.       else return self.fileData[line] end
  96.     end
  97.   end
  98.   fileSnipeNew.setLine = function(self,line,data)
  99.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  100.     self:handle(false)
  101.     if line<0 then line=#self.fileData+(line+1) end
  102.     if line>#self.fileData then
  103.       for i=1,line-#self.fileData do
  104.         table.insert(self.fileData,"")
  105.       end
  106.     elseif line<1 then
  107.       repeat
  108.         table.insert(self.fileData,1,"")
  109.         line = line+1
  110.       until line>0
  111.     end
  112.     self.fileData[line] = data
  113.     self:handle(true)
  114.     snipeAPIFiles[self.fileName] = self.fileData
  115.   end
  116.   fileSnipeNew.len = function(self)
  117.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  118.     self:handle(false)
  119.     return #self.fileData
  120.   end
  121.   fileSnipeNew.getFile = function(self)
  122.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  123.     self:handle(false)
  124.     return self.fileData
  125.   end
  126.   fileSnipeNew.setFile = function(self,...)
  127.     local data = {...}
  128.     self:handle(false)
  129.     if data[1]=="table" then data = data[1] end
  130.     self.fileData = data
  131.     self:handle(true)
  132.     snipeAPIFiles[self.fileName] = self.fileData
  133.   end
  134.   fileSnipeNew.remLine = function(self,...)
  135.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  136.     self:handle(false)
  137.     local args = {...}
  138.     table.sort(args,function(a,b) return a>b end)
  139.     for _,line in ipairs(args) do
  140.       table.remove(self.fileData,line)
  141.       self:handle(true)
  142.       snipeAPIFiles[self.fileName]=self.fileData
  143.     end
  144.   end
  145.   fileSnipeNew.insert = function(self,line,data)
  146.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  147.     self:handle(false)
  148.     if type(data)=="string" then table.insert(self.fileData,line,data)
  149.     elseif type(data)=="table" then
  150.       for i=#data,1,-1 do
  151.         table.insert(self.fileData,line,data[i])
  152.       end
  153.     end
  154.     self:handle(true)
  155.     snipeAPIFiles[self.fileName]=self.fileData
  156.   end
  157.   fileSnipeNew.find = function(self,str,whole)
  158.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  159.     self:handle(false)
  160.     local instances = {}
  161.     local cut = {}
  162.     for i=1,#self.fileData do
  163.       local t=1
  164.       cut = {string.find(self.fileData[i],str,t)}
  165.       while cut[1] do
  166.         local s = self.fileData[i]:sub(cut[2]+1,cut[2]+1)
  167.         if not whole or s==" " or s=="?" or s=="," or s=="!" or s=="." or s=="\"" or s=="'" or s=="(" or s==")" or s=="=" or s=="" then
  168.           table.insert(instances,{i,cut[1]})
  169.         end
  170.         t=cut[2]+1
  171.         cut = {string.find(self.fileData[i],str,t)}
  172.       end
  173.     end
  174.     return instances
  175.   end
  176.   fileSnipeNew.swap = function(self,line1,line2)
  177.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  178.     self:handle(false)
  179.     if line1<=#self.fileData and line2<=#self.fileData then
  180.       local ph = self.fileData[line1]
  181.       self.fileData[line1]=self.fileData[line2]
  182.       self.fileData[line2]=ph
  183.       self:handle(true)
  184.       snipeAPIFiles[self.fileName]=self.fileData
  185.     end
  186.   end
  187.   fileSnipeNew.replace = function(self,old,new,index,whole)
  188.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  189.     self:handle(false)
  190.     local tbl = self:find(old,whole)
  191.     local segment = self:getLine(tbl[index][1])
  192.     segment = segment:sub(1,tbl[index][2]-1)..new..segment:sub(tbl[index][2]+old:len(),segment:len())
  193.     self:setLine(tbl[index][1],segment)
  194.   end
  195.   fileSnipeNew.replaceAll = function(self,old,new,whole)
  196.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  197.     self:handle(false)
  198.     local tbl = self:find(old,whole)
  199.     for i=#tbl,1,-1 do
  200.       local segment = self:getLine(tbl[i][1])
  201.       segment = segment:sub(1,tbl[i][2]-1)..new..segment:sub(tbl[i][2]+old:len(),segment:len())
  202.       self:setLine(tbl[i][1],segment)
  203.     end
  204.   end
  205.   fileSnipeNew.append = function(self,...)
  206.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  207.     self:handle(false)
  208.     local req = {...}
  209.     if type(req[1])=="table" then req = req[1] end
  210.     for _,data in ipairs(req) do
  211.       table.insert(self.fileData,data)
  212.     end
  213.     self:handle(true)
  214.     snipeAPIFiles[self.fileName] = self.fileData
  215.   end
  216.   fileSnipeNew.combineLines = function(self,line1,line2,sep)
  217.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  218.     self:handle(false)
  219.     if not sep then sep=" " end
  220.     self.fileData[line1] = self.fileData[line1]..sep..self.fileData[line2]
  221.     table.remove(self.fileData,line2)
  222.     self:handle(true)
  223.     snipeAPIFiles[self.fileName] = self.fileData
  224.   end
  225.   fileSnipeNew.moveLine = function(self,from,to)
  226.     if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
  227.     self:handle(false)
  228.     if from<0 then from=#self.fileData+(from+1) end
  229.     if to<0 then to=#self.fileData+(to+1) end
  230.     if to>from then
  231.       table.insert(self.fileData,to+1,self.fileData[from])
  232.     else
  233.       table.insert(self.fileData,to,self.fileData[from])
  234.       from=from+1
  235.     end
  236.     table.remove(self.fileData,from)
  237.     self:handle(true)
  238.     snipeAPIFiles[self.fileName] = self.fileData
  239.   end
  240.   fileSnipeNew.concatLine = function(self,line,data)
  241.     if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
  242.     self:handle(false)
  243.     if line<0 then line=#self.fileData+(line+1) end
  244.     self.fileData[line] = self.fileData[line]..data
  245.     self:handle(true)
  246.     snipeAPIFiles[self.fileName] = self.fileData
  247.   end
  248.   fileSnipeNew.sepLines = function(self,line,sep)
  249.     if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
  250.     self:handle(false)
  251.     table.insert(self.fileData,line+1,self.fileData[line]:sub(sep,self.fileData[line]:len()))
  252.     self.fileData[line] = self.fileData[line]:sub(1,sep-1)
  253.     self:handle(true)
  254.     snipeAPIFiles[self.fileName] = self.fileData
  255.   end
  256.   fileSnipeNew.rawVar = function(self,key)
  257.     if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
  258.     local var = textutils.unserialize(self.fileData[6])
  259.     if not var then return nil
  260.     else return var[key] end
  261.   end
  262. end
  263. snipeAPIFiles = setmetatable({},mv)
  264. function open(sFile,mode,bypass)
  265.   init()
  266.   fileSnipeNew.fileName = sFile
  267.   if type(mode)~="string" then
  268.     bypass = mode
  269.     mode = nil
  270.   end
  271.   local setData = snipeAPIFiles[sFile]
  272.   if setData then fileSnipeNew.fileData = setData end
  273.   if mode=="d" then fileSnipeNew.fileMode=true
  274.   elseif mode=="s" or not mode then fileSnipeNew.fileMode=false
  275.   else error("mode format not supported") end
  276.   if bypass==nil then bypass = false end
  277.   fileSnipeNew.bypass = bypass
  278.   return fileSnipeNew
  279. end
Advertisement
Add Comment
Please, Sign In to add comment