Hello
It would be great if someone suggest me a library for handling configuration files.Is there a general method for this job?
PS:i need it for *nix platforms

Thanks in advance.

configuration files are just plain-ordinary text files, so just use ifstream to read them. There are several ways to format the file, one way is in the form <variable name> = <value> . For example:

UserName=John
Password=xxxyyy
Age=18
Address=101 Any Street
City=Anytown
State=Illinois
Country=USA

The list of items in the file is endless, completely dependent on your program.

I would also recomend making a function to retreive variables quickly like so:

char* GetVar(char* str,char* file)
{
    ifstream fin(file);
    if(!fin) // file doesent exist
    {
        fin.close();
        return "";
    }
    char buf[256];
    int i = 0;
    bool found = true;
    while(fin.getline(buf,256))
    {
        if(strlen(buf) >= strlen(str))
            for(i = 0; i < (int)strlen(str); i++)
            {
                if(buf[i] != str[i]) // not = ?
                    found = false; // dident find
            }
        if(found)
        {
             fin.close();
             char val[256];
             for(int n = 0; i < (int)strlen(buf); i++,n++)
                 val[n] = buf[i];
             return val;
        }
    }
    fin.close();
    return "";
}

int main()
{
    cout << GetVar("UserName=","config.ini") << endl; 
    // if "config.ini" exists and a line in it starts with UserName=
    // then you will get that return value.
    return 0;
}

NOTICE:
I just made this here, so its not tested. Might have to fix a few errors : ) but just to give you an idea

--- edit ---

just tested, it works on Dev C++ :/ but gives a warning because i pass a local variable as a return value.


Also im sorry, it says i shouldent give away code, but i just couldent help it. I will try to hold back :/

Or just simply:

void get_lines(vector<string> &lines, const char *name)
{
    ifstream file(name);
    string str;

    if(file.is_open())
    {
        while(getline(file, str))
        {
             lines.push_back(str);
        }

        file.close();
    }
    else
    {
        set some error flag;
    }
}

Make it part of some class that handles the config file, then handle the lines.

You can't close() a file if it doesn't exist, and you never closed() it before you returned in if(found) .
It's a bit messy.

Or just simply:

void get_lines(vector<string> &lines, const char *name)
{
    ifstream file(name);
    string str;

    if(file.is_open())
    {
        while(getline(file, str))
        {
             lines.push_back(str);
        }

        file.close();
    }
    else
    {
        set some error flag;
    }
}

Make it part of some class that handles the config file, then handle the lines.

You can't close() a file if it doesn't exist, and you never closed() it before you returned in if(found) .
It's a bit messy.

well i did make it in 5 minutes... but ok, i prefer using an array of char's rather than strings but everyones different : )

vector of strings are a lot simpler, and safer, than array of charcters because you don't have to worry about memory allocation and deallocation. You also can avoid standard C functions such as strcpy(), strcmp() etc. Just a simple = and == operators will do the trick, just like you use with numeric variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.