Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --get this file: pastebin get QFjF6SMs theinsektAPIs/fileHelp
- --load contents of a file as a string
- function loadString(path)
- local succes=true
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local value=h.readAll()
- h.close()
- return value
- end
- --save only the given string (value) to a file at the given path
- --will clear the previous contents of the file
- function saveString(path,value)
- --print("path: "..path)--DEBUG
- --print("value: "..value)--DEBUG
- if type(value)~="string" then
- error("Error: value must be a string")
- end
- --print("opening path: "..path)--DEBUG
- local h = fs.open(path, "w")
- if h==nil then
- return false
- end
- --print("writing value: "..value)--DEBUG
- h.write(value)
- h.close()
- return true
- end
- --load contents of a file as a string
- function loadValue(path)
- local succes=true
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local value=h.readAll()
- h.close()
- --unserialize and return data
- local data=textutils.unserialize(value)
- return data
- end
- --save only the given string (value) to a file at the given path
- --will clear the previous contents of the file
- function saveValue(path,value)
- local data=textutils.serialize(value)
- local h = fs.open(path, "w")
- if h==nil then
- return false
- end
- h.write(data)
- h.close()
- return true
- end
- --load lines from file into a table
- function loadLines(path)
- if type(path)~="string" then error("Error (loadLines): path should be a string",2) end
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local res={}
- while(true) do
- local line=h.readLine()
- if line==nil then break end
- res[#res+1]=line
- end
- h.close()
- return res
- end
- --splits a string on blank characters (probably)
- function splitLine(line)
- if type(line)~="string" then error("Error (getWords): line should be a string",2) end
- local words={}
- --"%S+"
- --"%a+"
- for word in string.gmatch(line, "%S+") do
- words[#words+1]=word
- end
- return words
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement