Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Patch
- -- By KillaVanilla
- local args = {...}
- if #args < 2 then
- print("patch - change files according to a patchfile")
- print(string.rep("=", term.getSize()))
- print("USAGE: <old file> <patchfile>")
- return
- end
- assert(fs.exists(args[1]), args[1].." does not exist!")
- -- read lines:
- local f = fs.open(args[1], "r")
- local lines = {}
- local last_pause = os.clock()
- while true do
- local line = f.readLine()
- if line then
- table.insert(lines, line)
- else
- break
- end
- if (os.clock() - last_pause) >= 2.8 then
- os.queueEvent("")
- os.pullEvent("")
- last_pause = os.clock()
- end
- end
- f.close()
- local f = fs.open(args[2], "r")
- local patch_lines = {}
- local last_pause = os.clock()
- while true do
- local line = f.readLine()
- if line then
- table.insert(patch_lines, line)
- else
- break
- end
- if (os.clock() - last_pause) >= 2.8 then
- os.queueEvent("")
- os.pullEvent("")
- last_pause = os.clock()
- end
- end
- f.close()
- local line_offset = 0
- for i=1, #patch_lines do
- local line_new, type, text = string.match(patch_lines[i], "(%d+)([%>%<])([^%>%<]*)")
- line_new = tonumber(line_new)
- --print(type)
- if type == ">" then
- --print("ADD LINE "..line_new..": \""..text.."\"")
- table.insert(lines, line_new, text)
- elseif type == "<" then
- local start_search = line_new-5
- local pos = line_new
- if start_search < 1 then
- start_search = 1
- end
- for j=start_search, line_new+5 do
- if lines[j] == text then
- pos = j
- break
- end
- end
- --print("REMOVE LINE "..pos.."("..line_new..")"..": \""..lines[pos].."\"")
- table.remove(lines, pos)
- end
- end
- local f = fs.open(args[1]..".out", "w")
- for i=1, #lines do
- f.writeLine(lines[i])
- end
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement