Configuration File Parser

closed account (1vD3vCM9)
Hello, i'm currently making my own Server and Client program called(SwC), I want to read from a configuration file called SETTINGS.ini
However I can't figure out how to do it.
And I don't want to use boost libraries (or any other library)

Can someone write a simple parser or help me somehow?
(I have read some guides online but I they don't answer my question.)

for example I want to have in the file:

SETTINGS.ini :
PROGRAM_VERSION: 0.03

And I want the program to read the configuration file, and make the variable(PROGRAM_VERSION) be the one from the SETTINGS.ini
Please help me.
If your exact file format is ID: value, you could do something like this:


1
2
3
4
5
std::ifstream input("SETTINGS.ini");
char name[256]; // A buffer for our ID
char value[256]; // A buffer for the value
input.getline(name,256,':'); // Read until we have a :
input.getline(value,256,'\n'); // Read until we have a newline 


I didn't test this code yet, but it should work. Use a loop to go through the entire file until you reach the end. Also add some error-handling code so you can make sure all calls succeed.

You can also use std::getline, which accepts a string. In that case you don't have to worry about using buffers.
Last edited on
closed account (1vD3vCM9)
I know how to use fstream.
But that's not what I want.
I want a function for example, that is named LoadConfig
It will load the file content's from the file ("SETTINGS.ini")

I want the function to check if the file has a word that is named for example:
VERSION: "0.0.1"
And it will take the value after the word
I know how to use fstream.
But that's not what I want.
I want a function for example, that is named LoadConfig


You could say.. make a function named LoadConfig and probably within that function you're going to use some kind of file stream, whether you want to or not. This forum is not of the "tell us what you want and we'll supply it for you" variety.
closed account (1vD3vCM9)
@cire
You may have understood me wrong, I know this forum and I don't just request things.
I said that for example I want a function called LoadConfig and inside I want to write a code for a file parser, and I wanted a guide or any help.
You should open the file stream in your function, then use the getline calls in a loop. Simply check if the found ID matches what you are looking for, if so, you know the value belonging to that ID is the one you were looking for. A simple while loop is all you need for the simplest configuration files.
I want the function to check if the file has a word that is named for example:
VERSION: "0.0.1"


Did you even look at Shadowwolf's code?
A really simple implementation of your function could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* The LoadConfig function loads the configuration file given by filename
     It returns a map of key-value pairs stored in the conifuration file */
std::map<std::string,std::string> LoadConfig(std::string filename)
{
    std::ifstream input(filename); //The input stream
    std::map<std::string,std::string> ans; //A map of key-value pairs in the file
    while(input) //Keep on going as long as the file stream is good
    {
        std::string key; //The key
        std::string value; //The value
        std::getline(input, key, ':'); //Read up to the : delimiter into key
        std::getline(input, value, '\n'); //Read up to the newline into value
        std::string::size_type pos1 = value.find_first_of("\""); //Find the first quote in the value
        std::string::size_type pos2 = value.find_last_of("\""); //Find the last quote in the value
        if(pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1) //Check if the found positions are all valid
        {
            value = value.substr(pos1+1,pos2-pos1-1); //Take a substring of the part between the quotes
            ans[key] = value; //Store the result in the map
        }
    }
    input.close(); //Close the file stream
    return ans; //And return the result
}


This function is all you need to actually parse your file. I took the freedom of implementing the entire parser for you, hope it helps. Just don't forget to add some extra error handling though. The comments should be enough of an explanation, if not, just go to the reference and look up the individual functions.
Topic archived. No new replies allowed.