glib 学习笔记1,读取程序配置文件 - 我的文章 - unix/solairs/lin...
来源:百度文库 编辑:神马文学网 时间:2024/04/20 05:01:10
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
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
{
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
glib 学习笔记1,读取程序配置文件 - 我的文章 - unix/solairs/lin...
linux输入自系统分析 - 我的文章 - unix/solairs/linux man
glib学习笔记2 使用glib链表
glib学习笔记3 命令行解析
java读取配置文件的几种方法
利用GetPrivateProfileString读取配置文件
GetPrivateProfileString读取配置文件(.ini)
利用GetPrivateProfileString读取配置文件(.ini)
UNIX 屏幕导向程序的发展利器
《走进我的交易室》学习笔记
我的亲子教育学习笔记
Linux 各类 程序 的配置文件 的目录位置,如网络配置文件,硬件配置文件 - 祥龙之子 - 博客园
程序中怎样读取word文档的数据
JR 精品文章 - Spring学习笔记
我如何从文件读取文本,然后使用我刚读取的文本重命名该文件?
我如何从文件读取文本,然后使用我刚读取的文本重命名该文件?
我的Dll(动态链接库)学习笔记
我的一些oracle9i学习笔记,与大家分享
植物学学习笔记 - 我的日志 - 网易博客
学习linux/unix编程方法的建议
学习linux/unix编程方法的建议
学习jQuery的一篇很好的文章:jQuery对象与dom对象 - 笔记 - 张庆 - C...
Log4J 学习笔记1
MySQL学习笔记1