MudkipTheEpic

EditLine API

Feb 3rd, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. function editLine(file, line, content)
  2. local lines = {}
  3. local num,d = 0,nil
  4. if fs.isReadOnly(file) then
  5. error("File is readonly",2)
  6. end
  7. if not type(content) == "string" then
  8. error("bad argument: string expected, got "..type(content), 2)
  9. end
  10. if not type(file) == "string" then
  11. error("bad argument: string expected, got "..type(file), 2)
  12. end
  13. if not type(line) == "number" then
  14. error("bad argument: number expected, got "..type(line), 2)
  15. end
  16. if not fs.exists(file) then
  17. error("File doesn't exist",2)
  18. end
  19. if fs.isDir(file) then
  20. error("File is directory",2)
  21. end
  22.  
  23. num = num-1
  24. if num > line then
  25. error("File smaller than "..line.." lines long.",2)
  26. end
  27. local reading = fs.open(file, "r")
  28. while true do
  29. f=nil
  30. f = reading.readLine()
  31. if not f then break end
  32. table.insert(lines, f)
  33. end
  34. reading.close()
  35. reading = nil
  36. lines[line] = content
  37. if num > #lines-1 then
  38. error("File smaller than "..line.." lines long.",2)
  39. end
  40. local write = fs.open(file, "w")
  41. for i=1, #lines do
  42. write.writeLine(lines[i])
  43. end
  44. write.close()
  45. write = nil
  46. return true
  47. end
Advertisement
Add Comment
Please, Sign In to add comment