Advertisement
Tatantyler

Diff Program

Sep 6th, 2013
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. -- Diff
  2. -- By KillaVanilla
  3.  
  4. local args = {...}
  5.  
  6. local function get_diff(f1, f2)
  7.      -- Do trimming:
  8.     local start = 1
  9.     local f1_end = #f1
  10.     local f2_end = #f2
  11.     while (start <= #f1) and (start <= #f2) do
  12.         if not (f1[start] == f2[start]) then
  13.             start = start-1
  14.             break
  15.         else
  16.             start = start+1
  17.         end
  18.     end
  19.     while (f1_end >= start) and (f2_end >= start) do
  20.        if not (f1[f1_end] == f2[f2_end]) then
  21.             f1_end = f1_end+1
  22.             f2_end = f2_end+1
  23.             break
  24.         else
  25.             f1_end = f1_end-1
  26.             f2_end = f2_end-1
  27.         end
  28.     end
  29.     local c = {}
  30.     for i=0, #f1 do
  31.         c[i] = {}
  32.         c[i][0] = 0
  33.     end
  34.     for i=0, #f2 do
  35.         c[0][i] = 0
  36.     end
  37.     for i=1, #f1 do
  38.         for j=1, #f2 do
  39.             if f1[i] == f2[j] then
  40.                 c[i][j] = c[i-1][j-1]+1
  41.             else
  42.                 c[i][j] = math.max(c[i][j-1], c[i-1][j])
  43.             end
  44.         end
  45.     end
  46.  
  47.     local d = {}
  48.     local function recurser(i, j)
  49.         if (i>0) and (j>0) and (f1[i] == f2[j]) then
  50.             recurser(i-1, j-1)
  51.             table.insert(d, {0, f1[i]})
  52.             --print(" "..f1[i])
  53.         elseif (j>0) and ((i==0) or (c[i][j-1] >= c[i-1][j])) then
  54.             recurser(i, j-1)
  55.             table.insert(d, {1, f2[j], i, j})
  56.             --print(j..">"..f2[j])
  57.         elseif (i>0) and ((j==0) or (c[i][j-1] < c[i-1][j])) then
  58.             recurser(i-1, j)
  59.             table.insert(d, {2, f1[i], i, j})
  60.             --print(i.."<"..f1[i])
  61.         end
  62.     end
  63.     recurser(#f1, #f2)
  64.     return d
  65. end
  66.  
  67. if #args < 3 then
  68.     print("diff - Get differences between files")
  69.     print(string.rep("=", term.getSize()))
  70.     print("USAGE: diff <file1> <file2> <output file>")
  71. else
  72.     assert(fs.exists(args[1]), args[1].." does not exist!")
  73.     assert(fs.exists(args[2]), args[2].." does not exist!")
  74.     local lines_1 = {}
  75.     local lines_2 = {}
  76.     local last_pause = os.clock()
  77.     local f1 = fs.open(args[1], "r")
  78.     while true do
  79.         local line = f1.readLine()
  80.         if line then
  81.             table.insert(lines_1, line)
  82.         else
  83.             break
  84.         end
  85.         if (os.clock() - last_pause) >= 2.8 then
  86.             os.queueEvent("")
  87.             os.pullEvent("")
  88.             last_pause = os.clock()
  89.         end
  90.     end
  91.     f1.close()
  92.     local f2 = fs.open(args[2], "r")
  93.     while true do
  94.         local line = f2.readLine()
  95.         if line then
  96.             table.insert(lines_2, line)
  97.         else
  98.             break
  99.         end
  100.         if (os.clock() - last_pause) >= 2.8 then
  101.             os.queueEvent("")
  102.             os.pullEvent("")
  103.             last_pause = os.clock()
  104.         end
  105.     end
  106.     f2.close()
  107.     local diff = get_diff(lines_1, lines_2)
  108.     local f3 = fs.open(args[3], "w")
  109.     for i=1, #diff do
  110.         if diff[i][1] == 1 then
  111.             f3.writeLine(diff[i][4]..">"..diff[i][2])
  112.         elseif diff[i][1] == 2 then
  113.             f3.writeLine(diff[i][4].."<"..diff[i][2])
  114.         end
  115.     end
  116.     --f3.writeLine(textutils.serialize(diff))
  117.     f3.close()
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement