Game Development Community

configuration files - how?

by Ben Heath · in Technical Issues · 04/09/2003 (8:07 pm) · 6 replies

I don't have much experience in writing programs that read and write from external files, but I'm teaching myself how to do it with the excellent man pages you find on Linux.

My engine design requires that I read from configuration files. How do you do it? Moreover, how do YOU do it?

How do you parse the variables and values in a configuration file? Please help.

Thank you.

#1
04/10/2003 (10:15 pm)
You should mention what programming language you are using.

If you want the configuration files to be text, you can do something like:

option1=whatever
option2=whatever
option3=whatever

Just use some string functions to find the '='. In C, you would use strtok or something. In Java, you would use String.indexOf and String.substring.

If your configuration files are NOT text, then you can basically do whatever you want.

Or are you asking about actually reading/writing files? Not specifics about configuration files? Then you'll definetly need to say what programming language you're using.
#2
04/11/2003 (1:13 pm)
Yeah, I forgot to do that. I'm using C, and I'm trying to read and parse text configuration files with the ANSI C library.


Ah hell just give me a link to some code and I'll learn from that....
#3
04/11/2003 (2:50 pm)
freshmeat.net/projects/confuse/?topic_id=809

That oughta answer all your questions :)

If not, look into using fscanf and co to read the file.
#4
04/12/2003 (10:12 am)
Thanks Ben. You got a good name. :)
#5
04/12/2003 (8:27 pm)
I'd suggest that you avoid fscanf. It works fine as long as the file you are reading is formatted exactly how you expect it to be. If the file becomes corrupted - either accidently or maliciously - fscanf will not produce good results.

You would be better off using fread to load the entire config file into a memory buffer, and then parsing it from there using string functions, or your own lexer/parser routines. Remember to check that the file is of reasonable size (say, less than a megabyte) before trying to malloc the buffer.

Personally, I found it useful to write some general-purpose lexing and parsing classes in C++ for dealing with configuration files and the like. The "dragon book" by Aho, Sethi, and Ullman is a good resource if you are interested in learning these techniques.
#6
04/12/2003 (9:26 pm)
That's what I was thinking: (1) open the file, (2) use fread() to cache the whole file into a buffer, (3) close the file, and (4) get parsin'.

I was considering an approach where the file would be opened and would remain open while it is parsed, one token at a time. (This is probably what fscanf() would be used for, right?) But, as you said John, that could get screwy.

Oh and Steve, I looked up strtok() in the Linux man pages. It's suggested that that function should _never_ be used for various reasons. There are still alternatives to it, of course, but I thought I'd let you know.