配置文件:
4 name = "Books, Movies & More";
5
7 inventory =
8 {
9 books = ( { title = "Treasure Island";
10 author = "Robert Louis Stevenson";
11 price = 29.99;
12 qty = 5; },
13 { title = "Snow Crash";
14 author = "Neal Stephenson";
15 price = 9.99;
16 qty = 8; }
17 );
}
查询:
查询上面配置数据如:
string name = cfg.lookup("name");
流程:
其中:
通过struct config_t,获取config_setting_t *root,然后调用下面函数
这里会看到,获取顶层后查询,如果是嵌套的话,查询会更慢
1215 config_setting_t *config_setting_lookup(config_setting_t *setting,
1216 const char *path)
1217 {
1218 const char *p = path;
1219 config_setting_t *found;
1220
1221 for(;;)
1222 {
1223 while(*p && strchr(PATH_TOKENS, *p))
1224 p++;
1225
1226 if(! *p)
1227 break;
1228
1229 if(*p == '[')
1230 found = config_setting_get_elem(setting, atoi(++p));
1231 else
1232 found = config_setting_get_member(setting, p);
1233
1234 if(! found)
1235 break;
1236
1237 setting = found;
1238
1239 while(! strchr(PATH_TOKENS, *p))
1240 p++;
1241 }
1242
1243 return(*p ? NULL : setting);
1244 }
最终实现:
list->length是2
list->elements内容分别是:name、inventory (这里是链表遍历,对每次查找有性能影响)
388 static config_setting_t *__config_list_search(config_list_t *list,
389 const char *name,
390 unsigned int *idx)
391 {
392 config_setting_t **found = NULL;
393 unsigned int i;
394
395 if(! list)
396 return(NULL);
397
398 for(i = 0, found = list->elements; i < list->length; i++, found++)
399 {
400 if(! (*found)->name)
401 continue;
402
403 if(! __config_name_compare(name, (*found)->name))
404 {
405 if(idx)
406 *idx = i;
407
408 return(*found);
409 }
410 }
411
412 return(NULL);
413 }
总结:
1.使用比较多样化
2.查询性能差(解决办法:特殊字段可以单独提取出来)
3.解析相关代码无法阅读(太乱)