Pine Blog

GLIB用户指南-读取程序配置文件

1.概述

有时在写一个程序时经常会从一个配置文件中读取一系列的参数,在度bluez代码时发现了一个非常好的方法。
glib

2.使用方法

这个方法基于glib-2.0,例如要从名为main.conf中读取里面的配置值。
offmode = NoScan
pagetimeout = 8192
age = 26

main.conf文件内容:

 [General]

 # List of plugins that should not be loaded on bluetoothd startup
 #DisablePlugins = network,input

 # Default adaper name
 # %h - substituted for hostname
 # %d - substituted for adapter id
 Name = %h-%d:

 # Default device class. Only the major and minor device class bits are
 # considered
 Class = 0x000100

 # How long to stay in discoverable mode before going back to non-discoverable
 # The value is in seconds. Default is 180, i.e. 3 minutes.
 # 0 = disable timer, i.e. stay discoverable forever
 DiscoverableTimeout = 0

 # Use some other page timeout than the controller default one
 # (16384 = 10 seconds)
 PageTimeout = 8192

 # Behaviour for Adapter.SetProperty("mode", "off")
 # Possible values: "DevDown", "NoScan" (default)
 OffMode = NoScan

 # Discover scheduler interval used in Adapter.DiscoverDevices
 # The value is in seconds. Defaults is 0 to use controller scheduler
 DiscoverSchedulerInterval = 0
 [test]
 age=26

代码如下:

#include <glib.h>
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

文章转载自:https://blog.csdn.net/l197803/article/details/138244885?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-138244885-blog-52932358.235%5Ev43%5Epc_blog_bottom_relevance_base2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-138244885-blog-52932358.235%5Ev43%5Epc_blog_bottom_relevance_base2&utm_relevant_index=5,如有问题请联系删除。

未经允许不得转载:Pine Blog » GLIB用户指南-读取程序配置文件

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Pine Blog
Anywhere, Anytime
E-mail:59054872@qq.com
苏ICP备15059480号-1