免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1060 | 回复: 0
打印 上一主题 下一主题

读取程序配置文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-15 11:40 |只看该作者 |倒序浏览

glib 学习笔记1,读取程序配置文件
glib 学习笔记,读取程序配置文件
转载请联系作者:fanyuanmail@126.com
有时在写一个程序时经常会从一个配置文件中读取一系列的参数,在度bluez代码时发现了一个非常好的方法。
这个方法基于glib-2.0,例如要从名为main.conf中读取里面的配置值。
offmode = NoScan
pagetimeout = 8192
age = 26
  1 [General]
  2
  3 # List of plugins that should not be loaded on bluetoothd startup
  4 #DisablePlugins = network,input
  5
  6 # Default adaper name
  7 # %h - substituted for hostname
  8 # %d - substituted for adapter id
  9 Name = %h-%d:
10
11 # Default device class. Only the major and minor device class bits are
12 # considered
13 Class = 0x000100
14
15 # How long to stay in discoverable mode before going back to non-discoverable
16 # The value is in seconds. Default is 180, i.e. 3 minutes.
17 # 0 = disable timer, i.e. stay discoverable forever
18 DiscoverableTimeout = 0
19
20 # Use some other page timeout than the controller default one
21 # (16384 = 10 seconds)
22 PageTimeout = 8192
23
24 # Behaviour for Adapter.SetProperty("mode", "off")
25 # Possible values: "DevDown", "NoScan" (default)
26 OffMode = NoScan
27
28 # Discover scheduler interval used in Adapter.DiscoverDevices
29 # The value is in seconds. Defaults is 0 to use controller scheduler
30 DiscoverSchedulerInterval = 0
31 [test]
32 age=26


#include
static GKeyFile *load_config(const char *file)
{
        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, file, 0, &err)) {
                error("Parsing %s failed: %s", file, err->message);
                g_error_free(err);
                g_key_file_free(keyfile);
                return NULL;
        }
        return keyfile;
}
int main(int argc, int **argv)
{
        char* str;
        int val1,val2;
        GError* err = NULL;
        GKeyFile* keyfile;
        keyfile = load_config("main.conf");
        str = g_key_file_get_string(keyfile,"General","OffMode",&err);
        if (err) {
                printf("%s",err->message);
                g_clear_error(&err);
        }
        val1 = g_key_file_get_integer(keyfile,"General","PageTimeout",&err);
        if (err) {
                printf("%s",err->message);
                g_clear_error(&err);
        }
        val2 = g_key_file_get_integer(keyfile,"test","age",&err);
        if (err) {
                printf("%s",err->message);
  g_clear_error(&err);
        }
        printf("offmode = %s\n",str);
        printf("pagetimeout = %d\n",val1);
        printf("age = %d\n",val2);
        return 0;
}
gcc -g `pkg-config --cflags --libs glib-2.0 gthread-2.0` glib_parser.c -o glib_parser

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/18047/showart_1797971.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP