Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Table saving and reading from files API
- -- by PaymentOption
- -- for evil of ccnet.
- -- Takes a table and saves it to a file.
- function saveTable(tTable, sFileName)
- if type(tTable) == "table" then
- if not fs.isDir(sFileName) then
- local fileHandle = fs.open(sFileName, "w")
- for nIndex, sValue in pairs(tTable) do
- if type(sValue) ~= "table" then
- fileHandle.writeLine(sValue)
- else
- fileHandle.close()
- error("Cannont write subtables.")
- end
- end
- fileHandle.close()
- else
- error("Cannon save " .. tTable .. " as a directory.")
- end
- else
- error("Cannont save " .. tTable .. " because it is not a table.")
- end
- end
- -- Takes a file path and attempts to read the contents into a table.
- function readTable(sFileName)
- if not fs.isDir(sFileName) and fs.exists(sFileName) then
- local tTable = {}
- local nFileLength_Lines = 0
- local fileHandle = io.open(sFileName, "r")
- for sLine in fileHandle:lines() do
- nFileLength_Lines = nFileLength_Lines + 1
- end
- fileHandle:close()
- fileHandle = fs.open(sFileName, "r")
- local sValueRead = ""
- for nLine=1, nFileLength_Lines do
- sValueRead = fileHandle.readLine()
- if tonumber(sValueRead) then
- table.insert(tTable, tonumber(sValueRead))
- else
- table.insert(tTable, sValueRead)
- end
- end
- fileHandle.close()
- return tTable
- else
- error("Cannot read a dreictory or a non-existant file.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement