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

Reading Varible from File? - Even Possible?

Started by 3ydney, 05 March 2013 - 05:57 PM
3ydney #1
Posted 05 March 2013 - 06:57 PM
Well as the title says I kinda need to be able to read a varible from a file for example I have a config file that has the varible debug. How can I read this and make a program know that it is on debug. That program needs to know if it is on debug to run a different function. I dont know how to better explain it. Yes I have tried fs api and oi api…



--[ Config ]--
debug = true


--[ Program ]--
if debug == false then
    --code
else
    --code
end
SuicidalSTDz #2
Posted 05 March 2013 - 07:04 PM
You will have to use a file library wether it be fs or io to write a serialized table of config options to a file, then unserialize it when the table is needed to be read again. Im not posting any code since I will probably be ninja'd by remiX ;)/>

EDIT: We could help you learn from a mistake you made if we saw the code that you made to save the variable
Kingdaro #3
Posted 05 March 2013 - 07:07 PM
You could cheat the system a little and use os.loadAPI.


os.loadAPI('config')
print(config.debug)
SuicidalSTDz #4
Posted 05 March 2013 - 07:09 PM
You could cheat the system a little and use os.loadAPI.


os.loadAPI('config')
print(config.debug)
Hmm, tricky now aren't we? ;)/> That's actually a pretty good idear.

Hmm remiX hasn't posted anything yet… Must be coming up with some concoction of complex algorithms ;)/>
remiX #5
Posted 05 March 2013 - 07:18 PM
Yes, but at the beginning of your program, use os.loadAPI("config") to load the file and then the variables inside will become global - remember not to make it local debug = true in the config file because then they will not be accessible from your programLol. I had typed everthing out but forgot to click post. Btw, I don't think you need to do config.debug, just debug, isn't it?
3ydney #6
Posted 05 March 2013 - 07:26 PM
Yes, but at the beginning of your program, use os.loadAPI("config") to load the file and then the variables inside will become global - remember not to make it local debug = true in the config file because then they will not be accessible from your programLol. I had typed everthing out but forgot to click post. Btw, I don't think you need to do config.debug, just debug, isn't it?

I already but thank you for the tip….


Next question is if can you even read vars by loading the API?
SuicidalSTDz #7
Posted 05 March 2013 - 07:26 PM
Saving the settings:

local debug = false
local configTable = {}
local handle = io.open(fileName,"w")
print("Is debug true or false?")
debugState = read()
if debugState ~= "true" or debugState ~= "false" then
return error("Unknown state")
else
  table.insert(configTable,debugState)
end
handle:write(textutils.serialize(configTable))
handle:close()

Reading settings:
local handle = io.open(fileName,"r")
local debugState = handle[1]
handle:close()
if debugState == true then
--Debug is true!
else
--Debug is false!
end

This is only the skeleton code, not sure if it is in working order(Up to par). remiX may have more to say.

EDIT: Ninja'd, this is not the answer to your question in the above post
3ydney #8
Posted 05 March 2013 - 07:29 PM
^^ Thanks may use that..
3ydney #9
Posted 05 March 2013 - 07:51 PM
@remix well nope.. hmm


I made a file called start and another called conf.

start:


os.loadAPI("conf")
v = version1
print(v)

conf


version1 = "1.0"

Its returned nothing but an empty line…. Help please

<h6>IM not gay < RANDOM</h6>
SuicidalSTDz #10
Posted 05 March 2013 - 07:56 PM
@remix well nope.. hmm


I made a file called start and another called conf.

start:


os.loadAPI("conf")
v = version1
print(v)

conf


version1 = "1.0"

Its returned nothing but an empty line…. Help please

<h6>IM not gay < RANDOM</h6>
That's all remiX, I can't help you there. I don't like loading APIs n' stuff… I like my box! I like to stay in my box…

My guess is that you didn't load the api correctly since it returned a response (The empty line) meaning the variable "version1" was never defined. But that's just my guess ;)/>
3ydney #11
Posted 05 March 2013 - 08:05 PM
Nvm I worked it out…. It would have to be conf.version
SuicidalSTDz #12
Posted 05 March 2013 - 08:06 PM
Nvm I worked it out…. It would have to be conf.version
Exactly why I don't like loading APIs :P/> Or whatever that ungodly thing is…
Exerro #13
Posted 06 March 2013 - 12:53 AM
i have already made an API for this:
Spoiler

split = function(str,pat)

local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end

variable = {
vars = { };
add = function( name, data )
  table.insert( variable.vars, { name = name, type = type( data ), data = data } )
end;
save = function( file )
  local t = { }
  for i = 1,#variable.vars do
   if variable.vars[i].type == "table" then
	variable.vars[i].data = textutils.serialize( variable.vars[i].data )
   end
   table.insert( t, variable.vars[i].name..";"..variable.vars[i].type..";"..tostring( variable.vars[i].data ) )
  end
  k = fs.open( file, "w" )
  for i = 1,#t do
   k.writeLine( t[i] )
  end
  k.close( )
end;
remove = function( index )
  if type( index ) == "string" then
   for i = 1,#variable.vars do
	if variable.vars[i].name == index then
	 table.remove( variable.vars, i )
	 return true
	end
   end
  else
   table.remove( variable.vars, index )
  end
end;
load = function( file )
  local k = assert( fs.open( file, "r" ), "File not found" )
  m = k.readAll( )
  k.close( )
  k = split( m, "\n" )
  for i = 1,#k do
   m = split( k[i], ";" )
   if m[2] == "table" then
	m[3] = textutils.unserialize( m[3] )
   elseif m[2] == "number" then
	m[3] = tonumber( m[3] )
   elseif m[2] == "boolean" then
	if m[3] == "false" then
	 m[3] = false
	else
	 m[3] = true
	end
   end
   variable.add( m[1], m[3] )
   _G[m[1]] = m[3]
  end
end;
}

edit: oops clicked post…
variable.add( name, data ) to add a variable to the save list
variable.remove( name ) to remove a variable from the save list
variable.save( path ) to save the variables to the path specified
variable.load( path ) to load the variables from the path