Read ini files

Oct 3, 2012 at 2:18pm
Hi.
Windows API has some functions for reading *.ini files
GetPrivateProfileString
GetPrivateProfileInt

Is there something similar in linux? or maybe with different format.

Thanks
Oct 3, 2012 at 6:52pm
The ini file layout is straight forward and well known and you can easily pickup up a some opensource code on the web quite easily.
Oct 15, 2012 at 6:27am
You have to use glib library
here is an example

ini file

1
2
3
4
5
6
[mysql]
host=localhost
port=3306
database=mysql
username=root
password=xxxxxxxxx


ini reader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
typedef struct {
        gchar *server, *user, *password, *database;
        gint port;
} Settings;

using namespace std;

void readConfigFile(char *cfgfile, char*& server, char*& username, char*& password, char*& database, int& port) {

        Settings *conf;
        GKeyFile *keyfile;
        GKeyFileFlags flags;
        GError *error = NULL;
        gsize length;

        keyfile = g_key_file_new ();
        //flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;

        cout << "Looking for config file " << cfgfile << endl;

        if (!g_key_file_load_from_file (keyfile, cfgfile, flags, &error)) {
                g_error (error->message);
        } else {
                cout << "config file loaded." << endl;
                conf = g_slice_new (Settings);
                conf->server    = g_key_file_get_string(keyfile, "mysql", "host", NULL);
                server = conf->server;
                conf->user      = g_key_file_get_string(keyfile, "mysql", "username", NULL);
                username = conf->user;
                conf->password  = g_key_file_get_string(keyfile, "mysql", "password", NULL);
                password = conf->password;
                conf->database  = g_key_file_get_string(keyfile, "mysql", "database", NULL);
                database = conf->database;
                conf->port      = g_key_file_get_integer(keyfile, "mysql", "port", NULL);
                port = conf->port;
        }

}


main fie (how to use)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using namespace std;

int main(int argc, char *argv[])
{
        int counter=0;
        MYSQL *conn, mysql;
        MYSQL_RES *res;
        MYSQL_ROW row;
        int query_state, port;
        char *server, *username, *password, *database, *cfgfile;

        if(argc == 2){
                cfgfile = argv[1];
        } else { cfgfile = "./config.ini"; }

        readConfigFile(cfgfile, server, username, password, database, port); /*here is your desire ;) */

        for(int i=0; i<1; i++) {

                mysql_init(&mysql);

                conn = mysql_real_connect(&mysql,server,username,password,database,port,NULL,0);
                if(conn == NULL){ cout << mysql_error(&mysql) << endl; }

                for(int x=0; x<1000; x++){
                        query_state = mysql_query(conn, "select Host from user");
                        if (query_state != 0) { cout << mysql_error(conn) << endl; }

                        res = mysql_store_result(conn);
                        while ( ( row = mysql_fetch_row(res)) != NULL ) { /*cout << row[0] << endl;*/ counter++; }
                }

                mysql_free_result(res);
                mysql_close(conn);
        }
        cout << counter <<endl;
        return EXIT_SUCCESS;
}
Last edited on Oct 15, 2012 at 6:34am
Oct 15, 2012 at 2:18pm
Well, as guestgulkan said, there are a number of open source libraries to handle ini-files. I don't thinks it's compulsory to use glib!

For example:

inih
Simple .INI file parser in C, good for embedded systems
http://code.google.com/p/inih/

libini
http://sourceforge.net/projects/libini/

Boost.Program_options
http://www.boost.org/doc/libs/1_51_0/doc/html/program_options.html

(The Boost library does more that just ini files.
http://stackoverflow.com/questions/4509062/boostprogram-options-how-to-handle-sections-in-ini-file )

And other popular formats include

JSON
http://en.wikipedia.org/wiki/JSON

YAML
http://en.wikipedia.org/wiki/YAML

XML
http://en.wikipedia.org/wiki/XML

Java style .properties file
http://en.wikipedia.org/wiki/.properties

Assorted libraries exist for all of these!

Andy

PS If I understand correctly, as primarily a Windows developer, there is no single Linux config file format? (I do also do cross-platform work, but not Linux specific)

http://redhatlinux4u.wordpress.com/2012/10/08/understanding-linux-configuration-files/

Is there a standard configuration file format in Linux?

In a word, no. Users who are new to Linux (rightly) feel frustrated that each configuration file looks like a new challenge to figure out. In Linux each programmer is free to choose the configuration file format he or she prefers. Format options range from the /etc/shells file, which contains a list of possible shells separated by a newline, to Apache’s complex /etc/httpd.conf file.



Last edited on Oct 15, 2012 at 2:27pm
Oct 21, 2012 at 6:31am
Just to add to the list:

http://code.jellycan.com/simpleini/
Nov 20, 2012 at 5:24pm
I apologize, that was my mistake, I hav to say you can use!
Topic archived. No new replies allowed.