Archived

This topic is now archived and is closed to further replies.

Parsing a config file

This topic is 5130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I'm working on a little config file loader and I'm not too sure how to do what I want... for example, my config file will look like this: " [Global] somevarible=somedata [Video] ScreenWidth=800 ScreenHeight=600 # Comment " what I want to do is call the load function and pass it a Section title, which would be in this case either Global, or Video. Then I want the function to scan through the file and once the section is found, load all the values under that section. Then it will be up to the calling function to pick and choose the values that are important. Now, I was thinking of making a ConfigData structure to hold the info, and this would be passed back. What I have so far is this:
typedef struct tagConfigData
{
	string		strSection;			// Section in config file that this struct holds data for
	string		*pstrData;			// Pointer to the actual data
	int			nDataLines;			// How many total pieces of data are there
}ConfigData, *pConfigData;
 
I would like the pointer pstrData to be like an array that will have all the lines in it, however, I don't know how many lines there will be. What are your thoughts on this: Find the section, then count how many lines there are, size the pointer accordingly, then find the section again, and start storing the data? or is there a better way? Thanks for taking the time to read this and help me out... Michael Rhodes Tiger Studios Entertainment http://www.tigerstudios.net [edited by - mrhodes on November 30, 2003 9:39:25 AM]

Share this post


Link to post
Share on other sites
well, a problem I see with returning all the data in a specific section is this. If you want it to return the actual data (ie 800, 600 or "hello" or whatever) then you will need a uniform way to store dissimilar types in the functions return (if indeed a section can mix text and values of other sorts). You could make this into a two stage process. Firstly, you use the stream function getline, to scan the file for the section you require (if you wish to be able to specify parts of the section throughout the file, then this could be more complex). Once the section is found... you read the lines of that section into a list (or vector or whatever you prefer) of strings. This list is what will be returned, and your calling function will have to provide its own parser. That it so say, it will have a struct for each section, corresponding to every possible field, and based on the value before the "=" it will appropriately convert the value (eg to string/int/float/whatever it expects) and place it within the struct. This should be fairly straightforward to implement... but depending on how insulated you want the calling function to be from the mechanics of the config file, you may or may not wish to take this step of converting the list of strings into a struct out of the calling function into a set of standalone config parsers. If you want this system to be more general, then you''ll probably have to return a list two lists, one list of structures with values and names of all integer datamembers, and one list of all string type data members. This method is probably the most general, and potentially useful to the widest range of applications.

ah, it just struck me that you might not be using C++ and have the benefits of the STL, in which case this whole setup might not be that sensible. oh well :S.

Share this post


Link to post
Share on other sites
Woohoo, I did it

I ended up making a linked list to read in the data. I made a little testing program and did this with only one function, GetConfigData(string strSection).

If anyone is interested in more detail for their own purposes just let me know... I''m sure there are better ways, but I just need something simple for now.

What I did returns a data pointer that has the var name, the data, and a pointer to the next one in the list...

Works nice now time to integrate it into my engine.

Michael Rhodes
Tiger Studios Entertainment
http://www.tigerstudios.net

Share this post


Link to post
Share on other sites
Wow, there goes my whole day of programming, lol!!

Thanks for the tip, looks promising! I think I''ll definately use that,

thanks,


Michael Rhodes
Tiger Studios Entertainment
http://www.tigerstudios.net

Share this post


Link to post
Share on other sites