This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MagicCraftMaster's profile picture

Loading File as readable table in one function

Started by MagicCraftMaster, 02 January 2016 - 04:31 AM
MagicCraftMaster #1
Posted 02 January 2016 - 05:31 AM
i am developing my first advanced OS and upgrading it as i go along with fresh scripts and i want to load a config/save file but, i don't know how to use tables and it is complex i need a tiny function to open a file as a table and allow it to be read with my own function by of witch i will need an example this is what i want it to look like:

function getTable(fileName)
–Insert Methods here
–load data after : symbol
end

function getData(tableName, line, dataType)
–Insert Small Method
end

Preferences = getTable("OS/Data")

term.write(getData(Preferences, 5, string)

if someone could write a code that is new and works on CC 1.7.5+ that would be great thanks :)/>

Edit: i am making a basic OS for personal use later to be improved and maybe released to the public however i requires a config to make and i need it in API form so i can just add "os.loadAPI("configAPI")" that way i can simply create new menus with little effort
Edited on 02 January 2016 - 08:20 PM
Creator #2
Posted 02 January 2016 - 09:45 AM
To get a table from a file, just read it and the loadstring it:

local file = fs.open( path, "r")
local data = "return "..file.readAll()
file.close()
local t = loadstring(data)
TableOfMine = t()
Wojbie #3
Posted 02 January 2016 - 10:17 AM
To get a table from a file, just read it and the loadstring it:

local file = fs.open( path, "r")
local data = "return "..file.readAll()
file.close()
local t = loadstring(data)
TableOfMine = t()
Why are you using return..data + loadstring instead of texutils.unserialize?
LeDark Lua #4
Posted 02 January 2016 - 10:42 AM
I think because you can nest tables then :)/>
Edited on 02 January 2016 - 09:44 AM
Creator #5
Posted 02 January 2016 - 11:38 AM
Because that is the unserialize works. Better know the functioning than using 'magic'.
Wojbie #6
Posted 02 January 2016 - 12:30 PM
Because that is the unserialize works. Better know the functioning than using 'magic'.
While I agree that it's better to know functionality than function unserialize also quarantines the code by running it with empty global table. That is important functionality when dealing with data from unsure source.
MagicCraftMaster #7
Posted 02 January 2016 - 07:20 PM
To get a table from a file, just read it and the loadstring it:

local file = fs.open( path, "r")
local data = "return "..file.readAll()
file.close()
local t = loadstring(data)
TableOfMine = t()
that is great but how do i read a line and get all data like i asked i need to be able to get all data on a single line after the : symbol. just like mentioned above. also what goes where the "r" is nothing? i am a slight lua noob an am working on getting better
LeDark Lua #8
Posted 02 January 2016 - 07:32 PM
do you wan't like, an settings API like INI? or something else?
MagicCraftMaster #9
Posted 02 January 2016 - 09:08 PM
do you wan't like, an settings API like INI? or something else?

what i want to do is make an API with other functions i am developing that allows a simple config control system aka load, read, write. however i dont know how to do this and i need a premade small set of 3 functions to do this simply called; getTable(file), getData(table, line, type), setData(table, line, file)
KingofGamesYami #10
Posted 02 January 2016 - 09:41 PM
I made a magical autosaving table API a while ago; you might use that.

Basically,

os.loadAPI( "API" )
local file_name = "permissions"
local Permissions = {}
if fs.exists( file_name ) then
  local file = fs.open( file_name, "r" )
  Permissions = textutils.unserialize( file.readAll() )
  file.close()
end

Permissions = API.makeAutoSaving( Permissions, file_name )

--#now you can use Permissions like a normal table
--#when you add data to it, it will save automatically
Edited on 02 January 2016 - 08:41 PM