[SOLVED] Reading from a configuration file.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Admicos
Prole
Posts: 8
Joined: Sun Nov 23, 2014 1:41 pm

[SOLVED] Reading from a configuration file.

Post by Admicos »

Hello. I Want to read a configuration file i made to use it on my game.

File syntax is something like:

Code: Select all

#Comment

hello: world
number: 10
(I Can change it.)
Last edited by Admicos on Sun Dec 14, 2014 2:58 pm, edited 1 time in total.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Reading from a configuration file.

Post by undef »

Take a look at Lua's String libary:

http://www.lua.org/manual/5.1/manual.html#5.4
http://lua-users.org/wiki/StringLibraryTutorial

"Programming in Lua" also has very nice examples.

You will have to use some kind of pattern matching, with string.gmatch or string.gsub for example to extract the data from the string (file).

I could go more into detail, but you look like an Arch Linux user, so you'll be fine ;)
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
Admicos
Prole
Posts: 8
Joined: Sun Nov 23, 2014 1:41 pm

Re: Reading from a configuration file.

Post by Admicos »

Ok did a bit of research on gmatch.

But when i do this:

Code: Select all

for word in string.gmatch(cfg, "%a:") do
	 print(word)
end
it prints ONLY the last word before the colon. Do i need to use another pattern?

BTW. Yes i use Arch Linux ;)
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Reading from a configuration file.

Post by undef »

Admicos wrote:...when i do this:

Code: Select all

for word in string.gmatch(cfg, "%a:") do
	 print(word)
end
it prints ONLY the last word before the colon. Do i need to use another pattern?
That's because the pattern you're describing is actually a letter and a colon.
So this should print all the letters before a colon including the colon.

Play around with it some more and if you don't find the solution (only keep on reading if you don't want to find it yourself):



The pattern you're looking for is ": %.*"
This reads: A colon, followed by a space, followed by 0 or more characters
twitter | steam | indieDB

Check out quadrant on Steam!
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Reading from a configuration file.

Post by davisdude »

If you want to get the title and value, you would do something like this:

Code: Select all

string.gsub( cfg, '([^#:\n]-):%s*([^\n]+)', function( name, value )
	print( name .. ': ' .. value ..'!!' )
	-- Put code here
end )
If you don't understand how it works, read this:
"[^" - means that this cannot appear.
"-" - means as few times as possible.
"%s" - means space.
"*" - means as many times as possible, or zero times.
"(" - means to start the capture.
")" - means to stop the capture.

In other words, that function does something like this:
CODE .. TRANSLATION
([^#:\n]-) ..... Capture anything but "#", ":", or "\n" (new line) as few times as possible.
:%s* ........... Make sure there's a colon and a maybe a couple spaces (or no spaces).
([^\n]+) ....... Capture anything that's not a new line as many times as possible (but not no times).

And that's all there is to it!
This is pretty flexible, and should work in any of these circumstances:

Code: Select all

cfg = [[hello: world
name:    bill
va-lua: try this

#comment
Was: sup
YOu can have: ..Spaces!
Or:NoSpaces
Titles: cannot have #, :, or \n

#comments can have spaces
and: still be ignored!
comments: cannot have colons!
  lines can start: with spaces.]]
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Admicos
Prole
Posts: 8
Joined: Sun Nov 23, 2014 1:41 pm

Re: Reading from a configuration file.

Post by Admicos »

undef wrote:
Admicos wrote:...when i do this:

Code: Select all

for word in string.gmatch(cfg, "%a:") do
	 print(word)
end
it prints ONLY the last word before the colon. Do i need to use another pattern?
That's because the pattern you're describing is actually a letter and a colon.
So this should print all the letters before a colon including the colon.

Play around with it some more and if you don't find the solution (only keep on reading if you don't want to find it yourself):



The pattern you're looking for is ": %.*"
This reads: A colon, followed by a space, followed by 0 or more characters
This still shows only the one letter before colorn. But whatever i solved it with davisdude's solution. Thanks to everybody who tried to help!
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Reading from a configuration file.

Post by Azhukar »

Take a look at how the framework itself handles a config file. A simple .lua file returning a table is ideal for what you're looking for.

myConfig.lua

Code: Select all

local myConfTable = {
	abc = "some string",
	num = 1234,
}

return myConfTable
main.lua

Code: Select all

cfg = require("myConfig")

print(cfg.abc,cfg.num)
User avatar
Admicos
Prole
Posts: 8
Joined: Sun Nov 23, 2014 1:41 pm

Re: Reading from a configuration file.

Post by Admicos »

Azhukar wrote:Take a look at how the framework itself handles a config file. A simple .lua file returning a table is ideal for what you're looking for.

myConfig.lua

Code: Select all

local myConfTable = {
	abc = "some string",
	num = 1234,
}

return myConfTable
main.lua

Code: Select all

cfg = require("myConfig")

print(cfg.abc,cfg.num)
I never thinked that! But i fixed my problem with another solution. Thanks!
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Reading from a configuration file.

Post by undef »

Admicos wrote: This still shows only the one letter before colorn. But whatever i solved it with davisdude's solution. Thanks to everybody who tried to help!
Whoops, I forgot to put the capture.
And apperently "%." does not work for any character but only special characters... what works for words would be "%a+".
However davisdude's pattern is pretty fancy and way more versitile. :)
twitter | steam | indieDB

Check out quadrant on Steam!
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Reading from a configuration file.

Post by davisdude »

undef wrote:And apperently "%." does not work for any character but only special characters... what works for words would be "%a+".
Yeah, %. only capture periods. "." captures everything.
undef wrote:However davisdude's pattern is pretty fancy and way more versitile. :)
Thanks! I think it's pretty fancy myself, although I'm sure it could be improved.

Azhukar's solution also makes a lot of sense and is the way I do it, but everybody has their own preferences.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests