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

How to create config?

Started by Konlab, 11 May 2014 - 03:45 PM
Konlab #1
Posted 11 May 2014 - 05:45 PM
How to create config?
Lignum #2
Posted 11 May 2014 - 05:56 PM
I assume you mean creating config files for your own programs. Have a look at the fs API, fs.open specifically.
RoD #3
Posted 12 May 2014 - 09:47 AM
I have this one based on luaIDE, that loads the config into a table:

local function loadConfig(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
  local k, v = string.match(l, "^(%a+) = (%a+)")
  if k and v then config[k] = v end
  l = f:read("*l")
end
f:close()
return config
end

If you the want to use the config just:

config = loadCOnfig("myFolders/config")
print( config.firstConfig )
print( config.secondConfig )
print( config.thirdConfig )

The config file would look like:

firstConfig = "Hey, i am line one!"
secondConfig = "Hello, i am the second line!"
thirdConfig = "Hi, third line here!"

So basically you loaded variables from a file into a table.
theoriginalbit #4
Posted 12 May 2014 - 10:00 AM
I also have a version which can be found on my programs page in the APIs section (link in signature) called ccConfig, it can handle other forms of data like numbers, strings, colours, and booleans.
RoD #5
Posted 12 May 2014 - 11:41 AM
I also have a version which can be found on my programs page in the APIs section (link in signature) called ccConfig, it can handle other forms of data like numbers, strings, colours, and booleans.
Its pretty usefull. I might test it soon. :)/>
Konlab #6
Posted 16 May 2014 - 06:01 PM
I have this one based on luaIDE, that loads the config into a table:

local function loadConfig(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
  local k, v = string.match(l, "^(%a+) = (%a+)")
  if k and v then config[k] = v end
  l = f:read("*l")
end
f:close()
return config
end

If you the want to use the config just:

config = loadCOnfig("myFolders/config")
print( config.firstConfig )
print( config.secondConfig )
print( config.thirdConfig )

The config file would look like:

firstConfig = "Hey, i am line one!"
secondConfig = "Hello, i am the second line!"
thirdConfig = "Hi, third line here!"

So basically you loaded variables from a file into a table.

f:read() io.open() what??? Why : and why io.open what is io… (Input-output I know but how can the io API manage files?)
I don't understand anything what was in your post RoD
I know that, I'm not any pro, then please tell me this as for noob.

Sorry for bad english
apemanzilla #7
Posted 16 May 2014 - 08:16 PM
I have this one based on luaIDE, that loads the config into a table:

local function loadConfig(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
  local k, v = string.match(l, "^(%a+) = (%a+)")
  if k and v then config[k] = v end
  l = f:read("*l")
end
f:close()
return config
end

If you the want to use the config just:

config = loadCOnfig("myFolders/config")
print( config.firstConfig )
print( config.secondConfig )
print( config.thirdConfig )

The config file would look like:

firstConfig = "Hey, i am line one!"
secondConfig = "Hello, i am the second line!"
thirdConfig = "Hi, third line here!"

So basically you loaded variables from a file into a table.

f:read() io.open() what??? Why : and why io.open what is io… (Input-output I know but how can the io API manage files?)
I don't understand anything what was in your post RoD
I know that, I'm not any pro, then please tell me this as for noob.

Sorry for bad english
Please take a look at the cc wiki page or the Lua reference manual page for the IO API.
Konlab #8
Posted 16 May 2014 - 08:18 PM
I have the answer
Please Lock this topic!
apemanzilla #9
Posted 16 May 2014 - 08:24 PM
I have the answer
Please Lock this topic!
We usually don't lock AaP section threads in case someone has a similar/continued issue (in a non-hijacking way)
Konlab #10
Posted 16 May 2014 - 08:26 PM
?
apemanzilla #11
Posted 16 May 2014 - 08:29 PM
Basically, if someone else had the same problem and wanted to post here, or if you had more trouble with it, you would have to start a whole new thread because this one is locked. That's why we keep them unlocked, so the section isn't flooded with duplicates as much.
Lyqyd #12
Posted 16 May 2014 - 10:34 PM
Also, for reference, when using the io API and wanting to process individual lines, it is much easier to use the handle:lines() iterator than to use a while loop and handle:read("*l").