Jump to content




Loading File as readable table in one function

computer api help

9 replies to this topic

#1 MagicCraftMaster

  • Members
  • 9 posts

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 by MagicCraftMaster, 02 January 2016 - 09:20 PM.


#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

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()

#3 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 02 January 2016 - 10:17 AM

View PostCreator, on 02 January 2016 - 09:45 AM, said:

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?

#4 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 02 January 2016 - 10:42 AM

I think because you can nest tables then :)

Edited by LeDark Lua, 02 January 2016 - 10:44 AM.


#5 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 02 January 2016 - 11:38 AM

Because that is the unserialize works. Better know the functioning than using 'magic'.

#6 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 02 January 2016 - 12:30 PM

View PostCreator, on 02 January 2016 - 11:38 AM, said:

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.

#7 MagicCraftMaster

  • Members
  • 9 posts

Posted 02 January 2016 - 07:20 PM

View PostCreator, on 02 January 2016 - 09:45 AM, said:

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

#8 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 02 January 2016 - 07:32 PM

do you wan't like, an settings API like INI? or something else?

#9 MagicCraftMaster

  • Members
  • 9 posts

Posted 02 January 2016 - 09:08 PM

View PostLeDark Lua, on 02 January 2016 - 07:32 PM, said:

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)

#10 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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 by KingofGamesYami, 02 January 2016 - 09:41 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users