`

glib-读取配置文件

阅读更多

1. 配置文件

配置文件的格式如下:

[组名]
#注释
Key = value
#注释
Key = value
#注释
Key = value
#注释
Key = value
#注释
Key = value
#注释
Key = value
 

2.glib读写配置文件

glib中的Key-value file parser 可以读写配置文件,下面具体介绍以下怎么使用:

加载配置文件:

static GKeyFile*
configure_open_file (const gchar *filename)
{
        GError *err = NULL;
        GKeyFile *keyfile;
        keyfile = g_key_file_new ();
        g_key_file_set_list_separator (keyfile, ',');
        if (!g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, &err)){
                g_error_free (err);
                g_key_file_free (keyfile);
                return NULL;
        }
        return keyfile;
}
 

该函数用来加载配置文件并返回GKeyFile指针,如果出错则返回NULL。

函数:

gboolean g_key_file_load_from_file (GKeyFile *key_file,

                                                                                     const gchar *file,

                                                                                     GKeyFileFlags flags,

                                                                                     GError **error);

参数 GKeyFileFlags falgs:

G_KEY_FILE_NONE

No flags, default behaviour

G_KEY_FILE_KEEP_COMMENTS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise all comments will be lost when the key file is written back.

G_KEY_FILE_KEEP_TRANSLATIONS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise only the translations for the current language will be written back.

 

从配置文件中读取整数:

 

static gint
configure_read_int (GKeyFile *keyfile, gchar *sec, gchar *key, gint default_val)
{
        GError *err = NULL;
        int val = g_key_file_get_integer (keyfile,
                                          sec,
                                          key,
                                          &err);
        if (err){
                error (err->message);
                g_error_free (err);
                return default_val;
        }
        return val;
}


 

 

从配置文件中读取字符串:

static void
configure_read_string (GKeyFile *keyfile, GString *ret, gchar *sec, gchar *key, gchar *default_val)
{
        GError *err = NULL;
        gchar* val = g_key_file_get_string (keyfile,
                                          sec,
                                          key,
                                          &err);
        if (err){
                g_string_assign (ret, default_val);
                g_error_free (err);
                return;
        }
        g_string_assign (ret, val);
        g_free (val);
}

 读取的字符串放在了ret中, 同时上面两个函数都提供可默认值的功能,即读取失败时设置默认值。

 

3.glib读写配置文件的例子

#include <glib.h>

void main ()
{       
       GKeyFile *keyfile = oj_configure_open_file ("test.conf");
       GString *test = g_string_new ("");
        oj_configure_read_string (keyfile, conf->oj_host_name,
                                  "TEST", "test1",
                                  "test1");
        gint int_test = oj_configure_read_int (keyfile, "TEST",
                               "test_int", 0);
        g_string_free (test, TRUE);
        g_key_file_free (keyfile);
}

 

编译命令:

gcc -o test test `pkg-config --cflags --libs glib-2.0`
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics